Skip to content

Commit 073b4f2

Browse files
legendecascodebytere
authored andcommitted
doc,src: clarify that one napi_env is per-module
PR-URL: #31102 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 6cfc4dc commit 073b4f2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

doc/api/n-api.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,12 @@ implementation can use to persist VM-specific state. This structure is passed
493493
to native functions when they're invoked, and it must be passed back when
494494
making N-API calls. Specifically, the same `napi_env` that was passed in when
495495
the initial native function was called must be passed to any subsequent
496-
nested N-API calls. Caching the `napi_env` for the purpose of general reuse is
497-
not allowed.
496+
nested N-API calls. Caching the `napi_env` for the purpose of general reuse,
497+
and passing the `napi_env` between instances of the same addon running on
498+
different [`Worker`][] threads is not allowed. The `napi_env` becomes invalid
499+
when an instance of a native addon is unloaded. Notification of this event is
500+
delivered through the callbacks given to [`napi_add_env_cleanup_hook`][] and
501+
[`napi_set_instance_data`][].
498502

499503
### napi_value
500504

@@ -5304,8 +5308,10 @@ This API may only be called from the main thread.
53045308
[Xcode]: https://developer.apple.com/xcode/
53055309
[`Number.MAX_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.max_safe_integer
53065310
[`Number.MIN_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.min_safe_integer
5311+
[`Worker`]: worker_threads.html#worker_threads_class_worker
53075312
[`global`]: globals.html#globals_global
53085313
[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
5314+
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
53095315
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
53105316
[`napi_async_init`]: #n_api_napi_async_init
53115317
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
@@ -5341,6 +5347,7 @@ This API may only be called from the main thread.
53415347
[`napi_queue_async_work`]: #n_api_napi_queue_async_work
53425348
[`napi_reference_ref`]: #n_api_napi_reference_ref
53435349
[`napi_reference_unref`]: #n_api_napi_reference_unref
5350+
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
53445351
[`napi_set_property`]: #n_api_napi_set_property
53455352
[`napi_throw_error`]: #n_api_napi_throw_error
53465353
[`napi_throw_range_error`]: #n_api_napi_throw_range_error

src/node_api.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,7 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
471471
return;
472472
}
473473

474-
// Create a new napi_env for this module or reference one if a pre-existing
475-
// one is found.
474+
// Create a new napi_env for this specific module.
476475
napi_env env = v8impl::NewEnv(context);
477476

478477
napi_value _exports;

0 commit comments

Comments
 (0)