-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
node-api: add status napi_cannot_run_js #47986
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
Closed
gabrielschulhof
wants to merge
9
commits into
nodejs:main
from
gabrielschulhof:cannot-call-into-js-status
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e89301
node-api: add status napi_cannot_run_js
gabrielschulhof 86277c9
doc: update doc/api/n-api.md
gabrielschulhof fa45e0a
Update test/node-api/test_cannot_run_js/test.js
gabrielschulhof 95137f5
test: access global property to illicit error status
gabrielschulhof 72980e2
heed the linter
gabrielschulhof d6f3232
test: address nit
gabrielschulhof d8bee9e
test: use variables instead of target_defaults
gabrielschulhof ab54044
test: explicitly copy dependency
gabrielschulhof 128a6f8
test: create job dependency to copy the common file
gabrielschulhof 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
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
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,32 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "copy_entry_point", | ||
"type": "none", | ||
"copies": [ | ||
{ | ||
"destination": ".", | ||
"files": [ "../entry_point.c" ] | ||
} | ||
] | ||
}, | ||
{ | ||
"target_name": "test_cannot_run_js", | ||
"sources": [ | ||
"entry_point.c", | ||
"test_cannot_run_js.c" | ||
], | ||
"defines": [ "NAPI_EXPERIMENTAL" ], | ||
"dependencies": [ "copy_entry_point" ], | ||
}, | ||
{ | ||
"target_name": "test_pending_exception", | ||
"sources": [ | ||
"entry_point.c", | ||
"test_cannot_run_js.c" | ||
], | ||
"defines": [ "NAPI_VERSION=8" ], | ||
"dependencies": [ "copy_entry_point" ], | ||
} | ||
] | ||
} |
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 @@ | ||
'use strict'; | ||
|
||
// Test that `napi_call_function()` 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. | ||
|
||
const { buildType, mustNotCall } = require('../../common'); | ||
const addon_v8 = require(`./build/${buildType}/test_pending_exception`); | ||
const addon_new = require(`./build/${buildType}/test_cannot_run_js`); | ||
|
||
function runTests(addon, isVersion8) { | ||
addon.createRef(mustNotCall()); | ||
} | ||
|
||
function runAllTests() { | ||
runTests(addon_v8, /* isVersion8 */ true); | ||
runTests(addon_new, /* isVersion8 */ false); | ||
} | ||
|
||
runAllTests(); |
49 changes: 49 additions & 0 deletions
49
test/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,49 @@ | ||
#include <js_native_api.h> | ||
#include "../common.h" | ||
#include "stdlib.h" | ||
|
||
static void Finalize(napi_env env, void* data, void* hint) { | ||
napi_value global, set_timeout; | ||
napi_ref* ref = data; | ||
#ifdef NAPI_EXPERIMENTAL | ||
napi_status expected_status = napi_cannot_run_js; | ||
#else | ||
napi_status expected_status = napi_pending_exception; | ||
#endif // NAPI_EXPERIMENTAL | ||
|
||
if (napi_delete_reference(env, *ref) != napi_ok) abort(); | ||
if (napi_get_global(env, &global) != napi_ok) abort(); | ||
if (napi_get_named_property(env, global, "setTimeout", &set_timeout) != | ||
expected_status) | ||
abort(); | ||
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 |
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.
Uh oh!
There was an error while loading. Please reload this page.