diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-15 13:48:56 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-02-17 20:09:58 +0300 |
commit | 441ef4d7f03131ddfd6e82e405bdd3640ec5bf5a (patch) | |
tree | f1e87015ee36ee209f85ee6eb9bde76be9705120 /src | |
parent | 783c65ebc4143ed9afa723f26eaebdecf5f98691 (diff) |
n-api: do not call into JS when that is not allowed
Check whether calling into JS is allowed before doing so.
PR-URL: https://github.com/nodejs/node/pull/26127
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/js_native_api_v8.h | 14 | ||||
-rw-r--r-- | src/node_api.cc | 5 |
2 files changed, 14 insertions, 5 deletions
diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 81b00f2aa59..094fc4afcc0 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -11,6 +11,7 @@ struct napi_env__ { context_persistent(isolate, context) { CHECK_EQ(isolate, context->GetIsolate()); } + virtual ~napi_env__() {} v8::Isolate* const isolate; // Shortcut for context()->GetIsolate() v8impl::Persistent<v8::Context> context_persistent; @@ -21,6 +22,8 @@ struct napi_env__ { inline void Ref() { refs++; } inline void Unref() { if ( --refs == 0) delete this; } + virtual bool can_call_into_js() const { return true; } + v8impl::Persistent<v8::Value> last_exception; napi_extended_error_info last_error; int open_handle_scopes = 0; @@ -68,11 +71,12 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code, RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status)) // NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope -#define NAPI_PREAMBLE(env) \ - CHECK_ENV((env)); \ - RETURN_STATUS_IF_FALSE((env), (env)->last_exception.IsEmpty(), \ - napi_pending_exception); \ - napi_clear_last_error((env)); \ +#define NAPI_PREAMBLE(env) \ + CHECK_ENV((env)); \ + RETURN_STATUS_IF_FALSE((env), \ + (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \ + napi_pending_exception); \ + napi_clear_last_error((env)); \ v8impl::TryCatch try_catch((env)) #define CHECK_TO_TYPE(env, type, context, result, src, status) \ diff --git a/src/node_api.cc b/src/node_api.cc index ff2e12f571a..010fb3fe0ff 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -12,9 +12,14 @@ struct node_napi_env__ : public napi_env__ { napi_env__(context) { CHECK_NOT_NULL(node_env()); } + inline node::Environment* node_env() const { return node::Environment::GetCurrent(context()); } + + bool can_call_into_js() const override { + return node_env()->can_call_into_js(); + } }; typedef node_napi_env__* node_napi_env; |