nodejs/test/node-api/test_threadsafe_function/test_uncaught_exception.c
Gabriel Schulhof 7a216d5fd6
node-api: segregate nogc APIs from rest via type system
We define a new type called `node_api_nogc_env` as the `const` version
of `napi_env` and `node_api_nogc_finalize` as a variant of
`napi_finalize` that accepts a `node_api_nogc_env` as its first
argument.

We then modify those APIs which do not affect GC state as accepting a
`node_api_nogc_env`. APIs accepting finalizer callbacks are modified to
accept `node_api_nogc_finalize` callbacks. Thus, the only way to attach
a `napi_finalize` callback, wherein Node-APIs affecting GC state may be
called is to call `node_api_post_finalizer` from a
`node_api_nogc_finalize` callback.

In keeping with the process of introducing new Node-APIs, this feature
is guarded by `NAPI_EXPERIMENTAL`. Since this feature modifies APIs
already marked as stable, it is additionally guared by
`NODE_API_EXPERIMENTAL_NOGC_ENV`, so as to provide a further buffer to
adoption. Nevertheless, both guards must be removed upon releasing a
new version of Node-API.

PR-URL: https://github.com/nodejs/node/pull/50060
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
2023-12-19 07:37:15 +00:00

75 lines
2.7 KiB
C

#include <node_api.h>
#include "../../js-native-api/common.h"
// Testing calling into JavaScript
static void ThreadSafeFunctionFinalize(napi_env env,
void* finalize_data,
void* finalize_hint) {
napi_ref js_func_ref = (napi_ref)finalize_data;
napi_value js_func;
napi_value recv;
NODE_API_CALL_RETURN_VOID(
env, napi_get_reference_value(env, js_func_ref, &js_func));
NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv));
NODE_API_CALL_RETURN_VOID(
env, napi_call_function(env, recv, js_func, 0, NULL, NULL));
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;
napi_value argv[4];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
napi_ref finalize_func;
NODE_API_CALL(env, napi_create_reference(env, argv[3], 1, &finalize_func));
napi_threadsafe_function tsfn;
NODE_API_CALL(env,
napi_create_threadsafe_function(env,
argv[0],
argv[1],
argv[2],
0,
1,
finalize_func,
ThreadSafeFunctionNogcFinalize,
NULL,
NULL,
&tsfn));
NODE_API_CALL(env,
napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking));
NODE_API_CALL(env, napi_release_threadsafe_function(tsfn, napi_tsfn_release));
return NULL;
}
// Module init
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NODE_API_PROPERTY("CallIntoModule", CallIntoModule),
};
NODE_API_CALL(
env,
napi_define_properties(env,
exports,
sizeof(properties) / sizeof(properties[0]),
properties));
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)