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 <fedor@indutny.com>
This commit is contained in:
Ben Noordhuis 2015-03-23 00:38:42 +01:00
parent 7e88a9322c
commit 2e5b87a147

View File

@ -2280,20 +2280,16 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
static void ProcessTitleGetter(Local<String> property, static void ProcessTitleGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
char buffer[512]; char buffer[512];
uv_get_process_title(buffer, sizeof(buffer)); 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<String> property, static void ProcessTitleSetter(Local<String> property,
Local<Value> value, Local<Value> value,
const PropertyCallbackInfo<void>& info) { const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info); node::Utf8Value title(info.GetIsolate(), value);
HandleScope scope(env->isolate());
node::Utf8Value title(env->isolate(), value);
// TODO(piscisaureus): protect with a lock // TODO(piscisaureus): protect with a lock
uv_set_process_title(*title); uv_set_process_title(*title);
} }
@ -2301,13 +2297,12 @@ static void ProcessTitleSetter(Local<String> property,
static void EnvGetter(Local<String> property, static void EnvGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info); Isolate* isolate = info.GetIsolate();
HandleScope scope(env->isolate());
#ifdef __POSIX__ #ifdef __POSIX__
node::Utf8Value key(env->isolate(), property); node::Utf8Value key(isolate, property);
const char* val = getenv(*key); const char* val = getenv(*key);
if (val) { if (val) {
return info.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), val)); return info.GetReturnValue().Set(String::NewFromUtf8(isolate, val));
} }
#else // _WIN32 #else // _WIN32
String::Value key(property); String::Value key(property);
@ -2321,7 +2316,7 @@ static void EnvGetter(Local<String> property,
if ((result > 0 || GetLastError() == ERROR_SUCCESS) && if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
result < ARRAY_SIZE(buffer)) { result < ARRAY_SIZE(buffer)) {
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer); const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
Local<String> rc = String::NewFromTwoByte(env->isolate(), two_byte_buffer); Local<String> rc = String::NewFromTwoByte(isolate, two_byte_buffer);
return info.GetReturnValue().Set(rc); return info.GetReturnValue().Set(rc);
} }
#endif #endif
@ -2331,11 +2326,9 @@ static void EnvGetter(Local<String> property,
static void EnvSetter(Local<String> property, static void EnvSetter(Local<String> property,
Local<Value> value, Local<Value> value,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
#ifdef __POSIX__ #ifdef __POSIX__
node::Utf8Value key(env->isolate(), property); node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(env->isolate(), value); node::Utf8Value val(info.GetIsolate(), value);
setenv(*key, *val, 1); setenv(*key, *val, 1);
#else // _WIN32 #else // _WIN32
String::Value key(property); String::Value key(property);
@ -2353,11 +2346,9 @@ static void EnvSetter(Local<String> property,
static void EnvQuery(Local<String> property, static void EnvQuery(Local<String> property,
const PropertyCallbackInfo<Integer>& info) { const PropertyCallbackInfo<Integer>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
int32_t rc = -1; // Not found unless proven otherwise. int32_t rc = -1; // Not found unless proven otherwise.
#ifdef __POSIX__ #ifdef __POSIX__
node::Utf8Value key(env->isolate(), property); node::Utf8Value key(info.GetIsolate(), property);
if (getenv(*key)) if (getenv(*key))
rc = 0; rc = 0;
#else // _WIN32 #else // _WIN32
@ -2381,11 +2372,9 @@ static void EnvQuery(Local<String> property,
static void EnvDeleter(Local<String> property, static void EnvDeleter(Local<String> property,
const PropertyCallbackInfo<Boolean>& info) { const PropertyCallbackInfo<Boolean>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
bool rc = true; bool rc = true;
#ifdef __POSIX__ #ifdef __POSIX__
node::Utf8Value key(env->isolate(), property); node::Utf8Value key(info.GetIsolate(), property);
rc = getenv(*key) != nullptr; rc = getenv(*key) != nullptr;
if (rc) if (rc)
unsetenv(*key); unsetenv(*key);
@ -2404,20 +2393,19 @@ static void EnvDeleter(Local<String> property,
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) { static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Environment* env = Environment::GetCurrent(info); Isolate* isolate = info.GetIsolate();
HandleScope scope(env->isolate());
#ifdef __POSIX__ #ifdef __POSIX__
int size = 0; int size = 0;
while (environ[size]) while (environ[size])
size++; size++;
Local<Array> envarr = Array::New(env->isolate(), size); Local<Array> envarr = Array::New(isolate, size);
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
const char* var = environ[i]; const char* var = environ[i];
const char* s = strchr(var, '='); const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var); const int length = s ? s - var : strlen(var);
Local<String> name = String::NewFromUtf8(env->isolate(), Local<String> name = String::NewFromUtf8(isolate,
var, var,
String::kNormalString, String::kNormalString,
length); length);
@ -2427,7 +2415,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
WCHAR* environment = GetEnvironmentStringsW(); WCHAR* environment = GetEnvironmentStringsW();
if (environment == nullptr) if (environment == nullptr)
return; // This should not happen. return; // This should not happen.
Local<Array> envarr = Array::New(env->isolate()); Local<Array> envarr = Array::New(isolate);
WCHAR* p = environment; WCHAR* p = environment;
int i = 0; int i = 0;
while (*p) { while (*p) {
@ -2444,7 +2432,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
} }
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p); const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
const size_t two_byte_buffer_len = s - p; const size_t two_byte_buffer_len = s - p;
Local<String> value = String::NewFromTwoByte(env->isolate(), Local<String> value = String::NewFromTwoByte(isolate,
two_byte_buffer, two_byte_buffer,
String::kNormalString, String::kNormalString,
two_byte_buffer_len); two_byte_buffer_len);
@ -2505,8 +2493,6 @@ static Handle<Object> GetFeatures(Environment* env) {
static void DebugPortGetter(Local<String> property, static void DebugPortGetter(Local<String> property,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
info.GetReturnValue().Set(debug_port); info.GetReturnValue().Set(debug_port);
} }
@ -2514,8 +2500,6 @@ static void DebugPortGetter(Local<String> property,
static void DebugPortSetter(Local<String> property, static void DebugPortSetter(Local<String> property,
Local<Value> value, Local<Value> value,
const PropertyCallbackInfo<void>& info) { const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info);
HandleScope scope(env->isolate());
debug_port = value->Int32Value(); debug_port = value->Int32Value();
} }
@ -2539,7 +2523,6 @@ static void NeedImmediateCallbackSetter(
Local<String> property, Local<String> property,
Local<Value> value, Local<Value> value,
const PropertyCallbackInfo<void>& info) { const PropertyCallbackInfo<void>& info) {
HandleScope handle_scope(info.GetIsolate());
Environment* env = Environment::GetCurrent(info); Environment* env = Environment::GetCurrent(info);
uv_check_t* immediate_check_handle = env->immediate_check_handle(); uv_check_t* immediate_check_handle = env->immediate_check_handle();