Skip to content

Commit 10ce3d1

Browse files
committed
Domain hooks in ReqWrap<T> and MakeCallback
1 parent 963459d commit 10ce3d1

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

src/node.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ static Persistent<String> listeners_symbol;
109109
static Persistent<String> uncaught_exception_symbol;
110110
static Persistent<String> emit_symbol;
111111

112+
static Persistent<String> domain_symbol;
113+
static Persistent<String> enter_symbol;
114+
static Persistent<String> exit_symbol;
115+
static Persistent<String> disposed_symbol;
116+
112117

113118
static bool print_eval = false;
114119
static bool force_repl = false;
@@ -1017,10 +1022,47 @@ MakeCallback(const Handle<Object> object,
10171022

10181023
TryCatch try_catch;
10191024

1025+
if (domain_symbol.IsEmpty()) {
1026+
domain_symbol = NODE_PSYMBOL("domain");
1027+
enter_symbol = NODE_PSYMBOL("enter");
1028+
exit_symbol = NODE_PSYMBOL("exit");
1029+
disposed_symbol = NODE_PSYMBOL("_disposed");
1030+
}
1031+
1032+
Local<Value> domain_v = object->Get(domain_symbol);
1033+
Local<Object> domain;
1034+
Local<Function> enter;
1035+
Local<Function> exit;
1036+
if (!domain_v->IsUndefined()) {
1037+
domain = domain_v->ToObject();
1038+
if (domain->Get(disposed_symbol)->BooleanValue()) {
1039+
// domain has been disposed of.
1040+
return Undefined();
1041+
}
1042+
enter = Local<Function>::Cast(domain->Get(enter_symbol));
1043+
enter->Call(domain, 0, NULL);
1044+
}
1045+
1046+
if (try_catch.HasCaught()) {
1047+
FatalException(try_catch);
1048+
return Undefined();
1049+
}
1050+
10201051
Local<Value> ret = callback->Call(object, argc, argv);
10211052

10221053
if (try_catch.HasCaught()) {
10231054
FatalException(try_catch);
1055+
return Undefined();
1056+
}
1057+
1058+
if (!domain_v->IsUndefined()) {
1059+
exit = Local<Function>::Cast(domain->Get(exit_symbol));
1060+
exit->Call(domain, 0, NULL);
1061+
}
1062+
1063+
if (try_catch.HasCaught()) {
1064+
FatalException(try_catch);
1065+
return Undefined();
10241066
}
10251067

10261068
return scope.Close(ret);

src/node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@
238238
for (var i = 0; i < l; i++) {
239239
var tock = q[i];
240240
var callback = tock.callback;
241-
if (tock.domain) tock.domain.enter();
241+
if (tock.domain) {
242+
if (tock.domain._disposed) continue;
243+
tock.domain.enter();
244+
}
242245
callback();
243246
if (tock.domain) tock.domain.exit();
244247
}

src/req_wrap.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@
2424

2525
namespace node {
2626

27+
static v8::Persistent<v8::String> process_symbol;
28+
static v8::Persistent<v8::String> domain_symbol;
29+
2730
template <typename T>
2831
class ReqWrap {
2932
public:
3033
ReqWrap() {
3134
v8::HandleScope scope;
3235
object_ = v8::Persistent<v8::Object>::New(v8::Object::New());
36+
37+
// TODO: grab a handle to the current process.domain
38+
if (process_symbol.IsEmpty()) {
39+
process_symbol = NODE_PSYMBOL("process");
40+
domain_symbol = NODE_PSYMBOL("domain");
41+
}
42+
43+
v8::Local<v8::Value> domain = v8::Context::GetCurrent()
44+
->Global()
45+
->Get(process_symbol)
46+
->ToObject()
47+
->Get(domain_symbol);
48+
49+
if (!domain->IsUndefined()) {
50+
// fprintf(stderr, "setting domain on ReqWrap\n");
51+
object_->Set(domain_symbol, domain);
52+
}
3353
}
3454

55+
3556
~ReqWrap() {
3657
// Assert that someone has called Dispatched()
3758
assert(req_.data == this);

0 commit comments

Comments
 (0)