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.】