From 19fa9f1bc47b0666be0747583bea8cb3d8ad5eb1 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sat, 13 May 2023 01:36:13 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/47986 Reviewed-By: Chengzhong Wu Signed-off-by: Gabriel Schulhof --- 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_cannot_run_js/binding.gyp | 32 ++++++++++++ test/js-native-api/test_cannot_run_js/test.js | 24 +++++++++ .../test_cannot_run_js/test_cannot_run_js.c | 49 +++++++++++++++++++ 7 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 test/js-native-api/test_cannot_run_js/binding.gyp create mode 100644 test/js-native-api/test_cannot_run_js/test.js create mode 100644 test/js-native-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 54c53efcef87f9..ec8df553562be7 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 (`NAPI_EXPERIMENTAL` is defined): + + 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`