napi_add_finalizer
napi_status napi_add_finalizer(napi_env env,
napi_value js_object,
void* native_object,
napi_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] native_object
:将附加到 JavaScript 对象的原生数据。¥
[in] native_object
: The native data that will be attached to the JavaScript object. -
[in] finalize_cb
:当 JavaScript 对象准备好进行垃圾回收时,将用于释放原生数据的原生回调。napi_finalize
提供了更多详细信息。¥
[in] finalize_cb
: Native callback that will be used to free the native data when the JavaScript object is ready for garbage-collection.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 对象准备好进行垃圾回收时将调用该回调。此 API 与 napi_wrap()
类似,不同之处在于:
¥Adds a napi_finalize
callback which will be called when the JavaScript object
in js_object
is ready for garbage collection. This API is similar to
napi_wrap()
except that:
-
以后无法使用
napi_unwrap()
检索原生数据,¥the native data cannot be retrieved later using
napi_unwrap()
, -
以后也不能使用
napi_remove_wrap()
将其删除,并且¥nor can it be removed later using
napi_remove_wrap()
, and -
可以使用不同的数据项多次调用 API,以便将它们中的每一个附加到 JavaScript 对象,并且
¥the API can be called multiple times with different data items in order to attach each of them to the JavaScript object, and
-
API 操作的对象可以与
napi_wrap()
一起使用。¥the object manipulated by the API can be used with
napi_wrap()
.
警告:可选的返回引用(如果获得)应仅通过 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.