-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
n-api: add methods to open/close callback scope #18089
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include "node_api.h" | ||
#include "uv.h" | ||
#include "../common.h" | ||
|
||
namespace { | ||
|
||
// the test needs to fake out the async structure, so we need to use | ||
// the raw structure here and then cast as done behind the scenes | ||
// in napi calls. | ||
struct async_context { | ||
double async_id; | ||
double trigger_async_id; | ||
}; | ||
|
||
|
||
napi_value RunInCallbackScope(napi_env env, napi_callback_info info) { | ||
size_t argc; | ||
napi_value args[4]; | ||
|
||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr)); | ||
NAPI_ASSERT(env, argc ==4 , "Wrong number of arguments"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will fix |
||
|
||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); | ||
|
||
napi_valuetype valuetype; | ||
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); | ||
NAPI_ASSERT(env, valuetype == napi_object, | ||
"Wrong type of arguments. Expects an object as first argument."); | ||
|
||
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype)); | ||
NAPI_ASSERT(env, valuetype == napi_number, | ||
"Wrong type of arguments. Expects a number as second argument."); | ||
|
||
NAPI_CALL(env, napi_typeof(env, args[2], &valuetype)); | ||
NAPI_ASSERT(env, valuetype == napi_number, | ||
"Wrong type of arguments. Expects a number as third argument."); | ||
|
||
NAPI_CALL(env, napi_typeof(env, args[3], &valuetype)); | ||
NAPI_ASSERT(env, valuetype == napi_function, | ||
"Wrong type of arguments. Expects a function as third argument."); | ||
|
||
struct async_context context; | ||
NAPI_CALL(env, napi_get_value_double(env, args[1], &context.async_id)); | ||
NAPI_CALL(env, | ||
napi_get_value_double(env, args[2], &context.trigger_async_id)); | ||
|
||
napi_callback_scope scope = nullptr; | ||
NAPI_CALL( | ||
env, | ||
napi_open_callback_scope(env, | ||
args[0], | ||
reinterpret_cast<napi_async_context>(&context), | ||
&scope)); | ||
|
||
// if the function has an exception pending after the call that is ok | ||
// so we don't use NAPI_CALL as we must close the callback scope regardless | ||
napi_value result; | ||
napi_status function_call_result = | ||
napi_call_function(env, args[0], args[3], 0, nullptr, &result); | ||
if (function_call_result != napi_ok) { | ||
GET_AND_THROW_LAST_ERROR((env)); | ||
} | ||
|
||
NAPI_CALL(env, napi_close_callback_scope(env, scope)); | ||
|
||
return result; | ||
} | ||
|
||
static napi_env shared_env = nullptr; | ||
static napi_deferred deferred = nullptr; | ||
|
||
static void Callback(uv_work_t* req, int ignored) { | ||
napi_env env = shared_env; | ||
|
||
napi_handle_scope handle_scope = nullptr; | ||
NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope)); | ||
|
||
napi_value resource_name; | ||
NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8( | ||
env, "test", NAPI_AUTO_LENGTH, &resource_name)); | ||
napi_async_context context; | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_async_init(env, nullptr, resource_name, &context)); | ||
|
||
napi_value resource_object; | ||
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object)); | ||
|
||
napi_value undefined_value; | ||
NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value)); | ||
|
||
napi_callback_scope scope = nullptr; | ||
NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env, | ||
resource_object, | ||
context, | ||
&scope)); | ||
|
||
NAPI_CALL_RETURN_VOID(env, | ||
napi_resolve_deferred(env, deferred, undefined_value)); | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope)); | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope)); | ||
delete req; | ||
} | ||
|
||
napi_value TestResolveAsync(napi_env env, napi_callback_info info) { | ||
napi_value promise; | ||
if (deferred == nullptr) { | ||
shared_env = env; | ||
NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); | ||
|
||
uv_loop_t* loop = nullptr; | ||
NAPI_CALL(env, napi_get_uv_event_loop(env, &loop)); | ||
|
||
uv_work_t* req = new uv_work_t; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will fix |
||
uv_queue_work(loop, | ||
req, | ||
[](uv_work_t*) {}, | ||
Callback); | ||
} | ||
return promise; | ||
} | ||
|
||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope), | ||
DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync) | ||
}; | ||
|
||
NAPI_CALL(env, napi_define_properties( | ||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
|
||
return exports; | ||
} | ||
|
||
} // namespace | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't the linter complain that you should be using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no complaints from the linter but I can update. |
||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
// The async_hook that we enable would register the process.emitWarning() | ||
// call from loading the N-API addon as asynchronous activity because | ||
// it contains a process.nextTick() call. Monkey patch it to be a no-op | ||
// before we load the addon in order to avoid this. | ||
process.emitWarning = () => {}; | ||
|
||
const { runInCallbackScope } = require(`./build/${common.buildType}/binding`); | ||
|
||
let insideHook = false; | ||
async_hooks.createHook({ | ||
before: common.mustCall((id) => { | ||
assert.strictEqual(id, 1000); | ||
insideHook = true; | ||
}), | ||
after: common.mustCall((id) => { | ||
assert.strictEqual(id, 1000); | ||
insideHook = false; | ||
}) | ||
}).enable(); | ||
|
||
runInCallbackScope({}, 1000, 1000, () => { | ||
assert(insideHook); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const { testResolveAsync } = require(`./build/${common.buildType}/binding`); | ||
|
||
let called = false; | ||
testResolveAsync().then(common.mustCall(() => { | ||
called = true; | ||
})); | ||
|
||
setTimeout(common.mustCall(() => { assert(called); }), | ||
common.platformTimeout(20)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name is a bit of a misnomer, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's consistent with the naming of the other similar functions. We may want to change them all but I think that should be in a different PR.