Skip to content

Commit 20896f7

Browse files
legendecasBridgeAR
authored andcommitted
n-api,doc: clarify napi_finalize related APIs
All these APIs have napi_finalize parameter but they don't behave exactly the same. Fixes: #29750 PR-URL: #29797 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent aea0253 commit 20896f7

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

doc/api/n-api.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,9 +1809,15 @@ Returns `napi_ok` if the API succeeded.
18091809

18101810
This API allocates a JavaScript value with external data attached to it. This
18111811
is used to pass external data through JavaScript code, so it can be retrieved
1812-
later by native code. The API allows the caller to pass in a finalize callback,
1813-
in case the underlying native resource needs to be cleaned up when the external
1814-
JavaScript value gets collected.
1812+
later by native code using [`napi_get_value_external`].
1813+
1814+
The API adds a `napi_finalize` callback which will be called when the JavaScript
1815+
object just created is ready for garbage collection. It is similar to
1816+
`napi_wrap()` except that:
1817+
1818+
* the native data cannot be retrieved later using `napi_unwrap()`,
1819+
* nor can it be removed later using `napi_remove_wrap()`, and
1820+
* the object created by the API can be used with `napi_wrap()`.
18151821

18161822
The created value is not an object, and therefore does not support additional
18171823
properties. It is considered a distinct value type: calling `napi_typeof()` with
@@ -1850,6 +1856,14 @@ The underlying byte buffer of the `ArrayBuffer` is externally allocated and
18501856
managed. The caller must ensure that the byte buffer remains valid until the
18511857
finalize callback is called.
18521858

1859+
The API adds a `napi_finalize` callback which will be called when the JavaScript
1860+
object just created is ready for garbage collection. It is similar to
1861+
`napi_wrap()` except that:
1862+
1863+
* the native data cannot be retrieved later using `napi_unwrap()`,
1864+
* nor can it be removed later using `napi_remove_wrap()`, and
1865+
* the object created by the API can be used with `napi_wrap()`.
1866+
18531867
JavaScript `ArrayBuffer`s are described in
18541868
[Section 24.1][] of the ECMAScript Language Specification.
18551869

@@ -1884,6 +1898,14 @@ This API allocates a `node::Buffer` object and initializes it with data
18841898
backed by the passed in buffer. While this is still a fully-supported data
18851899
structure, in most cases using a `TypedArray` will suffice.
18861900

1901+
The API adds a `napi_finalize` callback which will be called when the JavaScript
1902+
object just created is ready for garbage collection. It is similar to
1903+
`napi_wrap()` except that:
1904+
1905+
* the native data cannot be retrieved later using `napi_unwrap()`,
1906+
* nor can it be removed later using `napi_remove_wrap()`, and
1907+
* the object created by the API can be used with `napi_wrap()`.
1908+
18871909
For Node.js >=4 `Buffers` are `Uint8Array`s.
18881910

18891911
#### napi_create_object
@@ -4228,7 +4250,8 @@ in `js_object` is ready for garbage collection. This API is similar to
42284250
* the native data cannot be retrieved later using `napi_unwrap()`,
42294251
* nor can it be removed later using `napi_remove_wrap()`, and
42304252
* the API can be called multiple times with different data items in order to
4231-
attach each of them to the JavaScript object.
4253+
attach each of them to the JavaScript object, and
4254+
* the object manipulated by the API can be used with `napi_wrap()`.
42324255

42334256
*Caution*: The optional returned reference (if obtained) should be deleted via
42344257
[`napi_delete_reference`][] ONLY in response to the finalize callback
@@ -5164,6 +5187,7 @@ This API may only be called from the main thread.
51645187
[`napi_get_last_error_info`]: #n_api_napi_get_last_error_info
51655188
[`napi_get_property`]: #n_api_napi_get_property
51665189
[`napi_get_reference_value`]: #n_api_napi_get_reference_value
5190+
[`napi_get_value_external`]: #n_api_napi_get_value_external
51675191
[`napi_has_own_property`]: #n_api_napi_has_own_property
51685192
[`napi_has_property`]: #n_api_napi_has_property
51695193
[`napi_is_error`]: #n_api_napi_is_error

0 commit comments

Comments
 (0)