@@ -411,7 +411,8 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
411
411
return handle_scope.Escape (existing_value.As <Object>());
412
412
413
413
Local<Object> exports = Object::New (isolate);
414
- if (context->Global ()->SetPrivate (context, key, exports).IsNothing ())
414
+ if (context->Global ()->SetPrivate (context, key, exports).IsNothing () ||
415
+ !InitializePrimordials (context))
415
416
return MaybeLocal<Object>();
416
417
return handle_scope.Escape (exports);
417
418
}
@@ -467,49 +468,50 @@ bool InitializeContextForSnapshot(Local<Context> context) {
467
468
468
469
context->SetEmbedderData (ContextEmbedderIndex::kAllowWasmCodeGeneration ,
469
470
True (isolate));
471
+ return InitializePrimordials (context);
472
+ }
473
+
474
+ bool InitializePrimordials (Local<Context> context) {
475
+ // Run per-context JS files.
476
+ Isolate* isolate = context->GetIsolate ();
477
+ Context::Scope context_scope (context);
478
+ Local<Object> exports;
479
+
480
+ Local<String> primordials_string =
481
+ FIXED_ONE_BYTE_STRING (isolate, " primordials" );
482
+ Local<String> global_string = FIXED_ONE_BYTE_STRING (isolate, " global" );
483
+ Local<String> exports_string = FIXED_ONE_BYTE_STRING (isolate, " exports" );
484
+
485
+ // Create primordials first and make it available to per-context scripts.
486
+ Local<Object> primordials = Object::New (isolate);
487
+ if (!primordials->SetPrototype (context, Null (isolate)).FromJust () ||
488
+ !GetPerContextExports (context).ToLocal (&exports) ||
489
+ !exports->Set (context, primordials_string, primordials).FromJust ()) {
490
+ return false ;
491
+ }
470
492
471
- {
472
- // Run per-context JS files.
473
- Context::Scope context_scope (context);
474
- Local<Object> exports;
475
-
476
- Local<String> primordials_string =
477
- FIXED_ONE_BYTE_STRING (isolate, " primordials" );
478
- Local<String> global_string = FIXED_ONE_BYTE_STRING (isolate, " global" );
479
- Local<String> exports_string = FIXED_ONE_BYTE_STRING (isolate, " exports" );
480
-
481
- // Create primordials first and make it available to per-context scripts.
482
- Local<Object> primordials = Object::New (isolate);
483
- if (!primordials->SetPrototype (context, Null (isolate)).FromJust () ||
484
- !GetPerContextExports (context).ToLocal (&exports) ||
485
- !exports->Set (context, primordials_string, primordials).FromJust ()) {
493
+ static const char * context_files[] = {" internal/per_context/primordials" ,
494
+ " internal/per_context/domexception" ,
495
+ " internal/per_context/messageport" ,
496
+ nullptr };
497
+
498
+ for (const char ** module = context_files; *module != nullptr ; module++) {
499
+ std::vector<Local<String>> parameters = {
500
+ global_string, exports_string, primordials_string};
501
+ Local<Value> arguments[] = {context->Global (), exports, primordials};
502
+ MaybeLocal<Function> maybe_fn =
503
+ native_module::NativeModuleEnv::LookupAndCompile (
504
+ context, *module, ¶meters, nullptr );
505
+ if (maybe_fn.IsEmpty ()) {
486
506
return false ;
487
507
}
488
-
489
- static const char * context_files[] = {" internal/per_context/primordials" ,
490
- " internal/per_context/domexception" ,
491
- " internal/per_context/messageport" ,
492
- nullptr };
493
-
494
- for (const char ** module = context_files; *module != nullptr ; module++) {
495
- std::vector<Local<String>> parameters = {
496
- global_string, exports_string, primordials_string};
497
- Local<Value> arguments[] = {context->Global (), exports, primordials};
498
- MaybeLocal<Function> maybe_fn =
499
- native_module::NativeModuleEnv::LookupAndCompile (
500
- context, *module, ¶meters, nullptr );
501
- if (maybe_fn.IsEmpty ()) {
502
- return false ;
503
- }
504
- Local<Function> fn = maybe_fn.ToLocalChecked ();
505
- MaybeLocal<Value> result =
506
- fn->Call (context, Undefined (isolate),
507
- arraysize (arguments), arguments);
508
- // Execution failed during context creation.
509
- // TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
510
- if (result.IsEmpty ()) {
511
- return false ;
512
- }
508
+ Local<Function> fn = maybe_fn.ToLocalChecked ();
509
+ MaybeLocal<Value> result =
510
+ fn->Call (context, Undefined (isolate), arraysize (arguments), arguments);
511
+ // Execution failed during context creation.
512
+ // TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
513
+ if (result.IsEmpty ()) {
514
+ return false ;
513
515
}
514
516
}
515
517
0 commit comments