Skip to content

Commit 335f8dc

Browse files
committed
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: nodejs#45886 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 1e11247 commit 335f8dc

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
@@ -373,12 +373,13 @@ Environment* CreateEnvironment(
373373
// options than the global parse call.
374374
Environment* env = new Environment(
375375
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
376+
376377
#if HAVE_INSPECTOR
377378
if (env->should_create_inspector()) {
378379
if (inspector_parent_handle) {
379-
env->InitializeInspector(
380-
std::move(static_cast<InspectorParentHandleImpl*>(
381-
inspector_parent_handle.get())->impl));
380+
env->InitializeInspector(std::move(
381+
static_cast<InspectorParentHandleImpl*>(inspector_parent_handle.get())
382+
->impl));
382383
} else {
383384
env->InitializeInspector({});
384385
}

src/node_main_instance.cc

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

208197
return env;

src/node_snapshotable.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -1156,16 +1156,21 @@ int SnapshotBuilder::Generate(SnapshotData* out,
11561156
Context::Scope context_scope(main_context);
11571157

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

0 commit comments

Comments
 (0)