napi_handle_scope


这是一种抽象,用于控制和修改在特定范围内创建的对象的生命周期。通常,Node-API 值是在句柄范围的上下文中创建的。当从 JavaScript 调用原生方法时,将存在默认句柄范围。如果用户没有显式创建新的句柄范围,Node-API 值将在默认句柄范围内创建。对于原生方法执行之外的任何代码调用(例如,在 libuv 回调调用期间),模块需要在调用任何可能导致创建 JavaScript 值的函数之前创建范围。

¥This is an abstraction used to control and modify the lifetime of objects created within a particular scope. In general, Node-API values are created within the context of a handle scope. When a native method is called from JavaScript, a default handle scope will exist. If the user does not explicitly create a new handle scope, Node-API values will be created in the default handle scope. For any invocations of code outside the execution of a native method (for instance, during a libuv callback invocation), the module is required to create a scope before invoking any functions that can result in the creation of JavaScript values.

句柄作用域使用 napi_open_handle_scope 创建,使用 napi_close_handle_scope 销毁。关闭作用域可以向 GC 表明在句柄作用域的生命周期内创建的所有 napi_value 不再从当前堆栈帧中引用。

¥Handle scopes are created using napi_open_handle_scope and are destroyed using napi_close_handle_scope. Closing the scope can indicate to the GC that all napi_values created during the lifetime of the handle scope are no longer referenced from the current stack frame.

有关详细信息,请查看 对象生命周期管理

¥For more details, review the Object lifetime management.