From c6b44514ea64096332adfbbd9cb26582380d11df Mon Sep 17 00:00:00 2001 From: Steve Riddle Date: Tue, 12 Jul 2022 13:41:28 -0400 Subject: [PATCH] node-api: Add API to check safety of calling JS --- doc/api/n-api.md | 18 ++++++++++++++++++ src/js_native_api.h | 5 +++++ src/js_native_api_v8.cc | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 883e708cac8ad2..89bb5d5f77c164 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -6295,6 +6295,24 @@ node_api_get_module_file_name(napi_env env, const char** result); `result` may be an empty string if the add-on loading process fails to establish the add-on's file name during loading. +#### `napi_can_call_into_js` + + + +> Stability: 1 - Experimental + +```c +NAPI_EXTERN napi_status napi_can_call_into_js(napi_env env); +``` + +* `[in] env`: The environment that the API is invoked under. + +Returns `napi_ok` if the API succeeded. + +This API is used to check if it is safe to call into JavaScript. + [ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/ [AppVeyor]: https://www.appveyor.com [C++ Addons]: addons.md diff --git a/src/js_native_api.h b/src/js_native_api.h index 220d140d4bfe9a..04c277535cc227 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -566,6 +566,11 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_object_seal(napi_env env, napi_value object); #endif // NAPI_VERSION >= 8 +#ifdef NAPI_EXPERIMENTAL +NAPI_EXTERN napi_status NAPI_CDECL napi_can_call_into_js(napi_env env, + bool* result); +#endif + EXTERN_C_END #endif // SRC_JS_NATIVE_API_H_ diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 0ddbc87e45393e..d087f45cc93805 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -3252,3 +3252,10 @@ napi_status NAPI_CDECL napi_is_detached_arraybuffer(napi_env env, return napi_clear_last_error(env); } + +napi_status NAPI_CDECL napi_can_call_into_js(napi_env env, bool* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + *result = (env)->can_call_into_js(); + return napi_clear_last_error(env); +}