GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — node-api: find the Environment once by nigrosimone · Pull Request #66509 · nodejs/node

node-api: find the Environment once #66509

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.

Sign up for GitHub

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

Merged
nodejs-github-bot merged 1 commit into nodejs:main from nigrosimone:napi-make-callback-env
Nov 25, 2026
+137 −7
Merged

node-api: find the Environment once #66509

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
4000c25
node-api: find the Environment once
nigrosimone Oct 4, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 benchmark/napi/make_callback_napi/.gitignore
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
65 changes: 65 additions & 0 deletions 65 benchmark/napi/make_callback_napi/binding.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <node_api.h>
#include <stdint.h>
#include <stdlib.h>
#include <uv.h>

typedef struct {
uv_timer_t timer;
napi_env env;
int64_t n;
napi_ref fn;
napi_ref done;
} State;

static void OnClose(uv_handle_t* handle) {
free(handle->data);
}

// napi_make_callback without async context, n times from a libuv timer, then
// done: every call opens a top-level callback scope, like an I/O callback.
static void OnTimer(uv_timer_t* handle) {
State* state = (State*)handle->data;
napi_env env = state->env;
napi_handle_scope scope;
napi_value fn, done, recv;
napi_open_handle_scope(env, &scope);
napi_get_reference_value(env, state->fn, &fn);
napi_get_reference_value(env, state->done, &done);
napi_get_global(env, &recv);
for (int64_t i = 0; i < state->n; i++) {
napi_handle_scope inner;
napi_open_handle_scope(env, &inner);
napi_make_callback(env, NULL, recv, fn, 0, NULL, NULL);
napi_close_handle_scope(env, inner);
}
napi_make_callback(env, NULL, recv, done, 0, NULL, NULL);
napi_delete_reference(env, state->fn);
napi_delete_reference(env, state->done);
napi_close_handle_scope(env, scope);
uv_close((uv_handle_t*)&state->timer, OnClose);
}

// run(n, fn, done)
static napi_value Run(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value argv[3];
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
State* state = (State*)calloc(1, sizeof(State));
state->env = env;
napi_get_value_int64(env, argv[0], &state->n);
napi_create_reference(env, argv[1], 1, &state->fn);
napi_create_reference(env, argv[2], 1, &state->done);
uv_loop_t* loop;
napi_get_uv_event_loop(env, &loop);
state->timer.data = state;
uv_timer_init(loop, &state->timer);
uv_timer_start(&state->timer, OnTimer, 0, 0);
return NULL;
}

NAPI_MODULE_INIT() {
napi_value run;
napi_create_function(env, "run", NAPI_AUTO_LENGTH, Run, NULL, &run);
napi_set_named_property(env, exports, "run", run);
return exports;
}
15 changes: 15 additions & 0 deletions 15 benchmark/napi/make_callback_napi/binding.gyp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.c' ],
'conditions': [
# node-gyp does not pass the include dirs of the Node.js build, and
# the addon headers have uv.h only when libuv is bundled.
[ 'node_shared_libuv=="true"', {
'include_dirs': [ '<@(node_shared_libuv_include_dirs)' ],
}],
],
}
]
}
23 changes: 23 additions & 0 deletions 23 benchmark/napi/make_callback_napi/index.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../../common.js');

// napi_make_callback without async context from a libuv timer, so every call
// opens a top-level callback scope, like an I/O callback does.

let binding;
try {
binding = require(`./build/${common.buildType}/binding`);
} catch {
console.error('napi/make_callback_napi/index.js Binding failed to load');
process.exit(0);
}

const bench = common.createBenchmark(main, {
n: [1e6, 1e7],
});

function main({ n }) {
bench.start();
binding.run(n, () => {}, () => bench.end(n));
}
12 changes: 12 additions & 0 deletions 12 src/api/callback.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,18 @@ MaybeLocal<Value> InternalMakeCallback(Isolate* isolate,
}
Environment* env = Environment::GetCurrent(context);
CHECK_NOT_NULL(env);
return MakeCallbackInEnvironment(
env, recv, callback, argc, argv, asyncContext, context_frame);
}

MaybeLocal<Value> MakeCallbackInEnvironment(Environment* env,
Local<Object> recv,
const Local<Function> callback,
int argc,
Local<Value> argv[],
async_context asyncContext,
Local<Value> context_frame) {
Isolate* isolate = env->isolate();
// Entering the context changes nothing when it is already the entered and
// the current one, which is the common case for addons.
Local<Context> env_context = env->context();
Expand Down
12 changes: 8 additions & 4 deletions 12 src/node_api.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ static void ThrowNodeApiVersionError(node::Environment* node_env,
node_napi_env__::node_napi_env__(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version)
: napi_env__(context, module_api_version), filename(module_filename) {
: napi_env__(context, module_api_version),
filename(module_filename),
node_env_(node::Environment::GetCurrent(context)) {
CHECK_NOT_NULL(node_env());
}

Expand Down Expand Up @@ -1026,13 +1028,15 @@ napi_status NAPI_CDECL napi_make_callback(napi_env env,
v8::MaybeLocal<v8::Value> callback_result;

if (async_context == nullptr) {
callback_result = node::MakeCallback(
env->isolate,
// The Environment of the napi_env, not looked up from the callback again.
callback_result = node::MakeCallbackInEnvironment(
reinterpret_cast<node_napi_env>(env)->node_env(),
v8recv,
v8func,
argc,
reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)),
{0, 0});
{0, 0},
v8::Undefined(env->isolate));
} else {
v8impl::AsyncContext* node_async_context =
reinterpret_cast<v8impl::AsyncContext*>(async_context);
Expand Down
6 changes: 3 additions & 3 deletions 6 src/node_api_internals.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ struct node_napi_env__ : public napi_env__ {

void DeleteMe() override;

inline node::Environment* node_env() const {
return node::Environment::GetCurrent(context());
}
// Found once: a napi_env lives as long as its Environment.
inline node::Environment* node_env() const { return node_env_; }
inline const char* GetFilename() const { return filename.c_str(); }

std::string filename;
node::Environment* node_env_;
bool destructing = false;
bool finalization_scheduled = false;
};
Expand Down
10 changes: 10 additions & 0 deletions 10 src/node_internals.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ v8::MaybeLocal<v8::Value> InternalMakeCallback(
async_context asyncContext,
v8::Local<v8::Value> context_frame);

// The same, for a caller that already knows the Environment of `callback`.
v8::MaybeLocal<v8::Value> MakeCallbackInEnvironment(
Environment* env,
v8::Local<v8::Object> recv,
const v8::Local<v8::Function> callback,
int argc,
v8::Local<v8::Value> argv[],
async_context asyncContext,
v8::Local<v8::Value> context_frame);

v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
v8::Local<v8::Object> recv,
v8::Local<v8::Function> callback,
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.