Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d22ae3

Browse filesBrowse files
Gabriel Schulhofaddaleax
authored andcommitted
doc: document n-api callback scope usage
Document that it is not necessary to open handle and/or callback scopes inside finalizer, async work, thread-safe function etc. callbacks unless for reasons documented in the section about object lifetime management. Link usage of callback signatures to their definition. Fixes: #33893 PR-URL: #33915 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 88d12c0 commit 6d22ae3
Copy full SHA for 6d22ae3

File tree

Expand file treeCollapse file tree

1 file changed

+36
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-16
lines changed
Open diff view settings
Collapse file

‎doc/api/n-api.md‎

Copy file name to clipboardExpand all lines: doc/api/n-api.md
+36-16Lines changed: 36 additions & 16 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ napi_status napi_set_instance_data(napi_env env,
389389
* `[in] data`: The data item to make available to bindings of this instance.
390390
* `[in] finalize_cb`: The function to call when the environment is being torn
391391
down. The function receives `data` so that it might free it.
392+
[`napi_finalize`][] provides more details.
392393
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
393394
collection.
394395

@@ -592,6 +593,7 @@ minimum lifetimes explicitly.
592593
For more details, review the [Object lifetime management][].
593594

594595
### N-API callback types
596+
595597
#### napi_callback_info
596598
<!-- YAML
597599
added: v8.0.0
@@ -614,6 +616,9 @@ following signature:
614616
typedef napi_value (*napi_callback)(napi_env, napi_callback_info);
615617
```
616618

619+
Unless for reasons discussed in [Object Lifetime Management][], creating a
620+
handle and/or callback scope inside a `napi_callback` is not necessary.
621+
617622
#### napi_finalize
618623
<!-- YAML
619624
added: v8.0.0
@@ -632,6 +637,9 @@ typedef void (*napi_finalize)(napi_env env,
632637
void* finalize_hint);
633638
```
634639

640+
Unless for reasons discussed in [Object Lifetime Management][], creating a
641+
handle and/or callback scope inside the function body is not necessary.
642+
635643
#### napi_async_execute_callback
636644
<!-- YAML
637645
added: v8.0.0
@@ -644,12 +652,10 @@ operations. Callback functions must satisfy the following signature:
644652
typedef void (*napi_async_execute_callback)(napi_env env, void* data);
645653
```
646654

647-
Implementations of this function must avoid making N-API calls
648-
that execute JavaScript or interact with
649-
JavaScript objects. N-API
650-
calls should be in the `napi_async_complete_callback` instead.
651-
Do not use the `napi_env` parameter as it will likely
652-
result in execution of JavaScript.
655+
Implementations of this function must avoid making N-API calls that execute
656+
JavaScript or interact with JavaScript objects. N-API calls should be in the
657+
`napi_async_complete_callback` instead. Do not use the `napi_env` parameter as
658+
it will likely result in execution of JavaScript.
653659

654660
#### napi_async_complete_callback
655661
<!-- YAML
@@ -665,6 +671,9 @@ typedef void (*napi_async_complete_callback)(napi_env env,
665671
void* data);
666672
```
667673

674+
Unless for reasons discussed in [Object Lifetime Management][], creating a
675+
handle and/or callback scope inside the function body is not necessary.
676+
668677
#### napi_threadsafe_function_call_js
669678
<!-- YAML
670679
added: v10.6.0
@@ -708,6 +717,9 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
708717
This pointer is managed entirely by the threads and this callback. Thus this
709718
callback should free the data.
710719

720+
Unless for reasons discussed in [Object Lifetime Management][], creating a
721+
handle and/or callback scope inside the function body is not necessary.
722+
711723
## Error handling
712724

713725
N-API uses both return values and JavaScript exceptions for error handling.
@@ -1942,7 +1954,7 @@ napi_status napi_create_external(napi_env env,
19421954
* `[in] env`: The environment that the API is invoked under.
19431955
* `[in] data`: Raw pointer to the external data.
19441956
* `[in] finalize_cb`: Optional callback to call when the external value is being
1945-
collected.
1957+
collected. [`napi_finalize`][] provides more details.
19461958
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
19471959
collection.
19481960
* `[out] result`: A `napi_value` representing an external value.
@@ -1986,7 +1998,7 @@ napi_create_external_arraybuffer(napi_env env,
19861998
`ArrayBuffer`.
19871999
* `[in] byte_length`: The length in bytes of the underlying buffer.
19882000
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
1989-
collected.
2001+
collected. [`napi_finalize`][] provides more details.
19902002
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
19912003
collection.
19922004
* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
@@ -2029,7 +2041,7 @@ napi_status napi_create_external_buffer(napi_env env,
20292041
size of the new buffer).
20302042
* `[in] data`: Raw pointer to the underlying buffer to copy from.
20312043
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2032-
collected.
2044+
collected. [`napi_finalize`][] provides more details.
20332045
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
20342046
collection.
20352047
* `[out] result`: A `napi_value` representing a `node::Buffer`.
@@ -3548,16 +3560,16 @@ typedef struct {
35483560
If this is passed in, set `value` and `method` to `NULL` (since these members
35493561
won't be used). The given function is called implicitly by the runtime when
35503562
the property is accessed from JavaScript code (or if a get on the property is
3551-
performed using a N-API call).
3563+
performed using a N-API call). [`napi_callback`][] provides more details.
35523564
* `setter`: A function to call when a set access of the property is performed.
35533565
If this is passed in, set `value` and `method` to `NULL` (since these members
35543566
won't be used). The given function is called implicitly by the runtime when
35553567
the property is set from JavaScript code (or if a set on the property is
3556-
performed using a N-API call).
3568+
performed using a N-API call). [`napi_callback`][] provides more details.
35573569
* `method`: Set this to make the property descriptor object's `value`
35583570
property to be a JavaScript function represented by `method`. If this is
35593571
passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3560-
won't be used).
3572+
won't be used). [`napi_callback`][] provides more details.
35613573
* `attributes`: The attributes associated with the particular property. See
35623574
[`napi_property_attributes`][].
35633575
* `data`: The callback data passed into `method`, `getter` and `setter` if this
@@ -4032,7 +4044,7 @@ napi_status napi_create_function(napi_env env,
40324044
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
40334045
it is null-terminated.
40344046
* `[in] cb`: The native function which should be called when this function
4035-
object is invoked.
4047+
object is invoked. [`napi_callback`][] provides more details.
40364048
* `[in] data`: User-provided data context. This will be passed back into the
40374049
function when invoked later.
40384050
* `[out] result`: `napi_value` representing the JavaScript function object for
@@ -4266,8 +4278,8 @@ napi_status napi_define_class(napi_env env,
42664278
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
42674279
if it is null-terminated.
42684280
* `[in] constructor`: Callback function that handles constructing instances
4269-
of the class. (This should be a static method on the class, not an actual
4270-
C++ constructor function.)
4281+
of the class. This should be a static method on the class, not an actual
4282+
C++ constructor function. [`napi_callback`][] provides more details.
42714283
* `[in] data`: Optional data to be passed to the constructor callback as
42724284
the `data` property of the callback info.
42734285
* `[in] property_count`: Number of items in the `properties` array argument.
@@ -4329,6 +4341,7 @@ napi_status napi_wrap(napi_env env,
43294341
JavaScript object.
43304342
* `[in] finalize_cb`: Optional native callback that can be used to free the
43314343
native instance when the JavaScript object is ready for garbage-collection.
4344+
[`napi_finalize`][] provides more details.
43324345
* `[in] finalize_hint`: Optional contextual hint that is passed to the
43334346
finalize callback.
43344347
* `[out] result`: Optional reference to the wrapped object.
@@ -4437,6 +4450,7 @@ napi_status napi_add_finalizer(napi_env env,
44374450
object.
44384451
* `[in] finalize_cb`: Native callback that will be used to free the
44394452
native data when the JavaScript object is ready for garbage-collection.
4453+
[`napi_finalize`][] provides more details.
44404454
* `[in] finalize_hint`: Optional contextual hint that is passed to the
44414455
finalize callback.
44424456
* `[out] result`: Optional reference to the JavaScript object.
@@ -4544,7 +4558,8 @@ napi_status napi_create_async_work(napi_env env,
45444558
and can execute in parallel with the main event loop thread.
45454559
* `[in] complete`: The native function which will be called when the
45464560
asynchronous logic is completed or is cancelled. The given function is called
4547-
from the main event loop thread.
4561+
from the main event loop thread. [`napi_async_complete_callback`][] provides
4562+
more details.
45484563
* `[in] data`: User-provided data context. This will be passed back into the
45494564
execute and complete functions.
45504565
* `[out] result`: `napi_async_work*` which is the handle to the newly created
@@ -5229,6 +5244,7 @@ napi_create_threadsafe_function(napi_env env,
52295244
response to a call on a different thread. This callback will be called on the
52305245
main thread. If not given, the JavaScript function will be called with no
52315246
parameters and with `undefined` as its `this` value.
5247+
[`napi_threadsafe_function_call_js`][] provides more details.
52325248
* `[out] result`: The asynchronous thread-safe JavaScript function.
52335249

52345250
### napi_get_threadsafe_function_context
@@ -5427,7 +5443,9 @@ This API may only be called from the main thread.
54275443
[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
54285444
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
54295445
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5446+
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
54305447
[`napi_async_init`]: #n_api_napi_async_init
5448+
[`napi_callback`]: #n_api_napi_callback
54315449
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
54325450
[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope
54335451
[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope
@@ -5442,6 +5460,7 @@ This API may only be called from the main thread.
54425460
[`napi_delete_async_work`]: #n_api_napi_delete_async_work
54435461
[`napi_delete_reference`]: #n_api_napi_delete_reference
54445462
[`napi_escape_handle`]: #n_api_napi_escape_handle
5463+
[`napi_finalize`]: #n_api_napi_finalize
54455464
[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception
54465465
[`napi_get_array_length`]: #n_api_napi_get_array_length
54475466
[`napi_get_element`]: #n_api_napi_get_element
@@ -5463,6 +5482,7 @@ This API may only be called from the main thread.
54635482
[`napi_reference_unref`]: #n_api_napi_reference_unref
54645483
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
54655484
[`napi_set_property`]: #n_api_napi_set_property
5485+
[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
54665486
[`napi_throw_error`]: #n_api_napi_throw_error
54675487
[`napi_throw_range_error`]: #n_api_napi_throw_range_error
54685488
[`napi_throw_type_error`]: #n_api_napi_throw_type_error

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.