I am attempting to (re)write a node module to use N-API, but there is an issue that I cannot see a workaround for.
I want to use uv_poll_t/uv_poll_init_socket etc. to set up socket polling to handle socket events in the native addon. In the native callback there is a code path that executes a JS callback. Looks something like this:
// env is stored in data field of the uv_poll_t handle
Napi::HandleScope scope(env);
Value().Get("emit").As<Napi::Function>().Call(Value(), {
Napi::String::New(env, "message"),
msg.ToBuffer(env),
});
Unfortunately any exception thrown in the JS callback will crash the process. Instead I want to report a fatal exception to Node, so that it can be delegated to the uncaughtException event of process.
Essentially, I want to use this code, identical to what is used in the async callback:
|
if (!env->last_exception.IsEmpty()) { |
|
v8::TryCatch try_catch(env->isolate); |
|
env->isolate->ThrowException( |
|
v8::Local<v8::Value>::New(env->isolate, env->last_exception)); |
|
node::FatalException(env->isolate, try_catch); |
|
} |
But there seems to be no way to do this. Even if I include v8.h and node.h I cannot convert the exception from N-API back to a v8 exception so I can call node::FatalException.
Ideally I'd like either:
-
Ability to call node::FatalException() via a supported addition to N-API (so that I can cause an uncaughtException event to be emitted); or
-
Ability to use uv_poll_t/uv_poll_init_socket etc via a supported addition to N-API, similar to napi_create_async_work etc.
I am attempting to (re)write a node module to use N-API, but there is an issue that I cannot see a workaround for.
I want to use
uv_poll_t/uv_poll_init_socketetc. to set up socket polling to handle socket events in the native addon. In the native callback there is a code path that executes a JS callback. Looks something like this:Unfortunately any exception thrown in the JS callback will crash the process. Instead I want to report a fatal exception to Node, so that it can be delegated to the
uncaughtExceptionevent ofprocess.Essentially, I want to use this code, identical to what is used in the async callback:
node/src/node_api.cc
Lines 3296 to 3301 in a10856a
But there seems to be no way to do this. Even if I include
v8.handnode.hI cannot convert the exception from N-API back to a v8 exception so I can callnode::FatalException.Ideally I'd like either:
Ability to call
node::FatalException()via a supported addition to N-API (so that I can cause anuncaughtExceptionevent to be emitted); orAbility to use
uv_poll_t/uv_poll_init_socketetc via a supported addition to N-API, similar tonapi_create_async_worketc.