-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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.
- Loading branch information
1 parent
2dd6d76
commit a843857
Showing
7 changed files
with
107 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,14 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_cannot_run_js", | ||
"sources": [ "test_cannot_run_js.c" ], | ||
"defines": [ "NAPI_EXPERIMENTAL" ], | ||
}, | ||
{ | ||
"target_name": "test_pending_exception", | ||
"sources": [ "test_cannot_run_js.c" ], | ||
"defines": [ "NAPI_VERSION=8" ], | ||
} | ||
] | ||
} |
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,23 @@ | ||
'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_call_function()` | ||
// and will abort the process if the API doesn't return the correct status. | ||
|
||
const { buildType } = 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(runTests); | ||
} | ||
|
||
function runAllTests() { | ||
runTests(addon_v8, /* isVersion8 */ true); | ||
runTests(addon_new, /* isVersion8 */ false); | ||
} | ||
|
||
runAllTests(); |
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,44 @@ | ||
#include <node_api.h> | ||
#include "../../js-native-api/common.h" | ||
#include "stdlib.h" | ||
|
||
static void Finalize(napi_env env, void* data, void* hint) { | ||
napi_value cb; | ||
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_get_reference_value(env, *ref, &cb) != napi_ok) abort(); | ||
if (napi_delete_reference(env, *ref) != napi_ok) abort(); | ||
if (napi_call_function(env, cb, cb, 0, NULL, NULL) != 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; | ||
} | ||
|
||
NAPI_MODULE_INIT() { | ||
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; | ||
} |