-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: port test_exception to CTS #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bavulapati
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
bavulapati:feat/port-test-exception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add_node_api_cts_addon(test_exception test_exception.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| 'use strict'; | ||
|
|
||
| const theError = new Error('Some error'); | ||
|
|
||
| // The test module throws an error during Init, but in order for its exports to | ||
| // not be lost, it attaches them to the error's "bindings" property. This way, | ||
| // we can make sure that exceptions thrown during the module initialization | ||
| // phase are propagated through require() into JavaScript. | ||
| // https://github.com/nodejs/node/issues/19437 | ||
| const test_exception = (function() { | ||
| let resultingException; | ||
| try { | ||
| loadAddon('test_exception'); | ||
| } catch (anException) { | ||
| resultingException = anException; | ||
| } | ||
| assert.strictEqual(resultingException.message, 'Error during Init'); | ||
| return resultingException.binding; | ||
| })(); | ||
|
|
||
| { | ||
| const throwTheError = () => { | ||
| throw theError; | ||
| }; | ||
|
|
||
| // Test that the native side successfully captures the exception | ||
| let returnedError = test_exception.returnException(throwTheError); | ||
| assert.strictEqual(returnedError, theError); | ||
|
|
||
| // Test that the native side passes the exception through | ||
| assert.throws( | ||
| () => { | ||
| test_exception.allowException(throwTheError); | ||
| }, | ||
| (err) => err === theError, | ||
| ); | ||
|
|
||
| // Test that the exception thrown above was marked as pending | ||
| // before it was handled on the JS side | ||
| const exception_pending = test_exception.wasPending(); | ||
| assert.strictEqual( | ||
| exception_pending, | ||
| true, | ||
| 'Exception not pending as expected,' + | ||
| ` .wasPending() returned ${exception_pending}`, | ||
| ); | ||
|
|
||
| // Test that the native side does not capture a non-existing exception | ||
| returnedError = test_exception.returnException(mustCall()); | ||
| assert.strictEqual( | ||
| returnedError, | ||
| undefined, | ||
| 'Returned error should be undefined when no exception is' + | ||
| ` thrown, but ${returnedError} was passed`, | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| const throwTheError = class { | ||
| constructor() { | ||
| throw theError; | ||
| } | ||
| }; | ||
|
|
||
| // Test that the native side successfully captures the exception | ||
| let returnedError = test_exception.constructReturnException(throwTheError); | ||
| assert.strictEqual(returnedError, theError); | ||
|
|
||
| // Test that the native side passes the exception through | ||
| assert.throws( | ||
| () => { | ||
| test_exception.constructAllowException(throwTheError); | ||
| }, | ||
| (err) => err === theError, | ||
| ); | ||
|
|
||
| // Test that the exception thrown above was marked as pending | ||
| // before it was handled on the JS side | ||
| const exception_pending = test_exception.wasPending(); | ||
| assert.strictEqual( | ||
| exception_pending, | ||
| true, | ||
| 'Exception not pending as expected,' + | ||
| ` .wasPending() returned ${exception_pending}`, | ||
| ); | ||
|
|
||
| // Test that the native side does not capture a non-existing exception | ||
| returnedError = test_exception.constructReturnException(mustCall()); | ||
| assert.strictEqual( | ||
| returnedError, | ||
| undefined, | ||
| 'Returned error should be undefined when no exception is' + | ||
| ` thrown, but ${returnedError} was passed`, | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| // Test that no exception appears that was not thrown by us | ||
| let caughtError; | ||
| try { | ||
| test_exception.allowException(mustCall()); | ||
| } catch (anError) { | ||
| caughtError = anError; | ||
| } | ||
| assert.strictEqual( | ||
| caughtError, | ||
| undefined, | ||
| 'No exception originated on the native side, but' + | ||
| ` ${caughtError} was passed`, | ||
| ); | ||
|
|
||
| // Test that the exception state remains clear when no exception is thrown | ||
| const exception_pending = test_exception.wasPending(); | ||
| assert.strictEqual( | ||
| exception_pending, | ||
| false, | ||
| 'Exception state did not remain clear as expected,' + | ||
| ` .wasPending() returned ${exception_pending}`, | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| // Test that no exception appears that was not thrown by us | ||
| let caughtError; | ||
| try { | ||
| test_exception.constructAllowException(mustCall()); | ||
| } catch (anError) { | ||
| caughtError = anError; | ||
| } | ||
| assert.strictEqual( | ||
| caughtError, | ||
| undefined, | ||
| 'No exception originated on the native side, but' + | ||
| ` ${caughtError} was passed`, | ||
| ); | ||
|
|
||
| // Test that the exception state remains clear when no exception is thrown | ||
| const exception_pending = test_exception.wasPending(); | ||
| assert.strictEqual( | ||
| exception_pending, | ||
| false, | ||
| 'Exception state did not remain clear as expected,' + | ||
| ` .wasPending() returned ${exception_pending}`, | ||
| ); | ||
| } |
26 changes: 26 additions & 0 deletions
26
tests/js-native-api/test_exception/testFinalizerException.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // A C finalizer that throws during GC surfaces as an uncaught exception. This | ||
| // mirrors test_reference/test_finalizer.js, except the addon here throws from | ||
| // Init; its exports are still reachable via the error's `.binding`, so a | ||
| // finalizer can be installed even though initialization failed. | ||
| const binding = (function() { | ||
| let resultingException; | ||
| try { | ||
| loadAddon('test_exception'); | ||
| } catch (anException) { | ||
| resultingException = anException; | ||
| } | ||
| assert.strictEqual(resultingException.message, 'Error during Init'); | ||
| return resultingException.binding; | ||
| })(); | ||
|
|
||
| onUncaughtException(mustCall((err) => { | ||
| assert.match(err.message, /Error during Finalize/); | ||
| })); | ||
|
|
||
| (async function() { | ||
| binding.createExternal(); | ||
|
|
||
| // GC until the finalizer fires; the counter just bounds the loop. | ||
| let gcCount = 10; | ||
| await gcUntil('test', () => --gcCount <= 0); | ||
| })().then(mustCall()); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #include <js_native_api.h> | ||
| #include "../common.h" | ||
| #include "../entry_point.h" | ||
|
|
||
| static bool exceptionWasPending = false; | ||
| static int num = 0x23432; | ||
|
|
||
| static napi_value returnException(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value global; | ||
| NODE_API_CALL(env, napi_get_global(env, &global)); | ||
|
|
||
| napi_value result; | ||
| napi_status status = napi_call_function(env, global, args[0], 0, 0, &result); | ||
| if (status == napi_pending_exception) { | ||
| napi_value ex; | ||
| NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex)); | ||
| return ex; | ||
| } | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| static napi_value constructReturnException(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value result; | ||
| napi_status status = napi_new_instance(env, args[0], 0, 0, &result); | ||
| if (status == napi_pending_exception) { | ||
| napi_value ex; | ||
| NODE_API_CALL(env, napi_get_and_clear_last_exception(env, &ex)); | ||
| return ex; | ||
| } | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| static napi_value allowException(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value global; | ||
| NODE_API_CALL(env, napi_get_global(env, &global)); | ||
|
|
||
| napi_value result; | ||
| napi_call_function(env, global, args[0], 0, 0, &result); | ||
| // Ignore status and check napi_is_exception_pending() instead. | ||
|
|
||
| NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending)); | ||
| return NULL; | ||
| } | ||
|
|
||
| static napi_value constructAllowException(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| napi_value result; | ||
| napi_new_instance(env, args[0], 0, 0, &result); | ||
| // Ignore status and check napi_is_exception_pending() instead. | ||
|
|
||
| NODE_API_CALL(env, napi_is_exception_pending(env, &exceptionWasPending)); | ||
| return NULL; | ||
| } | ||
|
|
||
| static napi_value wasPending(napi_env env, napi_callback_info info) { | ||
| napi_value result; | ||
| NODE_API_CALL(env, napi_get_boolean(env, exceptionWasPending, &result)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| static void finalizer(napi_env env, void *data, void *hint) { | ||
| NODE_API_CALL_RETURN_VOID(env, | ||
| napi_throw_error(env, NULL, "Error during Finalize")); | ||
| } | ||
|
|
||
| static napi_value createExternal(napi_env env, napi_callback_info info) { | ||
| napi_value external; | ||
|
|
||
| NODE_API_CALL(env, | ||
| napi_create_external(env, &num, finalizer, NULL, &external)); | ||
|
|
||
| return external; | ||
| } | ||
|
|
||
| EXTERN_C_START | ||
| napi_value Init(napi_env env, napi_value exports) { | ||
| napi_property_descriptor descriptors[] = { | ||
| DECLARE_NODE_API_PROPERTY("returnException", returnException), | ||
| DECLARE_NODE_API_PROPERTY("allowException", allowException), | ||
| DECLARE_NODE_API_PROPERTY("constructReturnException", constructReturnException), | ||
| DECLARE_NODE_API_PROPERTY("constructAllowException", constructAllowException), | ||
| DECLARE_NODE_API_PROPERTY("wasPending", wasPending), | ||
| DECLARE_NODE_API_PROPERTY("createExternal", createExternal), | ||
| }; | ||
| NODE_API_CALL(env, napi_define_properties( | ||
| env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
|
|
||
| napi_value error, code, message; | ||
| NODE_API_CALL(env, napi_create_string_utf8(env, "Error during Init", | ||
| NAPI_AUTO_LENGTH, &message)); | ||
| NODE_API_CALL(env, napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &code)); | ||
| NODE_API_CALL(env, napi_create_error(env, code, message, &error)); | ||
| NODE_API_CALL(env, napi_set_named_property(env, error, "binding", exports)); | ||
| NODE_API_CALL(env, napi_throw(env, error)); | ||
|
|
||
| return exports; | ||
| } | ||
| EXTERN_C_END |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcUntilhas a counter already. This can be optimized to check untilonUncaughtExceptionis invoked.