node-api: make tsfn accept napi_finalize once more

The thread-safe function's finalizer is not called in conjunction with
the garbage collection of a JS value. In fact, it keeps a strong
reference to the JS function it is expected to call. Thus, it is safe
to make calls that affect GC state from its body.

PR-URL: https://github.com/nodejs/node/pull/51801
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Gabriel Schulhof authored and Richard Lau committed 2024-05-07 17:03:26 +00:00
1 parent 2c53ff31c9
commit 71616e8a8a
3 files changed
+3 -17

No files matched your search

@@ -16,18 +16,6 @@ static void ThreadSafeFunctionFinalize(napi_env env,
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref));
}
static void ThreadSafeFunctionNogcFinalize(node_api_nogc_env env,
void* data,
void* hint) {
#ifdef NAPI_EXPERIMENTAL
NODE_API_NOGC_CALL_RETURN_VOID(
env,
node_api_post_finalizer(env, ThreadSafeFunctionFinalize, data, hint));
#else
ThreadSafeFunctionFinalize(env, data, hint);
#endif
}
// Testing calling into JavaScript
static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
size_t argc = 4;
@@ -46,7 +34,7 @@ static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
0,
1,
finalize_func,
ThreadSafeFunctionNogcFinalize,
ThreadSafeFunctionFinalize,
NULL,
NULL,
&tsfn));