Skip to content

Commit

Permalink
node-api: handle pending exception in cb wrapper
Browse files Browse the repository at this point in the history
fixes: nodejs/node-addon-api#1025

The functionreference test from the node-api tests
was reporting a failed v8 check when Node.js was compiled
as debug. The failure was because an exception was
pending but the C++ wrappers were returning
a return value that was invalid.

Signed-off-by: Michael Dawson <[email protected]>

PR-URL: #39476
Fixes: nodejs/node-addon-api#1025
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
mhdawson authored and targos committed Sep 4, 2021
1 parent 78de83c commit 2ca12c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,16 @@ class CallbackWrapperBase : public CallbackWrapper {
napi_env env = _bundle->env;
napi_callback cb = _bundle->cb;

napi_value result;
napi_value result = nullptr;
bool exceptionOccurred = false;
env->CallIntoModule([&](napi_env env) {
result = cb(env, cbinfo_wrapper);
}, [&](napi_env env, v8::Local<v8::Value> value) {
exceptionOccurred = true;
env->isolate->ThrowException(value);
});

if (result != nullptr) {
if (!exceptionOccurred && (result != nullptr)) {
this->SetReturnValue(result);
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/js-native-api/test_function/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ assert.deepStrictEqual(test_function.TestCreateFunctionParameters(), {
cbIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});

assert.throws(
() => test_function.TestBadReturnExceptionPending(),
{
code: 'throwing exception',
name: 'Error'
}
);
23 changes: 23 additions & 0 deletions test/js-native-api/test_function/test_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ static napi_value MakeTrackedFunction(napi_env env, napi_callback_info info) {
return result;
}

static napi_value TestBadReturnExceptionPending(napi_env env, napi_callback_info info) {
napi_throw_error(env, "throwing exception", "throwing exception");

// addons should only ever return a valid napi_value even if an
// exception occurs, but we have seen that the C++ wrapper
// with exceptions enabled sometimes returns an invalid value
// when an exception is thrown. Test that we ignore the return
// value then an exeption is pending. We use 0xFFFFFFFF as a value
// that should never be a valid napi_value and node seems to
// crash if it is not ignored indicating that it is indeed invalid.
return (napi_value)(0xFFFFFFFFF);
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value fn1;
Expand Down Expand Up @@ -183,6 +196,12 @@ napi_value Init(napi_env env, napi_value exports) {
NULL,
&fn5));

napi_value fn6;
NAPI_CALL(env,
napi_create_function(
env, "TestBadReturnExceptionPending", NAPI_AUTO_LENGTH,
TestBadReturnExceptionPending, NULL, &fn6));

NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1));
NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2));
NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3));
Expand All @@ -196,6 +215,10 @@ napi_value Init(napi_env env, napi_value exports) {
"TestCreateFunctionParameters",
fn5));

NAPI_CALL(env,
napi_set_named_property(
env, exports, "TestBadReturnExceptionPending", fn6));

return exports;
}
EXTERN_C_END

0 comments on commit 2ca12c8

Please sign in to comment.