forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node-api: add status napi_cannot_run_js
Add the new status in order to distinguish a state wherein an exception is pending from one wherein the engine is unable to execute JS. We take advantage of the new runtime add-on version reporting in order to remain forward compatible with add-ons that do not expect the new status code. PR-URL: nodejs#47986 Reviewed-By: Chengzhong Wu <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
- Loading branch information
1 parent
a876334
commit bdd0639
Showing
7 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |