@@ -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
879897int Stop (Environment* env) {
0 commit comments