Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: expose MaybeInitializeContext to allow existing contexts #28544

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,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 @@ -382,7 +391,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 @@ -398,7 +407,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 @@ -407,12 +416,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 @@ -305,6 +305,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