Skip to content

Commit b8a17cc

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent e0206ba commit b8a17cc

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

doc/api/n-api.md

+36-16
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ napi_status napi_set_instance_data(napi_env env,
391391
* `[in] data`: The data item to make available to bindings of this instance.
392392
* `[in] finalize_cb`: The function to call when the environment is being torn
393393
down. The function receives `data` so that it might free it.
394+
[`napi_finalize`][] provides more details.
394395
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
395396
collection.
396397

@@ -597,6 +598,7 @@ minimum lifetimes explicitly.
597598
For more details, review the [Object lifetime management][].
598599

599600
### N-API callback types
601+
600602
#### napi_callback_info
601603
<!-- YAML
602604
added: v8.0.0
@@ -619,6 +621,9 @@ following signature:
619621
typedef napi_value (*napi_callback)(napi_env, napi_callback_info);
620622
```
621623

624+
Unless for reasons discussed in [Object Lifetime Management][], creating a
625+
handle and/or callback scope inside a `napi_callback` is not necessary.
626+
622627
#### napi_finalize
623628
<!-- YAML
624629
added: v8.0.0
@@ -637,6 +642,9 @@ typedef void (*napi_finalize)(napi_env env,
637642
void* finalize_hint);
638643
```
639644

645+
Unless for reasons discussed in [Object Lifetime Management][], creating a
646+
handle and/or callback scope inside the function body is not necessary.
647+
640648
#### napi_async_execute_callback
641649
<!-- YAML
642650
added: v8.0.0
@@ -649,12 +657,10 @@ operations. Callback functions must satisfy the following signature:
649657
typedef void (*napi_async_execute_callback)(napi_env env, void* data);
650658
```
651659

652-
Implementations of this function must avoid making N-API calls
653-
that execute JavaScript or interact with
654-
JavaScript objects. N-API
655-
calls should be in the `napi_async_complete_callback` instead.
656-
Do not use the `napi_env` parameter as it will likely
657-
result in execution of JavaScript.
660+
Implementations of this function must avoid making N-API calls that execute
661+
JavaScript or interact with JavaScript objects. N-API calls should be in the
662+
`napi_async_complete_callback` instead. Do not use the `napi_env` parameter as
663+
it will likely result in execution of JavaScript.
658664

659665
#### napi_async_complete_callback
660666
<!-- YAML
@@ -670,6 +676,9 @@ typedef void (*napi_async_complete_callback)(napi_env env,
670676
void* data);
671677
```
672678

679+
Unless for reasons discussed in [Object Lifetime Management][], creating a
680+
handle and/or callback scope inside the function body is not necessary.
681+
673682
#### napi_threadsafe_function_call_js
674683
<!-- YAML
675684
added: v10.6.0
@@ -713,6 +722,9 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
713722
This pointer is managed entirely by the threads and this callback. Thus this
714723
callback should free the data.
715724

725+
Unless for reasons discussed in [Object Lifetime Management][], creating a
726+
handle and/or callback scope inside the function body is not necessary.
727+
716728
## Error handling
717729

718730
N-API uses both return values and JavaScript exceptions for error handling.
@@ -1955,7 +1967,7 @@ napi_status napi_create_external(napi_env env,
19551967
* `[in] env`: The environment that the API is invoked under.
19561968
* `[in] data`: Raw pointer to the external data.
19571969
* `[in] finalize_cb`: Optional callback to call when the external value is being
1958-
collected.
1970+
collected. [`napi_finalize`][] provides more details.
19591971
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
19601972
collection.
19611973
* `[out] result`: A `napi_value` representing an external value.
@@ -1999,7 +2011,7 @@ napi_create_external_arraybuffer(napi_env env,
19992011
`ArrayBuffer`.
20002012
* `[in] byte_length`: The length in bytes of the underlying buffer.
20012013
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2002-
collected.
2014+
collected. [`napi_finalize`][] provides more details.
20032015
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
20042016
collection.
20052017
* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
@@ -2042,7 +2054,7 @@ napi_status napi_create_external_buffer(napi_env env,
20422054
size of the new buffer).
20432055
* `[in] data`: Raw pointer to the underlying buffer to copy from.
20442056
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2045-
collected.
2057+
collected. [`napi_finalize`][] provides more details.
20462058
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
20472059
collection.
20482060
* `[out] result`: A `napi_value` representing a `node::Buffer`.
@@ -3569,16 +3581,16 @@ typedef struct {
35693581
If this is passed in, set `value` and `method` to `NULL` (since these members
35703582
won't be used). The given function is called implicitly by the runtime when
35713583
the property is accessed from JavaScript code (or if a get on the property is
3572-
performed using a N-API call).
3584+
performed using a N-API call). [`napi_callback`][] provides more details.
35733585
* `setter`: A function to call when a set access of the property is performed.
35743586
If this is passed in, set `value` and `method` to `NULL` (since these members
35753587
won't be used). The given function is called implicitly by the runtime when
35763588
the property is set from JavaScript code (or if a set on the property is
3577-
performed using a N-API call).
3589+
performed using a N-API call). [`napi_callback`][] provides more details.
35783590
* `method`: Set this to make the property descriptor object's `value`
35793591
property to be a JavaScript function represented by `method`. If this is
35803592
passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3581-
won't be used).
3593+
won't be used). [`napi_callback`][] provides more details.
35823594
* `attributes`: The attributes associated with the particular property. See
35833595
[`napi_property_attributes`][].
35843596
* `data`: The callback data passed into `method`, `getter` and `setter` if this
@@ -4055,7 +4067,7 @@ napi_status napi_create_function(napi_env env,
40554067
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
40564068
it is null-terminated.
40574069
* `[in] cb`: The native function which should be called when this function
4058-
object is invoked.
4070+
object is invoked. [`napi_callback`][] provides more details.
40594071
* `[in] data`: User-provided data context. This will be passed back into the
40604072
function when invoked later.
40614073
* `[out] result`: `napi_value` representing the JavaScript function object for
@@ -4289,8 +4301,8 @@ napi_status napi_define_class(napi_env env,
42894301
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
42904302
if it is null-terminated.
42914303
* `[in] constructor`: Callback function that handles constructing instances
4292-
of the class. (This should be a static method on the class, not an actual
4293-
C++ constructor function.)
4304+
of the class. This should be a static method on the class, not an actual
4305+
C++ constructor function. [`napi_callback`][] provides more details.
42944306
* `[in] data`: Optional data to be passed to the constructor callback as
42954307
the `data` property of the callback info.
42964308
* `[in] property_count`: Number of items in the `properties` array argument.
@@ -4352,6 +4364,7 @@ napi_status napi_wrap(napi_env env,
43524364
JavaScript object.
43534365
* `[in] finalize_cb`: Optional native callback that can be used to free the
43544366
native instance when the JavaScript object is ready for garbage-collection.
4367+
[`napi_finalize`][] provides more details.
43554368
* `[in] finalize_hint`: Optional contextual hint that is passed to the
43564369
finalize callback.
43574370
* `[out] result`: Optional reference to the wrapped object.
@@ -4460,6 +4473,7 @@ napi_status napi_add_finalizer(napi_env env,
44604473
object.
44614474
* `[in] finalize_cb`: Native callback that will be used to free the
44624475
native data when the JavaScript object is ready for garbage-collection.
4476+
[`napi_finalize`][] provides more details.
44634477
* `[in] finalize_hint`: Optional contextual hint that is passed to the
44644478
finalize callback.
44654479
* `[out] result`: Optional reference to the JavaScript object.
@@ -4567,7 +4581,8 @@ napi_status napi_create_async_work(napi_env env,
45674581
and can execute in parallel with the main event loop thread.
45684582
* `[in] complete`: The native function which will be called when the
45694583
asynchronous logic is completed or is cancelled. The given function is called
4570-
from the main event loop thread.
4584+
from the main event loop thread. [`napi_async_complete_callback`][] provides
4585+
more details.
45714586
* `[in] data`: User-provided data context. This will be passed back into the
45724587
execute and complete functions.
45734588
* `[out] result`: `napi_async_work*` which is the handle to the newly created
@@ -5258,6 +5273,7 @@ napi_create_threadsafe_function(napi_env env,
52585273
response to a call on a different thread. This callback will be called on the
52595274
main thread. If not given, the JavaScript function will be called with no
52605275
parameters and with `undefined` as its `this` value.
5276+
[`napi_threadsafe_function_call_js`][] provides more details.
52615277
* `[out] result`: The asynchronous thread-safe JavaScript function.
52625278

52635279
### napi_get_threadsafe_function_context
@@ -5470,7 +5486,9 @@ This API may only be called from the main thread.
54705486
[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
54715487
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
54725488
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5489+
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
54735490
[`napi_async_init`]: #n_api_napi_async_init
5491+
[`napi_callback`]: #n_api_napi_callback
54745492
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
54755493
[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope
54765494
[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope
@@ -5485,6 +5503,7 @@ This API may only be called from the main thread.
54855503
[`napi_delete_async_work`]: #n_api_napi_delete_async_work
54865504
[`napi_delete_reference`]: #n_api_napi_delete_reference
54875505
[`napi_escape_handle`]: #n_api_napi_escape_handle
5506+
[`napi_finalize`]: #n_api_napi_finalize
54885507
[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception
54895508
[`napi_get_array_length`]: #n_api_napi_get_array_length
54905509
[`napi_get_element`]: #n_api_napi_get_element
@@ -5506,6 +5525,7 @@ This API may only be called from the main thread.
55065525
[`napi_reference_unref`]: #n_api_napi_reference_unref
55075526
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
55085527
[`napi_set_property`]: #n_api_napi_set_property
5528+
[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
55095529
[`napi_throw_error`]: #n_api_napi_throw_error
55105530
[`napi_throw_range_error`]: #n_api_napi_throw_range_error
55115531
[`napi_throw_type_error`]: #n_api_napi_throw_type_error

0 commit comments

Comments
 (0)