From 1c3cb49f004c44955e2a5c219e5a7ad5ee3bf9fc Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 16 Aug 2017 10:46:26 -0700 Subject: [PATCH] src: miscellaneous cleanups for node_config Includes a fix for setting the `icuDataDir` as a UTF8 string rather than one byte. Previously, if the dir contained any non-ascii characters they would be mangled. This won't cover cases that involve paths with other character encodings (thank you Linux).. but it will cover the most likely. Other miscellaneous cleanups are included PR-URL: https://github.com/nodejs/node/pull/14868 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig --- src/node_config.cc | 94 +++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/src/node_config.cc b/src/node_config.cc index 64263fb2d69af4..a2d980d793fc2e 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -11,6 +11,7 @@ namespace node { using v8::Boolean; using v8::Context; using v8::Integer; +using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; @@ -25,24 +26,24 @@ using v8::Value; #define READONLY_BOOLEAN_PROPERTY(str) \ do { \ - target->DefineOwnProperty(env->context(), \ - OneByteString(env->isolate(), str), \ - True(env->isolate()), ReadOnly).FromJust(); \ + target->DefineOwnProperty(context, \ + FIXED_ONE_BYTE_STRING(isolate, str), \ + True(isolate), ReadOnly).FromJust(); \ } while (0) #define READONLY_PROPERTY(obj, name, value) \ do { \ obj->DefineOwnProperty(env->context(), \ - OneByteString(env->isolate(), name), \ - value, \ - ReadOnly).FromJust(); \ + FIXED_ONE_BYTE_STRING(isolate, name), \ + value, ReadOnly).FromJust(); \ } while (0) - static void InitConfig(Local target, Local unused, Local context) { Environment* env = Environment::GetCurrent(context); + Isolate* isolate = env->isolate(); + #ifdef NODE_HAVE_I18N_SUPPORT READONLY_BOOLEAN_PROPERTY("hasIntl"); @@ -51,10 +52,13 @@ static void InitConfig(Local target, READONLY_BOOLEAN_PROPERTY("hasSmallICU"); #endif // NODE_HAVE_SMALL_ICU - target->DefineOwnProperty(env->context(), - OneByteString(env->isolate(), "icuDataDir"), - OneByteString(env->isolate(), icu_data_dir.data())) - .FromJust(); + target->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "icuDataDir"), + String::NewFromUtf8(isolate, + icu_data_dir.data(), + v8::NewStringType::kNormal).ToLocalChecked(), + ReadOnly).FromJust(); #endif // NODE_HAVE_I18N_SUPPORT @@ -64,37 +68,6 @@ static void InitConfig(Local target, if (config_pending_deprecation) READONLY_BOOLEAN_PROPERTY("pendingDeprecation"); - if (!config_warning_file.empty()) { - Local name = OneByteString(env->isolate(), "warningFile"); - Local value = String::NewFromUtf8(env->isolate(), - config_warning_file.data(), - v8::NewStringType::kNormal, - config_warning_file.size()) - .ToLocalChecked(); - target->DefineOwnProperty(env->context(), name, value).FromJust(); - } - - Local debugOptions = Object::New(env->isolate()); - - target->DefineOwnProperty(env->context(), - OneByteString(env->isolate(), "debugOptions"), - debugOptions).FromJust(); - - debugOptions->DefineOwnProperty(env->context(), - OneByteString(env->isolate(), "host"), - String::NewFromUtf8(env->isolate(), - debug_options.host_name().c_str())).FromJust(); - - debugOptions->DefineOwnProperty(env->context(), - OneByteString(env->isolate(), "port"), - Integer::New(env->isolate(), - debug_options.port())).FromJust(); - - debugOptions->DefineOwnProperty(env->context(), - OneByteString(env->isolate(), "inspectorEnabled"), - Boolean::New(env->isolate(), - debug_options.inspector_enabled())).FromJust(); - if (config_expose_internals) READONLY_BOOLEAN_PROPERTY("exposeInternals"); @@ -104,6 +77,43 @@ static void InitConfig(Local target, READONLY_PROPERTY(target, "bits", Number::New(env->isolate(), 8 * sizeof(intptr_t))); + + if (!config_warning_file.empty()) { + target->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "warningFile"), + String::NewFromUtf8(isolate, + config_warning_file.data(), + v8::NewStringType::kNormal).ToLocalChecked(), + ReadOnly).FromJust(); + } + + Local debugOptions = Object::New(isolate); + + target->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "debugOptions"), + debugOptions, ReadOnly).FromJust(); + + debugOptions->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "host"), + String::NewFromUtf8(isolate, + debug_options.host_name().c_str(), + v8::NewStringType::kNormal).ToLocalChecked(), + ReadOnly).FromJust(); + + debugOptions->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "port"), + Integer::New(isolate, debug_options.port()), + ReadOnly).FromJust(); + + debugOptions->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "inspectorEnabled"), + Boolean::New(isolate, debug_options.inspector_enabled()), ReadOnly) + .FromJust(); } // InitConfig } // namespace node