Skip to content

Commit af83dcd

Browse files
author
Gabriel Schulhof
committed
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 d408ee1 commit af83dcd

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

doc/api/n-api.md

Lines changed: 36 additions & 16 deletions
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.
@@ -1958,7 +1970,7 @@ napi_status napi_create_external(napi_env env,
19581970
* `[in] env`: The environment that the API is invoked under.
19591971
* `[in] data`: Raw pointer to the external data.
19601972
* `[in] finalize_cb`: Optional callback to call when the external value is being
1961-
collected.
1973+
collected. [`napi_finalize`][] provides more details.
19621974
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
19631975
collection.
19641976
* `[out] result`: A `napi_value` representing an external value.
@@ -2002,7 +2014,7 @@ napi_create_external_arraybuffer(napi_env env,
20022014
`ArrayBuffer`.
20032015
* `[in] byte_length`: The length in bytes of the underlying buffer.
20042016
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2005-
collected.
2017+
collected. [`napi_finalize`][] provides more details.
20062018
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
20072019
collection.
20082020
* `[out] result`: A `napi_value` representing a JavaScript `ArrayBuffer`.
@@ -2045,7 +2057,7 @@ napi_status napi_create_external_buffer(napi_env env,
20452057
size of the new buffer).
20462058
* `[in] data`: Raw pointer to the underlying buffer to copy from.
20472059
* `[in] finalize_cb`: Optional callback to call when the `ArrayBuffer` is being
2048-
collected.
2060+
collected. [`napi_finalize`][] provides more details.
20492061
* `[in] finalize_hint`: Optional hint to pass to the finalize callback during
20502062
collection.
20512063
* `[out] result`: A `napi_value` representing a `node::Buffer`.
@@ -3572,16 +3584,16 @@ typedef struct {
35723584
If this is passed in, set `value` and `method` to `NULL` (since these members
35733585
won't be used). The given function is called implicitly by the runtime when
35743586
the property is accessed from JavaScript code (or if a get on the property is
3575-
performed using a N-API call).
3587+
performed using a N-API call). [`napi_callback`][] provides more details.
35763588
* `setter`: A function to call when a set access of the property is performed.
35773589
If this is passed in, set `value` and `method` to `NULL` (since these members
35783590
won't be used). The given function is called implicitly by the runtime when
35793591
the property is set from JavaScript code (or if a set on the property is
3580-
performed using a N-API call).
3592+
performed using a N-API call). [`napi_callback`][] provides more details.
35813593
* `method`: Set this to make the property descriptor object's `value`
35823594
property to be a JavaScript function represented by `method`. If this is
35833595
passed in, set `value`, `getter` and `setter` to `NULL` (since these members
3584-
won't be used).
3596+
won't be used). [`napi_callback`][] provides more details.
35853597
* `attributes`: The attributes associated with the particular property. See
35863598
[`napi_property_attributes`][].
35873599
* `data`: The callback data passed into `method`, `getter` and `setter` if this
@@ -4059,7 +4071,7 @@ napi_status napi_create_function(napi_env env,
40594071
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH` if
40604072
it is null-terminated.
40614073
* `[in] cb`: The native function which should be called when this function
4062-
object is invoked.
4074+
object is invoked. [`napi_callback`][] provides more details.
40634075
* `[in] data`: User-provided data context. This will be passed back into the
40644076
function when invoked later.
40654077
* `[out] result`: `napi_value` representing the JavaScript function object for
@@ -4293,8 +4305,8 @@ napi_status napi_define_class(napi_env env,
42934305
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
42944306
if it is null-terminated.
42954307
* `[in] constructor`: Callback function that handles constructing instances
4296-
of the class. (This should be a static method on the class, not an actual
4297-
C++ constructor function.)
4308+
of the class. This should be a static method on the class, not an actual
4309+
C++ constructor function. [`napi_callback`][] provides more details.
42984310
* `[in] data`: Optional data to be passed to the constructor callback as
42994311
the `data` property of the callback info.
43004312
* `[in] property_count`: Number of items in the `properties` array argument.
@@ -4356,6 +4368,7 @@ napi_status napi_wrap(napi_env env,
43564368
JavaScript object.
43574369
* `[in] finalize_cb`: Optional native callback that can be used to free the
43584370
native instance when the JavaScript object is ready for garbage-collection.
4371+
[`napi_finalize`][] provides more details.
43594372
* `[in] finalize_hint`: Optional contextual hint that is passed to the
43604373
finalize callback.
43614374
* `[out] result`: Optional reference to the wrapped object.
@@ -4464,6 +4477,7 @@ napi_status napi_add_finalizer(napi_env env,
44644477
object.
44654478
* `[in] finalize_cb`: Native callback that will be used to free the
44664479
native data when the JavaScript object is ready for garbage-collection.
4480+
[`napi_finalize`][] provides more details.
44674481
* `[in] finalize_hint`: Optional contextual hint that is passed to the
44684482
finalize callback.
44694483
* `[out] result`: Optional reference to the JavaScript object.
@@ -4571,7 +4585,8 @@ napi_status napi_create_async_work(napi_env env,
45714585
and can execute in parallel with the main event loop thread.
45724586
* `[in] complete`: The native function which will be called when the
45734587
asynchronous logic is completed or is cancelled. The given function is called
4574-
from the main event loop thread.
4588+
from the main event loop thread. [`napi_async_complete_callback`][] provides
4589+
more details.
45754590
* `[in] data`: User-provided data context. This will be passed back into the
45764591
execute and complete functions.
45774592
* `[out] result`: `napi_async_work*` which is the handle to the newly created
@@ -5262,6 +5277,7 @@ napi_create_threadsafe_function(napi_env env,
52625277
response to a call on a different thread. This callback will be called on the
52635278
main thread. If not given, the JavaScript function will be called with no
52645279
parameters and with `undefined` as its `this` value.
5280+
[`napi_threadsafe_function_call_js`][] provides more details.
52655281
* `[out] result`: The asynchronous thread-safe JavaScript function.
52665282

52675283
### napi_get_threadsafe_function_context
@@ -5474,7 +5490,9 @@ This API may only be called from the main thread.
54745490
[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
54755491
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
54765492
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5493+
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
54775494
[`napi_async_init`]: #n_api_napi_async_init
5495+
[`napi_callback`]: #n_api_napi_callback
54785496
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work
54795497
[`napi_close_callback_scope`]: #n_api_napi_close_callback_scope
54805498
[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope
@@ -5489,6 +5507,7 @@ This API may only be called from the main thread.
54895507
[`napi_delete_async_work`]: #n_api_napi_delete_async_work
54905508
[`napi_delete_reference`]: #n_api_napi_delete_reference
54915509
[`napi_escape_handle`]: #n_api_napi_escape_handle
5510+
[`napi_finalize`]: #n_api_napi_finalize
54925511
[`napi_get_and_clear_last_exception`]: #n_api_napi_get_and_clear_last_exception
54935512
[`napi_get_array_length`]: #n_api_napi_get_array_length
54945513
[`napi_get_element`]: #n_api_napi_get_element
@@ -5510,6 +5529,7 @@ This API may only be called from the main thread.
55105529
[`napi_reference_unref`]: #n_api_napi_reference_unref
55115530
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
55125531
[`napi_set_property`]: #n_api_napi_set_property
5532+
[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
55135533
[`napi_throw_error`]: #n_api_napi_throw_error
55145534
[`napi_throw_range_error`]: #n_api_napi_throw_range_error
55155535
[`napi_throw_type_error`]: #n_api_napi_throw_type_error

0 commit comments

Comments
 (0)