napi_add_finalizer
napi_status napi_add_finalizer(napi_env env,
napi_value js_object,
void* finalize_data,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_ref* result);
-
[in] env
:调用 API 的环境。¥
[in] env
: The environment that the API is invoked under. -
[in] js_object
:原生数据将附加到的 JavaScript 对象。¥
[in] js_object
: The JavaScript object to which the native data will be attached. -
[in] finalize_data
:要传递给finalize_cb
的可选数据。¥
[in] finalize_data
: Optional data to be passed tofinalize_cb
. -
[in] finalize_cb
:当 JavaScript 对象被垃圾回收时,将用于释放原生数据的原生回调。napi_finalize
提供了更多详细信息。¥
[in] finalize_cb
: Native callback that will be used to free the native data when the JavaScript object has been garbage-collected.napi_finalize
provides more details. -
[in] finalize_hint
:传递给完成回调的可选上下文提示。¥
[in] finalize_hint
: Optional contextual hint that is passed to the finalize callback. -
[out] result
:对 JavaScript 对象的可选引用。¥
[out] result
: Optional reference to the JavaScript object.
如果 API 成功,则返回 napi_ok
。
¥Returns napi_ok
if the API succeeded.
添加 napi_finalize
回调,当 js_object
中的 JavaScript 对象被垃圾回收时调用。
¥Adds a napi_finalize
callback which will be called when the JavaScript object
in js_object
has been garbage-collected.
可以在单个 JavaScript 对象上多次调用此 API。
¥This API can be called multiple times on a single JavaScript object.
警告:可选的返回引用(如果获得)应仅通过 napi_delete_reference
删除以响应最终回调调用。如果在此之前删除它,则可能永远不会调用 finalize 回调。因此,在获取引用时,还需要一个 finalize 回调,以便能够正确处理引用。
¥Caution: The optional returned reference (if obtained) should be deleted via
napi_delete_reference
ONLY in response to the finalize callback
invocation. If it is deleted before then, then the finalize callback may never
be invoked. Therefore, when obtaining a reference a finalize callback is also
required in order to enable correct disposal of the reference.