Picking up from nodejs/node#28464 (comment):
@addaleax:
@gabrielschulhof It’s not safe to run JS at that point, but otherwise I think this should work. And sure, it’s possible to do this, it’s just not very ergonomical at this point (imo).
You're right. It's not ergonomical. WDYT about the API below?
NAPI_EXTERN napi_status napi_set_module_data(napi_env env,
void* data,
napi_finalizer finalize_cb,
void* finalize_hint);
NAPI_EXTERN napi_status napi_get_module_data(napi_env env, void** data);
We are already using can_call_into_js() to determine whether it's safe to call into JS, so I think we can offer napi_env in the cleanup hook, and thus we can render it as a napi_finalize.
This API would have the added advantage that bindings need not call napi_get_cb_info() to retrieve their void* data, because napi_get_cb_info() is fairly heavy.
Also, if we switch to a per-module napi_env, that's actually what we should've had all along, but any ObjectTemplates we may wish to store in the future (I hope there will be none) should be stored on the node::Environment and not on the per-addon napi_env.
Picking up from nodejs/node#28464 (comment):
@addaleax:
You're right. It's not ergonomical. WDYT about the API below?
We are already using
can_call_into_js()to determine whether it's safe to call into JS, so I think we can offernapi_envin the cleanup hook, and thus we can render it as anapi_finalize.This API would have the added advantage that bindings need not call
napi_get_cb_info()to retrieve theirvoid* data, becausenapi_get_cb_info()is fairly heavy.Also, if we switch to a per-module
napi_env, that's actually what we should've had all along, but anyObjectTemplates we may wish to store in the future (I hope there will be none) should be stored on thenode::Environmentand not on the per-addonnapi_env.