From 44c598158c8a135e0faf901b590689428398f4d5 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 11 Jun 2021 20:56:08 +0800 Subject: [PATCH] bootstrap: split NodeMainInstance::Run() Split the running of the instance so that it can be reused by the snapshot builder when we need to run the loop for user code. PR-URL: https://github.com/nodejs/node/pull/39007 Refs: https://github.com/nodejs/node/issues/35711 Refs: https://github.com/nodejs/node/pull/38905 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/node_main_instance.cc | 26 +++++++++++++------------- src/node_main_instance.h | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index b4ceba6adb6020..2320f556e9f453 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -132,21 +132,24 @@ int NodeMainInstance::Run(const EnvSerializeInfo* env_info) { int exit_code = 0; DeleteFnPtr env = CreateMainEnvironment(&exit_code, env_info); - CHECK_NOT_NULL(env); - { - Context::Scope context_scope(env->context()); - if (exit_code == 0) { - LoadEnvironment(env.get(), StartExecutionCallback{}); + Context::Scope context_scope(env->context()); + Run(&exit_code, env.get()); + return exit_code; +} - exit_code = SpinEventLoop(env.get()).FromMaybe(1); - } +void NodeMainInstance::Run(int* exit_code, Environment* env) { + if (*exit_code == 0) { + LoadEnvironment(env, StartExecutionCallback{}); - ResetStdio(); + *exit_code = SpinEventLoop(env).FromMaybe(1); + } - // TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really - // make sense here. + ResetStdio(); + + // TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really + // make sense here. #if HAVE_INSPECTOR && defined(__POSIX__) && !defined(NODE_SHARED_MODE) struct sigaction act; memset(&act, 0, sizeof(act)); @@ -161,9 +164,6 @@ int NodeMainInstance::Run(const EnvSerializeInfo* env_info) { #if defined(LEAK_SANITIZER) __lsan_do_leak_check(); #endif - } - - return exit_code; } DeleteFnPtr diff --git a/src/node_main_instance.h b/src/node_main_instance.h index 75d4b7eac50774..047bdca873ebfd 100644 --- a/src/node_main_instance.h +++ b/src/node_main_instance.h @@ -59,6 +59,7 @@ class NodeMainInstance { // Start running the Node.js instances, return the exit code when finished. int Run(const EnvSerializeInfo* env_info); + void Run(int* exit_code, Environment* env); IsolateData* isolate_data() { return isolate_data_.get(); }