diff --git a/src/node_contextify.cc b/src/node_contextify.cc index fd17ae179f610e..b7c7f38eb6f695 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -104,12 +104,12 @@ Local Uint32ToName(Local context, uint32_t index) { ContextifyContext::ContextifyContext( Environment* env, Local sandbox_obj, const ContextOptions& options) : env_(env) { - Local v8_context = CreateV8Context(env, sandbox_obj, options); - context_.Reset(env->isolate(), v8_context); + MaybeLocal v8_context = CreateV8Context(env, sandbox_obj, options); - // Allocation failure or maximum call stack size reached - if (context_.IsEmpty()) - return; + // Allocation failure, maximum call stack size reached, termination, etc. + if (v8_context.IsEmpty()) return; + + context_.Reset(env->isolate(), v8_context.ToLocalChecked()); context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter); } @@ -119,20 +119,19 @@ ContextifyContext::ContextifyContext( // pass the main JavaScript context object we're embedded in, then the // NamedPropertyHandler will store a reference to it forever and keep it // from getting gc'd. -Local ContextifyContext::CreateDataWrapper(Environment* env) { - EscapableHandleScope scope(env->isolate()); - Local wrapper = - env->script_data_constructor_function() - ->NewInstance(env->context()).FromMaybe(Local()); - if (wrapper.IsEmpty()) - return scope.Escape(Local::New(env->isolate(), Local())); +MaybeLocal ContextifyContext::CreateDataWrapper(Environment* env) { + Local wrapper; + if (!env->script_data_constructor_function() + ->NewInstance(env->context()) + .ToLocal(&wrapper)) { + return MaybeLocal(); + } wrapper->SetAlignedPointerInInternalField(0, this); - return scope.Escape(wrapper); + return wrapper; } - -Local ContextifyContext::CreateV8Context( +MaybeLocal ContextifyContext::CreateV8Context( Environment* env, Local sandbox_obj, const ContextOptions& options) { @@ -145,13 +144,17 @@ Local ContextifyContext::CreateV8Context( Local object_template = function_template->InstanceTemplate(); + Local data_wrapper; + if (!CreateDataWrapper(env).ToLocal(&data_wrapper)) + return MaybeLocal(); + NamedPropertyHandlerConfiguration config(PropertyGetterCallback, PropertySetterCallback, PropertyDescriptorCallback, PropertyDeleterCallback, PropertyEnumeratorCallback, PropertyDefinerCallback, - CreateDataWrapper(env)); + data_wrapper); IndexedPropertyHandlerConfiguration indexed_config( IndexedPropertyGetterCallback, @@ -160,7 +163,7 @@ Local ContextifyContext::CreateV8Context( IndexedPropertyDeleterCallback, PropertyEnumeratorCallback, IndexedPropertyDefinerCallback, - CreateDataWrapper(env)); + data_wrapper); object_template->SetHandler(config); object_template->SetHandler(indexed_config); @@ -169,7 +172,7 @@ Local ContextifyContext::CreateV8Context( if (ctx.IsEmpty()) { env->ThrowError("Could not instantiate context"); - return Local(); + return MaybeLocal(); } ctx->SetSecurityToken(env->context()->GetSecurityToken()); diff --git a/src/node_contextify.h b/src/node_contextify.h index 5f4f20554aed50..cb3ca83553a61c 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -23,9 +23,10 @@ class ContextifyContext { v8::Local sandbox_obj, const ContextOptions& options); - v8::Local CreateDataWrapper(Environment* env); - v8::Local CreateV8Context(Environment* env, - v8::Local sandbox_obj, const ContextOptions& options); + v8::MaybeLocal CreateDataWrapper(Environment* env); + v8::MaybeLocal CreateV8Context(Environment* env, + v8::Local sandbox_obj, + const ContextOptions& options); static void Init(Environment* env, v8::Local target); static ContextifyContext* ContextFromContextifiedSandbox(