node-api: handle pending exception in cb wrapper

fixes: https://github.com/nodejs/node-addon-api/issues/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 <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/39476
Fixes: https://github.com/nodejs/node-addon-api/issues/1025
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Michael Dawson authored and Michaël Zasso committed 2021-09-04 15:14:46 +02:00
1 parent 78de83cc74
commit 2ca12c83b4
3 files changed
+37 -2

No files matched your search

@@ -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;
@@ -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));
@@ -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