@@ -3499,7 +3499,7 @@ void Init(int* argc,
3499
3499
uv_disable_stdio_inheritance ();
3500
3500
3501
3501
// init async debug messages dispatching
3502
- // FIXME(bnoordhuis) Should be per-isolate or per-context, not global.
3502
+ // Main thread uses uv_default_loop
3503
3503
uv_async_init (uv_default_loop (),
3504
3504
&dispatch_debug_messages_async,
3505
3505
DispatchDebugMessagesAsyncCallback);
@@ -3663,6 +3663,18 @@ Environment* CreateEnvironment(Isolate* isolate,
3663
3663
return env;
3664
3664
}
3665
3665
3666
+ static Environment* CreateEnvironment (Isolate* isolate,
3667
+ Handle <Context> context,
3668
+ NodeInstanceData* instance_data) {
3669
+ return CreateEnvironment (isolate,
3670
+ instance_data->event_loop (),
3671
+ context,
3672
+ instance_data->argc (),
3673
+ instance_data->argv (),
3674
+ instance_data->exec_argc (),
3675
+ instance_data->exec_argv ());
3676
+ }
3677
+
3666
3678
3667
3679
static void HandleCloseCb (uv_handle_t * handle) {
3668
3680
Environment* env = reinterpret_cast <Environment*>(handle->data );
@@ -3746,62 +3758,32 @@ Environment* CreateEnvironment(Isolate* isolate,
3746
3758
}
3747
3759
3748
3760
3749
- int Start (int argc, char ** argv) {
3750
- PlatformInit ();
3751
-
3752
- const char * replaceInvalid = secure_getenv (" NODE_INVALID_UTF8" );
3753
-
3754
- if (replaceInvalid == nullptr )
3755
- WRITE_UTF8_FLAGS |= String::REPLACE_INVALID_UTF8;
3756
-
3757
- CHECK_GT (argc, 0 );
3758
-
3759
- // Hack around with the argv pointer. Used for process.title = "blah".
3760
- argv = uv_setup_args (argc, argv);
3761
-
3762
- // This needs to run *before* V8::Initialize(). The const_cast is not
3763
- // optional, in case you're wondering.
3764
- int exec_argc;
3765
- const char ** exec_argv;
3766
- Init (&argc, const_cast <const char **>(argv), &exec_argc, &exec_argv);
3767
-
3768
- #if HAVE_OPENSSL
3769
- // V8 on Windows doesn't have a good source of entropy. Seed it from
3770
- // OpenSSL's pool.
3771
- V8::SetEntropySource (crypto::EntropySource);
3772
- #endif
3773
-
3774
- V8::InitializePlatform (new Platform (4 ));
3775
-
3776
- int code;
3777
- V8::Initialize ();
3778
-
3779
- // Fetch a reference to the main isolate, so we have a reference to it
3761
+ // Entry point for new node instances, also called directly for the main
3762
+ // node instance.
3763
+ static void StartNodeInstance (void * arg) {
3764
+ NodeInstanceData* instance_data = static_cast <NodeInstanceData*>(arg);
3765
+ Isolate* isolate = Isolate::New ();
3766
+ // Fetch a reference to the main isolate, so we have a reference to it
3780
3767
// even when we need it to access it from another (debugger) thread.
3781
- node_isolate = Isolate::New ();
3768
+ if (instance_data->is_main ())
3769
+ node_isolate = isolate;
3782
3770
{
3783
- Locker locker (node_isolate);
3784
- Isolate::Scope isolate_scope (node_isolate);
3785
- HandleScope handle_scope (node_isolate);
3786
- Local<Context> context = Context::New (node_isolate);
3787
- Environment* env = CreateEnvironment (
3788
- node_isolate,
3789
- uv_default_loop (),
3790
- context,
3791
- argc,
3792
- argv,
3793
- exec_argc,
3794
- exec_argv);
3771
+ Locker locker (isolate);
3772
+ Isolate::Scope isolate_scope (isolate);
3773
+ HandleScope handle_scope (isolate);
3774
+ Local<Context> context = Context::New (isolate);
3775
+ Environment* env = CreateEnvironment (isolate, context, instance_data);
3795
3776
Context::Scope context_scope (context);
3796
- env->set_using_abort_on_uncaught_exc (abort_on_uncaught_exception);
3777
+ if (instance_data->is_main ())
3778
+ env->set_using_abort_on_uncaught_exc (abort_on_uncaught_exception);
3797
3779
// Start debug agent when argv has --debug
3798
- if (use_debug_agent)
3780
+ if (instance_data-> use_debug_agent () )
3799
3781
StartDebug (env, debug_wait_connect);
3800
3782
3801
3783
LoadEnvironment (env);
3802
3784
3803
3785
// Enable debugger
3804
- if (use_debug_agent)
3786
+ if (instance_data-> use_debug_agent () )
3805
3787
EnableDebug (env);
3806
3788
3807
3789
bool more;
@@ -3817,22 +3799,69 @@ int Start(int argc, char** argv) {
3817
3799
more = true ;
3818
3800
}
3819
3801
} while (more == true );
3820
- code = EmitExit (env);
3802
+
3803
+ int exit_code = EmitExit (env);
3804
+ if (instance_data->is_main ())
3805
+ instance_data->set_exit_code (exit_code);
3821
3806
RunAtExit (env);
3822
3807
3823
3808
env->Dispose ();
3824
3809
env = nullptr ;
3825
3810
}
3826
3811
3827
- CHECK_NE (node_isolate, nullptr );
3828
- node_isolate->Dispose ();
3829
- node_isolate = nullptr ;
3812
+ CHECK_NE (isolate, nullptr );
3813
+ isolate->Dispose ();
3814
+ isolate = nullptr ;
3815
+ if (instance_data->is_main ())
3816
+ node_isolate = nullptr ;
3817
+ }
3818
+
3819
+ int Start (int argc, char ** argv) {
3820
+ PlatformInit ();
3821
+
3822
+ const char * replace_invalid = secure_getenv (" NODE_INVALID_UTF8" );
3823
+
3824
+ if (replace_invalid == nullptr )
3825
+ WRITE_UTF8_FLAGS |= String::REPLACE_INVALID_UTF8;
3826
+
3827
+ CHECK_GT (argc, 0 );
3828
+
3829
+ // Hack around with the argv pointer. Used for process.title = "blah".
3830
+ argv = uv_setup_args (argc, argv);
3831
+
3832
+ // This needs to run *before* V8::Initialize(). The const_cast is not
3833
+ // optional, in case you're wondering.
3834
+ int exec_argc;
3835
+ const char ** exec_argv;
3836
+ Init (&argc, const_cast <const char **>(argv), &exec_argc, &exec_argv);
3837
+
3838
+ #if HAVE_OPENSSL
3839
+ // V8 on Windows doesn't have a good source of entropy. Seed it from
3840
+ // OpenSSL's pool.
3841
+ V8::SetEntropySource (crypto::EntropySource);
3842
+ #endif
3843
+
3844
+ V8::InitializePlatform (new Platform (4 ));
3845
+ V8::Initialize ();
3846
+
3847
+ int exit_code = 1 ;
3848
+ {
3849
+ NodeInstanceData instance_data (NodeInstanceType::MAIN,
3850
+ uv_default_loop (),
3851
+ argc,
3852
+ const_cast <const char **>(argv),
3853
+ exec_argc,
3854
+ exec_argv,
3855
+ use_debug_agent);
3856
+ StartNodeInstance (&instance_data);
3857
+ exit_code = instance_data.exit_code ();
3858
+ }
3830
3859
V8::Dispose ();
3831
3860
3832
3861
delete[] exec_argv;
3833
3862
exec_argv = nullptr ;
3834
3863
3835
- return code ;
3864
+ return exit_code ;
3836
3865
}
3837
3866
3838
3867
0 commit comments