From 2e5b87a147f0bb1e92de4a62f392bb4f3ac12f8f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 23 Mar 2015 00:38:42 +0100 Subject: [PATCH] src: remove unnecessary environment lookups Remove some unnecessary environment lookups and delete a few superfluous HandleScope variables. PR-URL: https://github.com/iojs/io.js/pull/1238 Reviewed-By: Fedor Indutny --- src/node.cc | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/node.cc b/src/node.cc index f47f31ab1fb30a..92ace4fe3ec8b7 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2280,20 +2280,16 @@ static void LinkedBinding(const FunctionCallbackInfo& args) { static void ProcessTitleGetter(Local property, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); char buffer[512]; uv_get_process_title(buffer, sizeof(buffer)); - info.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), buffer)); + info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer)); } static void ProcessTitleSetter(Local property, Local value, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); - node::Utf8Value title(env->isolate(), value); + node::Utf8Value title(info.GetIsolate(), value); // TODO(piscisaureus): protect with a lock uv_set_process_title(*title); } @@ -2301,13 +2297,12 @@ static void ProcessTitleSetter(Local property, static void EnvGetter(Local property, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); + Isolate* isolate = info.GetIsolate(); #ifdef __POSIX__ - node::Utf8Value key(env->isolate(), property); + node::Utf8Value key(isolate, property); const char* val = getenv(*key); if (val) { - return info.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), val)); + return info.GetReturnValue().Set(String::NewFromUtf8(isolate, val)); } #else // _WIN32 String::Value key(property); @@ -2321,7 +2316,7 @@ static void EnvGetter(Local property, if ((result > 0 || GetLastError() == ERROR_SUCCESS) && result < ARRAY_SIZE(buffer)) { const uint16_t* two_byte_buffer = reinterpret_cast(buffer); - Local rc = String::NewFromTwoByte(env->isolate(), two_byte_buffer); + Local rc = String::NewFromTwoByte(isolate, two_byte_buffer); return info.GetReturnValue().Set(rc); } #endif @@ -2331,11 +2326,9 @@ static void EnvGetter(Local property, static void EnvSetter(Local property, Local value, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); #ifdef __POSIX__ - node::Utf8Value key(env->isolate(), property); - node::Utf8Value val(env->isolate(), value); + node::Utf8Value key(info.GetIsolate(), property); + node::Utf8Value val(info.GetIsolate(), value); setenv(*key, *val, 1); #else // _WIN32 String::Value key(property); @@ -2353,11 +2346,9 @@ static void EnvSetter(Local property, static void EnvQuery(Local property, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); int32_t rc = -1; // Not found unless proven otherwise. #ifdef __POSIX__ - node::Utf8Value key(env->isolate(), property); + node::Utf8Value key(info.GetIsolate(), property); if (getenv(*key)) rc = 0; #else // _WIN32 @@ -2381,11 +2372,9 @@ static void EnvQuery(Local property, static void EnvDeleter(Local property, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); bool rc = true; #ifdef __POSIX__ - node::Utf8Value key(env->isolate(), property); + node::Utf8Value key(info.GetIsolate(), property); rc = getenv(*key) != nullptr; if (rc) unsetenv(*key); @@ -2404,20 +2393,19 @@ static void EnvDeleter(Local property, static void EnvEnumerator(const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); + Isolate* isolate = info.GetIsolate(); #ifdef __POSIX__ int size = 0; while (environ[size]) size++; - Local envarr = Array::New(env->isolate(), size); + Local envarr = Array::New(isolate, size); for (int i = 0; i < size; ++i) { const char* var = environ[i]; const char* s = strchr(var, '='); const int length = s ? s - var : strlen(var); - Local name = String::NewFromUtf8(env->isolate(), + Local name = String::NewFromUtf8(isolate, var, String::kNormalString, length); @@ -2427,7 +2415,7 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { WCHAR* environment = GetEnvironmentStringsW(); if (environment == nullptr) return; // This should not happen. - Local envarr = Array::New(env->isolate()); + Local envarr = Array::New(isolate); WCHAR* p = environment; int i = 0; while (*p) { @@ -2444,7 +2432,7 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { } const uint16_t* two_byte_buffer = reinterpret_cast(p); const size_t two_byte_buffer_len = s - p; - Local value = String::NewFromTwoByte(env->isolate(), + Local value = String::NewFromTwoByte(isolate, two_byte_buffer, String::kNormalString, two_byte_buffer_len); @@ -2505,8 +2493,6 @@ static Handle GetFeatures(Environment* env) { static void DebugPortGetter(Local property, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); info.GetReturnValue().Set(debug_port); } @@ -2514,8 +2500,6 @@ static void DebugPortGetter(Local property, static void DebugPortSetter(Local property, Local value, const PropertyCallbackInfo& info) { - Environment* env = Environment::GetCurrent(info); - HandleScope scope(env->isolate()); debug_port = value->Int32Value(); } @@ -2539,7 +2523,6 @@ static void NeedImmediateCallbackSetter( Local property, Local value, const PropertyCallbackInfo& info) { - HandleScope handle_scope(info.GetIsolate()); Environment* env = Environment::GetCurrent(info); uv_check_t* immediate_check_handle = env->immediate_check_handle();