From 98b6cf5e593039998ea34e6e341603b673d7eec1 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Thu, 4 Jul 2019 15:57:09 -0700 Subject: [PATCH 1/2] src: expose MaybeInitializeContext to allow existing contexts Splits the node.js specific tweak intialization of NewContext into a new helper MaybeInitializeContext so that embedders with existing contexts can still use them in a node Environment now that primordials are initialized and required so early. --- src/api/environment.cc | 7 +++++++ src/node.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/api/environment.cc b/src/api/environment.cc index 4389dba1394e5c..c0096fb79bcf46 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -362,6 +362,13 @@ Local NewContext(Isolate* isolate, Local object_template) { auto context = Context::New(isolate, nullptr, object_template); if (context.IsEmpty()) return context; + + return MaybeInitializeContext(context, object_template); +} + +Local MaybeInitializeContext(Local context, + Local object_template) { + Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, diff --git a/src/node.h b/src/node.h index 049690d92fb9e4..37ae0e433ca7a3 100644 --- a/src/node.h +++ b/src/node.h @@ -305,6 +305,12 @@ NODE_EXTERN v8::Local NewContext( v8::Local object_template = v8::Local()); +// Runs Node.js-specific tweaks on an already constructed context +NODE_EXTERN v8::Local MaybeInitializeContext( + v8::Local context, + v8::Local object_template = + v8::Local()); + // If `platform` is passed, it will be used to register new Worker instances. // It can be `nullptr`, in which case creating new Workers inside of // Environments that use this `IsolateData` will not work. From 2dcfc04a9b47658388289f20db5d6f0840201060 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 16 Jul 2019 11:30:11 -0700 Subject: [PATCH 2/2] src: update MaybeInitializeContext to return MaybeLocal --- src/api/environment.cc | 16 +++++++++------- src/node.h | 6 ++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index c0096fb79bcf46..dec43ee3a6c836 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -363,11 +363,13 @@ Local NewContext(Isolate* isolate, auto context = Context::New(isolate, nullptr, object_template); if (context.IsEmpty()) return context; - return MaybeInitializeContext(context, object_template); + if (!InitializeContext(context)) { + return Local(); + } + return context; } -Local MaybeInitializeContext(Local context, - Local object_template) { +bool InitializeContext(Local context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); @@ -389,7 +391,7 @@ Local MaybeInitializeContext(Local context, if (!primordials->SetPrototype(context, Null(isolate)).FromJust() || !GetPerContextExports(context).ToLocal(&exports) || !exports->Set(context, primordials_string, primordials).FromJust()) { - return Local(); + return false; } static const char* context_files[] = {"internal/per_context/primordials", @@ -405,7 +407,7 @@ Local MaybeInitializeContext(Local context, native_module::NativeModuleEnv::LookupAndCompile( context, *module, ¶meters, nullptr); if (maybe_fn.IsEmpty()) { - return Local(); + return false; } Local fn = maybe_fn.ToLocalChecked(); MaybeLocal result = @@ -414,12 +416,12 @@ Local MaybeInitializeContext(Local context, // Execution failed during context creation. // TODO(joyeecheung): deprecate this signature and return a MaybeLocal. if (result.IsEmpty()) { - return Local(); + return false; } } } - return context; + return true; } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node.h b/src/node.h index 37ae0e433ca7a3..a52f242cac1360 100644 --- a/src/node.h +++ b/src/node.h @@ -306,10 +306,8 @@ NODE_EXTERN v8::Local NewContext( v8::Local()); // Runs Node.js-specific tweaks on an already constructed context -NODE_EXTERN v8::Local MaybeInitializeContext( - v8::Local context, - v8::Local object_template = - v8::Local()); +// Return value indicates success of operation +NODE_EXTERN bool InitializeContext(v8::Local context); // If `platform` is passed, it will be used to register new Worker instances. // It can be `nullptr`, in which case creating new Workers inside of