Skip to content

Commit a8dec48

Browse files
mhdawsonMylesBorins
authored andcommittedApr 16, 2018
n-api: add methods to open/close callback scope
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. Backport-PR-URL: #19447 PR-URL: #18089 Fixes: #15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent fdd50fb commit a8dec48

File tree

10 files changed

+287
-6
lines changed

10 files changed

+287
-6
lines changed
 

‎doc/api/n-api.md

+38
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,42 @@ is sufficient and appropriate. Use of the `napi_make_callback` function
34443444
may be required when implementing custom async behavior that does not use
34453445
`napi_create_async_work`.
34463446

3447+
### *napi_open_callback_scope*
3448+
<!-- YAML
3449+
added: REPLACEME
3450+
-->
3451+
```C
3452+
NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
3453+
napi_value resource_object,
3454+
napi_async_context context,
3455+
napi_callback_scope* result)
3456+
```
3457+
- `[in] env`: The environment that the API is invoked under.
3458+
- `[in] resource_object`: An optional object associated with the async work
3459+
that will be passed to possible async_hooks [`init` hooks][].
3460+
- `[in] context`: Context for the async operation that is
3461+
invoking the callback. This should be a value previously obtained
3462+
from [`napi_async_init`][].
3463+
- `[out] result`: The newly created scope.
3464+
3465+
There are cases(for example resolving promises) where it is
3466+
necessary to have the equivalent of the scope associated with a callback
3467+
in place when making certain N-API calls. If there is no other script on
3468+
the stack the [`napi_open_callback_scope`][] and
3469+
[`napi_close_callback_scope`][] functions can be used to open/close
3470+
the required scope.
3471+
3472+
### *napi_close_callback_scope*
3473+
<!-- YAML
3474+
added: REPLACEME
3475+
-->
3476+
```C
3477+
NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env,
3478+
napi_callback_scope scope)
3479+
```
3480+
- `[in] env`: The environment that the API is invoked under.
3481+
- `[in] scope`: The scope to be closed.
3482+
34473483
## Version Management
34483484

