| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5a42be9 commit a74032a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2662,7 +2662,7 @@ napi_status napi_new_instance(napi_env env, | |||
| 2662 | 2662 | auto maybe = ctor->NewInstance(context, argc, | |
| 2663 | 2663 | reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv))); | |
| 2664 | 2664 | ||
| 2665 | - CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure); | ||
| 2665 | + CHECK_MAYBE_EMPTY(env, maybe, napi_pending_exception); | ||
| 2666 | 2666 | ||
| 2667 | 2667 | *result = v8impl::JsValueFromV8LocalValue(maybe.ToLocalChecked()); | |
| 2668 | 2668 | return GET_RETURN_STATUS(env); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,34 @@ const test_exception = (function() { | |||
| 48 | 48 | ` thrown, but ${returnedError} was passed`); | |
| 49 | 49 | } | |
| 50 | 50 | ||
| 51 | + | ||
| 52 | + { | ||
| 53 | + const throwTheError = class { constructor() { throw theError; } }; | ||
| 54 | + | ||
| 55 | + // Test that the native side successfully captures the exception | ||
| 56 | + let returnedError = test_exception.constructReturnException(throwTheError); | ||
| 57 | + assert.strictEqual(returnedError, theError); | ||
| 58 | + | ||
| 59 | + // Test that the native side passes the exception through | ||
| 60 | + assert.throws( | ||
| 61 | + () => { test_exception.constructAllowException(throwTheError); }, | ||
| 62 | + (err) => err === theError | ||
| 63 | + ); | ||
| 64 | + | ||
| 65 | + // Test that the exception thrown above was marked as pending | ||
| 66 | + // before it was handled on the JS side | ||
| 67 | + const exception_pending = test_exception.wasPending(); | ||
| 68 | + assert.strictEqual(exception_pending, true, | ||
| 69 | + 'Exception not pending as expected,' + | ||
| 70 | + ` .wasPending() returned ${exception_pending}`); | ||
| 71 | + | ||
| 72 | + // Test that the native side does not capture a non-existing exception | ||
| 73 | + returnedError = test_exception.constructReturnException(common.mustCall()); | ||
| 74 | + assert.strictEqual(returnedError, undefined, | ||
| 75 | + 'Returned error should be undefined when no exception is' + | ||
| 76 | + ` thrown, but ${returnedError} was passed`); | ||
| 77 | + } | ||
| 78 | + | ||
| 51 | 79 | { | |
| 52 | 80 | // Test that no exception appears that was not thrown by us | |
| 53 | 81 | let caughtError; | |
@@ -66,3 +94,22 @@ const test_exception = (function() { | |||
| 66 | 94 | 'Exception state did not remain clear as expected,' + | |
| 67 | 95 | ` .wasPending() returned ${exception_pending}`); | |
| 68 | 96 | } | |
| 97 | + | ||
| 98 | + { | ||
| 99 | + // Test that no exception appears that was not thrown by us | ||
| 100 | + let caughtError; | ||
| 101 | + try { | ||
| 102 | + test_exception.constructAllowException(common.mustCall()); | ||
| 103 | + } catch (anError) { | ||
| 104 | + caughtError = anError; | ||
| 105 | + } | ||
| 106 | + assert.strictEqual(caughtError, undefined, | ||
| 107 | + 'No exception originated on the native side, but' + | ||
| 108 | + ` ${caughtError} was passed`); | ||
| 109 | + | ||
| 110 | + // Test that the exception state remains clear when no exception is thrown | ||
| 111 | + const exception_pending = test_exception.wasPending(); | ||
| 112 | + assert.strictEqual(exception_pending, false, | ||
| 113 | + 'Exception state did not remain clear as expected,' + | ||
| 114 | + ` .wasPending() returned ${exception_pending}`); | ||
| 115 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,22 @@ static napi_value returnException(napi_env env, napi_callback_info info) { | |||
| 22 | 22 | return NULL; | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | + static napi_value constructReturnException(napi_env env, napi_callback_info info) { | ||
| 26 | + size_t argc = 1; | ||
| 27 | + napi_value args[1]; | ||
| 28 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
| 29 | + | ||
| 30 | + napi_value result; | ||
| 31 | + napi_status status = napi_new_instance(env, args[0], 0, 0, &result); | ||
| 32 | + if (status == napi_pending_exception) { | ||
| 33 | + napi_value ex; | ||
| 34 | + NAPI_CALL(env, napi_get_and_clear_last_exception(env, &ex)); | ||
| 35 | + return ex; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + return NULL; | ||
| 39 | + } | ||
| 40 | + | ||
| 25 | 41 | static napi_value allowException(napi_env env, napi_callback_info info) { | |
| 26 | 42 | size_t argc = 1; | |
| 27 | 43 | napi_value args[1]; | |
@@ -38,6 +54,19 @@ static napi_value allowException(napi_env env, napi_callback_info info) { | |||
| 38 | 54 | return NULL; | |
| 39 | 55 | } | |
| 40 | 56 | ||
| 57 | + static napi_value constructAllowException(napi_env env, napi_callback_info info) { | ||
| 58 | + size_t argc = 1; | ||
| 59 | + napi_value args[1]; | ||
| 60 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
| 61 | + | ||
| 62 | + napi_value result; | ||
| 63 | + napi_new_instance(env, args[0], 0, 0, &result); | ||
| 64 | + // Ignore status and check napi_is_exception_pending() instead. | ||
| 65 | + | ||
| 66 | + NAPI_CALL(env, napi_is_exception_pending(env, &exceptionWasPending)); | ||
| 67 | + return NULL; | ||
| 68 | + } | ||
| 69 | + | ||
| 41 | 70 | static napi_value wasPending(napi_env env, napi_callback_info info) { | |
| 42 | 71 | napi_value result; | |
| 43 | 72 | NAPI_CALL(env, napi_get_boolean(env, exceptionWasPending, &result)); | |
@@ -64,6 +93,8 @@ napi_value Init(napi_env env, napi_value exports) { | |||
| 64 | 93 | napi_property_descriptor descriptors[] = { | |
| 65 | 94 | DECLARE_NAPI_PROPERTY("returnException", returnException), | |
| 66 | 95 | DECLARE_NAPI_PROPERTY("allowException", allowException), | |
| 96 | + DECLARE_NAPI_PROPERTY("constructReturnException", constructReturnException), | ||
| 97 | + DECLARE_NAPI_PROPERTY("constructAllowException", constructAllowException), | ||
| 67 | 98 | DECLARE_NAPI_PROPERTY("wasPending", wasPending), | |
| 68 | 99 | DECLARE_NAPI_PROPERTY("createExternal", createExternal), | |
| 69 | 100 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments