@@ -105,6 +105,8 @@ Persistent<String> domain_symbol;
105
105
// declared in node_internals.h
106
106
Persistent<Object> process;
107
107
108
+ static Persistent<Array> domains_stack;
109
+
108
110
static Persistent<Function> process_tickFromSpinner;
109
111
static Persistent<Function> process_tickCallback;
110
112
@@ -126,6 +128,8 @@ static Persistent<String> exit_symbol;
126
128
static Persistent<String> disposed_symbol;
127
129
128
130
static Persistent<String> emitting_toplevel_domain_error_symbol;
131
+ static Persistent<String> _events_symbol;
132
+ static Persistent<String> error_symbol;
129
133
130
134
static bool print_eval = false ;
131
135
static bool force_repl = false ;
@@ -904,25 +908,77 @@ Handle<Value> FromConstructorTemplate(Persistent<FunctionTemplate> t,
904
908
return scope.Close (t->GetFunction ()->NewInstance (argc, argv));
905
909
}
906
910
907
- static bool IsDomainActive () {
908
- if (domain_symbol.IsEmpty ())
909
- domain_symbol = NODE_PSYMBOL (" domain" );
911
+ static bool DomainHasErrorHandler (const Local<Object>& domain) {
912
+ HandleScope scope;
913
+
914
+ if (_events_symbol.IsEmpty ())
915
+ _events_symbol = NODE_PSYMBOL (" _events" );
916
+
917
+ Local<Value> domain_event_listeners_v = domain->Get (_events_symbol);
918
+ if (!domain_event_listeners_v->IsObject ())
919
+ return false ;
920
+
921
+ Local<Object> domain_event_listeners_o =
922
+ domain_event_listeners_v.As <Object>();
923
+
924
+ if (error_symbol.IsEmpty ())
925
+ error_symbol = NODE_PSYMBOL (" error" );
926
+
927
+ Local<Value> domain_error_listeners_v =
928
+ domain_event_listeners_o->Get (error_symbol);
929
+
930
+ if (domain_error_listeners_v->IsFunction () ||
931
+ (domain_error_listeners_v->IsArray () &&
932
+ domain_error_listeners_v.As <Array>()->Length () > 0 ))
933
+ return true ;
934
+
935
+ return false ;
936
+ }
937
+
938
+ static bool TopDomainHasErrorHandler () {
939
+ HandleScope scope;
940
+
941
+ if (!using_domains)
942
+ return false ;
910
943
911
- Local<Value> domain_v = process->Get (domain_symbol);
944
+ Local<Array> domains_stack_array = Local<Array>::New (domains_stack);
945
+ if (domains_stack_array->Length () == 0 )
946
+ return false ;
912
947
913
- return domain_v->IsObject ();
948
+ uint32_t domains_stack_length = domains_stack_array->Length ();
949
+ if (domains_stack_length == 0 )
950
+ return false ;
951
+
952
+ Local<Value> domain_v = domains_stack_array->Get (domains_stack_length - 1 );
953
+ if (!domain_v->IsObject ())
954
+ return false ;
955
+
956
+ Local<Object> domain = domain_v.As <Object>();
957
+ if (DomainHasErrorHandler (domain))
958
+ return true ;
959
+
960
+ return false ;
914
961
}
915
962
916
963
bool ShouldAbortOnUncaughtException () {
917
964
Local<Value> emitting_toplevel_domain_error_v =
918
965
process->Get (emitting_toplevel_domain_error_symbol);
919
- return !IsDomainActive () || emitting_toplevel_domain_error_v->BooleanValue ();
966
+
967
+ return emitting_toplevel_domain_error_v->BooleanValue () ||
968
+ !TopDomainHasErrorHandler ();
920
969
}
921
970
922
971
Handle<Value> UsingDomains (const Arguments& args) {
923
972
HandleScope scope;
973
+
924
974
if (using_domains)
925
975
return scope.Close (Undefined ());
976
+
977
+ if (!args[0 ]->IsArray ()) {
978
+ fprintf (stderr, " domains stack must be an array\n " );
979
+ abort ();
980
+ }
981
+
926
982
using_domains = true ;
927
983
Local<Value> tdc_v = process->Get (String::New (" _tickDomainCallback" ));
928
984
Local<Value> ndt_v = process->Get (String::New (" _nextDomainTick" ));
@@ -934,6 +990,9 @@ Handle<Value> UsingDomains(const Arguments& args) {
934
990
fprintf (stderr, " process._nextDomainTick assigned to non-function\n " );
935
991
abort ();
936
992
}
993
+
994
+ domains_stack = Persistent<Array>::New (args[0 ].As <Array>());
995
+
937
996
Local<Function> tdc = tdc_v.As <Function>();
938
997
Local<Function> ndt = ndt_v.As <Function>();
939
998
process->Set (String::New (" _tickCallback" ), tdc);
@@ -2449,7 +2508,10 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
2449
2508
process->Set (String::NewSymbol (" _tickInfoBox" ), info_box);
2450
2509
2451
2510
// pre-set _events object for faster emit checks
2452
- process->Set (String::NewSymbol (" _events" ), Object::New ());
2511
+ if (_events_symbol.IsEmpty ())
2512
+ _events_symbol = NODE_PSYMBOL (" _events" );
2513
+
2514
+ process->Set (_events_symbol, Object::New ());
2453
2515
2454
2516
if (emitting_toplevel_domain_error_symbol.IsEmpty ())
2455
2517
emitting_toplevel_domain_error_symbol =
0 commit comments