Skip to content

Commit

Permalink
node-api: Add API to check safety of calling JS
Browse files Browse the repository at this point in the history
  • Loading branch information
srriddle committed Jul 12, 2022
1 parent 18a0ead commit c6b4451
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

<!-- YAML
added: REPLACEME
-->

> 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
Expand Down
5 changes: 5 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
7 changes: 7 additions & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit c6b4451

Please sign in to comment.