@@ -129,84 +129,101 @@ void SnapshotBuilder::Generate(SnapshotData* out,
129129 per_process::v8_platform.Platform (),
130130 args,
131131 exec_args);
132+ out->isolate_data_indices =
133+ main_instance->isolate_data ()->Serialize (&creator);
132134
133135 HandleScope scope (isolate);
136+
137+ // The default context with only things created by V8.
134138 creator.SetDefaultContext (Context::New (isolate));
135- out->isolate_data_indices =
136- main_instance->isolate_data ()->Serialize (&creator);
137139
138- // Run the per-context scripts
139- Local<Context> context;
140- {
140+ auto CreateBaseContext = [&]() {
141141 TryCatch bootstrapCatch (isolate);
142- context = NewContext (isolate);
142+ // Run the per-context scripts.
143+ Local<Context> base_context = NewContext (isolate);
143144 if (bootstrapCatch.HasCaught ()) {
144- PrintCaughtException (isolate, context , bootstrapCatch);
145+ PrintCaughtException (isolate, base_context , bootstrapCatch);
145146 abort ();
146147 }
148+ return base_context;
149+ };
150+
151+ // The Node.js-specific context with primodials, can be used by workers
152+ // TODO(joyeecheung): investigate if this can be used by vm contexts
153+ // without breaking compatibility.
154+ {
155+ size_t index = creator.AddContext (CreateBaseContext ());
156+ CHECK_EQ (index, SnapshotBuilder::kNodeBaseContextIndex );
147157 }
148- Context::Scope context_scope (context);
149-
150- // Create the environment
151- env = new Environment (main_instance->isolate_data (),
152- context,
153- args,
154- exec_args,
155- nullptr ,
156- node::EnvironmentFlags::kDefaultFlags ,
157- {});
158-
159- // Run scripts in lib/internal/bootstrap/
158+
159+ // The main instance context.
160160 {
161+ Local<Context> main_context = CreateBaseContext ();
162+ Context::Scope context_scope (main_context);
161163 TryCatch bootstrapCatch (isolate);
164+
165+ // Create the environment.
166+ env = new Environment (main_instance->isolate_data (),
167+ main_context,
168+ args,
169+ exec_args,
170+ nullptr ,
171+ node::EnvironmentFlags::kDefaultFlags ,
172+ {});
173+
174+ // Run scripts in lib/internal/bootstrap/
162175 MaybeLocal<Value> result = env->RunBootstrapping ();
163176 if (bootstrapCatch.HasCaught ()) {
164- PrintCaughtException (isolate, context, bootstrapCatch);
177+ // TODO(joyeecheung): fail by exiting with a non-zero exit code.
178+ PrintCaughtException (isolate, main_context, bootstrapCatch);
179+ abort ();
165180 }
166181 result.ToLocalChecked ();
167- }
168-
169- // If --build-snapshot is true, lib/internal/main/mksnapshot.js would be
170- // loaded via LoadEnvironment() to execute process.argv[1] as the entry
171- // point (we currently only support this kind of entry point, but we
172- // could also explore snapshotting other kinds of execution modes
173- // in the future).
174- if (per_process::cli_options->build_snapshot ) {
182+ // If --build-snapshot is true, lib/internal/main/mksnapshot.js would be
183+ // loaded via LoadEnvironment() to execute process.argv[1] as the entry
184+ // point (we currently only support this kind of entry point, but we
185+ // could also explore snapshotting other kinds of execution modes
186+ // in the future).
187+ if (per_process::cli_options->build_snapshot ) {
175188#if HAVE_INSPECTOR
176- env->InitializeInspector ({});
189+ env->InitializeInspector ({});
177190#endif
178- TryCatch bootstrapCatch (isolate);
179- // TODO(joyeecheung): we could use the result for something special,
180- // like setting up initializers that should be invoked at snapshot
181- // dehydration.
182- MaybeLocal<Value> result =
183- LoadEnvironment (env, StartExecutionCallback{});
184- if (bootstrapCatch.HasCaught ()) {
185- PrintCaughtException (isolate, context, bootstrapCatch);
191+ // TODO(joyeecheung): we could use the result for something special,
192+ // like setting up initializers that should be invoked at snapshot
193+ // dehydration.
194+ MaybeLocal<Value> result =
195+ LoadEnvironment (env, StartExecutionCallback{});
196+ if (bootstrapCatch.HasCaught ()) {
197+ // TODO(joyeecheung): fail by exiting with a non-zero exit code.
198+ PrintCaughtException (isolate, main_context, bootstrapCatch);
199+ abort ();
200+ }
201+ result.ToLocalChecked ();
202+ // FIXME(joyeecheung): right now running the loop in the snapshot
203+ // builder seems to introduces inconsistencies in JS land that need to
204+ // be synchronized again after snapshot restoration.
205+ int exit_code = SpinEventLoop (env).FromMaybe (1 );
206+ CHECK_EQ (exit_code, 0 );
207+ if (bootstrapCatch.HasCaught ()) {
208+ // TODO(joyeecheung): fail by exiting with a non-zero exit code.
209+ PrintCaughtException (isolate, main_context, bootstrapCatch);
210+ abort ();
211+ }
186212 }
187- result.ToLocalChecked ();
188- // FIXME(joyeecheung): right now running the loop in the snapshot
189- // builder seems to introduces inconsistencies in JS land that need to
190- // be synchronized again after snapshot restoration.
191- int exit_code = SpinEventLoop (env).FromMaybe (1 );
192- CHECK_EQ (exit_code, 0 );
193- if (bootstrapCatch.HasCaught ()) {
194- PrintCaughtException (isolate, context, bootstrapCatch);
195- abort ();
213+
214+ if (per_process::enabled_debug_list.enabled (
215+ DebugCategory::MKSNAPSHOT)) {
216+ env->PrintAllBaseObjects ();
217+ printf (" Environment = %p\n " , env);
196218 }
197- }
198219
199- if (per_process::enabled_debug_list.enabled (DebugCategory::MKSNAPSHOT)) {
200- env->PrintAllBaseObjects ();
201- printf (" Environment = %p\n " , env);
220+ // Serialize the native states
221+ out->env_info = env->Serialize (&creator);
222+ // Serialize the context
223+ size_t index = creator.AddContext (
224+ main_context, {SerializeNodeContextInternalFields, env});
225+ CHECK_EQ (index, SnapshotBuilder::kNodeMainContextIndex );
202226 }
203-
204- // Serialize the native states
205- out->env_info = env->Serialize (&creator);
206- // Serialize the context
207- size_t index = creator.AddContext (
208- context, {SerializeNodeContextInternalFields, env});
209- CHECK_EQ (index, NodeMainInstance::kNodeContextIndex );
210227 }
211228
212229 // Must be out of HandleScope
0 commit comments