| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 78de83c commit 2ca12c8
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -306,12 +306,16 @@ class CallbackWrapperBase : public CallbackWrapper { | |||
| 306 | 306 | napi_env env = _bundle->env; | |
| 307 | 307 | napi_callback cb = _bundle->cb; | |
| 308 | 308 | ||
| 309 | - napi_value result; | ||
| 309 | + napi_value result = nullptr; | ||
| 310 | + bool exceptionOccurred = false; | ||
| 310 | 311 | env->CallIntoModule([&](napi_env env) { | |
| 311 | 312 | result = cb(env, cbinfo_wrapper); | |
| 313 | + }, [&](napi_env env, v8::Local<v8::Value> value) { | ||
| 314 | + exceptionOccurred = true; | ||
| 315 | + env->isolate->ThrowException(value); | ||
| 312 | 316 | }); | |
| 313 | 317 | ||
| 314 | - if (result != nullptr) { | ||
| 318 | + if (!exceptionOccurred && (result != nullptr)) { | ||
| 315 | 319 | this->SetReturnValue(result); | |
| 316 | 320 | } | |
| 317 | 321 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,3 +42,11 @@ assert.deepStrictEqual(test_function.TestCreateFunctionParameters(), { | |||
| 42 | 42 | cbIsNull: 'Invalid argument', | |
| 43 | 43 | resultIsNull: 'Invalid argument' | |
| 44 | 44 | }); | |
| 45 | + | ||
| 46 | + assert.throws( | ||
| 47 | + () => test_function.TestBadReturnExceptionPending(), | ||
| 48 | + { | ||
| 49 | + code: 'throwing exception', | ||
| 50 | + name: 'Error' | ||
| 51 | + } | ||
| 52 | + ); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -153,6 +153,19 @@ static napi_value MakeTrackedFunction(napi_env env, napi_callback_info info) { | |||
| 153 | 153 | return result; | |
| 154 | 154 | } | |
| 155 | 155 | ||
| 156 | + static napi_value TestBadReturnExceptionPending(napi_env env, napi_callback_info info) { | ||
| 157 | + napi_throw_error(env, "throwing exception", "throwing exception"); | ||
| 158 | + | ||
| 159 | + // addons should only ever return a valid napi_value even if an | ||
| 160 | + // exception occurs, but we have seen that the C++ wrapper | ||
| 161 | + // with exceptions enabled sometimes returns an invalid value | ||
| 162 | + // when an exception is thrown. Test that we ignore the return | ||
| 163 | + // value then an exeption is pending. We use 0xFFFFFFFF as a value | ||
| 164 | + // that should never be a valid napi_value and node seems to | ||
| 165 | + // crash if it is not ignored indicating that it is indeed invalid. | ||
| 166 | + return (napi_value)(0xFFFFFFFFF); | ||
| 167 | + } | ||
| 168 | + | ||
| 156 | 169 | EXTERN_C_START | |
| 157 | 170 | napi_value Init(napi_env env, napi_value exports) { | |
| 158 | 171 | napi_value fn1; | |
@@ -183,6 +196,12 @@ napi_value Init(napi_env env, napi_value exports) { | |||
| 183 | 196 | NULL, | |
| 184 | 197 | &fn5)); | |
| 185 | 198 | ||
| 199 | + napi_value fn6; | ||
| 200 | + NAPI_CALL(env, | ||
| 201 | + napi_create_function( | ||
| 202 | + env, "TestBadReturnExceptionPending", NAPI_AUTO_LENGTH, | ||
| 203 | + TestBadReturnExceptionPending, NULL, &fn6)); | ||
| 204 | + | ||
| 186 | 205 | NAPI_CALL(env, napi_set_named_property(env, exports, "TestCall", fn1)); | |
| 187 | 206 | NAPI_CALL(env, napi_set_named_property(env, exports, "TestName", fn2)); | |
| 188 | 207 | NAPI_CALL(env, napi_set_named_property(env, exports, "TestNameShort", fn3)); | |
@@ -196,6 +215,10 @@ napi_value Init(napi_env env, napi_value exports) { | |||
| 196 | 215 | "TestCreateFunctionParameters", | |
| 197 | 216 | fn5)); | |
| 198 | 217 | ||
| 218 | + NAPI_CALL(env, | ||
| 219 | + napi_set_named_property( | ||
| 220 | + env, exports, "TestBadReturnExceptionPending", fn6)); | ||
| 221 | + | ||
| 199 | 222 | return exports; | |
| 200 | 223 | } | |
| 201 | 224 | EXTERN_C_END | |
| Back | FazBrowse Home | New Git URL |
0 commit comments