Browse Source

src: cast strong persistent handles to locals

Avoids the overhead of creating a new Local every time we unwrap a
Persistent handle.
v0.11.4-release
Ben Noordhuis 12 years ago
parent
commit
636ca7c684
  1. 4
      src/cares_wrap.cc
  2. 18
      src/node.cc
  3. 7
      src/node_crypto.cc
  4. 4
      src/node_file.cc
  5. 41
      src/node_internals.h
  6. 6
      src/node_script.cc
  7. 6
      src/smalloc.cc
  8. 2
      src/tls_wrap.cc

4
src/cares_wrap.cc

@ -268,7 +268,7 @@ static void SetAresErrno(int errorno) {
HandleScope scope(node_isolate); HandleScope scope(node_isolate);
Local<Value> key = String::NewSymbol("_errno"); Local<Value> key = String::NewSymbol("_errno");
Local<Value> value = String::NewSymbol(AresErrnoString(errorno)); 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); process->Set(key, value);
} }
@ -307,7 +307,7 @@ class QueryWrap {
} }
inline Local<Object> object() { inline Local<Object> object() {
return Local<Object>::New(node_isolate, persistent()); return PersistentToLocal(persistent());
} }
protected: protected:

18
src/node.cc

@ -911,7 +911,7 @@ void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
if (using_domains) return; if (using_domains) return;
HandleScope scope(node_isolate); HandleScope scope(node_isolate);
using_domains = true; 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> tdc_v = process->Get(String::New("_tickDomainCallback"));
Local<Value> ndt_v = process->Get(String::New("_nextDomainTick")); Local<Value> ndt_v = process->Get(String::New("_nextDomainTick"));
if (!tdc_v->IsFunction()) { if (!tdc_v->IsFunction()) {
@ -991,8 +991,8 @@ MakeDomainCallback(const Handle<Object> object,
} }
// process nextTicks after call // process nextTicks after call
Local<Object> process = Local<Object>::New(node_isolate, process_p); Local<Object> process = PersistentToLocal(process_p);
Local<Function> fn = Local<Function>::New(node_isolate, process_tickCallback); Local<Function> fn = PersistentToLocal(process_tickCallback);
fn->Call(process, 0, NULL); fn->Call(process, 0, NULL);
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
@ -1009,7 +1009,7 @@ MakeCallback(const Handle<Object> object,
int argc, int argc,
Handle<Value> argv[]) { Handle<Value> argv[]) {
// TODO Hook for long stack traces to be made here. // 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 // lazy load no domain next tick callbacks
if (process_tickCallback.IsEmpty()) { if (process_tickCallback.IsEmpty()) {
@ -1036,7 +1036,7 @@ MakeCallback(const Handle<Object> object,
} }
// process nextTicks after call // 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); fn->Call(process, 0, NULL);
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
@ -1078,7 +1078,7 @@ MakeCallback(const Handle<Object> object,
void SetErrno(uv_err_t err) { void SetErrno(uv_err_t err) {
HandleScope scope(node_isolate); 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; static Cached<String> errno_symbol;
if (errno_symbol.IsEmpty()) { if (errno_symbol.IsEmpty()) {
@ -1903,7 +1903,7 @@ void FatalException(Handle<Value> error, Handle<Message> message) {
if (fatal_exception_symbol.IsEmpty()) if (fatal_exception_symbol.IsEmpty())
fatal_exception_symbol = String::New("_fatalException"); 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); Local<Value> fatal_v = process->Get(fatal_exception_symbol);
if (!fatal_v->IsFunction()) { if (!fatal_v->IsFunction()) {
@ -1958,7 +1958,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
String::Utf8Value module_v(module); String::Utf8Value module_v(module);
node_module_struct* modp; node_module_struct* modp;
Local<Object> cache = Local<Object>::New(node_isolate, binding_cache); Local<Object> cache = PersistentToLocal(binding_cache);
Local<Object> exports; Local<Object> exports;
if (cache->Has(module)) { if (cache->Has(module)) {
@ -1971,7 +1971,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
char buf[1024]; char buf[1024];
snprintf(buf, 1024, "Binding %s", *module_v); 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(); uint32_t l = modules->Length();
modules->Set(l, String::New(buf)); modules->Set(l, String::New(buf));

7
src/node_crypto.cc

@ -1189,9 +1189,8 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
if (!p->sniObject_.IsEmpty()) { if (!p->sniObject_.IsEmpty()) {
p->sniContext_.Dispose(); p->sniContext_.Dispose();
Local<Value> arg = Local<String>::New(node_isolate, p->servername_); Local<Value> arg = PersistentToLocal(p->servername_);
Local<Value> ret = Local<Value>::New( Local<Value> ret = MakeCallback(p->sniObject_, "onselect", 1, &arg);
node_isolate, MakeCallback(p->sniObject_, "onselect", 1, &arg));
// If ret is SecureContext // If ret is SecureContext
if (HasInstance(secure_context_constructor, ret)) { if (HasInstance(secure_context_constructor, ret)) {
@ -3247,7 +3246,7 @@ void EIO_PBKDF2After(uv_work_t* work_req, int status) {
assert(status == 0); assert(status == 0);
pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req); pbkdf2_req* req = container_of(work_req, pbkdf2_req, work_req);
HandleScope scope(node_isolate); HandleScope scope(node_isolate);
Local<Object> obj = Local<Object>::New(node_isolate, req->obj); Local<Object> obj = PersistentToLocal(req->obj);
req->obj.Dispose(); req->obj.Dispose();
Local<Value> argv[2]; Local<Value> argv[2];
EIO_PBKDF2After(req, argv); EIO_PBKDF2After(req, argv);

4
src/node_file.cc

@ -303,9 +303,7 @@ Local<Object> BuildStatsObject(const uv_stat_t* s) {
ctime_symbol = String::New("ctime"); ctime_symbol = String::New("ctime");
} }
Local<Function> constructor = Local<Function> constructor = PersistentToLocal(stats_constructor);
Local<Function>::New(node_isolate, stats_constructor);
Local<Object> stats = constructor->NewInstance(); Local<Object> stats = constructor->NewInstance();
if (stats.IsEmpty()) return Local<Object>(); if (stats.IsEmpty()) return Local<Object>();

41
src/node_internals.h

@ -62,6 +62,15 @@ public:
void operator=(v8::Handle<v8::Value> that); 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> template <typename TypeName>
v8::Handle<v8::Value> MakeCallback( v8::Handle<v8::Value> MakeCallback(
const v8::Persistent<v8::Object>& recv, const v8::Persistent<v8::Object>& recv,
@ -244,13 +253,31 @@ inline MUST_USE_RESULT bool ParseArrayIndex(v8::Handle<v8::Value> arg,
return true; 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> template <typename TypeName>
CachedBase<TypeName>::CachedBase() { CachedBase<TypeName>::CachedBase() {
} }
template <typename TypeName> template <typename TypeName>
CachedBase<TypeName>::operator v8::Handle<TypeName>() const { CachedBase<TypeName>::operator v8::Handle<TypeName>() const {
return v8::Local<TypeName>::New(node_isolate, handle_); return PersistentToLocal(handle_);
} }
template <typename TypeName> template <typename TypeName>
@ -305,8 +332,7 @@ v8::Handle<v8::Value> MakeCallback(
const TypeName method, const TypeName method,
int argc, int argc,
v8::Handle<v8::Value>* argv) { v8::Handle<v8::Value>* argv) {
v8::Local<v8::Object> recv_obj = v8::Local<v8::Object> recv_obj = PersistentToLocal(recv);
v8::Local<v8::Object>::New(node_isolate, recv);
return MakeCallback(recv_obj, method, argc, argv); 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, inline bool HasInstance(v8::Persistent<v8::FunctionTemplate>& function_template,
v8::Handle<v8::Value> value) { v8::Handle<v8::Value> value) {
v8::Local<v8::FunctionTemplate> function_template_handle = 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); return function_template_handle->HasInstance(value);
} }
inline v8::Local<v8::Object> NewInstance(v8::Persistent<v8::Function>& ctor, inline v8::Local<v8::Object> NewInstance(v8::Persistent<v8::Function>& ctor,
int argc, int argc,
v8::Handle<v8::Value>* argv) { v8::Handle<v8::Value>* argv) {
v8::Local<v8::Function> constructor_handle = v8::Local<v8::Function> constructor_handle = PersistentToLocal(ctor);
v8::Local<v8::Function>::New(node_isolate, ctor);
return constructor_handle->NewInstance(argc, argv); return constructor_handle->NewInstance(argc, argv);
} }
@ -341,14 +366,14 @@ template <typename TypeName>
inline char* Data(v8::Persistent<TypeName>& val) { inline char* Data(v8::Persistent<TypeName>& val) {
NODE_EXTERN char* Data(v8::Handle<v8::Value>); NODE_EXTERN char* Data(v8::Handle<v8::Value>);
NODE_EXTERN char* Data(v8::Handle<v8::Object>); NODE_EXTERN char* Data(v8::Handle<v8::Object>);
return Data(v8::Local<TypeName>::New(node_isolate, val)); return Data(PersistentToLocal(val));
} }
template <typename TypeName> template <typename TypeName>
inline size_t Length(v8::Persistent<TypeName>& val) { 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::Value>);
NODE_EXTERN size_t Length(v8::Handle<v8::Object>); NODE_EXTERN size_t Length(v8::Handle<v8::Object>);
return Length(v8::Local<TypeName>::New(node_isolate, val)); return Length(PersistentToLocal(val));
} }
} // namespace Buffer } // namespace Buffer

6
src/node_script.cc

@ -168,13 +168,13 @@ WrappedContext::~WrappedContext() {
Local<Object> WrappedContext::NewInstance() { Local<Object> WrappedContext::NewInstance() {
Local<FunctionTemplate> constructor_template_handle = Local<FunctionTemplate> constructor_template_handle =
Local<FunctionTemplate>::New(node_isolate, constructor_template); PersistentToLocal(constructor_template);
return constructor_template_handle->GetFunction()->NewInstance(); return constructor_template_handle->GetFunction()->NewInstance();
} }
Local<Context> WrappedContext::GetV8Context() { 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."); "'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) { if (output_flag == returnResult) {

6
src/smalloc.cc

@ -166,7 +166,7 @@ void TargetCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
char* data) { char* data) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
Local<Object> obj = Local<Object>::New(isolate, *target); Local<Object> obj = PersistentToLocal(isolate, *target);
int len = obj->GetIndexedPropertiesExternalArrayDataLength(); int len = obj->GetIndexedPropertiesExternalArrayDataLength();
if (data != NULL && len > 0) { if (data != NULL && len > 0) {
isolate->AdjustAmountOfExternalAllocatedMemory(-len); isolate->AdjustAmountOfExternalAllocatedMemory(-len);
@ -187,7 +187,7 @@ void AllocDispose(Handle<Object> obj) {
if (using_alloc_cb && obj->Has(smalloc_sym)) { if (using_alloc_cb && obj->Has(smalloc_sym)) {
Local<External> ext = obj->Get(smalloc_sym).As<External>(); Local<External> ext = obj->Get(smalloc_sym).As<External>();
CallbackInfo* cb_info = static_cast<CallbackInfo*>(ext->Value()); 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); TargetFreeCallback(node_isolate, obj, cb_info);
return; return;
} }
@ -247,7 +247,7 @@ void TargetFreeCallback(Isolate* isolate,
Persistent<Object>* target, Persistent<Object>* target,
CallbackInfo* cb_info) { CallbackInfo* cb_info) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
Local<Object> obj = Local<Object>::New(isolate, *target); Local<Object> obj = PersistentToLocal(isolate, *target);
TargetFreeCallback(isolate, obj, cb_info); TargetFreeCallback(isolate, obj, cb_info);
} }

2
src/tls_wrap.cc

@ -1289,7 +1289,7 @@ int TLSCallbacks::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
if (object->Has(onsniselect_sym)) { if (object->Has(onsniselect_sym)) {
p->sni_context_.Dispose(); 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); Handle<Value> ret = MakeCallback(object, onsniselect_sym, 1, &arg);
// If ret is SecureContext // If ret is SecureContext

Loading…
Cancel
Save