From 481bbb4c596bc473f9f25bf6644ed2eabd7e7951 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 21 Dec 2022 17:21:48 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/45886 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu Reviewed-By: Joyee Cheung --- src/api/environment.cc | 7 ++++--- src/node_main_instance.cc | 15 ++------------- src/node_snapshotable.cc | 25 +++++++++++++++---------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index a17495d93cd98f..b906fe10385b94 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -392,12 +392,13 @@ Environment* CreateEnvironment( // options than the global parse call. Environment* env = new Environment( isolate_data, context, args, exec_args, nullptr, flags, thread_id); + #if HAVE_INSPECTOR if (env->should_create_inspector()) { if (inspector_parent_handle) { - env->InitializeInspector( - std::move(static_cast( - inspector_parent_handle.get())->impl)); + env->InitializeInspector(std::move( + static_cast(inspector_parent_handle.get()) + ->impl)); } else { env->InitializeInspector({}); } diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index 33f6856561df73..a8661c3c2263fc 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -180,19 +180,8 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) { context = NewContext(isolate_); CHECK(!context.IsEmpty()); Context::Scope context_scope(context); - env.reset(new Environment(isolate_data_.get(), - context, - args_, - exec_args_, - nullptr, - EnvironmentFlags::kDefaultFlags, - {})); -#if HAVE_INSPECTOR - env->InitializeInspector({}); -#endif - if (env->principal_realm()->RunBootstrapping().IsEmpty()) { - return nullptr; - } + env.reset( + CreateEnvironment(isolate_data_.get(), context, args_, exec_args_)); } return env; diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 37c61594e0adb3..aae05b3bf65f32 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -1159,16 +1159,21 @@ int SnapshotBuilder::Generate(SnapshotData* out, Context::Scope context_scope(main_context); // Create the environment. - env = new Environment(main_instance->isolate_data(), - main_context, - args, - exec_args, - nullptr, - node::EnvironmentFlags::kDefaultFlags, - {}); - - // Run scripts in lib/internal/bootstrap/ - if (env->principal_realm()->RunBootstrapping().IsEmpty()) { + // It's not guaranteed that a context that goes through + // v8_inspector::V8Inspector::contextCreated() is runtime-independent, + // so do not start the inspector on the main context when building + // the default snapshot. + uint64_t env_flags = EnvironmentFlags::kDefaultFlags | + EnvironmentFlags::kNoCreateInspector; + + env = CreateEnvironment(main_instance->isolate_data(), + main_context, + args, + exec_args, + static_cast(env_flags)); + + // This already ran scripts in lib/internal/bootstrap/, if it fails return + if (env == nullptr) { return BOOTSTRAP_ERROR; } // If --build-snapshot is true, lib/internal/main/mksnapshot.js would be