diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-07 14:24:18 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-07 14:24:18 +0200 |
commit | 636ca7c68412f667a72328bbe1e110d99b58a690 (patch) | |
tree | 8be2365f1ec2ce7e50f54433251643b0197bc127 | |
parent | 3220bc4254f53dec3a8c73a3a691fcfee56d8c8b (diff) | |
download | node-636ca7c68412f667a72328bbe1e110d99b58a690.tar.gz |
src: cast strong persistent handles to locals
Avoids the overhead of creating a new Local every time we unwrap a
Persistent handle.
-rw-r--r-- | src/cares_wrap.cc | 4 | ||||
-rw-r--r-- | src/node.cc | 18 | ||||
-rw-r--r-- | src/node_crypto.cc | 7 | ||||
-rw-r--r-- | src/node_file.cc | 4 | ||||
-rw-r--r-- | src/node_internals.h | 41 | ||||
-rw-r--r-- | src/node_script.cc | 6 | ||||
-rw-r--r-- | src/smalloc.cc | 6 | ||||
-rw-r--r-- | src/tls_wrap.cc | 2 |
8 files changed, 55 insertions, 33 deletions
diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 14adf5452..f7c67e2d5 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -268,7 +268,7 @@ static void SetAresErrno(int errorno) { HandleScope scope(node_isolate); Local<Value> key = String::NewSymbol("_errno"); Local<Value> value = String::NewSymbol(AresErrnoString(errorno)); - Local<Object> process = Local<Object>::New(node_isolate, process_p); + Local<Object> process = PersistentToLocal(process_p); process->Set(key, value); } @@ -307,7 +307,7 @@ class QueryWrap { } inline Local<Object> object() { - return Local<Object>::New(node_isolate, persistent()); + return PersistentToLocal(persistent()); } protected: diff --git a/src/node.cc b/src/node.cc index 7af31b6b8..228538375 100644 --- a/src/node.cc +++ b/src/node.cc @@ -911,7 +911,7 @@ void SetupDomainUse(const FunctionCallbackInfo<Value>& args) { if (using_domains) return; HandleScope scope(node_isolate); using_domains = true; - Local<Object> process = Local<Object>::New(node_isolate, process_p); + Local<Object> process = PersistentToLocal(process_p); Local<Value> tdc_v = process->Get(String::New("_tickDomainCallback")); Local<Value> ndt_v = process->Get(String::New("_nextDomainTick")); if (!tdc_v->IsFunction()) { @@ -991,8 +991,8 @@ MakeDomainCallback(const Handle<Object> object, } // process nextTicks after call - Local<Object> process = Local<Object>::New(node_isolate, process_p); - Local<Function> fn = Local<Function>::New(node_isolate, process_tickCallback); + Local<Object> process = PersistentToLocal(process_p); + Local<Function> fn = PersistentToLocal(process_tickCallback); fn->Call(process, 0, NULL); if (try_catch.HasCaught()) { @@ -1009,7 +1009,7 @@ MakeCallback(const Handle<Object> object, int argc, Handle<Value> argv[]) { // TODO Hook for long stack traces to be made here. - Local<Object> process = Local<Object>::New(node_isolate, process_p); + Local<Object> process = PersistentToLocal(process_p); // lazy load no domain next tick callbacks if (process_tickCallback.IsEmpty()) { @@ -1036,7 +1036,7 @@ MakeCallback(const Handle<Object> object, } // process nextTicks after call - Local<Function> fn = Local<Function>::New(node_isolate, process_tickCallback); + Local<Function> fn = PersistentToLocal(process_tickCallback); fn->Call(process, 0, NULL); if (try_catch.HasCaught()) { @@ -1078,7 +1078,7 @@ MakeCallback(const Handle<Object> object, void SetErrno(uv_err_t err) { HandleScope scope(node_isolate); - Local<Object> process = Local<Object>::New(node_isolate, process_p); + Local<Object> process = PersistentToLocal(process_p); static Cached<String> errno_symbol; if (errno_symbol.IsEmpty()) { @@ -1903,7 +1903,7 @@ void FatalException(Handle<Value> error, Handle<Message> message) { if (fatal_exception_symbol.IsEmpty()) fatal_exception_symbol = String::New("_fatalException"); - Local<Object> process = Local<Object>::New(node_isolate, process_p); + Local<Object> process = PersistentToLocal(process_p); Local<Value> fatal_v = process->Get(fatal_exception_symbol); if (!fatal_v->IsFunction()) { @@ -1958,7 +1958,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) { String::Utf8Value module_v(module); node_module_struct* modp; - Local<Object> cache = Local<Object>::New(node_isolate, binding_cache); + Local<Object> cache = PersistentToLocal(binding_cache); Local<Object> exports; if (cache->Has(module)) { @@ -1971,7 +1971,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) { char buf[1024]; snprintf(buf, 1024, "Binding %s", *module_v); - Local<Array> modules = Local<Array>::New(node_isolate, module_load_list); + Local<Array> modules = PersistentToLocal(module_load_list); uint32_t l = modules->Length(); modules->Set(l, String::New(buf)); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index fc826c154..d60a89cde 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -1189,9 +1189,8 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) { if (!p->sniObject_.IsEmpty()) { p->sniContext_.Dispose(); - Local<Value> arg = Local<String>::New(node_isolate, p->servername_); - Local<Value> ret = Local<Value>::New( - node_isolate, MakeCallback(p->sniObject_, "onselect", 1, &arg)); + Local<Value> arg = PersistentToLocal(p->servername_); + Local<Value> ret = MakeCallback(p->sniObject_, "onselect", 1, &arg); // If ret is SecureContext if (HasInstance(secure_context_constructor, ret)) { @@ -3247,7 +3246,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) { assert(status == 0); pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req); HandleScope scope(node_isolate); - Local<Object> obj = Local<Object>::New(node_isolate, req->obj); + Local<Object> obj = PersistentToLocal(req->obj); req->obj.Dispose(); Local<Value> argv[2]; EIO_PBKDF2After(req, argv); diff --git a/src/node_file.cc b/src/node_file.cc index 68eb668d8..1defa6bf2 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -303,9 +303,7 @@ Local<Object> BuildStatsObject(const uv_stat_t* s) { ctime_symbol = String::New("ctime"); } - Local<Function> constructor = - Local<Function>::New(node_isolate, stats_constructor); - + Local<Function> constructor = PersistentToLocal(stats_constructor); Local<Object> stats = constructor->NewInstance(); if (stats.IsEmpty()) return Local<Object>(); diff --git a/src/node_internals.h b/src/node_internals.h index 29c38adb0..a77112391 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -62,6 +62,15 @@ public: void operator=(v8::Handle<v8::Value> that); }; +template <class TypeName> +inline v8::Local<TypeName> PersistentToLocal( + const v8::Persistent<TypeName>& persistent); + +template <class TypeName> +inline v8::Local<TypeName> PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent<TypeName>& persistent); + template <typename TypeName> v8::Handle<v8::Value> MakeCallback( const v8::Persistent<v8::Object>& recv, @@ -244,13 +253,31 @@ inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle<v8::Value> arg, return true; } +template <class TypeName> +inline v8::Local<TypeName> PersistentToLocal( + const v8::Persistent<TypeName>& persistent) { + return PersistentToLocal(node_isolate, persistent); +} + +template <class TypeName> +inline v8::Local<TypeName> PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent<TypeName>& persistent) { + if (persistent.IsWeak()) { + return v8::Local<TypeName>::New(isolate, persistent); + } else { + return *reinterpret_cast<v8::Local<TypeName>*>( + const_cast<v8::Persistent<TypeName>*>(&persistent)); + } +} + template <typename TypeName> CachedBase<TypeName>::CachedBase() { } template <typename TypeName> CachedBase<TypeName>::operator v8::Handle<TypeName>() const { - return v8::Local<TypeName>::New(node_isolate, handle_); + return PersistentToLocal(handle_); } template <typename TypeName> @@ -305,8 +332,7 @@ v8::Handle<v8::Value> MakeCallback( const TypeName method, int argc, v8::Handle<v8::Value>* argv) { - v8::Local<v8::Object> recv_obj = - v8::Local<v8::Object>::New(node_isolate, recv); + v8::Local<v8::Object> recv_obj = PersistentToLocal(recv); return MakeCallback(recv_obj, method, argc, argv); } @@ -323,15 +349,14 @@ v8::Handle<v8::Value> MakeCallback( inline bool HasInstance(v8::Persistent<v8::FunctionTemplate>& function_template, v8::Handle<v8::Value> value) { v8::Local<v8::FunctionTemplate> function_template_handle = - v8::Local<v8::FunctionTemplate>::New(node_isolate, function_template); + PersistentToLocal(function_template); return function_template_handle->HasInstance(value); } inline v8::Local<v8::Object> NewInstance(v8::Persistent<v8::Function>& ctor, int argc, v8::Handle<v8::Value>* argv) { - v8::Local<v8::Function> constructor_handle = - v8::Local<v8::Function>::New(node_isolate, ctor); + v8::Local<v8::Function> constructor_handle = PersistentToLocal(ctor); return constructor_handle->NewInstance(argc, argv); } @@ -341,14 +366,14 @@ template <typename TypeName> inline char* Data(v8::Persistent<TypeName>& val) { NODE_EXTERN char* Data(v8::Handle<v8::Value>); NODE_EXTERN char* Data(v8::Handle<v8::Object>); - return Data(v8::Local<TypeName>::New(node_isolate, val)); + return Data(PersistentToLocal(val)); } template <typename TypeName> inline size_t Length(v8::Persistent<TypeName>& val) { NODE_EXTERN size_t Length(v8::Handle<v8::Value>); NODE_EXTERN size_t Length(v8::Handle<v8::Object>); - return Length(v8::Local<TypeName>::New(node_isolate, val)); + return Length(PersistentToLocal(val)); } } // namespace Buffer diff --git a/src/node_script.cc b/src/node_script.cc index 0e98f0676..6e3598456 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -168,13 +168,13 @@ WrappedContext::~WrappedContext() { Local<Object> WrappedContext::NewInstance() { Local<FunctionTemplate> constructor_template_handle = - Local<FunctionTemplate>::New(node_isolate, constructor_template); + PersistentToLocal(constructor_template); return constructor_template_handle->GetFunction()->NewInstance(); } Local<Context> WrappedContext::GetV8Context() { - return Local<Context>::New(node_isolate, context_); + return PersistentToLocal(context_); } @@ -405,7 +405,7 @@ void WrappedScript::EvalMachine(const FunctionCallbackInfo<Value>& args) { "'this' must be a result of previous new Script(code) call."); } - script = Local<Script>::New(node_isolate, n_script->script_); + script = PersistentToLocal(n_script->script_); } if (output_flag == returnResult) { diff --git a/src/smalloc.cc b/src/smalloc.cc index 6eee4a095..ad4231fc1 100644 --- a/src/smalloc.cc +++ b/src/smalloc.cc @@ -166,7 +166,7 @@ void TargetCallback(Isolate* isolate, Persistent<Object>* target, char* data) { HandleScope handle_scope(isolate); - Local<Object> obj = Local<Object>::New(isolate, *target); + Local<Object> obj = PersistentToLocal(isolate, *target); int len = obj->GetIndexedPropertiesExternalArrayDataLength(); if (data != NULL && len > 0) { isolate->AdjustAmountOfExternalAllocatedMemory(-len); @@ -187,7 +187,7 @@ void AllocDispose(Handle<Object> obj) { if (using_alloc_cb && obj->Has(smalloc_sym)) { Local<External> ext = obj->Get(smalloc_sym).As<External>(); CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value()); - Local<Object> obj = Local<Object>::New(node_isolate, cb_info->p_obj); + Local<Object> obj = PersistentToLocal(cb_info->p_obj); TargetFreeCallback(node_isolate, obj, cb_info); return; } @@ -247,7 +247,7 @@ void TargetFreeCallback(Isolate* isolate, Persistent<Object>* target, CallbackInfo* cb_info) { HandleScope handle_scope(isolate); - Local<Object> obj = Local<Object>::New(isolate, *target); + Local<Object> obj = PersistentToLocal(isolate, *target); TargetFreeCallback(isolate, obj, cb_info); } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 26ba90794..f28ccd7ed 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -1289,7 +1289,7 @@ int TLSCallbacks::SelectSNIContextCallback(SSL* s, int* ad, void* arg) { if (object->Has(onsniselect_sym)) { p->sni_context_.Dispose(); - Local<Value> arg = Local<String>::New(node_isolate, p->servername_); + Local<Value> arg = PersistentToLocal(p->servername_); Handle<Value> ret = MakeCallback(object, onsniselect_sym, 1, &arg); // If ret is SecureContext |