Skip to content

Commit 6f056e5

Browse files
MarshallOfSoundBridgeAR
authored andcommitted
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.js Environment now that primordials are initialized and required so early. Update MaybeInitializeContext to return MaybeLocal, PR-URL: #28544 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a39caed commit 6f056e5

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/api/environment.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ Local<Context> NewContext(Isolate* isolate,
353353
Local<ObjectTemplate> object_template) {
354354
auto context = Context::New(isolate, nullptr, object_template);
355355
if (context.IsEmpty()) return context;
356+
357+
if (!InitializeContext(context)) {
358+
return Local<Context>();
359+
}
360+
return context;
361+
}
362+
363+
bool InitializeContext(Local<Context> context) {
364+
Isolate* isolate = context->GetIsolate();
356365
HandleScope handle_scope(isolate);
357366

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

379388
static const char* context_files[] = {"internal/per_context/primordials",
@@ -389,7 +398,7 @@ Local<Context> NewContext(Isolate* isolate,
389398
native_module::NativeModuleEnv::LookupAndCompile(
390399
context, *module, &parameters, nullptr);
391400
if (maybe_fn.IsEmpty()) {
392-
return Local<Context>();
401+
return false;
393402
}
394403
Local<Function> fn = maybe_fn.ToLocalChecked();
395404
MaybeLocal<Value> result =
@@ -398,12 +407,12 @@ Local<Context> NewContext(Isolate* isolate,
398407
// Execution failed during context creation.
399408
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
400409
if (result.IsEmpty()) {
401-
return Local<Context>();
410+
return false;
402411
}
403412
}
404413
}
405414

406-
return context;
415+
return true;
407416
}
408417

409418
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {

src/node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ NODE_EXTERN v8::Local<v8::Context> NewContext(
299299
v8::Local<v8::ObjectTemplate> object_template =
300300
v8::Local<v8::ObjectTemplate>());
301301

302+
// Runs Node.js-specific tweaks on an already constructed context
303+
// Return value indicates success of operation
304+
NODE_EXTERN bool InitializeContext(v8::Local<v8::Context> context);
305+
302306
// If `platform` is passed, it will be used to register new Worker instances.
303307
// It can be `nullptr`, in which case creating new Workers inside of
304308
// Environments that use this `IsolateData` will not work.

0 commit comments

Comments
 (0)