Skip to content

Commit

Permalink
n-api: add helper for addons to get the event loop
Browse files Browse the repository at this point in the history
Add a utility functions for addons to use when they need
a reference to the current event loop.

While the libuv API is not directly part of N-API, it
provides a quite stable C API as well, and is tightly
integrated with Node itself.

As a particular use case, without access to the event loop
it is hard to do something interesting from inside a N-API
finalizer function, since calls into JS and therefore virtually
all other N-API functions are not allowed.

Backport-PR-URL: #19447
PR-URL: #17109
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax authored and MylesBorins committed Apr 16, 2018
1 parent 35dc8ba commit f4391b9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
17 changes: 17 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3684,6 +3684,23 @@ NAPI_EXTERN napi_status napi_run_script(napi_env env,
- `[in] script`: A JavaScript string containing the script to execute.
- `[out] result`: The value resulting from having executed the script.

## libuv event loop

N-API provides a function for getting the current event loop associated with
a specific `napi_env`.

### napi_get_uv_event_loop
<!-- YAML
added: REPLACEME
-->
```C
NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
uv_loop_t** loop);
```

- `[in] env`: The environment that the API is invoked under.
- `[out] loop`: The current libuv loop instance.

[Promises]: #n_api_promises
[Simple Asynchronous Operations]: #n_api_simple_asynchronous_operations
[Custom Asynchronous Operations]: #n_api_custom_asynchronous_operations
Expand Down
29 changes: 20 additions & 9 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "node_api_backport.h"
#include "util.h"

#define NAPI_VERSION 1
#define NAPI_VERSION 2

static
napi_status napi_set_last_error(napi_env env, napi_status error_code,
Expand All @@ -31,8 +31,11 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code,
static napi_status napi_clear_last_error(napi_env env);

struct napi_env__ {
explicit napi_env__(v8::Isolate* _isolate): isolate(_isolate),
has_instance_available(true), last_error() {}
explicit napi_env__(v8::Isolate* _isolate, uv_loop_t *_loop):
isolate(_isolate),
has_instance_available(true),
last_error(),
loop(_loop) {}
~napi_env__() {
last_exception.Reset();
has_instance.Reset();
Expand All @@ -49,6 +52,7 @@ struct napi_env__ {
bool has_instance_available;
napi_extended_error_info last_error;
int open_handle_scopes = 0;
uv_loop_t* loop = nullptr;
};

#define ENV_OBJECT_TEMPLATE(env, prefix, destination, field_count) \
Expand Down Expand Up @@ -780,7 +784,7 @@ napi_env GetEnv(v8::Local<v8::Context> context) {
if (value->IsExternal()) {
result = static_cast<napi_env>(value.As<v8::External>()->Value());
} else {
result = new napi_env__(isolate);
result = new napi_env__(isolate, node::GetCurrentEventLoop(isolate));
auto external = v8::External::New(isolate, result);

// We must also stop hard if the result of assigning the env to the global
Expand Down Expand Up @@ -3494,15 +3498,22 @@ napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
return napi_clear_last_error(env);
}

napi_status napi_get_uv_event_loop(napi_env env, uv_loop_t** loop) {
CHECK_ENV(env);
CHECK_ARG(env, loop);
*loop = env->loop;
return napi_clear_last_error(env);
}

napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
CHECK_ENV(env);
CHECK_ARG(env, work);

// Consider: Encapsulate the uv_loop_t into an opaque pointer parameter.
// Currently the environment event loop is the same as the UV default loop.
// Someday (if node ever supports multiple isolates), it may be better to get
// the loop from node::Environment::GetCurrent(env->isolate)->event_loop();
uv_loop_t* event_loop = uv_default_loop();
napi_status status;
uv_loop_t* event_loop = nullptr;
status = napi_get_uv_event_loop(env, &event_loop);
if (status != napi_ok)
return napi_set_last_error(env, status);

uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);

Expand Down
6 changes: 6 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <stdbool.h>
#include "node_api_types.h"

struct uv_loop_s; // Forward declaration.

#ifdef _WIN32
#ifdef BUILDING_NODE_EXTENSION
#ifdef EXTERNAL_NAPI
Expand Down Expand Up @@ -581,6 +583,10 @@ NAPI_EXTERN napi_status napi_run_script(napi_env env,
napi_value script,
napi_value* result);

// Return the current libuv event loop for a given environment
NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
struct uv_loop_s** loop);

EXTERN_C_END

#endif // SRC_NODE_API_H_
8 changes: 8 additions & 0 deletions src/node_api_backport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ CallbackScope::~CallbackScope() {
env->tick_callback_function()->Call(env->process_object(), 0, nullptr);
}

uv_loop_t *GetCurrentEventLoop(v8::Isolate *isolate) {
HandleScope handle_scope(isolate);
auto context = isolate->GetCurrentContext();
if (context.IsEmpty())
return nullptr;
return Environment::GetCurrent(context)->event_loop();
}

AsyncResource::AsyncResource(v8::Isolate* _isolate,
v8::Local<v8::Object> _object,
char* name) : isolate(_isolate) {
Expand Down
2 changes: 2 additions & 0 deletions src/node_api_backport.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class CallbackScope {
Environment::AsyncCallbackScope callback_scope;
};

uv_loop_t *GetCurrentEventLoop(v8::Isolate *isolate);

NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
v8::Local<v8::Object> resource,
v8::Local<v8::String> name,
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ assert.ok(test_general.testGetPrototype(baseObject) !==

// test version management funcitons
// expected version is currently 1
assert.strictEqual(test_general.testGetVersion(), 1);
assert.strictEqual(test_general.testGetVersion(), 2);

const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
Expand Down

0 comments on commit f4391b9

Please sign in to comment.