Skip to content

Commit

Permalink
src: expose MaybeInitializeContext to allow existing contexts
Browse files Browse the repository at this point in the history
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.js Environment now that primordials are
initialized and required so early.

Update MaybeInitializeContext to return MaybeLocal,

PR-URL: #28544
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
MarshallOfSound authored and BridgeAR committed Sep 3, 2019
1 parent a39caed commit 6f056e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ Local<Context> NewContext(Isolate* isolate,
Local<ObjectTemplate> object_template) {
auto context = Context::New(isolate, nullptr, object_template);
if (context.IsEmpty()) return context;

if (!InitializeContext(context)) {
return Local<Context>();
}
return context;
}

bool InitializeContext(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
HandleScope handle_scope(isolate);

context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
Expand All @@ -373,7 +382,7 @@ Local<Context> NewContext(Isolate* isolate,
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
!GetPerContextExports(context).ToLocal(&exports) ||
!exports->Set(context, primordials_string, primordials).FromJust()) {
return Local<Context>();
return false;
}

static const char* context_files[] = {"internal/per_context/primordials",
Expand All @@ -389,7 +398,7 @@ Local<Context> NewContext(Isolate* isolate,
native_module::NativeModuleEnv::LookupAndCompile(
context, *module, &parameters, nullptr);
if (maybe_fn.IsEmpty()) {
return Local<Context>();
return false;
}
Local<Function> fn = maybe_fn.ToLocalChecked();
MaybeLocal<Value> result =
Expand All @@ -398,12 +407,12 @@ Local<Context> NewContext(Isolate* isolate,
// Execution failed during context creation.
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
if (result.IsEmpty()) {
return Local<Context>();
return false;
}
}
}

return context;
return true;
}

uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
Expand Down
4 changes: 4 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(
v8::Local<v8::ObjectTemplate> object_template =
v8::Local<v8::ObjectTemplate>());

// Runs Node.js-specific tweaks on an already constructed context
// Return value indicates success of operation
NODE_EXTERN bool InitializeContext(v8::Local<v8::Context> 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
// Environments that use this `IsolateData` will not work.
Expand Down

0 comments on commit 6f056e5

Please sign in to comment.