GeezerVison is showing github.com — node-api: find the Environment once · nodejs/node@5bcd171
File treeExpand file treeCollapse file tree Open diff view settings benchmark/napi/make_callback_napi
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#include <node_api.h> | | 2 | +#include <uv.h> | | 3 | +#include <cstdint> | | 4 | +#include <cstdlib> | | 5 | + | | 6 | +struct State { | | 7 | + uv_timer_t timer; | | 8 | + napi_env env; | | 9 | + int64_t n; | | 10 | + napi_ref fn; | | 11 | + napi_ref done; | | 12 | +}; | | 13 | + | | 14 | +static void OnClose(uv_handle_t* handle) { | | 15 | + free(handle->data); | | 16 | +} | | 17 | + | | 18 | +// napi_make_callback without async context, n times from a libuv timer, then | | 19 | +// done: every call opens a top-level callback scope, like an I/O callback. | | 20 | +static void OnTimer(uv_timer_t* handle) { | | 21 | + State* state = static_cast<State*>(handle->data); | | 22 | + napi_env env = state->env; | | 23 | + napi_handle_scope scope; | | 24 | + napi_value fn, done, recv; | | 25 | + napi_open_handle_scope(env, &scope); | | 26 | + napi_get_reference_value(env, state->fn, &fn); | | 27 | + napi_get_reference_value(env, state->done, &done); | | 28 | + napi_get_global(env, &recv); | | 29 | + for (int64_t i = 0; i < state->n; i++) { | | 30 | + napi_handle_scope inner; | | 31 | + napi_open_handle_scope(env, &inner); | | 32 | + napi_make_callback(env, nullptr, recv, fn, 0, nullptr, nullptr); | | 33 | + napi_close_handle_scope(env, inner); | | 34 | + } | | 35 | + napi_make_callback(env, nullptr, recv, done, 0, nullptr, nullptr); | | 36 | + napi_delete_reference(env, state->fn); | | 37 | + napi_delete_reference(env, state->done); | | 38 | + napi_close_handle_scope(env, scope); | | 39 | + uv_close(reinterpret_cast<uv_handle_t*>(&state->timer), OnClose); | | 40 | +} | | 41 | + | | 42 | +// run(n, fn, done) | | 43 | +static napi_value Run(napi_env env, napi_callback_info info) { | | 44 | + size_t argc = 3; | | 45 | + napi_value argv[3]; | | 46 | + napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); | | 47 | + State* state = static_cast<State*>(calloc(1, sizeof(State))); | | 48 | + state->env = env; | | 49 | + napi_get_value_int64(env, argv[0], &state->n); | | 50 | + napi_create_reference(env, argv[1], 1, &state->fn); | | 51 | + napi_create_reference(env, argv[2], 1, &state->done); | | 52 | + uv_loop_t* loop; | | 53 | + napi_get_uv_event_loop(env, &loop); | | 54 | + state->timer.data = state; | | 55 | + uv_timer_init(loop, &state->timer); | | 56 | + uv_timer_start(&state->timer, OnTimer, 0, 0); | | 57 | + return nullptr; | | 58 | +} | | 59 | + | | 60 | +NAPI_MODULE_INIT() { | | 61 | + napi_value run; | | 62 | + napi_create_function(env, "run", NAPI_AUTO_LENGTH, Run, nullptr, &run); | | 63 | + napi_set_named_property(env, exports, "run", run); | | 64 | + return exports; | | 65 | +} |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + 'targets': [ | | 3 | + { | | 4 | + 'target_name': 'binding', | | 5 | + 'sources': [ 'binding.cc' ] | | 6 | + } | | 7 | + ] | | 8 | +} |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../../common.js'); | | 4 | + | | 5 | +// napi_make_callback without async context from a libuv timer, so every call | | 6 | +// opens a top-level callback scope, like an I/O callback does. | | 7 | + | | 8 | +let binding; | | 9 | +try { | | 10 | + binding = require(`./build/${common.buildType}/binding`); | | 11 | +} catch { | | 12 | + console.error('napi/make_callback_napi/index.js Binding failed to load'); | | 13 | + process.exit(0); | | 14 | +} | | 15 | + | | 16 | +const bench = common.createBenchmark(main, { | | 17 | + n: [1e6, 1e7], | | 18 | +}); | | 19 | + | | 20 | +function main({ n }) { | | 21 | + bench.start(); | | 22 | + binding.run(n, () => {}, () => bench.end(n)); | | 23 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/api/callback.cc +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -350,6 +350,18 @@ MaybeLocal<Value> InternalMakeCallback(Isolate* isolate, | 350 | 350 | } | 351 | 351 | Environment* env = Environment::GetCurrent(context); | 352 | 352 | CHECK_NOT_NULL(env); | | 353 | + return MakeCallbackInEnvironment( | | 354 | + env, recv, callback, argc, argv, asyncContext, context_frame); | | 355 | +} | | 356 | + | | 357 | +MaybeLocal<Value> MakeCallbackInEnvironment(Environment* env, | | 358 | + Local<Object> recv, | | 359 | + const Local<Function> callback, | | 360 | + int argc, | | 361 | + Local<Value> argv[], | | 362 | + async_context asyncContext, | | 363 | + Local<Value> context_frame) { | | 364 | + Isolate* isolate = env->isolate(); | 353 | 365 | // Entering the context changes nothing when it is already the entered and | 354 | 366 | // the current one, which is the common case for addons. | 355 | 367 | Local<Context> env_context = env->context(); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.cc +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,9 @@ static void ThrowNodeApiVersionError(node::Environment* node_env, | 71 | 71 | node_napi_env__::node_napi_env__(v8::Local<v8::Context> context, | 72 | 72 | const std::string& module_filename, | 73 | 73 | int32_t module_api_version) | 74 | | - : napi_env__(context, module_api_version), filename(module_filename) { | | 74 | + : napi_env__(context, module_api_version), | | 75 | + filename(module_filename), | | 76 | + node_env_(node::Environment::GetCurrent(context)) { | 75 | 77 | CHECK_NOT_NULL(node_env()); | 76 | 78 | } | 77 | 79 |
| @@ -1026,13 +1028,15 @@ napi_status NAPI_CDECL napi_make_callback(napi_env env, | 1026 | 1028 | v8::MaybeLocal<v8::Value> callback_result; | 1027 | 1029 |
| 1028 | 1030 | if (async_context == nullptr) { | 1029 | | - callback_result = node::MakeCallback( | 1030 | | - env->isolate, | | 1031 | + // The Environment of the napi_env, not looked up from the callback again. | | 1032 | + callback_result = node::MakeCallbackInEnvironment( | | 1033 | + reinterpret_cast<node_napi_env>(env)->node_env(), | 1031 | 1034 | v8recv, | 1032 | 1035 | v8func, | 1033 | 1036 | argc, | 1034 | 1037 | reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)), | 1035 | | - {0, 0}); | | 1038 | + {0, 0}, | | 1039 | + v8::Undefined(env->isolate)); | 1036 | 1040 | } else { | 1037 | 1041 | v8impl::AsyncContext* node_async_context = | 1038 | 1042 | reinterpret_cast<v8impl::AsyncContext*>(async_context); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_api_internals.h +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,12 +33,12 @@ struct node_napi_env__ : public napi_env__ { | 33 | 33 |
| 34 | 34 | void DeleteMe() override; | 35 | 35 |
| 36 | | - inline node::Environment* node_env() const { | 37 | | - return node::Environment::GetCurrent(context()); | 38 | | - } | | 36 | + // Found once: a napi_env lives as long as its Environment. | | 37 | + inline node::Environment* node_env() const { return node_env_; } | 39 | 38 | inline const char* GetFilename() const { return filename.c_str(); } | 40 | 39 |
| 41 | 40 | std::string filename; | | 41 | + node::Environment* node_env_; | 42 | 42 | bool destructing = false; | 43 | 43 | bool finalization_scheduled = false; | 44 | 44 | }; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_internals.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -230,6 +230,16 @@ v8::MaybeLocal<v8::Value> InternalMakeCallback( | 230 | 230 | async_context asyncContext, | 231 | 231 | v8::Local<v8::Value> context_frame); | 232 | 232 |
| | 233 | +// The same, for a caller that already knows the Environment of `callback`. | | 234 | +v8::MaybeLocal<v8::Value> MakeCallbackInEnvironment( | | 235 | + Environment* env, | | 236 | + v8::Local<v8::Object> recv, | | 237 | + const v8::Local<v8::Function> callback, | | 238 | + int argc, | | 239 | + v8::Local<v8::Value> argv[], | | 240 | + async_context asyncContext, | | 241 | + v8::Local<v8::Value> context_frame); | | 242 | + | 233 | 243 | v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate, | 234 | 244 | v8::Local<v8::Object> recv, | 235 | 245 | v8::Local<v8::Function> callback, | |
|