node_api_basic_finalize


稳定性: 1 - 实验性

用于附加功能的函数指针类型,当外部拥有的数据准备好被清理时,用户可以收到通知,因为与其相关联的对象已经被垃圾回收。用户必须提供一个满足以下签名的函数,当对象被回收时该函数会被调用。目前,可以使用 node_api_basic_finalize 来了解具有外部数据的对象何时被收集。

【Function pointer type for add-on provided functions that allow the user to be notified when externally-owned data is ready to be cleaned up because the object it was associated with has been garbage-collected. The user must provide a function satisfying the following signature which would get called upon the object's collection. Currently, node_api_basic_finalize can be used for finding out when objects that have external data are collected.】

typedef void (*node_api_basic_finalize)(node_api_basic_env env,
                                      void* finalize_data,
                                      void* finalize_hint); 

除非出于 对象生命周期管理 中讨论的原因,否则在函数体内创建 handle 和/或回调作用域并非必要。

【Unless for reasons discussed in Object Lifetime Management, creating a handle and/or callback scope inside the function body is not necessary.】

由于这些函数可能在 JavaScript 引擎处于无法执行 JavaScript 代码的状态时被调用,因此只能调用将 node_api_basic_env 作为第一个参数的 Node-API。node_api_post_finalizer 可用于安排需要访问 JavaScript 引擎状态的 Node-API 调用,以便在当前垃圾回收周期完成后运行。

【Since these functions may be called while the JavaScript engine is in a state where it cannot execute JavaScript code, only Node-APIs which accept a node_api_basic_env as their first parameter may be called. node_api_post_finalizer can be used to schedule Node-API calls that require access to the JavaScript engine's state to run after the current garbage collection cycle has completed.】

node_api_create_external_string_latin1node_api_create_external_string_utf16 的情况下,env 参数可能为 null,因为外部字符串可能会在环境关闭的后期被收集。

【In the case of node_api_create_external_string_latin1 and node_api_create_external_string_utf16 the env parameter may be null, because external strings can be collected during the latter part of environment shutdown.】

变更历史:

【Change History:】

  • 实验性(NAPI_EXPERIMENTAL):

    只有接受 node_api_basic_env 作为第一个参数的 Node-API 调用可以被执行,否则应用将以适当的错误信息终止。通过定义 NODE_API_EXPERIMENTAL_BASIC_ENV_OPT_OUT 可以关闭此功能。