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

Commit

Permalink
dns callbacks should go through MakeCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 18, 2011
1 parent 100fc27 commit 12798c6
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions src/cares_wrap.cc
Expand Up @@ -63,7 +63,7 @@ using v8::Persistent;
using v8::String;
using v8::Value;

static Persistent<String> onanswer_sym;
static Persistent<String> oncomplete_sym;

static ares_channel ares_channel;

Expand Down Expand Up @@ -152,7 +152,7 @@ class QueryWrap {
~QueryWrap() {
assert(!object_.IsEmpty());

object_->DeleteHiddenValue(onanswer_sym);
object_->Delete(oncomplete_sym);

object_.Dispose();
object_.Clear();
Expand All @@ -162,9 +162,9 @@ class QueryWrap {
return object_;
}

void SetOnAnswer(Handle<Value> onanswer) {
assert(onanswer->IsFunction());
object_->SetHiddenValue(onanswer_sym, onanswer);
void SetOnComplete(Handle<Value> oncomplete) {
assert(oncomplete->IsFunction());
object_->Set(oncomplete_sym, oncomplete);
}

// Subclasses should implement the appropriate Send method.
Expand Down Expand Up @@ -209,24 +209,16 @@ class QueryWrap {
delete wrap;
}

Handle<Function> GetOnAnswer() {
HandleScope scope;
assert(!object_.IsEmpty());
Handle<Value> onanswer = object_->GetHiddenValue(onanswer_sym);
assert(onanswer->IsFunction());
return scope.Close(Handle<Function>::Cast(onanswer));
}

void CallOnAnswer(Local<Value> answer) {
void CallOnComplete(Local<Value> answer) {
HandleScope scope;
Local<Value> argv[2] = { Integer::New(0), answer };
GetOnAnswer()->Call(this->object_, 2, argv);
MakeCallback(object_, "oncomplete", 2, argv);
}

void CallOnAnswer(Local<Value> answer, Local<Value> family) {
void CallOnComplete(Local<Value> answer, Local<Value> family) {
HandleScope scope;
Local<Value> argv[3] = { Integer::New(0), answer, family };
GetOnAnswer()->Call(this->object_, 3, argv);
MakeCallback(object_, "oncomplete", 3, argv);
}

void ParseError(int status) {
Expand All @@ -235,7 +227,7 @@ class QueryWrap {

HandleScope scope;
Local<Value> argv[1] = { Integer::New(-1) };
GetOnAnswer()->Call(this->object_, 1, argv);
MakeCallback(object_, "oncomplete", 1, argv);
}

// Subclasses should implement the appropriate Parse method.
Expand Down Expand Up @@ -274,7 +266,7 @@ class QueryAWrap: public QueryWrap {
Local<Array> addresses = HostentToAddresses(host);
ares_free_hostent(host);

this->CallOnAnswer(addresses);
this->CallOnComplete(addresses);
}
};

Expand Down Expand Up @@ -306,7 +298,7 @@ class QueryAaaaWrap: public QueryWrap {
Local<Array> addresses = HostentToAddresses(host);
ares_free_hostent(host);

this->CallOnAnswer(addresses);
this->CallOnComplete(addresses);
}
};

Expand Down Expand Up @@ -341,7 +333,7 @@ class QueryCnameWrap: public QueryWrap {
result->Set(0, String::New(host->h_name));
ares_free_hostent(host);

this->CallOnAnswer(result);
this->CallOnComplete(result);
}
};

Expand Down Expand Up @@ -379,7 +371,7 @@ class QueryMxWrap: public QueryWrap {

ares_free_data(mx_start);

this->CallOnAnswer(mx_records);
this->CallOnComplete(mx_records);
}
};

Expand All @@ -404,7 +396,7 @@ class QueryNsWrap: public QueryWrap {
Local<Array> names = HostentToNames(host);
ares_free_hostent(host);

this->CallOnAnswer(names);
this->CallOnComplete(names);
}
};

Expand Down Expand Up @@ -451,7 +443,7 @@ class QuerySrvWrap: public QueryWrap {

ares_free_data(srv_start);

this->CallOnAnswer(srv_records);
this->CallOnComplete(srv_records);
}
};

Expand Down Expand Up @@ -485,7 +477,7 @@ class GetHostByAddrWrap: public QueryWrap {
void Parse(struct hostent* host) {
HandleScope scope;

this->CallOnAnswer(HostentToNames(host));
this->CallOnComplete(HostentToNames(host));
}
};

Expand All @@ -504,7 +496,7 @@ class GetHostByNameWrap: public QueryWrap {
Local<Array> addresses = HostentToAddresses(host);
Local<Integer> family = Integer::New(host->h_addrtype);

this->CallOnAnswer(addresses, family);
this->CallOnComplete(addresses, family);
}
};

Expand All @@ -518,7 +510,7 @@ static Handle<Value> Query(const Arguments& args) {
assert(args[1]->IsFunction());

Wrap* wrap = new Wrap();
wrap->SetOnAnswer(args[1]);
wrap->SetOnComplete(args[1]);

// We must cache the wrap's js object here, because cares might make the
// callback from the wrap->Send stack. This will destroy the wrap's internal
Expand Down Expand Up @@ -547,7 +539,7 @@ static Handle<Value> QueryWithFamily(const Arguments& args) {
assert(args[2]->IsFunction());

Wrap* wrap = new Wrap();
wrap->SetOnAnswer(args[2]);
wrap->SetOnComplete(args[2]);

// We must cache the wrap's js object here, because cares might make the
// callback from the wrap->Send stack. This will destroy the wrap's internal
Expand Down Expand Up @@ -592,7 +584,7 @@ static void Initialize(Handle<Object> target) {
target->Set(String::NewSymbol("AF_INET6"), Integer::New(AF_INET6));
target->Set(String::NewSymbol("AF_UNSPEC"), Integer::New(AF_UNSPEC));

onanswer_sym = Persistent<String>::New(String::NewSymbol("onanswer"));
oncomplete_sym = Persistent<String>::New(String::NewSymbol("oncomplete"));
}


Expand Down

0 comments on commit 12798c6

Please sign in to comment.