Skip to content

Commit b995138

Browse files
addaleaxRafaelGSS
authored andcommitted
src: use CreateEnvironment instead of inlining its code where possible
We had a number of places in which we created an `Environment` instance by performing each step in `CreateEnvironment` manually. Instead, just call the function itself. PR-URL: #45886 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent cf2ff81 commit b995138

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/api/environment.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,13 @@ Environment* CreateEnvironment(
375375
// options than the global parse call.
376376
Environment* env = new Environment(
377377
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
378+
378379
#if HAVE_INSPECTOR
379380
if (env->should_create_inspector()) {
380381
if (inspector_parent_handle) {
381-
env->InitializeInspector(
382-
std::move(static_cast<InspectorParentHandleImpl*>(
383-
inspector_parent_handle.get())->impl));
382+
env->InitializeInspector(std::move(
383+
static_cast<InspectorParentHandleImpl*>(inspector_parent_handle.get())
384+
->impl));
384385
} else {
385386
env->InitializeInspector({});
386387
}

src/node_main_instance.cc

+2-13
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,8 @@ NodeMainInstance::CreateMainEnvironment(ExitCode* exit_code) {
191191
context = NewContext(isolate_);
192192
CHECK(!context.IsEmpty());
193193
Context::Scope context_scope(context);
194-
env.reset(new Environment(isolate_data_.get(),
195-
context,
196-
args_,
197-
exec_args_,
198-
nullptr,
199-
EnvironmentFlags::kDefaultFlags,
200-
{}));
201-
#if HAVE_INSPECTOR
202-
env->InitializeInspector({});
203-
#endif
204-
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
205-
return nullptr;
206-
}
194+
env.reset(
195+
CreateEnvironment(isolate_data_.get(), context, args_, exec_args_));
207196
}
208197

209198
return env;

src/node_snapshotable.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -1151,16 +1151,21 @@ ExitCode SnapshotBuilder::Generate(SnapshotData* out,
11511151
Context::Scope context_scope(main_context);
11521152

11531153
// Create the environment.
1154-
env = new Environment(main_instance->isolate_data(),
1155-
main_context,
1156-
args,
1157-
exec_args,
1158-
nullptr,
1159-
node::EnvironmentFlags::kDefaultFlags,
1160-
{});
1161-
1162-
// Run scripts in lib/internal/bootstrap/
1163-
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
1154+
// It's not guaranteed that a context that goes through
1155+
// v8_inspector::V8Inspector::contextCreated() is runtime-independent,
1156+
// so do not start the inspector on the main context when building
1157+
// the default snapshot.
1158+
uint64_t env_flags = EnvironmentFlags::kDefaultFlags |
1159+
EnvironmentFlags::kNoCreateInspector;
1160+
1161+
env = CreateEnvironment(main_instance->isolate_data(),
1162+
main_context,
1163+
args,
1164+
exec_args,
1165+
static_cast<EnvironmentFlags::Flags>(env_flags));
1166+
1167+
// This already ran scripts in lib/internal/bootstrap/, if it fails return
1168+
if (env == nullptr) {
11641169
return ExitCode::kBootstrapFailure;
11651170
}
11661171
// If --build-snapshot is true, lib/internal/main/mksnapshot.js would be

0 commit comments

Comments
 (0)