From 0e8930103f3a72436ccb6e6c78209d1cf482573d Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sat, 13 May 2023 01:36:13 -0700 Subject: [PATCH 1/9] 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. --- doc/api/n-api.md | 19 ++++++-- src/js_native_api_types.h | 3 +- src/js_native_api_v8.cc | 3 +- src/js_native_api_v8.h | 9 ++-- test/node-api/test_cannot_run_js/binding.gyp | 14 ++++++ test/node-api/test_cannot_run_js/test.js | 23 ++++++++++ .../test_cannot_run_js/test_cannot_run_js.c | 46 +++++++++++++++++++ 7 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 test/node-api/test_cannot_run_js/binding.gyp create mode 100644 test/node-api/test_cannot_run_js/test.js create mode 100644 test/node-api/test_cannot_run_js/test_cannot_run_js.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 77cc49946c4231..e14bf18c5c235d 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -580,7 +580,8 @@ typedef enum { napi_arraybuffer_expected, napi_detachable_arraybuffer_expected, napi_would_deadlock, /* unused */ - napi_no_external_buffers_allowed + napi_no_external_buffers_allowed, + napi_cannot_run_js } napi_status; ``` @@ -814,6 +815,18 @@ typedef void (*napi_finalize)(napi_env env, Unless for reasons discussed in [Object Lifetime Management][], creating a handle and/or callback scope inside the function body is not necessary. +Since these functions may be called while the JavaScript engine is in a state +where it cannot execute JavaScript code, some Node-API calls may return +`napi_pending_exception` even when there is no exception pending. + +Change History: + +* experimental: + + Node-API calls made from a finalizer will return `napi_cannot_run_js` when + the JavaScript engine is unable to execute JavaScript, and will return + `napi_exception_pending` if there is a pending exception. + #### `napi_async_execute_callback`