napi_env
napi_env
用于表示底层 Node-API 实现可用于持久化 VM 特定状态的上下文。此结构在调用时传递给原生函数,并且在进行 Node-API 调用时必须传回。具体来说,调用初始原生函数时传入的相同 napi_env
必须传递给任何后续的嵌套 Node-API 调用。出于一般重用的目的缓存 napi_env
,以及在不同 Worker
线程上运行的同一插件的实例之间传递 napi_env
是不允许的。当卸载原生插件的实例时,napi_env
变得无效。此事件的通知通过给 napi_add_env_cleanup_hook
和 napi_set_instance_data
的回调传递。
¥napi_env
is used to represent a context that the underlying Node-API
implementation can use to persist VM-specific state. This structure is passed
to native functions when they're invoked, and it must be passed back when
making Node-API calls. Specifically, the same napi_env
that was passed in when
the initial native function was called must be passed to any subsequent
nested Node-API calls. Caching the napi_env
for the purpose of general reuse,
and passing the napi_env
between instances of the same addon running on
different Worker
threads is not allowed. The napi_env
becomes invalid
when an instance of a native addon is unloaded. Notification of this event is
delivered through the callbacks given to napi_add_env_cleanup_hook
and
napi_set_instance_data
.