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