Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Move MakeCallback to JS
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 17, 2012
1 parent 8973c3d commit 0109a9f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
50 changes: 15 additions & 35 deletions src/node.cc
Expand Up @@ -113,9 +113,7 @@ static Persistent<String> listeners_symbol;
static Persistent<String> uncaught_exception_symbol;
static Persistent<String> emit_symbol;

static Persistent<String> enter_symbol;
static Persistent<String> exit_symbol;
static Persistent<String> disposed_symbol;
static Persistent<Function> process_makeCallback;


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

TryCatch try_catch;

if (enter_symbol.IsEmpty()) {
enter_symbol = NODE_PSYMBOL("enter");
exit_symbol = NODE_PSYMBOL("exit");
disposed_symbol = NODE_PSYMBOL("_disposed");
}

Local<Value> domain_v = object->Get(domain_symbol);
Local<Object> domain;
Local<Function> enter;
Local<Function> exit;
if (!domain_v->IsUndefined()) {
domain = domain_v->ToObject();
if (domain->Get(disposed_symbol)->BooleanValue()) {
// domain has been disposed of.
return Undefined();
if (process_makeCallback.IsEmpty()) {
Local<Value> cb_v = process->Get(String::New("_makeCallback"));
if (!cb_v->IsFunction()) {
fprintf(stderr, "process._makeCallback assigned to non-function\n");
abort();
}
enter = Local<Function>::Cast(domain->Get(enter_symbol));
enter->Call(domain, 0, NULL);
Local<Function> cb = cb_v.As<Function>();
process_makeCallback = Persistent<Function>::New(cb);
}

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

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

if (try_catch.HasCaught()) {
FatalException(try_catch);
return Undefined();
}
Local<Value> args[3] = { object_l, callback_l, argArray };

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

if (try_catch.HasCaught()) {
FatalException(try_catch);
Expand Down Expand Up @@ -1858,9 +1841,6 @@ void FatalException(TryCatch &try_catch) {
ReportException(event_try_catch, true);
exit(1);
}

// This makes sure uncaught exceptions don't interfere with process.nextTick
StartTickSpinner();
}


Expand Down
42 changes: 38 additions & 4 deletions src/node.js
Expand Up @@ -45,6 +45,7 @@
startup.processAssert();
startup.processConfig();
startup.processNextTick();
startup.processMakeCallback();
startup.processStdio();
startup.processKillAndExit();
startup.processSignalHandlers();
Expand Down Expand Up @@ -224,15 +225,43 @@
if (value === 'false') return false;
return value;
});
}
};

startup.processMakeCallback = function() {
process._makeCallback = function(obj, fn, args) {
var domain = obj.domain;
if (domain) {
if (domain._disposed) return;
domain.enter();
}

var ret = fn.apply(obj, args);

if (domain) domain.exit();

return ret;
};
};

startup.processNextTick = function() {
var nextTickQueue = [];
var nextTickIndex = 0;
var inTick = false;

function tickDone() {
nextTickQueue.splice(0, nextTickIndex);
nextTickIndex = 0;
inTick = false;
if (nextTickQueue.length) {
process._needTickCallback();
}
}

process._tickCallback = function() {
if (inTick) return;
var nextTickLength = nextTickQueue.length;
if (nextTickLength === 0) return;
inTick = true;

while (nextTickIndex < nextTickLength) {
var tock = nextTickQueue[nextTickIndex++];
Expand All @@ -241,14 +270,19 @@
if (tock.domain._disposed) continue;
tock.domain.enter();
}
callback();
var threw = true;
try {
callback();
threw = false;
} finally {
if (threw) tickDone();
}
if (tock.domain) {
tock.domain.exit();
}
}

nextTickQueue.splice(0, nextTickIndex);
nextTickIndex = 0;
tickDone();
};

process.nextTick = function(callback) {
Expand Down

0 comments on commit 0109a9f

Please sign in to comment.