@@ -478,11 +478,11 @@ may be called multiple times, from multiple contexts, and even concurrently from
478
478
multiple threads.
479
479
480
480
Native addons may need to allocate global state which they use during
481
- their entire life cycle such that the state must be unique to each instance of
482
- the addon.
481
+ their life cycle of an Node.js environment such that the state can be
482
+ unique to each instance of the addon.
483
483
484
- To this end, Node-API provides a way to allocate data such that its life cycle
485
- is tied to the life cycle of the Agent .
484
+ To this end, Node-API provides a way to associate data such that its life cycle
485
+ is tied to the life cycle of a Node.js environment .
486
486
487
487
### `napi_set_instance_data`
488
488
@@ -510,11 +510,11 @@ napi_status napi_set_instance_data(napi_env env,
510
510
511
511
Returns `napi_ok` if the API succeeded.
512
512
513
- This API associates `data` with the currently running Agent. `data` can later
514
- be retrieved using `napi_get_instance_data()`. Any existing data associated with
515
- the currently running Agent which was set by means of a previous call to
516
- `napi_set_instance_data()` will be overwritten. If a `finalize_cb` was provided
517
- by the previous call, it will not be called.
513
+ This API associates `data` with the currently running Node.js environment. `data`
514
+ can later be retrieved using `napi_get_instance_data()`. Any existing data
515
+ associated with the currently running Node.js environment which was set by means
516
+ of a previous call to `napi_set_instance_data()` will be overwritten. If a
517
+ `finalize_cb` was provided by the previous call, it will not be called.
518
518
519
519
### `napi_get_instance_data`
520
520
@@ -532,13 +532,13 @@ napi_status napi_get_instance_data(napi_env env,
532
532
533
533
* `[in] env`: The environment that the Node-API call is invoked under.
534
534
* `[out] data`: The data item that was previously associated with the currently
535
- running Agent by a call to `napi_set_instance_data()`.
535
+ running Node.js environment by a call to `napi_set_instance_data()`.
536
536
537
537
Returns `napi_ok` if the API succeeded.
538
538
539
539
This API retrieves data that was previously associated with the currently
540
- running Agent via `napi_set_instance_data()`. If no data is set, the call will
541
- succeed and `data` will be set to `NULL`.
540
+ running Node.js environment via `napi_set_instance_data()`. If no data is set,
541
+ the call will succeed and `data` will be set to `NULL`.
542
542
543
543
## Basic Node-API data types
544
544
@@ -1799,11 +1799,11 @@ If still valid, this API returns the `napi_value` representing the
1799
1799
JavaScript `Object` associated with the `napi_ref`. Otherwise, result
1800
1800
will be `NULL`.
1801
1801
1802
- ### Cleanup on exit of the current Node.js instance
1802
+ ### Cleanup on exit of the current Node.js environment
1803
1803
1804
1804
While a Node.js process typically releases all its resources when exiting,
1805
1805
embedders of Node.js, or future Worker support, may require addons to register
1806
- clean-up hooks that will be run once the current Node.js instance exits.
1806
+ clean-up hooks that will be run once the current Node.js environment exits.
1807
1807
1808
1808
Node-API provides functions for registering and un-registering such callbacks.
1809
1809
When those callbacks are run, all resources that are being held by the addon
@@ -1929,6 +1929,22 @@ the hook from being executed, unless it has already started executing.
1929
1929
This must be called on any `napi_async_cleanup_hook_handle` value obtained
1930
1930
from [`napi_add_async_cleanup_hook`][].
1931
1931
1932
+ ### Finalization on the exit of the Node.js environment
1933
+
1934
+ The Node.js environment may be torn down at an arbitrary time as soon as
1935
+ possible with JavaScript execution disallowed, like on the request of
1936
+ [`worker.terminate()`][]. When the environment is being torn down, the
1937
+ registered `napi_finalize` callbacks of JavaScript objects, Thread-safe
1938
+ functions and environment instance data are invoked immediately and
1939
+ independently.
1940
+
1941
+ The invocation of `napi_finalize` callbacks are scheduled after the manually
1942
+ registered cleanup hooks. In order to ensure a proper order of addon
1943
+ finalization during environment shutdown to avoid use-after-free in the
1944
+ `napi_finalize` callback, addons should register a cleanup hook with
1945
+ `napi_add_env_cleanup_hook` and `napi_add_async_cleanup_hook` to manually
1946
+ release the allocated resource in a proper order.
1947
+
1932
1948
## Module registration
1933
1949
1934
1950
Node-API modules are registered in a manner similar to other modules
@@ -6433,6 +6449,7 @@ the add-on's file name during loading.
6433
6449
[`process.release`]: process.md#processrelease
6434
6450
[`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref
6435
6451
[`uv_unref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref
6452
+ [`worker.terminate()`]: worker_threads.md#workerterminate
6436
6453
[async_hooks `type`]: async_hooks.md#type
6437
6454
[context-aware addons]: addons.md#context-aware-addons
6438
6455
[docs]: https://github.com/nodejs/node-addon-api#api-documentation
0 commit comments