@@ -386,9 +386,9 @@ napi_status napi_set_instance_data(napi_env env,
386
386
* `[in] env`: The environment that the N-API call is invoked under.
387
387
* `[in] data`: The data item to make available to bindings of this instance.
388
388
* `[in] finalize_cb`: The function to call when the environment is being torn
389
- down. The function receives `data` so that it might free it.
390
- * `[in] finalize_hint`: Optional hint to pass to the finalize callback
391
- during collection.
389
+ down. The function receives `data` so that it might free it.
390
+ * `[in] finalize_hint`: Optional hint to pass to the finalize callback during
391
+ collection.
392
392
393
393
Returns `napi_ok` if the API succeeded.
394
394
@@ -410,7 +410,7 @@ napi_status napi_get_instance_data(napi_env env,
410
410
411
411
* `[in] env`: The environment that the N-API call is invoked under.
412
412
* `[out] data`: The data item that was previously associated with the currently
413
- running Agent by a call to `napi_set_instance_data()`.
413
+ running Agent by a call to `napi_set_instance_data()`.
414
414
415
415
Returns `napi_ok` if the API succeeded.
416
416
@@ -487,6 +487,7 @@ typedef struct {
487
487
See the [Error Handling][] section for additional information.
488
488
489
489
### napi_env
490
+
490
491
`napi_env` is used to represent a context that the underlying N-API
491
492
implementation can use to persist VM-specific state. This structure is passed
492
493
to native functions when they're invoked, and it must be passed back when
@@ -496,6 +497,7 @@ nested N-API calls. Caching the `napi_env` for the purpose of general reuse is
496
497
not allowed.
497
498
498
499
### napi_value
500
+
499
501
This is an opaque pointer that is used to represent a JavaScript value.
500
502
501
503
### napi_threadsafe_function
@@ -545,6 +547,7 @@ typedef enum {
545
547
546
548
### N-API Memory Management types
547
549
#### napi_handle_scope
550
+
548
551
This is an abstraction used to control and modify the lifetime of objects
549
552
created within a particular scope. In general, N-API values are created within
550
553
the context of a handle scope. When a native method is called from
@@ -685,23 +688,26 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
685
688
```
686
689
687
690
* `[in] env`: The environment to use for API calls, or `NULL` if the thread-safe
688
- function is being torn down and `data` may need to be freed.
691
+ function is being torn down and `data` may need to be freed.
689
692
* `[in] js_callback`: The JavaScript function to call, or `NULL` if the
690
- thread-safe function is being torn down and `data` may need to be freed. It may
691
- also be `NULL` if the thread-safe function was created without `js_callback`.
693
+ thread-safe function is being torn down and `data` may need to be freed. It
694
+ may also be `NULL` if the thread-safe function was created without
695
+ `js_callback`.
692
696
* `[in] context`: The optional data with which the thread-safe function was
693
- created.
697
+ created.
694
698
* `[in] data`: Data created by the secondary thread. It is the responsibility of
695
- the callback to convert this native data to JavaScript values (with N-API
696
- functions) that can be passed as parameters when `js_callback` is invoked. This
697
- pointer is managed entirely by the threads and this callback. Thus this callback
698
- should free the data.
699
+ the callback to convert this native data to JavaScript values (with N-API
700
+ functions) that can be passed as parameters when `js_callback` is invoked.
701
+ This pointer is managed entirely by the threads and this callback. Thus this
702
+ callback should free the data.
699
703
700
704
## Error Handling
705
+
701
706
N-API uses both return values and JavaScript exceptions for error handling.
702
707
The following sections explain the approach for each case.
703
708
704
709
### Return values
710
+
705
711
All of the N-API functions share the same error handling pattern. The
706
712
return type of all API functions is `napi_status`.
707
713
@@ -785,6 +791,7 @@ logging purposes.
785
791
This API can be called even if there is a pending JavaScript exception.
786
792
787
793
### Exceptions
794
+
788
795
Any N-API function call may result in a pending JavaScript exception. This is
789
796
obviously the case for any function that may cause the execution of
790
797
JavaScript, but N-API specifies that an exception may be pending
@@ -895,8 +902,7 @@ NAPI_EXTERN napi_status napi_throw_error(napi_env env,
895
902
896
903
* `[in] env`: The environment that the API is invoked under.
897
904
* `[in] code`: Optional error code to be set on the error.
898
- * `[in] msg`: C string representing the text to be associated with
899
- the error.
905
+ * `[in] msg`: C string representing the text to be associated with the error.
900
906
901
907
Returns `napi_ok` if the API succeeded.
902
908
@@ -916,8 +922,7 @@ NAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
916
922
917
923
* `[in] env`: The environment that the API is invoked under.
918
924
* `[in] code`: Optional error code to be set on the error.
919
- * `[in] msg`: C string representing the text to be associated with
920
- the error.
925
+ * `[in] msg`: C string representing the text to be associated with the error.
921
926
922
927
Returns `napi_ok` if the API succeeded.
923
928
@@ -937,8 +942,7 @@ NAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
937
942
938
943
* `[in] env`: The environment that the API is invoked under.
939
944
* `[in] code`: Optional error code to be set on the error.
940
- * `[in] msg`: C string representing the text to be associated with
941
- the error.
945
+ * `[in] msg`: C string representing the text to be associated with the error.
942
946
943
947
Returns `napi_ok` if the API succeeded.
944
948
@@ -959,7 +963,7 @@ NAPI_EXTERN napi_status napi_is_error(napi_env env,
959
963
* `[in] env`: The environment that the API is invoked under.
960
964
* `[in] value`: The `napi_value` to be checked.
961
965
* `[out] result`: Boolean value that is set to true if `napi_value` represents
962
- an error, false otherwise.
966
+ an error, false otherwise.
963
967
964
968
Returns `napi_ok` if the API succeeded.
965
969
@@ -979,10 +983,10 @@ NAPI_EXTERN napi_status napi_create_error(napi_env env,
979
983
```
980
984
981
985
* `[in] env`: The environment that the API is invoked under.
982
- * `[in] code`: Optional `napi_value` with the string for the error code to
983
- be associated with the error.
984
- * `[in] msg`: `napi_value` that references a JavaScript `String` to be
985
- used as the message for the `Error`.
986
+ * `[in] code`: Optional `napi_value` with the string for the error code to be
987
+ associated with the error.
988
+ * `[in] msg`: `napi_value` that references a JavaScript `String` to be used as
989
+ the message for the `Error`.
986
990
* `[out] result`: `napi_value` representing the error created.
987
991
988
992
Returns `napi_ok` if the API succeeded.
@@ -1003,10 +1007,10 @@ NAPI_EXTERN napi_status napi_create_type_error(napi_env env,
1003
1007
```
1004
1008
1005
1009
* `[in] env`: The environment that the API is invoked under.
1006
- * `[in] code`: Optional `napi_value` with the string for the error code to
1007
- be associated with the error.
1008
- * `[in] msg`: `napi_value` that references a JavaScript `String` to be
1009
- used as the message for the `Error`.
1010
+ * `[in] code`: Optional `napi_value` with the string for the error code to be
1011
+ associated with the error.
1012
+ * `[in] msg`: `napi_value` that references a JavaScript `String` to be used as
1013
+ the message for the `Error`.
1010
1014
* `[out] result`: `napi_value` representing the error created.
1011
1015
1012
1016
Returns `napi_ok` if the API succeeded.
@@ -1027,10 +1031,10 @@ NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
1027
1031
```
1028
1032
1029
1033
* `[in] env`: The environment that the API is invoked under.
1030
- * `[in] code`: Optional `napi_value` with the string for the error code to
1031
- be associated with the error.
1032
- * `[in] msg`: `napi_value` that references a JavaScript `String` to be
1033
- used as the message for the `Error`.
1034
+ * `[in] code`: Optional `napi_value` with the string for the error code to be
1035
+ associated with the error.
1036
+ * `[in] msg`: `napi_value` that references a JavaScript `String` to be used as
1037
+ the message for the `Error`.
1034
1038
* `[out] result`: `napi_value` representing the error created.
1035
1039
1036
1040
Returns `napi_ok` if the API succeeded.
@@ -1112,11 +1116,10 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
1112
1116
1113
1117
* `[in] location`: Optional location at which the error occurred.
1114
1118
* `[in] location_len`: The length of the location in bytes, or
1115
- `NAPI_AUTO_LENGTH` if it is null-terminated.
1119
+ `NAPI_AUTO_LENGTH` if it is null-terminated.
1116
1120
* `[in] message`: The message associated with the error.
1117
- * `[in] message_len`: The length of the message in bytes, or
1118
- `NAPI_AUTO_LENGTH` if it is
1119
- null-terminated.
1121
+ * `[in] message_len`: The length of the message in bytes, or `NAPI_AUTO_LENGTH`
1122
+ if it is null-terminated.
1120
1123
1121
1124
The function call does not return, the process will be terminated.
1122
1125
@@ -1309,9 +1312,9 @@ napi_status napi_escape_handle(napi_env env,
1309
1312
* `[in] env`: The environment that the API is invoked under.
1310
1313
* `[in] scope`: `napi_value` representing the current scope.
1311
1314
* `[in] escapee`: `napi_value` representing the JavaScript `Object` to be
1312
- escaped.
1313
- * `[out] result`: `napi_value` representing the handle to the escaped
1314
- `Object` in the outer scope.
1315
+ escaped.
1316
+ * `[out] result`: `napi_value` representing the handle to the escaped `Object`
1317
+ in the outer scope.
1315
1318
1316
1319
Returns `napi_ok` if the API succeeded.
1317
1320
@@ -1374,8 +1377,8 @@ NAPI_EXTERN napi_status napi_create_reference(napi_env env,
1374
1377
```
1375
1378
1376
1379
* `[in] env`: The environment that the API is invoked under.
1377
- * `[in] value`: `napi_value` representing the `Object` to which we want
1378
- a reference.
1380
+ * `[in] value`: `napi_value` representing the `Object` to which we want a
1381
+ reference.
1379
1382
* `[in] initial_refcount`: Initial reference count for the new reference.
1380
1383
* `[out] result`: `napi_ref` pointing to the new reference.
1381
1384
@@ -1463,7 +1466,7 @@ object to which the reference is related.
1463
1466
* `[in] env`: The environment that the API is invoked under.
1464
1467
* `[in] ref`: `napi_ref` for which we requesting the corresponding `Object`.
1465
1468
* `[out] result`: The `napi_value` for the `Object` referenced by the
1466
- `napi_ref`.
1469
+ `napi_ref`.
1467
1470
1468
1471
Returns `napi_ok` if the API succeeded.
1469
1472
@@ -1825,8 +1828,8 @@ napi_status napi_create_buffer_copy(napi_env env,
1825
1828
```
1826
1829
1827
1830
* `[in] env`: The environment that the API is invoked under.
1828
- * `[in] size`: Size in bytes of the input buffer (should be the same as the
1829
- size of the new buffer).
1831
+ * `[in] size`: Size in bytes of the input buffer (should be the same as the size
1832
+ of the new buffer).
1830
1833
* `[in] data`: Raw pointer to the underlying buffer to copy from.
1831
1834
* `[out] result_data`: Pointer to the new `Buffer`'s underlying data buffer.
1832
1835
* `[out] result`: A `napi_value` representing a `node::Buffer`.
@@ -1879,10 +1882,10 @@ napi_status napi_create_external(napi_env env,
1879
1882
1880
1883
* `[in] env`: The environment that the API is invoked under.
1881
1884
* `[in] data`: Raw pointer to the external data.
1882
- * `[in] finalize_cb`: Optional callback to call when the external value
1883
- is being collected.
1884
- * `[in] finalize_hint`: Optional hint to pass to the finalize callback
1885
- during collection.
1885
+ * `[in] finalize_cb`: Optional callback to call when the external value is being
1886
+ collected.
1887
+ * `[in] finalize_hint`: Optional hint to pass to the finalize callback during
1888
+ collection.
1886
1889
* `[out] result`: A `napi_value` representing an external value.
1887
1890
1888
1891
Returns `napi_ok` if the API succeeded.
@@ -1921,12 +1924,12 @@ napi_create_external_arraybuffer(napi_env env,
1921
1924
1922
1925
* `[in] env`: The environment that the API is invoked under.
1923
1926
* `[in] external_data`: Pointer to the underlying byte buffer of the
1924
- `ArrayBuffer`.
1927
+ `ArrayBuffer`.
1925
1928
* `[in] byte_length`: The length in bytes of the underlying buffer.
1926
- * `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is
1927
- being collected.
1928
- * `[in] finalize_hint`: Optional hint to pass to the finalize callback
1929
- during collection.
1929
+ * `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
1930
+ collected.
1931
+ * `[in] finalize_hint`: Optional hint to pass to the finalize callback during
1932
+ collection.
1930
1933
* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
1931
1934
1932
1935
Returns `napi_ok` if the API succeeded.
@@ -1963,13 +1966,13 @@ napi_status napi_create_external_buffer(napi_env env,
1963
1966
```
1964
1967
1965
1968
* `[in] env`: The environment that the API is invoked under.
1966
- * `[in] length`: Size in bytes of the input buffer (should be the same as
1967
- the size of the new buffer).
1969
+ * `[in] length`: Size in bytes of the input buffer (should be the same as the
1970
+ size of the new buffer).
1968
1971
* `[in] data`: Raw pointer to the underlying buffer to copy from.
1969
- * `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is
1970
- being collected.
1971
- * `[in] finalize_hint`: Optional hint to pass to the finalize callback
1972
- during collection.
1972
+ * `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
1973
+ collected.
1974
+ * `[in] finalize_hint`: Optional hint to pass to the finalize callback during
1975
+ collection.
1973
1976
* `[out] result`: A `napi_value` representing a `node::Buffer`.
1974
1977
1975
1978
Returns `napi_ok` if the API succeeded.
@@ -2023,7 +2026,7 @@ napi_status napi_create_symbol(napi_env env,
2023
2026
2024
2027
* `[in] env`: The environment that the API is invoked under.
2025
2028
* `[in] description`: Optional `napi_value` which refers to a JavaScript
2026
- `String` to be set as the description for the symbol.
2029
+ `String` to be set as the description for the symbol.
2027
2030
* `[out] result`: A `napi_value` representing a JavaScript `Symbol`.
2028
2031
2029
2032
Returns `napi_ok` if the API succeeded.
@@ -2053,7 +2056,7 @@ napi_status napi_create_typedarray(napi_env env,
2053
2056
* `[in] length`: Number of elements in the `TypedArray`.
2054
2057
* `[in] arraybuffer`: `ArrayBuffer` underlying the typed array.
2055
2058
* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
2056
- start projecting the `TypedArray`.
2059
+ start projecting the `TypedArray`.
2057
2060
* `[out] result`: A `napi_value` representing a JavaScript `TypedArray`.
2058
2061
2059
2062
Returns `napi_ok` if the API succeeded.
@@ -2283,8 +2286,8 @@ napi_status napi_create_string_latin1(napi_env env,
2283
2286
2284
2287
* `[in] env`: The environment that the API is invoked under.
2285
2288
* `[in] str`: Character buffer representing an ISO-8859-1-encoded string.
2286
- * `[in] length`: The length of the string in bytes, or
2287
- `NAPI_AUTO_LENGTH` if it is null-terminated.
2289
+ * `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
2290
+ is null-terminated.
2288
2291
* `[out] result`: A `napi_value` representing a JavaScript `String`.
2289
2292
2290
2293
Returns `napi_ok` if the API succeeded.
@@ -2311,7 +2314,7 @@ napi_status napi_create_string_utf16(napi_env env,
2311
2314
* `[in] env`: The environment that the API is invoked under.
2312
2315
* `[in] str`: Character buffer representing a UTF16-LE-encoded string.
2313
2316
* `[in] length`: The length of the string in two-byte code units, or
2314
- `NAPI_AUTO_LENGTH` if it is null-terminated.
2317
+ `NAPI_AUTO_LENGTH` if it is null-terminated.
2315
2318
* `[out] result`: A `napi_value` representing a JavaScript `String`.
2316
2319
2317
2320
Returns `napi_ok` if the API succeeded.
@@ -2337,8 +2340,8 @@ napi_status napi_create_string_utf8(napi_env env,
2337
2340
2338
2341
* `[in] env`: The environment that the API is invoked under.
2339
2342
* `[in] str`: Character buffer representing a UTF8-encoded string.
2340
- * `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH`
2341
- if it is null-terminated.
2343
+ * `[in] length`: The length of the string in bytes, or `NAPI_AUTO_LENGTH` if it
2344
+ is null-terminated.
2342
2345
* `[out] result`: A `napi_value` representing a JavaScript `String`.
2343
2346
2344
2347
Returns `napi_ok` if the API succeeded.
@@ -2364,7 +2367,7 @@ napi_status napi_get_array_length(napi_env env,
2364
2367
2365
2368
* `[in] env`: The environment that the API is invoked under.
2366
2369
* `[in] value`: `napi_value` representing the JavaScript `Array` whose length is
2367
- being queried.
2370
+ being queried.
2368
2371
* `[out] result`: `uint32` representing length of the array.
2369
2372
2370
2373
Returns `napi_ok` if the API succeeded.
@@ -2445,8 +2448,8 @@ napi_status napi_get_prototype(napi_env env,
2445
2448
2446
2449
* `[in] env`: The environment that the API is invoked under.
2447
2450
* `[in] object`: `napi_value` representing JavaScript `Object` whose prototype
2448
- to return. This returns the equivalent of `Object.getPrototypeOf` (which is
2449
- not the same as the function's `prototype` property).
2451
+ to return. This returns the equivalent of `Object.getPrototypeOf` (which is
2452
+ not the same as the function's `prototype` property).
2450
2453
* `[out] result`: `napi_value` representing prototype of the given object.
2451
2454
2452
2455
Returns `napi_ok` if the API succeeded.
@@ -2531,8 +2534,8 @@ napi_status napi_get_date_value(napi_env env,
2531
2534
2532
2535
* `[in] env`: The environment that the API is invoked under.
2533
2536
* `[in] value`: `napi_value` representing a JavaScript `Date`.
2534
- * `[out] result`: Time value as a `double` represented as milliseconds
2535
- since midnight at the beginning of 01 January, 1970 UTC.
2537
+ * `[out] result`: Time value as a `double` represented as milliseconds since
2538
+ midnight at the beginning of 01 January, 1970 UTC.
2536
2539
2537
2540
This API does not observe leap seconds; they are ignored, as
2538
2541
ECMAScript aligns with POSIX time specification.
@@ -2556,7 +2559,7 @@ napi_status napi_get_value_bool(napi_env env, napi_value value, bool* result)
2556
2559
* `[in] env`: The environment that the API is invoked under.
2557
2560
* `[in] value`: `napi_value` representing JavaScript `Boolean`.
2558
2561
* `[out] result`: C boolean primitive equivalent of the given JavaScript
2559
- `Boolean`.
2562
+ `Boolean`.
2560
2563
2561
2564
Returns `napi_ok` if the API succeeded. If a non-boolean `napi_value` is
2562
2565
passed in it returns `napi_boolean_expected`.
@@ -2579,7 +2582,7 @@ napi_status napi_get_value_double(napi_env env,
2579
2582
* `[in] env`: The environment that the API is invoked under.
2580
2583
* `[in] value`: `napi_value` representing JavaScript `Number`.
2581
2584
* `[out] result`: C double primitive equivalent of the given JavaScript
2582
- `Number`.
2585
+ `Number`.
2583
2586
2584
2587
Returns `napi_ok` if the API succeeded. If a non-number `napi_value` is passed
2585
2588
in it returns `napi_number_expected`.
@@ -2769,11 +2772,11 @@ napi_status napi_get_value_string_latin1(napi_env env,
2769
2772
* `[in] env`: The environment that the API is invoked under.
2770
2773
* `[in] value`: `napi_value` representing JavaScript string.
2771
2774
* `[in] buf`: Buffer to write the ISO-8859-1-encoded string into. If NULL is
2772
- passed in, the length of the string (in bytes) is returned.
2775
+ passed in, the length of the string (in bytes) is returned.
2773
2776
* `[in] bufsize`: Size of the destination buffer. When this value is
2774
- insufficient, the returned string will be truncated.
2777
+ insufficient, the returned string will be truncated.
2775
2778
* `[out] result`: Number of bytes copied into the buffer, excluding the null
2776
- terminator.
2779
+ terminator.
2777
2780
2778
2781
Returns `napi_ok` if the API succeeded. If a non-`String` `napi_value`
2779
2782
is passed in it returns `napi_string_expected`.
@@ -2798,11 +2801,11 @@ napi_status napi_get_value_string_utf8(napi_env env,
2798
2801
* `[in] env`: The environment that the API is invoked under.
2799
2802
* `[in] value`: `napi_value` representing JavaScript string.
2800
2803
* `[in] buf`: Buffer to write the UTF8-encoded string into. If NULL is passed
2801
- in, the length of the string (in bytes) is returned.
2804
+ in, the length of the string (in bytes) is returned.
2802
2805
* `[in] bufsize`: Size of the destination buffer. When this value is
2803
- insufficient, the returned string will be truncated.
2806
+ insufficient, the returned string will be truncated.
2804
2807
* `[out] result`: Number of bytes copied into the buffer, excluding the null
2805
- terminator.
2808
+ terminator.
2806
2809
2807
2810
Returns `napi_ok` if the API succeeded. If a non-`String` `napi_value`
2808
2811
is passed in it returns `napi_string_expected`.
@@ -2826,11 +2829,11 @@ napi_status napi_get_value_string_utf16(napi_env env,
2826
2829
* `[in] env`: The environment that the API is invoked under.
2827
2830
* `[in] value`: `napi_value` representing JavaScript string.
2828
2831
* `[in] buf`: Buffer to write the UTF16-LE-encoded string into. If NULL is
2829
- passed in, the length of the string (in 2-byte code units) is returned.
2832
+ passed in, the length of the string (in 2-byte code units) is returned.
2830
2833
* `[in] bufsize`: Size of the destination buffer. When this value is
2831
- insufficient, the returned string will be truncated.
2834
+ insufficient, the returned string will be truncated.
2832
2835
* `[out] result`: Number of 2-byte code units copied into the buffer, excluding
2833
- the null terminator.
2836
+ the null terminator.
2834
2837
2835
2838
Returns `napi_ok` if the API succeeded. If a non-`String` `napi_value`
2836
2839
is passed in it returns `napi_string_expected`.
@@ -2852,7 +2855,7 @@ napi_status napi_get_value_uint32(napi_env env,
2852
2855
* `[in] env`: The environment that the API is invoked under.
2853
2856
* `[in] value`: `napi_value` representing JavaScript `Number`.
2854
2857
* `[out] result`: C primitive equivalent of the given `napi_value` as a
2855
- `uint32_t`.
2858
+ `uint32_t`.
2856
2859
2857
2860
Returns `napi_ok` if the API succeeded. If a non-number `napi_value`
2858
2861
is passed in it returns `napi_number_expected`.
@@ -2874,7 +2877,7 @@ napi_status napi_get_boolean(napi_env env, bool value, napi_value* result)
2874
2877
* `[in] env`: The environment that the API is invoked under.
2875
2878
* `[in] value`: The value of the boolean to retrieve.
2876
2879
* `[out] result`: `napi_value` representing JavaScript `Boolean` singleton to
2877
- retrieve.
2880
+ retrieve.
2878
2881
2879
2882
Returns `napi_ok` if the API succeeded.
2880
2883
@@ -3072,10 +3075,10 @@ napi_status napi_instanceof(napi_env env,
3072
3075
3073
3076
* `[in] env`: The environment that the API is invoked under.
3074
3077
* `[in] object`: The JavaScript value to check.
3075
- * `[in] constructor`: The JavaScript function object of the constructor
3076
- function to check against.
3078
+ * `[in] constructor`: The JavaScript function object of the constructor function
3079
+ to check against.
3077
3080
* `[out] result`: Boolean that is set to true if `object instanceof constructor`
3078
- is true.
3081
+ is true.
3079
3082
3080
3083
Returns `napi_ok` if the API succeeded.
3081
3084
@@ -3132,7 +3135,7 @@ napi_status napi_is_buffer(napi_env env, napi_value value, bool* result)
3132
3135
* `[in] env`: The environment that the API is invoked under.
3133
3136
* `[in] value`: The JavaScript value to check.
3134
3137
* `[out] result`: Whether the given `napi_value` represents a `node::Buffer`
3135
- object.
3138
+ object.
3136
3139
3137
3140
Returns `napi_ok` if the API succeeded.
3138
3141
@@ -3151,7 +3154,7 @@ napi_status napi_is_date(napi_env env, napi_value value, bool* result)
3151
3154
* `[in] env`: The environment that the API is invoked under.
3152
3155
* `[in] value`: The JavaScript value to check.
3153
3156
* `[out] result`: Whether the given `napi_value` represents a JavaScript `Date`
3154
- object.
3157
+ object.
3155
3158
3156
3159
Returns `napi_ok` if the API succeeded.
3157
3160
@@ -3451,32 +3454,32 @@ typedef struct {
3451
3454
```
3452
3455
3453
3456
* `utf8name`: Optional `String` describing the key for the property,
3454
- encoded as UTF8. One of `utf8name` or `name` must be provided for the
3455
- property.
3457
+ encoded as UTF8. One of `utf8name` or `name` must be provided for the
3458
+ property.
3456
3459
* `name`: Optional `napi_value` that points to a JavaScript string or symbol
3457
- to be used as the key for the property. One of `utf8name` or `name` must
3458
- be provided for the property.
3460
+ to be used as the key for the property. One of `utf8name` or `name` must
3461
+ be provided for the property.
3459
3462
* `value`: The value that's retrieved by a get access of the property if the
3460
- property is a data property. If this is passed in, set `getter`, `setter`,
3461
- `method` and `data` to `NULL` (since these members won't be used).
3463
+ property is a data property. If this is passed in, set `getter`, `setter`,
3464
+ `method` and `data` to `NULL` (since these members won't be used).
3462
3465
* `getter`: A function to call when a get access of the property is performed.
3463
- If this is passed in, set `value` and `method` to `NULL` (since these members
3464
- won't be used). The given function is called implicitly by the runtime when the
3465
- property is accessed from JavaScript code (or if a get on the property is
3466
- performed using a N-API call).
3466
+ If this is passed in, set `value` and `method` to `NULL` (since these members
3467
+ won't be used). The given function is called implicitly by the runtime when
3468
+ the property is accessed from JavaScript code (or if a get on the property is
3469
+ performed using a N-API call).
3467
3470
* `setter`: A function to call when a set access of the property is performed.
3468
- If this is passed in, set `value` and `method` to `NULL` (since these members
3469
- won't be used). The given function is called implicitly by the runtime when the
3470
- property is set from JavaScript code (or if a set on the property is
3471
- performed using a N-API call).
3471
+ If this is passed in, set `value` and `method` to `NULL` (since these members
3472
+ won't be used). The given function is called implicitly by the runtime when
3473
+ the property is set from JavaScript code (or if a set on the property is
3474
+ performed using a N-API call).
3472
3475
* `method`: Set this to make the property descriptor object's `value`
3473
- property to be a JavaScript function represented by `method`. If this is
3474
- passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3475
- won't be used).
3476
- * `attributes`: The attributes associated with the particular property.
3477
- See [`napi_property_attributes`][].
3478
- * `data`: The callback data passed into `method`, `getter` and `setter` if
3479
- this function is invoked.
3476
+ property to be a JavaScript function represented by `method`. If this is
3477
+ passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3478
+ won't be used).
3479
+ * `attributes`: The attributes associated with the particular property. See
3480
+ [`napi_property_attributes`][].
3481
+ * `data`: The callback data passed into `method`, `getter` and `setter` if this
3482
+ function is invoked.
3480
3483
3481
3484
### Functions
3482
3485
#### napi_get_property_names
@@ -3494,9 +3497,9 @@ napi_status napi_get_property_names(napi_env env,
3494
3497
* `[in] env`: The environment that the N-API call is invoked under.
3495
3498
* `[in] object`: The object from which to retrieve the properties.
3496
3499
* `[out] result`: A `napi_value` representing an array of JavaScript values
3497
- that represent the property names of the object. The API can be used to
3498
- iterate over `result` using [`napi_get_array_length`][]
3499
- and [`napi_get_element`][].
3500
+ that represent the property names of the object. The API can be used to
3501
+ iterate over `result` using [`napi_get_array_length`][]
3502
+ and [`napi_get_element`][].
3500
3503
3501
3504
Returns `napi_ok` if the API succeeded.
3502
3505
@@ -3587,7 +3590,7 @@ napi_status napi_delete_property(napi_env env,
3587
3590
* `[in] object`: The object to query.
3588
3591
* `[in] key`: The name of the property to delete.
3589
3592
* `[out] result`: Whether the property deletion succeeded or not. `result` can
3590
- optionally be ignored by passing `NULL`.
3593
+ optionally be ignored by passing `NULL`.
3591
3594
3592
3595
Returns `napi_ok` if the API succeeded.
3593
3596
@@ -3770,7 +3773,7 @@ napi_status napi_delete_element(napi_env env,
3770
3773
* `[in] object`: The object to query.
3771
3774
* `[in] index`: The index of the property to delete.
3772
3775
* `[out] result`: Whether the element deletion succeeded or not. `result` can
3773
- optionally be ignored by passing `NULL`.
3776
+ optionally be ignored by passing `NULL`.
3774
3777
3775
3778
Returns `napi_ok` if the API succeeded.
3776
3779
@@ -3844,11 +3847,10 @@ NAPI_EXTERN napi_status napi_call_function(napi_env env,
3844
3847
3845
3848
* `[in] env`: The environment that the API is invoked under.
3846
3849
* `[in] recv`: The `this` object passed to the called function.
3847
- * `[in] func`: `napi_value` representing the JavaScript function
3848
- to be invoked.
3850
+ * `[in] func`: `napi_value` representing the JavaScript function to be invoked.
3849
3851
* `[in] argc`: The count of elements in the `argv` array.
3850
- * `[in] argv`: Array of `napi_values` representing JavaScript values passed
3851
- in as arguments to the function.
3852
+ * `[in] argv`: Array of `napi_values` representing JavaScript values passed in
3853
+ as arguments to the function.
3852
3854
* `[out] result`: `napi_value` representing the JavaScript object returned.
3853
3855
3854
3856
Returns `napi_ok` if the API succeeded.
@@ -3914,15 +3916,15 @@ napi_status napi_create_function(napi_env env,
3914
3916
3915
3917
* `[in] env`: The environment that the API is invoked under.
3916
3918
* `[in] utf8Name`: The name of the function encoded as UTF8. This is visible
3917
- within JavaScript as the new function object's `name` property.
3918
- * `[in] length`: The length of the `utf8name` in bytes, or
3919
- `NAPI_AUTO_LENGTH` if it is null-terminated.
3919
+ within JavaScript as the new function object's `name` property.
3920
+ * `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
3921
+ it is null-terminated.
3920
3922
* `[in] cb`: The native function which should be called when this function
3921
- object is invoked.
3923
+ object is invoked.
3922
3924
* `[in] data`: User-provided data context. This will be passed back into the
3923
- function when invoked later.
3925
+ function when invoked later.
3924
3926
* `[out] result`: `napi_value` representing the JavaScript function object for
3925
- the newly created function.
3927
+ the newly created function.
3926
3928
3927
3929
Returns `napi_ok` if the API succeeded.
3928
3930
@@ -3995,13 +3997,13 @@ napi_status napi_get_cb_info(napi_env env,
3995
3997
3996
3998
* `[in] env`: The environment that the API is invoked under.
3997
3999
* `[in] cbinfo`: The callback info passed into the callback function.
3998
- * `[in-out] argc`: Specifies the size of the provided `argv` array
3999
- and receives the actual count of arguments.
4000
- * `[out] argv`: Buffer to which the `napi_value` representing the
4001
- arguments are copied. If there are more arguments than the provided
4002
- count, only the requested number of arguments are copied. If there are fewer
4003
- arguments provided than claimed, the rest of `argv` is filled with `napi_value`
4004
- values that represent `undefined`.
4000
+ * `[in-out] argc`: Specifies the size of the provided `argv` array and receives
4001
+ the actual count of arguments.
4002
+ * `[out] argv`: Buffer to which the `napi_value` representing the arguments are
4003
+ copied. If there are more arguments than the provided count, only the
4004
+ requested number of arguments are copied. If there are fewer arguments
4005
+ provided than claimed, the rest of `argv` is filled with `napi_value` values
4006
+ that represent `undefined`.
4005
4007
* `[out] this`: Receives the JavaScript `this` argument for the call.
4006
4008
* `[out] data`: Receives the data pointer for the callback.
4007
4009
@@ -4046,13 +4048,13 @@ napi_status napi_new_instance(napi_env env,
4046
4048
```
4047
4049
4048
4050
* `[in] env`: The environment that the API is invoked under.
4049
- * `[in] cons`: `napi_value` representing the JavaScript function
4050
- to be invoked as a constructor.
4051
+ * `[in] cons`: `napi_value` representing the JavaScript function to be invoked
4052
+ as a constructor.
4051
4053
* `[in] argc`: The count of elements in the `argv` array.
4052
- * `[in] argv`: Array of JavaScript values as `napi_value`
4053
- representing the arguments to the constructor.
4054
+ * `[in] argv`: Array of JavaScript values as `napi_value` representing the
4055
+ arguments to the constructor.
4054
4056
* `[out] result`: `napi_value` representing the JavaScript object returned,
4055
- which in this case is the constructed object.
4057
+ which in this case is the constructed object.
4056
4058
4057
4059
This method is used to instantiate a new JavaScript value using a given
4058
4060
`napi_value` that represents the constructor for the object. For example,
@@ -4423,18 +4425,18 @@ napi_status napi_create_async_work(napi_env env,
4423
4425
* `[in] env`: The environment that the API is invoked under.
4424
4426
* `[in] async_resource`: An optional object associated with the async work
4425
4427
that will be passed to possible `async_hooks` [`init` hooks][].
4426
- * `[in] async_resource_name`: Identifier for the kind of resource that is
4427
- being provided for diagnostic information exposed by the `async_hooks` API.
4428
- * `[in] execute`: The native function which should be called to execute
4429
- the logic asynchronously. The given function is called from a worker pool
4430
- thread and can execute in parallel with the main event loop thread.
4428
+ * `[in] async_resource_name`: Identifier for the kind of resource that is being
4429
+ provided for diagnostic information exposed by the `async_hooks` API.
4430
+ * `[in] execute`: The native function which should be called to execute the
4431
+ logic asynchronously. The given function is called from a worker pool thread
4432
+ and can execute in parallel with the main event loop thread.
4431
4433
* `[in] complete`: The native function which will be called when the
4432
- asynchronous logic is completed or is cancelled. The given function is called
4433
- from the main event loop thread.
4434
+ asynchronous logic is completed or is cancelled. The given function is called
4435
+ from the main event loop thread.
4434
4436
* `[in] data`: User-provided data context. This will be passed back into the
4435
- execute and complete functions.
4437
+ execute and complete functions.
4436
4438
* `[out] result`: `napi_async_work*` which is the handle to the newly created
4437
- async work.
4439
+ async work.
4438
4440
4439
4441
Returns `napi_ok` if the API succeeded.
4440
4442
@@ -4515,6 +4517,7 @@ callback invocation, even if it has been successfully cancelled.
4515
4517
This API can be called even if there is a pending JavaScript exception.
4516
4518
4517
4519
## Custom Asynchronous Operations
4520
+
4518
4521
The simple asynchronous work APIs above may not be appropriate for every
4519
4522
scenario. When using any other asynchronous mechanism, the following APIs
4520
4523
are necessary to ensure an asynchronous operation is properly tracked by
@@ -4587,11 +4590,10 @@ NAPI_EXTERN napi_status napi_make_callback(napi_env env,
4587
4590
which indicates the current async context (if any) is to be used
4588
4591
for the callback.
4589
4592
* `[in] recv`: The `this` object passed to the called function.
4590
- * `[in] func`: `napi_value` representing the JavaScript function
4591
- to be invoked.
4593
+ * `[in] func`: `napi_value` representing the JavaScript function to be invoked.
4592
4594
* `[in] argc`: The count of elements in the `argv` array.
4593
- * `[in] argv`: Array of JavaScript values as `napi_value`
4594
- representing the arguments to the function.
4595
+ * `[in] argv`: Array of JavaScript values as `napi_value` representing the
4596
+ arguments to the function.
4595
4597
* `[out] result`: `napi_value` representing the JavaScript object returned.
4596
4598
4597
4599
Returns `napi_ok` if the API succeeded.
@@ -4625,9 +4627,8 @@ NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
4625
4627
* `[in] env`: The environment that the API is invoked under.
4626
4628
* `[in] resource_object`: An object associated with the async work
4627
4629
that will be passed to possible `async_hooks` [`init` hooks][].
4628
- * `[in] context`: Context for the async operation that is
4629
- invoking the callback. This should be a value previously obtained
4630
- from [`napi_async_init`][].
4630
+ * `[in] context`: Context for the async operation that is invoking the callback.
4631
+ This should be a value previously obtained from [`napi_async_init`][].
4631
4632
* `[out] result`: The newly created scope.
4632
4633
4633
4634
There are cases (for example, resolving promises) where it is
@@ -4729,8 +4730,8 @@ NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
4729
4730
```
4730
4731
4731
4732
* `[in] env`: The environment that the API is invoked under.
4732
- * `[in] change_in_bytes`: The change in externally allocated memory that is
4733
- kept alive by JavaScript objects.
4733
+ * `[in] change_in_bytes`: The change in externally allocated memory that is kept
4734
+ alive by JavaScript objects.
4734
4735
* `[out] result`: The adjusted value
4735
4736
4736
4737
Returns `napi_ok` if the API succeeded.
@@ -4811,8 +4812,8 @@ napi_status napi_create_promise(napi_env env,
4811
4812
4812
4813
* `[in] env`: The environment that the API is invoked under.
4813
4814
* `[out] deferred`: A newly created deferred object which can later be passed to
4814
- `napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject
4815
- the associated promise.
4815
+ `napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject
4816
+ the associated promise.
4816
4817
* `[out] promise`: The JavaScript promise associated with the deferred object.
4817
4818
4818
4819
Returns `napi_ok` if the API succeeded.
@@ -5062,25 +5063,25 @@ napi_create_threadsafe_function(napi_env env,
5062
5063
```
5063
5064
5064
5065
* `[in] env`: The environment that the API is invoked under.
5065
- * `[in] func`: An optional JavaScript function to call from another thread.
5066
- It must be provided if `NULL` is passed to `call_js_cb`.
5066
+ * `[in] func`: An optional JavaScript function to call from another thread. It
5067
+ must be provided if `NULL` is passed to `call_js_cb`.
5067
5068
* `[in] async_resource`: An optional object associated with the async work that
5068
- will be passed to possible `async_hooks` [`init` hooks][].
5069
+ will be passed to possible `async_hooks` [`init` hooks][].
5069
5070
* `[in] async_resource_name`: A JavaScript string to provide an identifier for
5070
- the kind of resource that is being provided for diagnostic information exposed
5071
- by the `async_hooks` API.
5071
+ the kind of resource that is being provided for diagnostic information exposed
5072
+ by the `async_hooks` API.
5072
5073
* `[in] max_queue_size`: Maximum size of the queue. `0` for no limit.
5073
5074
* `[in] initial_thread_count`: The initial number of threads, including the main
5074
- thread, which will be making use of this function.
5075
+ thread, which will be making use of this function.
5075
5076
* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
5076
5077
* `[in] thread_finalize_cb`: Optional function to call when the
5077
- `napi_threadsafe_function` is being destroyed.
5078
+ `napi_threadsafe_function` is being destroyed.
5078
5079
* `[in] context`: Optional data to attach to the resulting
5079
- `napi_threadsafe_function`.
5080
+ `napi_threadsafe_function`.
5080
5081
* `[in] call_js_cb`: Optional callback which calls the JavaScript function in
5081
- response to a call on a different thread. This callback will be called on the
5082
- main thread. If not given, the JavaScript function will be called with no
5083
- parameters and with `undefined` as its `this` value.
5082
+ response to a call on a different thread. This callback will be called on the
5083
+ main thread. If not given, the JavaScript function will be called with no
5084
+ parameters and with `undefined` as its `this` value.
5084
5085
* `[out] result`: The asynchronous thread-safe JavaScript function.
5085
5086
5086
5087
### napi_get_threadsafe_function_context
@@ -5117,11 +5118,11 @@ napi_call_threadsafe_function(napi_threadsafe_function func,
5117
5118
5118
5119
* `[in] func`: The asynchronous thread-safe JavaScript function to invoke.
5119
5120
* `[in] data`: Data to send into JavaScript via the callback `call_js_cb`
5120
- provided during the creation of the thread-safe JavaScript function.
5121
+ provided during the creation of the thread-safe JavaScript function.
5121
5122
* `[in] is_blocking`: Flag whose value can be either `napi_tsfn_blocking` to
5122
- indicate that the call should block if the queue is full or
5123
- `napi_tsfn_nonblocking` to indicate that the call should return immediately with
5124
- a status of `napi_queue_full` whenever the queue is full.
5123
+ indicate that the call should block if the queue is full or
5124
+ `napi_tsfn_nonblocking` to indicate that the call should return immediately
5125
+ with a status of `napi_queue_full` whenever the queue is full.
5125
5126
5126
5127
This API will return `napi_closing` if `napi_release_threadsafe_function()` was
5127
5128
called with `abort` set to `napi_tsfn_abort` from any thread. The value is only
@@ -5142,7 +5143,7 @@ napi_acquire_threadsafe_function(napi_threadsafe_function func);
5142
5143
```
5143
5144
5144
5145
* `[in] func`: The asynchronous thread-safe JavaScript function to start making
5145
- use of.
5146
+ use of.
5146
5147
5147
5148
A thread should call this API before passing `func` to any other thread-safe
5148
5149
function APIs to indicate that it will be making use of `func`. This prevents
@@ -5165,13 +5166,14 @@ napi_release_threadsafe_function(napi_threadsafe_function func,
5165
5166
```
5166
5167
5167
5168
* `[in] func`: The asynchronous thread-safe JavaScript function whose reference
5168
- count to decrement.
5169
+ count to decrement.
5169
5170
* `[in] mode`: Flag whose value can be either `napi_tsfn_release` to indicate
5170
- that the current thread will make no further calls to the thread-safe function,
5171
- or `napi_tsfn_abort` to indicate that in addition to the current thread, no
5172
- other thread should make any further calls to the thread-safe function. If set
5173
- to `napi_tsfn_abort`, further calls to `napi_call_threadsafe_function()` will
5174
- return `napi_closing`, and no further values will be placed in the queue.
5171
+ that the current thread will make no further calls to the thread-safe
5172
+ function, or `napi_tsfn_abort` to indicate that in addition to the current
5173
+ thread, no other thread should make any further calls to the thread-safe
5174
+ function. If set to `napi_tsfn_abort`, further calls to
5175
+ `napi_call_threadsafe_function()` will return `napi_closing`, and no further
5176
+ values will be placed in the queue.
5175
5177
5176
5178
A thread should call this API when it stops making use of `func`. Passing `func`
5177
5179
to any thread-safe APIs after having called this API has undefined results, as
0 commit comments