@@ -796,7 +796,7 @@ void Init(int* argc,
796
796
argv[i] = strdup (argv_[i].c_str ());
797
797
}
798
798
799
- int Start (int argc, char ** argv) {
799
+ InitializationResult InitializeOncePerProcess (int argc, char ** argv) {
800
800
atexit ([] () { uv_tty_reset_mode (); });
801
801
PlatformInit ();
802
802
per_process::node_start_time = uv_hrtime ();
@@ -814,20 +814,27 @@ int Start(int argc, char** argv) {
814
814
// Hack around with the argv pointer. Used for process.title = "blah".
815
815
argv = uv_setup_args (argc, argv);
816
816
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) ;
819
819
std::vector<std::string> errors;
820
+
820
821
// This needs to run *before* V8::Initialize().
821
822
{
822
- const int exit_code = InitializeNodeWithArgs (&args, &exec_args, &errors);
823
+ result.exit_code =
824
+ InitializeNodeWithArgs (&(result.args ), &(result.exec_args ), &errors);
823
825
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
+ }
826
831
}
827
832
828
833
if (per_process::cli_options->print_version ) {
829
834
printf (" %s\n " , NODE_VERSION);
830
- return 0 ;
835
+ result.exit_code = 0 ;
836
+ result.early_return = true ;
837
+ return result;
831
838
}
832
839
833
840
if (per_process::cli_options->print_v8_help ) {
@@ -855,13 +862,10 @@ int Start(int argc, char** argv) {
855
862
V8::Initialize ();
856
863
performance::performance_v8_start = PERFORMANCE_NOW ();
857
864
per_process::v8_initialized = true ;
865
+ return result;
866
+ }
858
867
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 () {
865
869
per_process::v8_initialized = false ;
866
870
V8::Dispose ();
867
871
@@ -872,8 +876,22 @@ int Start(int argc, char** argv) {
872
876
// Since uv_run cannot be called, uv_async handles held by the platform
873
877
// will never be fully cleaned up.
874
878
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
+ }
875
892
876
- return exit_code;
893
+ TearDownOncePerProcess ();
894
+ return result.exit_code ;
877
895
}
878
896
879
897
int Stop (Environment* env) {
0 commit comments