Skip to content

Commit 0109a9f

Browse files
committed
Move MakeCallback to JS
1 parent 8973c3d commit 0109a9f

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

src/node.cc

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ static Persistent<String> listeners_symbol;
113113
static Persistent<String> uncaught_exception_symbol;
114114
static Persistent<String> emit_symbol;
115115

116-
static Persistent<String> enter_symbol;
117-
static Persistent<String> exit_symbol;
118-
static Persistent<String> disposed_symbol;
116+
static Persistent<Function> process_makeCallback;
119117

120118

121119
static bool print_eval = false;
@@ -1010,42 +1008,27 @@ MakeCallback(const Handle<Object> object,
10101008

10111009
TryCatch try_catch;
10121010

1013-
if (enter_symbol.IsEmpty()) {
1014-
enter_symbol = NODE_PSYMBOL("enter");
1015-
exit_symbol = NODE_PSYMBOL("exit");
1016-
disposed_symbol = NODE_PSYMBOL("_disposed");
1017-
}
1018-
1019-
Local<Value> domain_v = object->Get(domain_symbol);
1020-
Local<Object> domain;
1021-
Local<Function> enter;
1022-
Local<Function> exit;
1023-
if (!domain_v->IsUndefined()) {
1024-
domain = domain_v->ToObject();
1025-
if (domain->Get(disposed_symbol)->BooleanValue()) {
1026-
// domain has been disposed of.
1027-
return Undefined();
1011+
if (process_makeCallback.IsEmpty()) {
1012+
Local<Value> cb_v = process->Get(String::New("_makeCallback"));
1013+
if (!cb_v->IsFunction()) {
1014+
fprintf(stderr, "process._makeCallback assigned to non-function\n");
1015+
abort();
10281016
}
1029-
enter = Local<Function>::Cast(domain->Get(enter_symbol));
1030-
enter->Call(domain, 0, NULL);
1017+
Local<Function> cb = cb_v.As<Function>();
1018+
process_makeCallback = Persistent<Function>::New(cb);
10311019
}
10321020

1033-
if (try_catch.HasCaught()) {
1034-
FatalException(try_catch);
1035-
return Undefined();
1021+
Local<Array> argArray = Array::New(argc);
1022+
for (int i = 0; i < argc; i++) {
1023+
argArray->Set(Integer::New(i), argv[i]);
10361024
}
10371025

1038-
Local<Value> ret = callback->Call(object, argc, argv);
1026+
Local<Value> object_l = Local<Value>::New(object);
1027+
Local<Value> callback_l = Local<Value>::New(callback);
10391028

1040-
if (try_catch.HasCaught()) {
1041-
FatalException(try_catch);
1042-
return Undefined();
1043-
}
1029+
Local<Value> args[3] = { object_l, callback_l, argArray };
10441030

1045-
if (!domain_v->IsUndefined()) {
1046-
exit = Local<Function>::Cast(domain->Get(exit_symbol));
1047-
exit->Call(domain, 0, NULL);
1048-
}
1031+
Local<Value> ret = process_makeCallback->Call(process, ARRAY_SIZE(args), args);
10491032

10501033
if (try_catch.HasCaught()) {
10511034
FatalException(try_catch);
@@ -1858,9 +1841,6 @@ void FatalException(TryCatch &try_catch) {
18581841
ReportException(event_try_catch, true);
18591842
exit(1);
18601843
}
1861-
1862-
// This makes sure uncaught exceptions don't interfere with process.nextTick
1863-
StartTickSpinner();
18641844
}
18651845

18661846

src/node.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
startup.processAssert();
4646
startup.processConfig();
4747
startup.processNextTick();
48+
startup.processMakeCallback();
4849
startup.processStdio();
4950
startup.processKillAndExit();
5051
startup.processSignalHandlers();
@@ -224,15 +225,43 @@
224225
if (value === 'false') return false;
225226
return value;
226227
});
227-
}
228+
};
229+
230+
startup.processMakeCallback = function() {
231+
process._makeCallback = function(obj, fn, args) {
232+
var domain = obj.domain;
233+
if (domain) {
234+
if (domain._disposed) return;
235+
domain.enter();
236+
}
237+
238+
var ret = fn.apply(obj, args);
239+
240+
if (domain) domain.exit();
241+
242+
return ret;
243+
};
244+
};
228245

229246
startup.processNextTick = function() {
230247
var nextTickQueue = [];
231248
var nextTickIndex = 0;
249+
var inTick = false;
250+
251+
function tickDone() {
252+
nextTickQueue.splice(0, nextTickIndex);
253+
nextTickIndex = 0;
254+
inTick = false;
255+
if (nextTickQueue.length) {
256+
process._needTickCallback();
257+
}
258+
}
232259

233260
process._tickCallback = function() {
261+
if (inTick) return;
234262
var nextTickLength = nextTickQueue.length;
235263
if (nextTickLength === 0) return;
264+
inTick = true;
236265

237266
while (nextTickIndex < nextTickLength) {
238267
var tock = nextTickQueue[nextTickIndex++];
@@ -241,14 +270,19 @@
241270
if (tock.domain._disposed) continue;
242271
tock.domain.enter();
243272
}
244-
callback();
273+
var threw = true;
274+
try {
275+
callback();
276+
threw = false;
277+
} finally {
278+
if (threw) tickDone();
279+
}
245280
if (tock.domain) {
246281
tock.domain.exit();
247282
}
248283
}
249284

250-
nextTickQueue.splice(0, nextTickIndex);
251-
nextTickIndex = 0;
285+
tickDone();
252286
};
253287

254288
process.nextTick = function(callback) {

0 commit comments

Comments
 (0)