Skip to content

Commit 636ca7c

Browse files
committed
src: cast strong persistent handles to locals
Avoids the overhead of creating a new Local every time we unwrap a Persistent handle.
1 parent 3220bc4 commit 636ca7c

8 files changed

Lines changed: 55 additions & 33 deletions

File tree

src/cares_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ 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-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
271+
Local<Object> process = PersistentToLocal(process_p);
272272
process->Set(key, value);
273273
}
274274

@@ -307,7 +307,7 @@ class QueryWrap {
307307
}
308308

309309
inline Local<Object> object() {
310-
return Local<Object>::New(node_isolate, persistent());
310+
return PersistentToLocal(persistent());
311311
}
312312

313313
protected:

src/node.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
911911
if (using_domains) return;
912912
HandleScope scope(node_isolate);
913913
using_domains = true;
914-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
914+
Local<Object> process = PersistentToLocal(process_p);
915915
Local<Value> tdc_v = process->Get(String::New("_tickDomainCallback"));
916916
Local<Value> ndt_v = process->Get(String::New("_nextDomainTick"));
917917
if (!tdc_v->IsFunction()) {
@@ -991,8 +991,8 @@ MakeDomainCallback(const Handle<Object> object,
991991
}
992992

993993
// process nextTicks after call
994-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
995-
Local<Function> fn = Local<Function>::New(node_isolate, process_tickCallback);
994+
Local<Object> process = PersistentToLocal(process_p);
995+
Local<Function> fn = PersistentToLocal(process_tickCallback);
996996
fn->Call(process, 0, NULL);
997997

998998
if (try_catch.HasCaught()) {
@@ -1009,7 +1009,7 @@ MakeCallback(const Handle<Object> object,
10091009
int argc,
10101010
Handle<Value> argv[]) {
10111011
// TODO Hook for long stack traces to be made here.
1012-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
1012+
Local<Object> process = PersistentToLocal(process_p);
10131013

10141014
// lazy load no domain next tick callbacks
10151015
if (process_tickCallback.IsEmpty()) {
@@ -1036,7 +1036,7 @@ MakeCallback(const Handle<Object> object,
10361036
}
10371037

10381038
// process nextTicks after call
1039-
Local<Function> fn = Local<Function>::New(node_isolate, process_tickCallback);
1039+
Local<Function> fn = PersistentToLocal(process_tickCallback);
10401040
fn->Call(process, 0, NULL);
10411041

10421042
if (try_catch.HasCaught()) {
@@ -1078,7 +1078,7 @@ MakeCallback(const Handle<Object> object,
10781078

10791079
void SetErrno(uv_err_t err) {
10801080
HandleScope scope(node_isolate);
1081-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
1081+
Local<Object> process = PersistentToLocal(process_p);
10821082

10831083
static Cached<String> errno_symbol;
10841084
if (errno_symbol.IsEmpty()) {
@@ -1903,7 +1903,7 @@ void FatalException(Handle<Value> error, Handle<Message> message) {
19031903
if (fatal_exception_symbol.IsEmpty())
19041904
fatal_exception_symbol = String::New("_fatalException");
19051905

1906-
Local<Object> process = Local<Object>::New(node_isolate, process_p);
1906+
Local<Object> process = PersistentToLocal(process_p);
19071907
Local<Value> fatal_v = process->Get(fatal_exception_symbol);
19081908

19091909
if (!fatal_v->IsFunction()) {
@@ -1958,7 +1958,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
19581958
String::Utf8Value module_v(module);
19591959
node_module_struct* modp;
19601960

1961-
Local<Object> cache = Local<Object>::New(node_isolate, binding_cache);
1961+
Local<Object> cache = PersistentToLocal(binding_cache);
19621962
Local<Object> exports;
19631963

19641964
if (cache->Has(module)) {
@@ -1971,7 +1971,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
19711971
char buf[1024];
19721972
snprintf(buf, 1024, "Binding %s", *module_v);
19731973

1974-
Local<Array> modules = Local<Array>::New(node_isolate, module_load_list);
1974+
Local<Array> modules = PersistentToLocal(module_load_list);
19751975
uint32_t l = modules->Length();
19761976
modules->Set(l, String::New(buf));
19771977

src/node_crypto.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,9 +1189,8 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
11891189
if (!p->sniObject_.IsEmpty()) {
11901190
p->sniContext_.Dispose();
11911191

1192-
Local<Value> arg = Local<String>::New(node_isolate, p->servername_);
1193-
Local<Value> ret = Local<Value>::New(
1194-
node_isolate, MakeCallback(p->sniObject_, "onselect", 1, &arg));
1192+
Local<Value> arg = PersistentToLocal(p->servername_);
1193+
Local<Value> ret = MakeCallback(p->sniObject_, "onselect", 1, &arg);
11951194

11961195
// If ret is SecureContext
11971196
if (HasInstance(secure_context_constructor, ret)) {
@@ -3247,7 +3246,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
32473246
assert(status == 0);
32483247
pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req);
32493248
HandleScope scope(node_isolate);
3250-
Local<Object> obj = Local<Object>::New(node_isolate, req->obj);
3249+
Local<Object> obj = PersistentToLocal(req->obj);
32513250
req->obj.Dispose();
32523251
Local<Value> argv[2];
32533252
EIO_PBKDF2After(req, argv);

src/node_file.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ Local<Object> BuildStatsObject(const uv_stat_t* s) {
303303
ctime_symbol = String::New("ctime");
304304
}
305305

306-
Local<Function> constructor =
307-
Local<Function>::New(node_isolate, stats_constructor);
308-
306+
Local<Function> constructor = PersistentToLocal(stats_constructor);
309307
Local<Object> stats = constructor->NewInstance();
310308
if (stats.IsEmpty()) return Local<Object>();
311309

src/node_internals.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ class Cached<v8::Value> : public CachedBase<v8::Value> {
6262
void operator=(v8::Handle<v8::Value> that);
6363
};
6464

65+
template <class TypeName>
66+
inline v8::Local<TypeName> PersistentToLocal(
67+
const v8::Persistent<TypeName>& persistent);
68+
69+
template <class TypeName>
70+
inline v8::Local<TypeName> PersistentToLocal(
71+
v8::Isolate* isolate,
72+
const v8::Persistent<TypeName>& persistent);
73+
6574
template <typename TypeName>
6675
v8::Handle<v8::Value> MakeCallback(
6776
const v8::Persistent<v8::Object>& recv,
@@ -244,13 +253,31 @@ inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle<v8::Value> arg,
244253
return true;
245254
}
246255

256+
template <class TypeName>
257+
inline v8::Local<TypeName> PersistentToLocal(
258+
const v8::Persistent<TypeName>& persistent) {
259+
return PersistentToLocal(node_isolate, persistent);
260+
}
261+
262+
template <class TypeName>
263+
inline v8::Local<TypeName> PersistentToLocal(
264+
v8::Isolate* isolate,
265+
const v8::Persistent<TypeName>& persistent) {
266+
if (persistent.IsWeak()) {
267+
return v8::Local<TypeName>::New(isolate, persistent);
268+
} else {
269+
return *reinterpret_cast<v8::Local<TypeName>*>(
270+
const_cast<v8::Persistent<TypeName>*>(&persistent));
271+
}
272+
}
273+
247274
template <typename TypeName>
248275
CachedBase<TypeName>::CachedBase() {
249276
}
250277

251278
template <typename TypeName>
252279
CachedBase<TypeName>::operator v8::Handle<TypeName>() const {
253-
return v8::Local<TypeName>::New(node_isolate, handle_);
280+
return PersistentToLocal(handle_);
254281
}
255282

256283
template <typename TypeName>
@@ -305,8 +332,7 @@ v8::Handle<v8::Value> MakeCallback(
305332
const TypeName method,
306333
int argc,
307334
v8::Handle<v8::Value>* argv) {
308-
v8::Local<v8::Object> recv_obj =
309-
v8::Local<v8::Object>::New(node_isolate, recv);
335+
v8::Local<v8::Object> recv_obj = PersistentToLocal(recv);
310336
return MakeCallback(recv_obj, method, argc, argv);
311337
}
312338

@@ -323,15 +349,14 @@ v8::Handle<v8::Value> MakeCallback(
323349
inline bool HasInstance(v8::Persistent<v8::FunctionTemplate>& function_template,
324350
v8::Handle<v8::Value> value) {
325351
v8::Local<v8::FunctionTemplate> function_template_handle =
326-
v8::Local<v8::FunctionTemplate>::New(node_isolate, function_template);
352+
PersistentToLocal(function_template);
327353
return function_template_handle->HasInstance(value);
328354
}
329355

330356
inline v8::Local<v8::Object> NewInstance(v8::Persistent<v8::Function>& ctor,
331357
int argc,
332358
v8::Handle<v8::Value>* argv) {
333-
v8::Local<v8::Function> constructor_handle =
334-
v8::Local<v8::Function>::New(node_isolate, ctor);
359+
v8::Local<v8::Function> constructor_handle = PersistentToLocal(ctor);
335360
return constructor_handle->NewInstance(argc, argv);
336361
}
337362

@@ -341,14 +366,14 @@ template <typename TypeName>
341366
inline char* Data(v8::Persistent<TypeName>& val) {
342367
NODE_EXTERN char* Data(v8::Handle<v8::Value>);
343368
NODE_EXTERN char* Data(v8::Handle<v8::Object>);
344-
return Data(v8::Local<TypeName>::New(node_isolate, val));
369+
return Data(PersistentToLocal(val));
345370
}
346371

347372
template <typename TypeName>
348373
inline size_t Length(v8::Persistent<TypeName>& val) {
349374
NODE_EXTERN size_t Length(v8::Handle<v8::Value>);
350375
NODE_EXTERN size_t Length(v8::Handle<v8::Object>);
351-
return Length(v8::Local<TypeName>::New(node_isolate, val));
376+
return Length(PersistentToLocal(val));
352377
}
353378

354379
} // namespace Buffer

src/node_script.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ WrappedContext::~WrappedContext() {
168168

169169
Local<Object> WrappedContext::NewInstance() {
170170
Local<FunctionTemplate> constructor_template_handle =
171-
Local<FunctionTemplate>::New(node_isolate, constructor_template);
171+
PersistentToLocal(constructor_template);
172172
return constructor_template_handle->GetFunction()->NewInstance();
173173
}
174174

175175

176176
Local<Context> WrappedContext::GetV8Context() {
177-
return Local<Context>::New(node_isolate, context_);
177+
return PersistentToLocal(context_);
178178
}
179179

180180

@@ -405,7 +405,7 @@ void WrappedScript::EvalMachine(const FunctionCallbackInfo<Value>& args) {
405405
"'this' must be a result of previous new Script(code) call.");
406406
}
407407

408-
script = Local<Script>::New(node_isolate, n_script->script_);
408+
script = PersistentToLocal(n_script->script_);
409409
}
410410

411411
if (output_flag == returnResult) {

src/smalloc.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void TargetCallback(Isolate* isolate,
166166
Persistent<Object>* target,
167167
char* data) {
168168
HandleScope handle_scope(isolate);
169-
Local<Object> obj = Local<Object>::New(isolate, *target);
169+
Local<Object> obj = PersistentToLocal(isolate, *target);
170170
int len = obj->GetIndexedPropertiesExternalArrayDataLength();
171171
if (data != NULL && len > 0) {
172172
isolate->AdjustAmountOfExternalAllocatedMemory(-len);
@@ -187,7 +187,7 @@ void AllocDispose(Handle<Object> obj) {
187187
if (using_alloc_cb && obj->Has(smalloc_sym)) {
188188
Local<External> ext = obj->Get(smalloc_sym).As<External>();
189189
CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value());
190-
Local<Object> obj = Local<Object>::New(node_isolate, cb_info->p_obj);
190+
Local<Object> obj = PersistentToLocal(cb_info->p_obj);
191191
TargetFreeCallback(node_isolate, obj, cb_info);
192192
return;
193193
}
@@ -247,7 +247,7 @@ void TargetFreeCallback(Isolate* isolate,
247247
Persistent<Object>* target,
248248
CallbackInfo* cb_info) {
249249
HandleScope handle_scope(isolate);
250-
Local<Object> obj = Local<Object>::New(isolate, *target);
250+
Local<Object> obj = PersistentToLocal(isolate, *target);
251251
TargetFreeCallback(isolate, obj, cb_info);
252252
}
253253

src/tls_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ int TLSCallbacks::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
12891289
if (object->Has(onsniselect_sym)) {
12901290
p->sni_context_.Dispose();
12911291

1292-
Local<Value> arg = Local<String>::New(node_isolate, p->servername_);
1292+
Local<Value> arg = PersistentToLocal(p->servername_);
12931293
Handle<Value> ret = MakeCallback(object, onsniselect_sym, 1, &arg);
12941294

12951295
// If ret is SecureContext

0 commit comments

Comments
 (0)