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

Commit

Permalink
Domain hooks in ReqWrap<T> and MakeCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs authored and erikdubbelboer committed Apr 18, 2012
1 parent 8a0f17d commit 01f1719
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/node.cc
Expand Up @@ -109,6 +109,11 @@ static Persistent<String> listeners_symbol;
static Persistent<String> uncaught_exception_symbol;
static Persistent<String> emit_symbol;

static Persistent<String> domain_symbol;
static Persistent<String> enter_symbol;
static Persistent<String> exit_symbol;
static Persistent<String> disposed_symbol;


static bool print_eval = false;
static bool force_repl = false;
Expand Down Expand Up @@ -1017,10 +1022,47 @@ MakeCallback(const Handle<Object> object,

TryCatch try_catch;

if (domain_symbol.IsEmpty()) {
domain_symbol = NODE_PSYMBOL("domain");
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();
}
enter = Local<Function>::Cast(domain->Get(enter_symbol));
enter->Call(domain, 0, NULL);
}

if (try_catch.HasCaught()) {
FatalException(try_catch);
return Undefined();
}

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

if (try_catch.HasCaught()) {
FatalException(try_catch);
return Undefined();
}

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

if (try_catch.HasCaught()) {
FatalException(try_catch);
return Undefined();
}

return scope.Close(ret);
Expand Down
5 changes: 4 additions & 1 deletion src/node.js
Expand Up @@ -238,7 +238,10 @@
for (var i = 0; i < l; i++) {
var tock = q[i];
var callback = tock.callback;
if (tock.domain) tock.domain.enter();
if (tock.domain) {
if (tock.domain._disposed) continue;
tock.domain.enter();
}
callback();
if (tock.domain) tock.domain.exit();
}
Expand Down
21 changes: 21 additions & 0 deletions src/req_wrap.h
Expand Up @@ -24,14 +24,35 @@

namespace node {

static v8::Persistent<v8::String> process_symbol;
static v8::Persistent<v8::String> domain_symbol;

template <typename T>
class ReqWrap {
public:
ReqWrap() {
v8::HandleScope scope;
object_ = v8::Persistent<v8::Object>::New(v8::Object::New());

// TODO: grab a handle to the current process.domain
if (process_symbol.IsEmpty()) {
process_symbol = NODE_PSYMBOL("process");
domain_symbol = NODE_PSYMBOL("domain");
}

v8::Local<v8::Value> domain = v8::Context::GetCurrent()
->Global()
->Get(process_symbol)
->ToObject()
->Get(domain_symbol);

if (!domain->IsUndefined()) {
// fprintf(stderr, "setting domain on ReqWrap\n");
object_->Set(domain_symbol, domain);
}
}


~ReqWrap() {
// Assert that someone has called Dispatched()
assert(req_.data == this);
Expand Down

0 comments on commit 01f1719

Please sign in to comment.