-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: port test_cannot_run_js to CTS #28
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
kraenhansen
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
kraenhansen:feat/port-test-cannot-run-js
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.
+96
−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,5 @@ | ||
| add_node_api_cts_addon(test_cannot_run_js test_cannot_run_js.c) | ||
| target_compile_definitions(test_cannot_run_js PRIVATE NAPI_VERSION=10) | ||
|
|
||
| add_node_api_cts_addon(test_pending_exception test_cannot_run_js.c) | ||
| target_compile_definitions(test_pending_exception PRIVATE NAPI_VERSION=9) |
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,24 @@ | ||
| // Test that `napi_get_named_property()` returns `napi_cannot_run_js` in | ||
| // experimental mode and `napi_pending_exception` otherwise. This test calls | ||
| // the add-on's `createRef()` method, which creates a strong reference to a JS | ||
| // function. When the process exits, it calls all reference finalizers. The | ||
| // finalizer for the strong reference created herein will attempt to call | ||
| // `napi_get_property()` on a property of the global object and will abort the | ||
| // process if the API doesn't return the correct status. | ||
|
|
||
| if (napiVersion < 10) skipTest(); | ||
| const addon_v8 = loadAddon("test_pending_exception"); | ||
| const addon_new = loadAddon("test_cannot_run_js"); | ||
|
|
||
| function runTests(addon) { | ||
| addon.createRef(function () { | ||
| throw new Error("function should not have been called"); | ||
| }); | ||
| } | ||
|
|
||
| function runAllTests() { | ||
| runTests(addon_v8); | ||
| runTests(addon_new); | ||
| } | ||
|
|
||
| runAllTests(); | ||
66 changes: 66 additions & 0 deletions
66
tests/js-native-api/test_cannot_run_js/test_cannot_run_js.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,66 @@ | ||
| #include <js_native_api.h> | ||
| #include "../common.h" | ||
| #include "../entry_point.h" | ||
| #include "stdlib.h" | ||
|
|
||
| static void Finalize(napi_env env, void* data, void* hint) { | ||
| napi_value global, set_timeout; | ||
| napi_ref* ref = data; | ||
|
|
||
| NODE_API_BASIC_ASSERT_RETURN_VOID( | ||
| napi_delete_reference(env, *ref) == napi_ok, | ||
| "deleting reference in finalizer should succeed"); | ||
| NODE_API_BASIC_ASSERT_RETURN_VOID( | ||
| napi_get_global(env, &global) == napi_ok, | ||
| "getting global reference in finalizer should succeed"); | ||
| napi_status result = | ||
| napi_get_named_property(env, global, "setTimeout", &set_timeout); | ||
|
|
||
| // The finalizer could be invoked either from check callbacks (as native | ||
| // immediates) if the event loop is still running (where napi_ok is returned) | ||
| // or during environment shutdown (where napi_cannot_run_js or | ||
| // napi_pending_exception is returned). This is not deterministic from | ||
| // the point of view of the addon. | ||
|
|
||
| #if NAPI_VERSION > 9 | ||
| NODE_API_BASIC_ASSERT_RETURN_VOID( | ||
| result == napi_cannot_run_js || result == napi_ok, | ||
| "getting named property from global in finalizer should succeed " | ||
| "or return napi_cannot_run_js"); | ||
| #else | ||
| NODE_API_BASIC_ASSERT_RETURN_VOID( | ||
| result == napi_pending_exception || result == napi_ok, | ||
| "getting named property from global in finalizer should succeed " | ||
| "or return napi_pending_exception"); | ||
| #endif // NAPI_VERSION > 9 | ||
| free(ref); | ||
| } | ||
|
|
||
| static napi_value CreateRef(napi_env env, napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value cb; | ||
| napi_valuetype value_type; | ||
| napi_ref* ref = malloc(sizeof(*ref)); | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &cb, NULL, NULL)); | ||
| NODE_API_ASSERT(env, argc == 1, "Function takes only one argument"); | ||
| NODE_API_CALL(env, napi_typeof(env, cb, &value_type)); | ||
| NODE_API_ASSERT( | ||
| env, value_type == napi_function, "argument must be function"); | ||
| NODE_API_CALL(env, napi_add_finalizer(env, cb, ref, Finalize, NULL, ref)); | ||
| return cb; | ||
| } | ||
|
|
||
| EXTERN_C_START | ||
| napi_value Init(napi_env env, napi_value exports) { | ||
| napi_property_descriptor properties[] = { | ||
| DECLARE_NODE_API_PROPERTY("createRef", CreateRef), | ||
| }; | ||
|
|
||
| NODE_API_CALL( | ||
| env, | ||
| napi_define_properties( | ||
| env, exports, sizeof(properties) / sizeof(*properties), properties)); | ||
|
|
||
| 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.
let's split this into two tests. so that we can only skip
test_cannot_run_jswhennapiVersion < 10. We did the split similarly in #44There 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.
I see we did that 🤔 My hope is that we can keep these tests very close (almost verbatim) to the original source as this makes it easier to automatically spot regressions in coverage.
And then have follow-up PRs with refactors, which are easy to revert in isolation.
I suggest that we create an issue to track splitting this or simply follow up with a PR splitting this once it merges. @legendecas would you be okay with that approach?