From: Ben Noordhuis Date: Sun, 7 Jul 2013 12:24:18 +0000 (+0200) Subject: src: cast strong persistent handles to locals X-Git-Tag: v0.11.4~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=636ca7c68412f667a72328bbe1e110d99b58a690;p=platform%2Fupstream%2Fnodejs.git src: cast strong persistent handles to locals Avoids the overhead of creating a new Local every time we unwrap a Persistent handle. --- diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 14adf54..f7c67e2 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 key = String::NewSymbol("_errno"); Local value = String::NewSymbol(AresErrnoString(errorno)); - Local process = Local::New(node_isolate, process_p); + Local process = PersistentToLocal(process_p); process->Set(key, value); } @@ -307,7 +307,7 @@ class QueryWrap { } inline Local object() { - return Local::New(node_isolate, persistent()); + return PersistentToLocal(persistent()); } protected: diff --git a/src/node.cc b/src/node.cc index 7af31b6..2285383 100644 --- a/src/node.cc +++ b/src/node.cc @@ -911,7 +911,7 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { if (using_domains) return; HandleScope scope(node_isolate); using_domains = true; - Local process = Local::New(node_isolate, process_p); + Local process = PersistentToLocal(process_p); Local tdc_v = process->Get(String::New("_tickDomainCallback")); Local ndt_v = process->Get(String::New("_nextDomainTick")); if (!tdc_v->IsFunction()) { @@ -991,8 +991,8 @@ MakeDomainCallback(const Handle object, } // process nextTicks after call - Local process = Local::New(node_isolate, process_p); - Local fn = Local::New(node_isolate, process_tickCallback); + Local process = PersistentToLocal(process_p); + Local fn = PersistentToLocal(process_tickCallback); fn->Call(process, 0, NULL); if (try_catch.HasCaught()) { @@ -1009,7 +1009,7 @@ MakeCallback(const Handle object, int argc, Handle argv[]) { // TODO Hook for long stack traces to be made here. - Local process = Local::New(node_isolate, process_p); + Local process = PersistentToLocal(process_p); // lazy load no domain next tick callbacks if (process_tickCallback.IsEmpty()) { @@ -1036,7 +1036,7 @@ MakeCallback(const Handle object, } // process nextTicks after call - Local fn = Local::New(node_isolate, process_tickCallback); + Local fn = PersistentToLocal(process_tickCallback); fn->Call(process, 0, NULL); if (try_catch.HasCaught()) { @@ -1078,7 +1078,7 @@ MakeCallback(const Handle object, void SetErrno(uv_err_t err) { HandleScope scope(node_isolate); - Local process = Local::New(node_isolate, process_p); + Local process = PersistentToLocal(process_p); static Cached errno_symbol; if (errno_symbol.IsEmpty()) { @@ -1903,7 +1903,7 @@ void FatalException(Handle error, Handle message) { if (fatal_exception_symbol.IsEmpty()) fatal_exception_symbol = String::New("_fatalException"); - Local process = Local::New(node_isolate, process_p); + Local process = PersistentToLocal(process_p); Local fatal_v = process->Get(fatal_exception_symbol); if (!fatal_v->IsFunction()) { @@ -1958,7 +1958,7 @@ static void Binding(const FunctionCallbackInfo& args) { String::Utf8Value module_v(module); node_module_struct* modp; - Local cache = Local::New(node_isolate, binding_cache); + Local cache = PersistentToLocal(binding_cache); Local exports; if (cache->Has(module)) { @@ -1971,7 +1971,7 @@ static void Binding(const FunctionCallbackInfo& args) { char buf[1024]; snprintf(buf, 1024, "Binding %s", *module_v); - Local modules = Local::New(node_isolate, module_load_list); + Local 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 fc826c15..d60a89c 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 arg = Local::New(node_isolate, p->servername_); - Local ret = Local::New( - node_isolate, MakeCallback(p->sniObject_, "onselect", 1, &arg)); + Local arg = PersistentToLocal(p->servername_); + Local 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 obj = Local::New(node_isolate, req->obj); + Local obj = PersistentToLocal(req->obj); req->obj.Dispose(); Local argv[2]; EIO_PBKDF2After(req, argv); diff --git a/src/node_file.cc b/src/node_file.cc index 68eb668..1defa6b 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -303,9 +303,7 @@ Local BuildStatsObject(const uv_stat_t* s) { ctime_symbol = String::New("ctime"); } - Local constructor = - Local::New(node_isolate, stats_constructor); - + Local constructor = PersistentToLocal(stats_constructor); Local stats = constructor->NewInstance(); if (stats.IsEmpty()) return Local(); diff --git a/src/node_internals.h b/src/node_internals.h index 29c38ad..a771123 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -62,6 +62,15 @@ public: void operator=(v8::Handle that); }; +template +inline v8::Local PersistentToLocal( + const v8::Persistent& persistent); + +template +inline v8::Local PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent& persistent); + template v8::Handle MakeCallback( const v8::Persistent& recv, @@ -244,13 +253,31 @@ inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle arg, return true; } +template +inline v8::Local PersistentToLocal( + const v8::Persistent& persistent) { + return PersistentToLocal(node_isolate, persistent); +} + +template +inline v8::Local PersistentToLocal( + v8::Isolate* isolate, + const v8::Persistent& persistent) { + if (persistent.IsWeak()) { + return v8::Local::New(isolate, persistent); + } else { + return *reinterpret_cast*>( + const_cast*>(&persistent)); + } +} + template CachedBase::CachedBase() { } template CachedBase::operator v8::Handle() const { - return v8::Local::New(node_isolate, handle_); + return PersistentToLocal(handle_); } template @@ -305,8 +332,7 @@ v8::Handle MakeCallback( const TypeName method, int argc, v8::Handle* argv) { - v8::Local recv_obj = - v8::Local::New(node_isolate, recv); + v8::Local recv_obj = PersistentToLocal(recv); return MakeCallback(recv_obj, method, argc, argv); } @@ -323,15 +349,14 @@ v8::Handle MakeCallback( inline bool HasInstance(v8::Persistent& function_template, v8::Handle value) { v8::Local function_template_handle = - v8::Local::New(node_isolate, function_template); + PersistentToLocal(function_template); return function_template_handle->HasInstance(value); } inline v8::Local NewInstance(v8::Persistent& ctor, int argc, v8::Handle* argv) { - v8::Local constructor_handle = - v8::Local::New(node_isolate, ctor); + v8::Local constructor_handle = PersistentToLocal(ctor); return constructor_handle->NewInstance(argc, argv); } @@ -341,14 +366,14 @@ template inline char* Data(v8::Persistent& val) { NODE_EXTERN char* Data(v8::Handle); NODE_EXTERN char* Data(v8::Handle); - return Data(v8::Local::New(node_isolate, val)); + return Data(PersistentToLocal(val)); } template inline size_t Length(v8::Persistent& val) { NODE_EXTERN size_t Length(v8::Handle); NODE_EXTERN size_t Length(v8::Handle); - return Length(v8::Local::New(node_isolate, val)); + return Length(PersistentToLocal(val)); } } // namespace Buffer diff --git a/src/node_script.cc b/src/node_script.cc index 0e98f06..6e35984 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -168,13 +168,13 @@ WrappedContext::~WrappedContext() { Local WrappedContext::NewInstance() { Local constructor_template_handle = - Local::New(node_isolate, constructor_template); + PersistentToLocal(constructor_template); return constructor_template_handle->GetFunction()->NewInstance(); } Local WrappedContext::GetV8Context() { - return Local::New(node_isolate, context_); + return PersistentToLocal(context_); } @@ -405,7 +405,7 @@ void WrappedScript::EvalMachine(const FunctionCallbackInfo& args) { "'this' must be a result of previous new Script(code) call."); } - script = Local