@@ -200,11 +200,10 @@ void SignalExit(int signo) {
200
200
raise (signo);
201
201
}
202
202
203
- static MaybeLocal<Value> ExecuteBootstrapper (
204
- Environment* env,
205
- const char * id,
206
- std::vector<Local<String>>* parameters,
207
- std::vector<Local<Value>>* arguments) {
203
+ MaybeLocal<Value> ExecuteBootstrapper (Environment* env,
204
+ const char * id,
205
+ std::vector<Local<String>>* parameters,
206
+ std::vector<Local<Value>>* arguments) {
208
207
EscapableHandleScope scope (env->isolate ());
209
208
MaybeLocal<Function> maybe_fn =
210
209
per_process::native_module_loader.LookupAndCompile (
@@ -453,9 +452,7 @@ void LoadEnvironment(Environment* env) {
453
452
// StartMainThreadExecution() make sense for embedders. Pick the
454
453
// useful ones out, and allow embedders to customize the entry
455
454
// point more directly without using _third_party_main.js
456
- if (!RunBootstrapping (env).IsEmpty ()) {
457
- USE (StartMainThreadExecution (env));
458
- }
455
+ USE (StartMainThreadExecution (env));
459
456
}
460
457
461
458
@@ -780,90 +777,117 @@ void RunBeforeExit(Environment* env) {
780
777
EmitBeforeExit (env);
781
778
}
782
779
783
- inline int StartNodeWithIsolate (Isolate* isolate,
784
- IsolateData* isolate_data,
785
- const std::vector<std::string>& args,
786
- const std::vector<std::string>& exec_args) {
780
+ // TODO(joyeecheung): align this with the CreateEnvironment exposed in node.h
781
+ // and the environment creation routine in workers somehow.
782
+ inline std::unique_ptr<Environment> CreateMainEnvironment (
783
+ IsolateData* isolate_data,
784
+ const std::vector<std::string>& args,
785
+ const std::vector<std::string>& exec_args,
786
+ int * exit_code) {
787
+ Isolate* isolate = isolate_data->isolate ();
787
788
HandleScope handle_scope (isolate);
789
+
790
+ // TODO(addaleax): This should load a real per-Isolate option, currently
791
+ // this is still effectively per-process.
792
+ if (isolate_data->options ()->track_heap_objects ) {
793
+ isolate->GetHeapProfiler ()->StartTrackingHeapObjects (true );
794
+ }
795
+
788
796
Local<Context> context = NewContext (isolate);
789
797
Context::Scope context_scope (context);
790
- int exit_code = 0 ;
791
- Environment env (
798
+
799
+ std::unique_ptr< Environment> env = std::make_unique<Environment> (
792
800
isolate_data,
793
801
context,
794
802
static_cast <Environment::Flags>(Environment::kIsMainThread |
795
803
Environment::kOwnsProcessState |
796
804
Environment::kOwnsInspector ));
797
- env. InitializeLibuv (per_process::v8_is_profiling);
798
- env. ProcessCliArgs (args, exec_args);
805
+ env-> InitializeLibuv (per_process::v8_is_profiling);
806
+ env-> ProcessCliArgs (args, exec_args);
799
807
800
808
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
801
- CHECK (!env. inspector_agent ()->IsListening ());
809
+ CHECK (!env-> inspector_agent ()->IsListening ());
802
810
// Inspector agent can't fail to start, but if it was configured to listen
803
811
// right away on the websocket port and fails to bind/etc, this will return
804
812
// false.
805
- env. inspector_agent ()->Start (args.size () > 1 ? args[1 ].c_str () : " " ,
806
- env. options ()->debug_options (),
807
- env. inspector_host_port (),
808
- true );
809
- if (env. options ()->debug_options ().inspector_enabled &&
810
- !env. inspector_agent ()->IsListening ()) {
811
- exit_code = 12 ; // Signal internal error.
812
- goto exit ;
813
+ env-> inspector_agent ()->Start (args.size () > 1 ? args[1 ].c_str () : " " ,
814
+ env-> options ()->debug_options (),
815
+ env-> inspector_host_port (),
816
+ true );
817
+ if (env-> options ()->debug_options ().inspector_enabled &&
818
+ !env-> inspector_agent ()->IsListening ()) {
819
+ * exit_code = 12 ; // Signal internal error.
820
+ return env ;
813
821
}
814
822
#else
815
823
// inspector_enabled can't be true if !HAVE_INSPECTOR or !NODE_USE_V8_PLATFORM
816
824
// - the option parser should not allow that.
817
- CHECK (!env. options ()->debug_options ().inspector_enabled );
825
+ CHECK (!env-> options ()->debug_options ().inspector_enabled );
818
826
#endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
819
827
820
- {
821
- AsyncCallbackScope callback_scope (&env);
822
- env.async_hooks ()->push_async_ids (1 , 0 );
823
- LoadEnvironment (&env);
824
- env.async_hooks ()->pop_async_id (1 );
828
+ if (RunBootstrapping (env.get ()).IsEmpty ()) {
829
+ *exit_code = 1 ;
825
830
}
826
831
827
- {
828
- SealHandleScope seal (isolate);
829
- bool more;
830
- env.performance_state ()->Mark (
831
- node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
832
- do {
833
- uv_run (env.event_loop (), UV_RUN_DEFAULT);
832
+ return env;
833
+ }
834
834
835
- per_process::v8_platform.DrainVMTasks (isolate);
835
+ inline int StartNodeWithIsolate (Isolate* isolate,
836
+ IsolateData* isolate_data,
837
+ const std::vector<std::string>& args,
838
+ const std::vector<std::string>& exec_args) {
839
+ int exit_code = 0 ;
840
+ std::unique_ptr<Environment> env =
841
+ CreateMainEnvironment (isolate_data, args, exec_args, &exit_code);
842
+ CHECK_NOT_NULL (env);
843
+ HandleScope handle_scope (env->isolate ());
844
+ Context::Scope context_scope (env->context ());
845
+
846
+ if (exit_code == 0 ) {
847
+ {
848
+ AsyncCallbackScope callback_scope (env.get ());
849
+ env->async_hooks ()->push_async_ids (1 , 0 );
850
+ LoadEnvironment (env.get ());
851
+ env->async_hooks ()->pop_async_id (1 );
852
+ }
836
853
837
- more = uv_loop_alive (env.event_loop ());
838
- if (more && !env.is_stopping ()) continue ;
854
+ {
855
+ SealHandleScope seal (isolate);
856
+ bool more;
857
+ env->performance_state ()->Mark (
858
+ node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
859
+ do {
860
+ uv_run (env->event_loop (), UV_RUN_DEFAULT);
839
861
840
- RunBeforeExit (&env );
862
+ per_process::v8_platform. DrainVMTasks (isolate );
841
863
842
- // Emit `beforeExit` if the loop became alive either after emitting
843
- // event, or after running some callbacks.
844
- more = uv_loop_alive (env.event_loop ());
845
- } while (more == true && !env.is_stopping ());
846
- env.performance_state ()->Mark (
847
- node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
848
- }
864
+ more = uv_loop_alive (env->event_loop ());
865
+ if (more && !env->is_stopping ()) continue ;
849
866
850
- env.set_trace_sync_io ( false );
867
+ RunBeforeExit ( env.get () );
851
868
852
- exit_code = EmitExit (&env);
869
+ // Emit `beforeExit` if the loop became alive either after emitting
870
+ // event, or after running some callbacks.
871
+ more = uv_loop_alive (env->event_loop ());
872
+ } while (more == true && !env->is_stopping ());
873
+ env->performance_state ()->Mark (
874
+ node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
875
+ }
853
876
854
- WaitForInspectorDisconnect (&env);
877
+ env->set_trace_sync_io (false );
878
+ exit_code = EmitExit (env.get ());
879
+ WaitForInspectorDisconnect (env.get ());
880
+ }
855
881
856
- #if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
857
- exit :
858
- #endif
859
- env.set_can_call_into_js (false );
860
- env.stop_sub_worker_contexts ();
882
+ env->set_can_call_into_js (false );
883
+ env->stop_sub_worker_contexts ();
861
884
uv_tty_reset_mode ();
862
- env. RunCleanup ();
863
- RunAtExit (& env);
885
+ env-> RunCleanup ();
886
+ RunAtExit (env. get () );
864
887
865
888
per_process::v8_platform.DrainVMTasks (isolate);
866
889
per_process::v8_platform.CancelVMTasks (isolate);
890
+
867
891
#if defined(LEAK_SANITIZER)
868
892
__lsan_do_leak_check ();
869
893
#endif
@@ -891,11 +915,6 @@ inline int StartNodeWithLoopAndArgs(uv_loop_t* event_loop,
891
915
per_process::v8_platform.Platform (),
892
916
allocator.get ()),
893
917
&FreeIsolateData);
894
- // TODO(addaleax): This should load a real per-Isolate option, currently
895
- // this is still effectively per-process.
896
- if (isolate_data->options ()->track_heap_objects ) {
897
- isolate->GetHeapProfiler ()->StartTrackingHeapObjects (true );
898
- }
899
918
exit_code =
900
919
StartNodeWithIsolate (isolate, isolate_data.get (), args, exec_args);
901
920
}
0 commit comments