34493485
### napi_get_node_version
@@ -3723,6 +3759,7 @@ NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
37233759
[`napi_async_init`]: #n_api_napi_async_init
37243760
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
37253761
[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope
3762+
[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope
37263763
[`napi_close_handle_scope`]: #n_api_napi_close_handle_scope
37273764
[`napi_create_async_work`]: #n_api_napi_create_async_work
37283765
[`napi_create_error`]: #n_api_napi_create_error
@@ -3749,6 +3786,7 @@ NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
37493786
[`napi_get_last_error_info`]: #n_api_napi_get_last_error_info
37503787
[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception
37513788
[`napi_make_callback`]: #n_api_napi_make_callback
3789+
[`napi_open_callback_scope`]: #n_api_napi_open_callback_scope
37523790
[`napi_open_escapable_handle_scope`]: #n_api_napi_open_escapable_handle_scope
37533791
[`napi_open_handle_scope`]: #n_api_napi_open_handle_scope
37543792
[`napi_property_descriptor`]: #n_api_napi_property_descriptor

‎src/env-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
111111
env_->makecallback_cntr_--;
112112
}
113113

114-
inline bool Environment::AsyncCallbackScope::in_makecallback() {
114+
inline bool Environment::AsyncCallbackScope::in_makecallback() const {
115115
return env_->makecallback_cntr_ > 1;
116116
}
117117

‎src/env.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class Environment {
297297
explicit AsyncCallbackScope(Environment* env);
298298
~AsyncCallbackScope();
299299

300-
inline bool in_makecallback();
300+
inline bool in_makecallback() const;
301301

302302
private:
303303
Environment* env_;

‎src/node_api.cc

+59-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct napi_env__ {
4949
bool has_instance_available;
5050
napi_extended_error_info last_error;
5151
int open_handle_scopes = 0;
52+
int open_callback_scopes = 0;
5253
uv_loop_t* loop = nullptr;
5354
};
5455

@@ -261,6 +262,18 @@ V8EscapableHandleScopeFromJsEscapableHandleScope(
261262
return reinterpret_cast<EscapableHandleScopeWrapper*>(s);
262263
}
263264

265+
static
266+
napi_callback_scope JsCallbackScopeFromV8CallbackScope(
267+
node::CallbackScope* s) {
268+
return reinterpret_cast<napi_callback_scope>(s);
269+
}
270+
271+
static
272+
node::CallbackScope* V8CallbackScopeFromJsCallbackScope(
273+
napi_callback_scope s) {
274+
return reinterpret_cast<node::CallbackScope*>(s);
275+
}
276+
264277
//=== Conversion between V8 Handles and napi_value ========================
265278

266279
// This asserts v8::Local<> will always be implemented with a single
@@ -552,6 +565,7 @@ class CallbackWrapperBase : public CallbackWrapper {
552565
napi_clear_last_error(env);
553566

554567
int open_handle_scopes = env->open_handle_scopes;
568+
int open_callback_scopes = env->open_callback_scopes;
555569

556570
napi_value result = cb(env, cbinfo_wrapper);
557571

@@ -560,6 +574,7 @@ class CallbackWrapperBase : public CallbackWrapper {
560574
}
561575

562576
CHECK_EQ(env->open_handle_scopes, open_handle_scopes);
577+
CHECK_EQ(env->open_callback_scopes, open_callback_scopes);
563578

564579
if (!env->last_exception.IsEmpty()) {
565580
isolate->ThrowException(
@@ -917,7 +932,8 @@ const char* error_messages[] = {nullptr,
917932
"An exception is pending",
918933
"The async work item was cancelled",
919934
"napi_escape_handle already called on scope",
920-
"Invalid handle scope usage"};
935+
"Invalid handle scope usage",
936+
"Invalid callback scope usage"};
921937

922938
static inline napi_status napi_clear_last_error(napi_env env) {
923939
env->last_error.error_code = napi_ok;
@@ -948,9 +964,9 @@ napi_status napi_get_last_error_info(napi_env env,
948964
// We don't have a napi_status_last as this would result in an ABI
949965
// change each time a message was added.
950966
static_assert(
951-
node::arraysize(error_messages) == napi_handle_scope_mismatch + 1,
967+
node::arraysize(error_messages) == napi_callback_scope_mismatch + 1,
952968
"Count of error messages must match count of error values");
953-
CHECK_LE(env->last_error.error_code, napi_handle_scope_mismatch);
969+
CHECK_LE(env->last_error.error_code, napi_callback_scope_mismatch);
954970

955971
// Wait until someone requests the last error information to fetch the error
956972
// message string
@@ -2639,6 +2655,46 @@ napi_status napi_escape_handle(napi_env env,
26392655
return napi_set_last_error(env, napi_escape_called_twice);
26402656
}
26412657

2658+
napi_status napi_open_callback_scope(napi_env env,
2659+
napi_value resource_object,
2660+
napi_async_context async_context_handle,
2661+
napi_callback_scope* result) {
2662+
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw
2663+
// JS exceptions.
2664+
CHECK_ENV(env);
2665+
CHECK_ARG(env, result);
2666+
2667+
v8::Local<v8::Context> context = env->isolate->GetCurrentContext();
2668+
2669+
node::async_context* node_async_context =
2670+
reinterpret_cast<node::async_context*>(async_context_handle);
2671+
2672+
v8::Local<v8::Object> resource;
2673+
CHECK_TO_OBJECT(env, context, resource, resource_object);
2674+
2675+
*result = v8impl::JsCallbackScopeFromV8CallbackScope(
2676+
new node::CallbackScope(env->isolate,
2677+
resource,
2678+
*node_async_context));
2679+
2680+
env->open_callback_scopes++;
2681+
return napi_clear_last_error(env);
2682+
}
2683+
2684+
napi_status napi_close_callback_scope(napi_env env, napi_callback_scope scope) {
2685+
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw
2686+
// JS exceptions.
2687+
CHECK_ENV(env);
2688+
CHECK_ARG(env, scope);
2689+
if (env->open_callback_scopes == 0) {
2690+
return napi_callback_scope_mismatch;
2691+
}
2692+
2693+
env->open_callback_scopes--;
2694+
delete v8impl::V8CallbackScopeFromJsCallbackScope(scope);
2695+
return napi_clear_last_error(env);
2696+
}
2697+
26422698
napi_status napi_new_instance(napi_env env,
26432699
napi_value constructor,
26442700
size_t argc,

‎src/node_api.h

+8
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ NAPI_EXTERN napi_status napi_escape_handle(napi_env env,
424424
napi_value escapee,
425425
napi_value* result);
426426

427+
NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
428+
napi_value resource_object,
429+
napi_async_context context,
430+
napi_callback_scope* result);
431+
432+
NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env,
433+
napi_callback_scope scope);
434+
427435
// Methods to support error handling
428436
NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error);
429437
NAPI_EXTERN napi_status napi_throw_error(napi_env env,

‎src/node_api_types.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct napi_value__ *napi_value;
1515
typedef struct napi_ref__ *napi_ref;
1616
typedef struct napi_handle_scope__ *napi_handle_scope;
1717
typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope;
18+
typedef struct napi_callback_scope__ *napi_callback_scope;
1819
typedef struct napi_callback_info__ *napi_callback_info;
1920
typedef struct napi_async_context__ *napi_async_context;
2021
typedef struct napi_async_work__ *napi_async_work;
@@ -70,7 +71,8 @@ typedef enum {
7071
napi_pending_exception,
7172
napi_cancelled,
7273
napi_escape_called_twice,
73-
napi_handle_scope_mismatch
74+
napi_handle_scope_mismatch,
75+
napi_callback_scope_mismatch
7476
} napi_status;
7577

7678
typedef napi_value (*napi_callback)(napi_env env,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include "node_api.h"
2+
#include "uv.h"
3+
#include "../common.h"
4+
5+
namespace {
6+
7+
// the test needs to fake out the async structure, so we need to use
8+
// the raw structure here and then cast as done behind the scenes
9+
// in napi calls.
10+
struct async_context {
11+
double async_id;
12+
double trigger_async_id;
13+
};
14+
15+
16+
napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
17+
size_t argc;
18+
napi_value args[4];
19+
20+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr));
21+
NAPI_ASSERT(env, argc == 4 , "Wrong number of arguments");
22+
23+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
24+
25+
napi_valuetype valuetype;
26+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
27+
NAPI_ASSERT(env, valuetype == napi_object,
28+
"Wrong type of arguments. Expects an object as first argument.");
29+
30+
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype));
31+
NAPI_ASSERT(env, valuetype == napi_number,
32+
"Wrong type of arguments. Expects a number as second argument.");
33+
34+
NAPI_CALL(env, napi_typeof(env, args[2], &valuetype));
35+
NAPI_ASSERT(env, valuetype == napi_number,
36+
"Wrong type of arguments. Expects a number as third argument.");
37+
38+
NAPI_CALL(env, napi_typeof(env, args[3], &valuetype));
39+
NAPI_ASSERT(env, valuetype == napi_function,
40+
"Wrong type of arguments. Expects a function as third argument.");
41+
42+
struct async_context context;
43+
NAPI_CALL(env, napi_get_value_double(env, args[1], &context.async_id));
44+
NAPI_CALL(env,
45+
napi_get_value_double(env, args[2], &context.trigger_async_id));
46+
47+
napi_callback_scope scope = nullptr;
48+
NAPI_CALL(
49+
env,
50+
napi_open_callback_scope(env,
51+
args[0],
52+
reinterpret_cast<napi_async_context>(&context),
53+
&scope));
54+
55+
// if the function has an exception pending after the call that is ok
56+
// so we don't use NAPI_CALL as we must close the callback scope regardless
57+
napi_value result = nullptr;
58+
napi_status function_call_result =
59+
napi_call_function(env, args[0], args[3], 0, nullptr, &result);
60+
if (function_call_result != napi_ok) {
61+
GET_AND_THROW_LAST_ERROR((env));
62+
}
63+
64+
NAPI_CALL(env, napi_close_callback_scope(env, scope));
65+
66+
return result;
67+
}
68+
69+
static napi_env shared_env = nullptr;
70+
static napi_deferred deferred = nullptr;
71+
72+
static void Callback(uv_work_t* req, int ignored) {
73+
napi_env env = shared_env;
74+
75+
napi_handle_scope handle_scope = nullptr;
76+
NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope));
77+
78+
napi_value resource_name;
79+
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(
80+
env, "test", NAPI_AUTO_LENGTH, &resource_name));
81+
napi_async_context context;
82+
NAPI_CALL_RETURN_VOID(env,
83+
napi_async_init(env, nullptr, resource_name, &context));
84+
85+
napi_value resource_object;
86+
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object));
87+
88+
napi_value undefined_value;
89+
NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value));
90+
91+
napi_callback_scope scope = nullptr;
92+
NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env,
93+
resource_object,
94+
context,
95+
&scope));
96+
97+
NAPI_CALL_RETURN_VOID(env,
98+
napi_resolve_deferred(env, deferred, undefined_value));
99+
100+
NAPI_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope));
101+
102+
NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope));
103+
delete req;
104+
}
105+
106+
napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
107+
napi_value promise = nullptr;
108+
if (deferred == nullptr) {
109+
shared_env = env;
110+
NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
111+
112+
uv_loop_t* loop = nullptr;
113+
NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));
114+
115+
uv_work_t* req = new uv_work_t();
116+
uv_queue_work(loop,
117+
req,
118+
[](uv_work_t*) {},
119+
Callback);
120+
}
121+
return promise;
122+
}
123+
124+
napi_value Init(napi_env env, napi_value exports) {
125+
napi_property_descriptor descriptors[] = {
126+
DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope),
127+
DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync)
128+
};
129+
130+
NAPI_CALL(env, napi_define_properties(
131+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
132+
133+
return exports;
134+
}
135+
136+
} // anonymous namespace
137+
138+
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
6+
'sources': [ 'binding.cc' ]
7+
}
8+
]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const { testResolveAsync } = require(`./build/${common.buildType}/binding`);
6+
7+
let called = false;
8+
testResolveAsync().then(common.mustCall(() => {
9+
called = true;
10+
}));
11+
12+
setTimeout(common.mustCall(() => { assert(called); }),
13+
common.platformTimeout(20));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
6+
7+
assert.strictEqual(runInCallbackScope({}, 0, 0, () => 42), 42);
8+
9+
{
10+
process.once('uncaughtException', common.mustCall((err) => {
11+
assert.strictEqual(err.message, 'foo');
12+
}));
13+
14+
runInCallbackScope({}, 0, 0, () => {
15+
throw new Error('foo');
16+
});
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.