@@ -129,84 +129,101 @@ void SnapshotBuilder::Generate(SnapshotData* out,
129
129
per_process::v8_platform.Platform (),
130
130
args,
131
131
exec_args);
132
+ out->isolate_data_indices =
133
+ main_instance->isolate_data ()->Serialize (&creator);
132
134
133
135
HandleScope scope (isolate);
136
+
137
+ // The default context with only things created by V8.
134
138
creator.SetDefaultContext (Context::New (isolate));
135
- out->isolate_data_indices =
136
- main_instance->isolate_data ()->Serialize (&creator);
137
139
138
- // Run the per-context scripts
139
- Local<Context> context;
140
- {
140
+ auto CreateBaseContext = [&]() {
141
141
TryCatch bootstrapCatch (isolate);
142
- context = NewContext (isolate);
142
+ // Run the per-context scripts.
143
+ Local<Context> base_context = NewContext (isolate);
143
144
if (bootstrapCatch.HasCaught ()) {
144
- PrintCaughtException (isolate, context , bootstrapCatch);
145
+ PrintCaughtException (isolate, base_context , bootstrapCatch);
145
146
abort ();
146
147
}
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 );
147
157
}
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.
160
160
{
161
+ Local<Context> main_context = CreateBaseContext ();
162
+ Context::Scope context_scope (main_context);
161
163
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/
162
175
MaybeLocal<Value> result = env->RunBootstrapping ();
163
176
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 ();
165
180
}
166
181
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 ) {
175
188
#if HAVE_INSPECTOR
176
- env->InitializeInspector ({});
189
+ env->InitializeInspector ({});
177
190
#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
+ }
186
212
}
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);
196
218
}
197
- }
198
219
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 );
202
226
}
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 );
210
227
}
211
228
212
229
// Must be out of HandleScope
0 commit comments