Skip to content

Commit c6b4451

Browse files
committed
node-api: Add API to check safety of calling JS
1 parent 18a0ead commit c6b4451

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/api/n-api.md

+18
Original file line numberDiff line numberDiff line change
@@ -6295,6 +6295,24 @@ node_api_get_module_file_name(napi_env env, const char** result);
62956295
`result` may be an empty string if the add-on loading process fails to establish
62966296
the add-on's file name during loading.
62976297

6298+
#### `napi_can_call_into_js`
6299+
6300+
<!-- YAML
6301+
added: REPLACEME
6302+
-->
6303+
6304+
> Stability: 1 - Experimental
6305+
6306+
```c
6307+
NAPI_EXTERN napi_status napi_can_call_into_js(napi_env env);
6308+
```
6309+
6310+
* `[in] env`: The environment that the API is invoked under.
6311+
6312+
Returns `napi_ok` if the API succeeded.
6313+
6314+
This API is used to check if it is safe to call into JavaScript.
6315+
62986316
[ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/
62996317
[AppVeyor]: https://www.appveyor.com
63006318
[C++ Addons]: addons.md

src/js_native_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_object_seal(napi_env env,
566566
napi_value object);
567567
#endif // NAPI_VERSION >= 8
568568

569+
#ifdef NAPI_EXPERIMENTAL
570+
NAPI_EXTERN napi_status NAPI_CDECL napi_can_call_into_js(napi_env env,
571+
bool* result);
572+
#endif
573+
569574
EXTERN_C_END
570575

571576
#endif // SRC_JS_NATIVE_API_H_

src/js_native_api_v8.cc

+7
Original file line numberDiff line numberDiff line change
@@ -3252,3 +3252,10 @@ napi_status NAPI_CDECL napi_is_detached_arraybuffer(napi_env env,
32523252

32533253
return napi_clear_last_error(env);
32543254
}
3255+
3256+
napi_status NAPI_CDECL napi_can_call_into_js(napi_env env, bool* result) {
3257+
CHECK_ENV(env);
3258+
CHECK_ARG(env, result);
3259+
*result = (env)->can_call_into_js();
3260+
return napi_clear_last_error(env);
3261+
}

0 commit comments

Comments
 (0)