Skip to content

Commit 110a9cd

Browse files
committed
lib, src: upgrade after v8 api change
This is a big commit that touches just about every file in the src/ directory. The V8 API has changed in significant ways. The most important changes are: * Binding functions take a const v8::FunctionCallbackInfo<T>& argument rather than a const v8::Arguments& argument. * Binding functions return void rather than v8::Handle<v8::Value>. The return value is returned with the args.GetReturnValue().Set() family of functions. * v8::Persistent<T> no longer derives from v8::Handle<T> and no longer allows you to directly dereference the object that the persistent handle points to. This means that the common pattern of caching oft-used JS values in a persistent handle no longer quite works, you first need to reconstruct a v8::Local<T> from the persistent handle with the Local<T>::New(isolate, persistent) factory method. A handful of (internal) convenience classes and functions have been added to make dealing with the new API a little easier. The most visible one is node::Cached<T>, which wraps a v8::Persistent<T> with some template sugar. It can hold arbitrary types but so far it's exclusively used for v8::Strings (which was by far the most commonly cached handle type.)
1 parent 9b3de60 commit 110a9cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2241
-3609
lines changed

lib/net.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ Socket.prototype._getpeername = function() {
556556
}
557557
if (!this._peername) {
558558
this._peername = this._handle.getpeername();
559-
// getpeername() returns null on error
560-
if (this._peername === null) {
559+
if (!this._peername) {
561560
return {};
562561
}
563562
}
@@ -581,7 +580,7 @@ Socket.prototype._getsockname = function() {
581580
}
582581
if (!this._sockname) {
583582
this._sockname = this._handle.getsockname();
584-
if (this._sockname === null) {
583+
if (!this._sockname) {
585584
return {};
586585
}
587586
}
@@ -648,7 +647,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
648647
var writeReq = createWriteReq(this._handle, data, enc);
649648
}
650649

651-
if (!writeReq || typeof writeReq !== 'object')
650+
if (!writeReq)
652651
return this._destroy(errnoException(process._errno, 'write'), cb);
653652

654653
writeReq.oncomplete = afterWrite;

node.gyp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
'src/timer_wrap.cc',
113113
'src/tty_wrap.cc',
114114
'src/process_wrap.cc',
115-
'src/v8_typed_array.cc',
116115
'src/udp_wrap.cc',
117116
# headers to make for a more pleasant IDE experience
118117
'src/handle_wrap.h',
@@ -142,7 +141,6 @@
142141
'src/string_bytes.h',
143142
'src/stream_wrap.h',
144143
'src/tree.h',
145-
'src/v8_typed_array.h',
146144
'deps/http_parser/http_parser.h',
147145
'<(SHARED_INTERMEDIATE_DIR)/node_natives.h',
148146
# javascript files to make for an even more pleasant IDE experience

src/cares_wrap.cc

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ namespace node {
4545

4646
namespace cares_wrap {
4747

48-
using v8::Arguments;
4948
using v8::Array;
5049
using v8::Function;
50+
using v8::FunctionCallbackInfo;
5151
using v8::Handle;
5252
using v8::HandleScope;
5353
using v8::Integer;
@@ -69,7 +69,7 @@ struct ares_task_t {
6969
};
7070

7171

72-
static Persistent<String> oncomplete_sym;
72+
static Cached<String> oncomplete_sym;
7373
static ares_channel ares_channel;
7474
static uv_timer_t ares_timer;
7575
static RB_HEAD(ares_task_list, ares_task_t) ares_tasks;
@@ -268,34 +268,27 @@ static void SetAresErrno(int errorno) {
268268
HandleScope scope(node_isolate);
269269
Local<Value> key = String::NewSymbol("_errno");
270270
Local<Value> value = String::NewSymbol(AresErrnoString(errorno));
271-
node::process->Set(key, value);
271+
Local<Object> process = Local<Object>::New(node_isolate, process_p);
272+
process->Set(key, value);
272273
}
273274

274275

275276
class QueryWrap {
276277
public:
277278
QueryWrap() {
278279
HandleScope scope(node_isolate);
279-
280-
object_ = Persistent<Object>::New(node_isolate, Object::New());
280+
persistent().Reset(node_isolate, Object::New());
281281
}
282282

283283
virtual ~QueryWrap() {
284-
assert(!object_.IsEmpty());
285-
286-
object_->Delete(oncomplete_sym);
287-
288-
object_.Dispose(node_isolate);
289-
object_.Clear();
290-
}
291-
292-
Handle<Object> GetObject() {
293-
return object_;
284+
assert(!persistent().IsEmpty());
285+
object()->Delete(oncomplete_sym);
286+
persistent().Dispose();
294287
}
295288

296289
void SetOnComplete(Handle<Value> oncomplete) {
297290
assert(oncomplete->IsFunction());
298-
object_->Set(oncomplete_sym, oncomplete);
291+
object()->Set(oncomplete_sym, oncomplete);
299292
}
300293

301294
// Subclasses should implement the appropriate Send method.
@@ -309,6 +302,14 @@ class QueryWrap {
309302
return 0;
310303
}
311304

305+
inline Persistent<Object>& persistent() {
306+
return object_;
307+
}
308+
309+
inline Local<Object> object() {
310+
return Local<Object>::New(node_isolate, persistent());
311+
}
312+
312313
protected:
313314
void* GetQueryArg() {
314315
return static_cast<void*>(this);
@@ -343,13 +344,13 @@ class QueryWrap {
343344
void CallOnComplete(Local<Value> answer) {
344345
HandleScope scope(node_isolate);
345346
Local<Value> argv[2] = { Integer::New(0, node_isolate), answer };
346-
MakeCallback(object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
347+
MakeCallback(object(), oncomplete_sym, ARRAY_SIZE(argv), argv);
347348
}
348349

349350
void CallOnComplete(Local<Value> answer, Local<Value> family) {
350351
HandleScope scope(node_isolate);
351352
Local<Value> argv[3] = { Integer::New(0, node_isolate), answer, family };
352-
MakeCallback(object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
353+
MakeCallback(object(), oncomplete_sym, ARRAY_SIZE(argv), argv);
353354
}
354355

355356
void ParseError(int status) {
@@ -358,7 +359,7 @@ class QueryWrap {
358359

359360
HandleScope scope(node_isolate);
360361
Local<Value> argv[1] = { Integer::New(-1, node_isolate) };
361-
MakeCallback(object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
362+
MakeCallback(object(), oncomplete_sym, ARRAY_SIZE(argv), argv);
362363
}
363364

364365
// Subclasses should implement the appropriate Parse method.
@@ -730,7 +731,7 @@ class GetHostByNameWrap: public QueryWrap {
730731

731732

732733
template <class Wrap>
733-
static Handle<Value> Query(const Arguments& args) {
734+
static void Query(const FunctionCallbackInfo<Value>& args) {
734735
HandleScope scope(node_isolate);
735736

736737
assert(!args.IsConstructCall());
@@ -742,24 +743,23 @@ static Handle<Value> Query(const Arguments& args) {
742743

743744
// We must cache the wrap's js object here, because cares might make the
744745
// callback from the wrap->Send stack. This will destroy the wrap's internal
745-
// object reference, causing wrap->GetObject() to return undefined.
746-
Local<Object> object = Local<Object>::New(node_isolate, wrap->GetObject());
746+
// object reference, causing wrap->object() to return an empty handle.
747+
Local<Object> object = wrap->object();
747748

748749
String::Utf8Value name(args[0]);
749750

750751
int r = wrap->Send(*name);
751752
if (r) {
752753
SetAresErrno(r);
753754
delete wrap;
754-
return scope.Close(v8::Null(node_isolate));
755755
} else {
756-
return scope.Close(object);
756+
args.GetReturnValue().Set(object);
757757
}
758758
}
759759

760760

761761
template <class Wrap>
762-
static Handle<Value> QueryWithFamily(const Arguments& args) {
762+
static void QueryWithFamily(const FunctionCallbackInfo<Value>& args) {
763763
HandleScope scope(node_isolate);
764764

765765
assert(!args.IsConstructCall());
@@ -771,8 +771,8 @@ static Handle<Value> QueryWithFamily(const Arguments& args) {
771771

772772
// We must cache the wrap's js object here, because cares might make the
773773
// callback from the wrap->Send stack. This will destroy the wrap's internal
774-
// object reference, causing wrap->GetObject() to return undefined.
775-
Local<Object> object = Local<Object>::New(node_isolate, wrap->GetObject());
774+
// object reference, causing wrap->object() to return an empty handle.
775+
Local<Object> object = wrap->object();
776776

777777
String::Utf8Value name(args[0]);
778778
int family = args[1]->Int32Value();
@@ -781,9 +781,8 @@ static Handle<Value> QueryWithFamily(const Arguments& args) {
781781
if (r) {
782782
SetAresErrno(r);
783783
delete wrap;
784-
return scope.Close(v8::Null(node_isolate));
785784
} else {
786-
return scope.Close(object);
785+
args.GetReturnValue().Set(object);
787786
}
788787
}
789788

@@ -877,31 +876,29 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
877876
uv_freeaddrinfo(res);
878877

879878
// Make the callback into JavaScript
880-
MakeCallback(req_wrap->object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
879+
MakeCallback(req_wrap->object(), oncomplete_sym, ARRAY_SIZE(argv), argv);
881880

882881
delete req_wrap;
883882
}
884883

885884

886-
static Handle<Value> IsIP(const Arguments& args) {
885+
static void IsIP(const FunctionCallbackInfo<Value>& args) {
887886
HandleScope scope(node_isolate);
888887

889888
String::AsciiValue ip(args[0]);
890889
char address_buffer[sizeof(struct in6_addr)];
891890

892-
if (uv_inet_pton(AF_INET, *ip, &address_buffer).code == UV_OK) {
893-
return scope.Close(v8::Integer::New(4, node_isolate));
894-
}
895-
896-
if (uv_inet_pton(AF_INET6, *ip, &address_buffer).code == UV_OK) {
897-
return scope.Close(v8::Integer::New(6, node_isolate));
898-
}
891+
int rc = 0;
892+
if (uv_inet_pton(AF_INET, *ip, &address_buffer).code == UV_OK)
893+
rc = 4;
894+
else if (uv_inet_pton(AF_INET6, *ip, &address_buffer).code == UV_OK)
895+
rc = 6;
899896

900-
return scope.Close(v8::Integer::New(0, node_isolate));
897+
args.GetReturnValue().Set(rc);
901898
}
902899

903900

904-
static Handle<Value> GetAddrInfo(const Arguments& args) {
901+
static void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
905902
HandleScope scope(node_isolate);
906903

907904
String::Utf8Value hostname(args[0]);
@@ -937,14 +934,13 @@ static Handle<Value> GetAddrInfo(const Arguments& args) {
937934
if (r) {
938935
SetErrno(uv_last_error(uv_default_loop()));
939936
delete req_wrap;
940-
return scope.Close(v8::Null(node_isolate));
941937
} else {
942-
return scope.Close(req_wrap->object_);
938+
args.GetReturnValue().Set(req_wrap->persistent());
943939
}
944940
}
945941

946942

947-
static Handle<Value> GetServers(const Arguments& args) {
943+
static void GetServers(const FunctionCallbackInfo<Value>& args) {
948944
HandleScope scope(node_isolate);
949945

950946
Local<Array> server_array = Array::New();
@@ -969,11 +965,11 @@ static Handle<Value> GetServers(const Arguments& args) {
969965

970966
ares_free_data(servers);
971967

972-
return scope.Close(server_array);
968+
args.GetReturnValue().Set(server_array);
973969
}
974970

975971

976-
static Handle<Value> SetServers(const Arguments& args) {
972+
static void SetServers(const FunctionCallbackInfo<Value>& args) {
977973
HandleScope scope(node_isolate);
978974

979975
assert(args[0]->IsArray());
@@ -984,7 +980,7 @@ static Handle<Value> SetServers(const Arguments& args) {
984980

985981
if (len == 0) {
986982
int rv = ares_set_servers(ares_channel, NULL);
987-
return scope.Close(Integer::New(rv));
983+
return args.GetReturnValue().Set(rv);
988984
}
989985

990986
ares_addr_node* servers = new ares_addr_node[len];
@@ -1039,16 +1035,14 @@ static Handle<Value> SetServers(const Arguments& args) {
10391035

10401036
delete[] servers;
10411037

1042-
return scope.Close(Integer::New(r));
1038+
args.GetReturnValue().Set(r);
10431039
}
10441040

10451041

1046-
static Handle<Value> StrError(const Arguments& args) {
1047-
HandleScope scope;
1048-
1049-
int r = args[0]->Int32Value();
1050-
1051-
return scope.Close(String::New(ares_strerror(r)));
1042+
static void StrError(const FunctionCallbackInfo<Value>& args) {
1043+
HandleScope scope(node_isolate);
1044+
const char* errmsg = ares_strerror(args[0]->Int32Value());
1045+
args.GetReturnValue().Set(String::New(errmsg));
10521046
}
10531047

10541048

@@ -1100,7 +1094,7 @@ static void Initialize(Handle<Object> target) {
11001094
target->Set(String::NewSymbol("AF_UNSPEC"),
11011095
Integer::New(AF_UNSPEC, node_isolate));
11021096

1103-
oncomplete_sym = NODE_PSYMBOL("oncomplete");
1097+
oncomplete_sym = String::New("oncomplete");
11041098
}
11051099

11061100

0 commit comments

Comments
 (0)