Skip to content

Commit 5356b4a

Browse files
committed
src: split per-process initialization and teardown routines
This patch makes it possible to instantiate NodeMainInstance in a separate target and use it to e.g. create snapshot. PR-URL: #27276 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 964174e commit 5356b4a

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/node.cc

+32-14
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ void Init(int* argc,
796796
argv[i] = strdup(argv_[i].c_str());
797797
}
798798

799-
int Start(int argc, char** argv) {
799+
InitializationResult InitializeOncePerProcess(int argc, char** argv) {
800800
atexit([] () { uv_tty_reset_mode(); });
801801
PlatformInit();
802802
per_process::node_start_time = uv_hrtime();
@@ -814,20 +814,27 @@ int Start(int argc, char** argv) {
814814
// Hack around with the argv pointer. Used for process.title = "blah".
815815
argv = uv_setup_args(argc, argv);
816816

817-
std::vector<std::string> args(argv, argv + argc);
818-
std::vector<std::string> exec_args;
817+
InitializationResult result;
818+
result.args = std::vector<std::string>(argv, argv + argc);
819819
std::vector<std::string> errors;
820+
820821
// This needs to run *before* V8::Initialize().
821822
{
822-
const int exit_code = InitializeNodeWithArgs(&args, &exec_args, &errors);
823+
result.exit_code =
824+
InitializeNodeWithArgs(&(result.args), &(result.exec_args), &errors);
823825
for (const std::string& error : errors)
824-
fprintf(stderr, "%s: %s\n", args.at(0).c_str(), error.c_str());
825-
if (exit_code != 0) return exit_code;
826+
fprintf(stderr, "%s: %s\n", result.args.at(0).c_str(), error.c_str());
827+
if (result.exit_code != 0) {
828+
result.early_return = true;
829+
return result;
830+
}
826831
}
827832

828833
if (per_process::cli_options->print_version) {
829834
printf("%s\n", NODE_VERSION);
830-
return 0;
835+
result.exit_code = 0;
836+
result.early_return = true;
837+
return result;
831838
}
832839

833840
if (per_process::cli_options->print_v8_help) {
@@ -855,13 +862,10 @@ int Start(int argc, char** argv) {
855862
V8::Initialize();
856863
performance::performance_v8_start = PERFORMANCE_NOW();
857864
per_process::v8_initialized = true;
865+
return result;
866+
}
858867

859-
int exit_code = 0;
860-
{
861-
NodeMainInstance main_instance(uv_default_loop(), args, exec_args);
862-
exit_code = main_instance.Run();
863-
}
864-
868+
void TearDownOncePerProcess() {
865869
per_process::v8_initialized = false;
866870
V8::Dispose();
867871

@@ -872,8 +876,22 @@ int Start(int argc, char** argv) {
872876
// Since uv_run cannot be called, uv_async handles held by the platform
873877
// will never be fully cleaned up.
874878
per_process::v8_platform.Dispose();
879+
}
880+
881+
int Start(int argc, char** argv) {
882+
InitializationResult result = InitializeOncePerProcess(argc, argv);
883+
if (result.early_return) {
884+
return result.exit_code;
885+
}
886+
887+
{
888+
NodeMainInstance main_instance(
889+
uv_default_loop(), result.args, result.exec_args);
890+
result.exit_code = main_instance.Run();
891+
}
875892

876-
return exit_code;
893+
TearDownOncePerProcess();
894+
return result.exit_code;
877895
}
878896

879897
int Stop(Environment* env) {

src/node_internals.h

+9
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ v8::MaybeLocal<v8::Value> ExecuteBootstrapper(
311311
std::vector<v8::Local<v8::Value>>* arguments);
312312
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
313313

314+
struct InitializationResult {
315+
int exit_code = 0;
316+
std::vector<std::string> args;
317+
std::vector<std::string> exec_args;
318+
bool early_return = false;
319+
};
320+
InitializationResult InitializeOncePerProcess(int argc, char** argv);
321+
void TearDownOncePerProcess();
322+
314323
#if HAVE_INSPECTOR
315324
namespace profiler {
316325
void StartCoverageCollection(Environment* env);

0 commit comments

Comments
 (0)