Skip to content

Commit 8adaa13

Browse files
addaleaxjuanarbol
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 Backport-PR-URL: #46330 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 5019b54 commit 8adaa13

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
@@ -392,12 +392,13 @@ Environment* CreateEnvironment(
392392
// options than the global parse call.
393393
Environment* env = new Environment(
394394
isolate_data, context, args, exec_args, nullptr, flags, thread_id);
395+
395396
#if HAVE_INSPECTOR
396397
if (env->should_create_inspector()) {
397398
if (inspector_parent_handle) {
398-
env->InitializeInspector(
399-
std::move(static_cast<InspectorParentHandleImpl*>(
400-
inspector_parent_handle.get())->impl));
399+
env->InitializeInspector(std::move(
400+
static_cast<InspectorParentHandleImpl*>(inspector_parent_handle.get())
401+
->impl));
401402
} else {
402403
env->InitializeInspector({});
403404
}

src/node_main_instance.cc

+2-13
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,8 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) {
180180
context = NewContext(isolate_);
181181
CHECK(!context.IsEmpty());
182182
Context::Scope context_scope(context);
183-
env.reset(new Environment(isolate_data_.get(),
184-
context,
185-
args_,
186-
exec_args_,
187-
nullptr,
188-
EnvironmentFlags::kDefaultFlags,
189-
{}));
190-
#if HAVE_INSPECTOR
191-
env->InitializeInspector({});
192-
#endif
193-
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
194-
return nullptr;
195-
}
183+
env.reset(
184+
CreateEnvironment(isolate_data_.get(), context, args_, exec_args_));
196185
}
197186

198187
return env;

src/node_snapshotable.cc

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

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

0 commit comments

Comments
 (0)