@@ -78,7 +78,7 @@ it still gets the benefits of the ABI stability provided by the C API.
78
78
When using `node-addon-api` instead of the C APIs, start with the API [docs][]
79
79
for `node-addon-api`.
80
80
81
- The [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers
81
+ The [Node-API Resource](https://nodejs.github.io/node-addon-examples/) offers
82
82
an excellent orientation and tips for developers just getting started with
83
83
Node-API and `node-addon-api`. Additional media resources can be found on the
84
84
[Node-API Media][] page.
@@ -175,7 +175,8 @@ developers have run into limitations in node-gyp.
175
175
[CMake.js][] is an alternative build system based on [CMake][].
176
176
177
177
CMake.js is a good choice for projects that already use CMake or for
178
- developers affected by limitations in node-gyp.
178
+ developers affected by limitations in node-gyp. [`build_with_cmake`][] is an
179
+ example of a CMake-based native addon project.
179
180
180
181
### Uploading precompiled binaries
181
182
@@ -237,6 +238,18 @@ Some of the Node-API surface is experimental and requires explicit opt-in:
237
238
In this case the entire API surface, including any experimental APIs, will be
238
239
available to the module code.
239
240
241
+ Occasionally, experimental features are introduced that affect already-released
242
+ and stable APIs. These features can be disabled by an opt-out:
243
+
244
+ ```c
245
+ #define NAPI_EXPERIMENTAL
246
+ #define NODE_API_EXPERIMENTAL_<FEATURE_NAME>_OPT_OUT
247
+ #include <node_api.h>
248
+ ```
249
+
250
+ where `<FEATURE_NAME>` is the name of an experimental feature that affects both
251
+ experimental and stable APIs.
252
+
240
253
## Node-API version matrix
241
254
242
255
Up until version 9, Node-API versions were additive and versioned
@@ -419,7 +432,7 @@ napi_value create_addon(napi_env env) {
419
432
#include <node_api.h>
420
433
#include "addon.h"
421
434
422
- NAPI_MODULE_INIT() {
435
+ NAPI_MODULE_INIT(/* napi_env env, napi_value exports */ ) {
423
436
// This function body is expected to return a `napi_value`.
424
437
// The variables `napi_env env` and `napi_value exports` may be used within
425
438
// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.
@@ -464,7 +477,7 @@ napiVersion: 6
464
477
-->
465
478
466
479
```c
467
- napi_status napi_set_instance_data(napi_env env,
480
+ napi_status napi_set_instance_data(node_api_nogc_env env,
468
481
void* data,
469
482
napi_finalize finalize_cb,
470
483
void* finalize_hint);
@@ -496,7 +509,7 @@ napiVersion: 6
496
509
-->
497
510
498
511
```c
499
- napi_status napi_get_instance_data(napi_env env,
512
+ napi_status napi_get_instance_data(node_api_nogc_env env,
500
513
void** data);
501
514
```
502
515
@@ -598,6 +611,22 @@ when an instance of a native addon is unloaded. Notification of this event is
598
611
delivered through the callbacks given to [`napi_add_env_cleanup_hook`][] and
599
612
[`napi_set_instance_data`][].
600
613
614
+ ### `node_api_nogc_env`
615
+
616
+ > Stability: 1 - Experimental
617
+
618
+ This variant of `napi_env` is passed to synchronous finalizers
619
+ ([`node_api_nogc_finalize`][]). There is a subset of Node-APIs which accept
620
+ a parameter of type `node_api_nogc_env` as their first argument. These APIs do
621
+ not access the state of the JavaScript engine and are thus safe to call from
622
+ synchronous finalizers. Passing a parameter of type `napi_env` to these APIs is
623
+ allowed, however, passing a parameter of type `node_api_nogc_env` to APIs that
624
+ access the JavaScript engine state is not allowed. Attempting to do so without
625
+ a cast will produce a compiler warning or an error when add-ons are compiled
626
+ with flags which cause them to emit warnings and/or errors when incorrect
627
+ pointer types are passed into a function. Calling such APIs from a synchronous
628
+ finalizer will ultimately result in the termination of the application.
629
+
601
630
### `napi_value`
602
631
603
632
This is an opaque pointer that is used to represent a JavaScript value.
@@ -762,32 +791,36 @@ typedef napi_value (*napi_callback)(napi_env, napi_callback_info);
762
791
Unless for reasons discussed in [Object Lifetime Management][], creating a
763
792
handle and/or callback scope inside a `napi_callback` is not necessary.
764
793
765
- #### `napi_finalize `
794
+ #### `node_api_nogc_finalize `
766
795
767
796
<!-- YAML
768
- added: v8.0.0
769
- napiVersion: 1
797
+ added: REPLACEME
770
798
-->
771
799
800
+ > Stability: 1 - Experimental
801
+
772
802
Function pointer type for add-on provided functions that allow the user to be
773
803
notified when externally-owned data is ready to be cleaned up because the
774
- object with which it was associated with has been garbage-collected. The user
775
- must provide a function satisfying the following signature which would get
776
- called upon the object's collection. Currently, `napi_finalize ` can be used for
804
+ object it was associated with has been garbage-collected. The user must provide
805
+ a function satisfying the following signature which would get called upon the
806
+ object's collection. Currently, `node_api_nogc_finalize ` can be used for
777
807
finding out when objects that have external data are collected.
778
808
779
809
```c
780
- typedef void (*napi_finalize)(napi_env env,
781
- void* finalize_data,
782
- void* finalize_hint);
810
+ typedef void (*node_api_nogc_finalize)(node_api_nogc_env env,
811
+ void* finalize_data,
812
+ void* finalize_hint);
783
813
```
784
814
785
815
Unless for reasons discussed in [Object Lifetime Management][], creating a
786
816
handle and/or callback scope inside the function body is not necessary.
787
817
788
818
Since these functions may be called while the JavaScript engine is in a state
789
- where it cannot execute JavaScript code, some Node-API calls may return
790
- `napi_pending_exception` even when there is no exception pending.
819
+ where it cannot execute JavaScript code, only Node-APIs which accept a
820
+ `node_api_nogc_env` as their first parameter may be called.
821
+ [`node_api_post_finalizer`][] can be used to schedule Node-API calls that
822
+ require access to the JavaScript engine's state to run after the current
823
+ garbage collection cycle has completed.
791
824
792
825
In the case of [`node_api_create_external_string_latin1`][] and
793
826
[`node_api_create_external_string_utf16`][] the `env` parameter may be null,
@@ -796,11 +829,39 @@ shutdown.
796
829
797
830
Change History:
798
831
832
+ * experimental (`NAPI_EXPERIMENTAL`):
833
+
834
+ Only Node-API calls that accept a `node_api_nogc_env` as their first
835
+ parameter may be called, otherwise the application will be terminated with an
836
+ appropriate error message. This feature can be turned off by defining
837
+ `NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT`.
838
+
839
+ #### `napi_finalize`
840
+
841
+ <!-- YAML
842
+ added: v8.0.0
843
+ napiVersion: 1
844
+ -->
845
+
846
+ Function pointer type for add-on provided function that allow the user to
847
+ schedule a group of calls to Node-APIs in response to a garbage collection
848
+ event, after the garbage collection cycle has completed. These function
849
+ pointers can be used with [`node_api_post_finalizer`][].
850
+
851
+ ```c
852
+ typedef void (*napi_finalize)(napi_env env,
853
+ void* finalize_data,
854
+ void* finalize_hint);
855
+ ```
856
+
857
+ Change History:
858
+
799
859
* experimental (`NAPI_EXPERIMENTAL` is defined):
800
860
801
- Node-API calls made from a finalizer will return `napi_cannot_run_js` when
802
- the JavaScript engine is unable to execute JavaScript, and will return
803
- `napi_exception_pending` if there is a pending exception.
861
+ A function of this type may no longer be used as a finalizer, except with
862
+ [`node_api_post_finalizer`][]. [`node_api_nogc_finalize`][] must be used
863
+ instead. This feature can be turned off by defining
864
+ `NODE_API_EXPERIMENTAL_NOGC_ENV_OPT_OUT`.
804
865
805
866
#### `napi_async_execute_callback`
806
867
@@ -1002,7 +1063,7 @@ napiVersion: 1
1002
1063
1003
1064
```c
1004
1065
napi_status
1005
- napi_get_last_error_info(napi_env env,
1066
+ napi_get_last_error_info(node_api_nogc_env env,
1006
1067
const napi_extended_error_info** result);
1007
1068
```
1008
1069
@@ -1821,7 +1882,7 @@ napiVersion: 3
1821
1882
-->
1822
1883
1823
1884
```c
1824
- NODE_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
1885
+ NODE_EXTERN napi_status napi_add_env_cleanup_hook(node_api_nogc_env env,
1825
1886
napi_cleanup_hook fun,
1826
1887
void* arg);
1827
1888
```
@@ -1851,7 +1912,7 @@ napiVersion: 3
1851
1912
-->
1852
1913
1853
1914
```c
1854
- NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env,
1915
+ NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(node_api_nogc_env env,
1855
1916
void (*fun)(void* arg),
1856
1917
void* arg);
1857
1918
```
@@ -1880,7 +1941,7 @@ changes:
1880
1941
1881
1942
```c
1882
1943
NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1883
- napi_env env,
1944
+ node_api_nogc_env env,
1884
1945
napi_async_cleanup_hook hook,
1885
1946
void* arg,
1886
1947
napi_async_cleanup_hook_handle* remove_handle);
@@ -2038,7 +2099,7 @@ You can also use the `NAPI_MODULE_INIT` macro, which acts as a shorthand
2038
2099
for `NAPI_MODULE` and defining an `Init` function:
2039
2100
2040
2101
```c
2041
- NAPI_MODULE_INIT() {
2102
+ NAPI_MODULE_INIT(/* napi_env env, napi_value exports */ ) {
2042
2103
napi_value answer;
2043
2104
napi_status result;
2044
2105
@@ -2052,6 +2113,9 @@ NAPI_MODULE_INIT() {
2052
2113
}
2053
2114
```
2054
2115
2116
+ The parameters `env` and `exports` are provided to the body of the
2117
+ `NAPI_MODULE_INIT` macro.
2118
+
2055
2119
All Node-API addons are context-aware, meaning they may be loaded multiple
2056
2120
times. There are a few design considerations when declaring such a module.
2057
2121
The documentation on [context-aware addons][] provides more details.
@@ -5416,7 +5480,7 @@ napiVersion: 5
5416
5480
napi_status napi_add_finalizer(napi_env env,
5417
5481
napi_value js_object,
5418
5482
void* finalize_data,
5419
- napi_finalize finalize_cb,
5483
+ node_api_nogc_finalize finalize_cb,
5420
5484
void* finalize_hint,
5421
5485
napi_ref* result);
5422
5486
```
@@ -5454,7 +5518,7 @@ added: v20.10.0
5454
5518
> Stability: 1 - Experimental
5455
5519
5456
5520
```c
5457
- napi_status node_api_post_finalizer(napi_env env,
5521
+ napi_status node_api_post_finalizer(node_api_nogc_env env,
5458
5522
napi_finalize finalize_cb,
5459
5523
void* finalize_data,
5460
5524
void* finalize_hint);
@@ -5524,7 +5588,7 @@ Once created the async worker can be queued
5524
5588
for execution using the [`napi_queue_async_work`][] function:
5525
5589
5526
5590
```c
5527
- napi_status napi_queue_async_work(napi_env env,
5591
+ napi_status napi_queue_async_work(node_api_nogc_env env,
5528
5592
napi_async_work work);
5529
5593
```
5530
5594
@@ -5616,7 +5680,7 @@ napiVersion: 1
5616
5680
-->
5617
5681
5618
5682
```c
5619
- napi_status napi_queue_async_work(napi_env env,
5683
+ napi_status napi_queue_async_work(node_api_nogc_env env,
5620
5684
napi_async_work work);
5621
5685
```
5622
5686
@@ -5637,7 +5701,7 @@ napiVersion: 1
5637
5701
-->
5638
5702
5639
5703
```c
5640
- napi_status napi_cancel_async_work(napi_env env,
5704
+ napi_status napi_cancel_async_work(node_api_nogc_env env,
5641
5705
napi_async_work work);
5642
5706
```
5643
5707
@@ -5841,7 +5905,7 @@ typedef struct {
5841
5905
const char* release;
5842
5906
} napi_node_version;
5843
5907
5844
- napi_status napi_get_node_version(napi_env env,
5908
+ napi_status napi_get_node_version(node_api_nogc_env env,
5845
5909
const napi_node_version** version);
5846
5910
```
5847
5911
@@ -5864,7 +5928,7 @@ napiVersion: 1
5864
5928
-->
5865
5929
5866
5930
```c
5867
- napi_status napi_get_version(napi_env env,
5931
+ napi_status napi_get_version(node_api_nogc_env env,
5868
5932
uint32_t* result);
5869
5933
```
5870
5934
@@ -5897,7 +5961,7 @@ napiVersion: 1
5897
5961
-->
5898
5962
5899
5963
```c
5900
- NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
5964
+ NAPI_EXTERN napi_status napi_adjust_external_memory(node_api_nogc_env env,
5901
5965
int64_t change_in_bytes,
5902
5966
int64_t* result);
5903
5967
```
@@ -6114,7 +6178,7 @@ napiVersion: 2
6114
6178
-->
6115
6179
6116
6180
```c
6117
- NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
6181
+ NAPI_EXTERN napi_status napi_get_uv_event_loop(node_api_nogc_env env,
6118
6182
struct uv_loop_s** loop);
6119
6183
```
6120
6184
@@ -6428,7 +6492,7 @@ napiVersion: 4
6428
6492
6429
6493
```c
6430
6494
NAPI_EXTERN napi_status
6431
- napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
6495
+ napi_ref_threadsafe_function(node_api_nogc_env env, napi_threadsafe_function func);
6432
6496
```
6433
6497
6434
6498
* `[in] env`: The environment that the API is invoked under.
@@ -6454,7 +6518,7 @@ napiVersion: 4
6454
6518
6455
6519
```c
6456
6520
NAPI_EXTERN napi_status
6457
- napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
6521
+ napi_unref_threadsafe_function(node_api_nogc_env env, napi_threadsafe_function func);
6458
6522
```
6459
6523
6460
6524
* `[in] env`: The environment that the API is invoked under.
@@ -6480,7 +6544,7 @@ napiVersion: 9
6480
6544
6481
6545
```c
6482
6546
NAPI_EXTERN napi_status
6483
- node_api_get_module_file_name(napi_env env, const char** result);
6547
+ node_api_get_module_file_name(node_api_nogc_env env, const char** result);
6484
6548
6485
6549
```
6486
6550
@@ -6544,6 +6608,7 @@ the add-on's file name during loading.
6544
6608
[`Number.MIN_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.min_safe_integer
6545
6609
[`Worker`]: worker_threads.md#class-worker
6546
6610
[`async_hooks.executionAsyncResource()`]: async_hooks.md#async_hooksexecutionasyncresource
6611
+ [`build_with_cmake`]: https://github.com/nodejs/node-addon-examples/tree/main/build_with_cmake
6547
6612
[`global`]: globals.md#global
6548
6613
[`init` hooks]: async_hooks.md#initasyncid-type-triggerasyncid-resource
6549
6614
[`napi_add_async_cleanup_hook`]: #napi_add_async_cleanup_hook
@@ -6607,6 +6672,8 @@ the add-on's file name during loading.
6607
6672
[`node_api_create_external_string_latin1`]: #node_api_create_external_string_latin1
6608
6673
[`node_api_create_external_string_utf16`]: #node_api_create_external_string_utf16
6609
6674
[`node_api_create_syntax_error`]: #node_api_create_syntax_error
6675
+ [`node_api_nogc_finalize`]: #node_api_nogc_finalize
6676
+ [`node_api_post_finalizer`]: #node_api_post_finalizer
6610
6677
[`node_api_throw_syntax_error`]: #node_api_throw_syntax_error
6611
6678
[`process.release`]: process.md#processrelease
6612
6679
[`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref
0 commit comments