From 53b59b4066fb4956ec6682f795a67935363c393e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 30 Nov 2018 07:33:20 +0800 Subject: [PATCH] src: move READONLY_* macros into util.h Move these macros to util.h so they can be shared among different C++ files. Also, renames `READONLY_BOOLEAN_PROPERTY` to `READONLY_TRUE_PROPERTY` (since it sets the property to true), and use `ToV8Value` in `READONLY_STRING_PROPERTY`. PR-URL: https://github.com/nodejs/node/pull/24774 Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/node.cc | 31 +++---------------------- src/node_config.cc | 58 ++++++++++++---------------------------------- src/util.h | 25 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 71 deletions(-) diff --git a/src/node.cc b/src/node.cc index 975373baa6c56d..bb11eaa75f01f3 100644 --- a/src/node.cc +++ b/src/node.cc @@ -124,7 +124,6 @@ using v8::Array; using v8::Boolean; using v8::Context; using v8::DEFAULT; -using v8::DontEnum; using v8::EscapableHandleScope; using v8::Exception; using v8::Function; @@ -147,8 +146,6 @@ using v8::Nothing; using v8::Null; using v8::Object; using v8::ObjectTemplate; -using v8::PropertyAttribute; -using v8::ReadOnly; using v8::Script; using v8::ScriptOrigin; using v8::SealHandleScope; @@ -954,31 +951,12 @@ static Local GetFeatures(Environment* env) { static void DebugProcess(const FunctionCallbackInfo& args); static void DebugEnd(const FunctionCallbackInfo& args); -namespace { - -#define READONLY_PROPERTY(obj, str, var) \ - do { \ - obj->DefineOwnProperty(env->context(), \ - OneByteString(env->isolate(), str), \ - var, \ - ReadOnly).FromJust(); \ - } while (0) - -#define READONLY_DONT_ENUM_PROPERTY(obj, str, var) \ - do { \ - obj->DefineOwnProperty(env->context(), \ - OneByteString(env->isolate(), str), \ - var, \ - static_cast(ReadOnly|DontEnum)) \ - .FromJust(); \ - } while (0) - -} // anonymous namespace - void SetupProcessObject(Environment* env, const std::vector& args, const std::vector& exec_args) { - HandleScope scope(env->isolate()); + Isolate* isolate = env->isolate(); + HandleScope scope(isolate); + Local context = env->context(); Local process = env->process_object(); @@ -1319,9 +1297,6 @@ void SetupProcessObject(Environment* env, } -#undef READONLY_PROPERTY - - void SignalExit(int signo) { uv_tty_reset_mode(); #ifdef __FreeBSD__ diff --git a/src/node_config.cc b/src/node_config.cc index c3592dc3e8803d..6b7f4e62b7cd94 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -12,8 +12,6 @@ using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; -using v8::ReadOnly; -using v8::String; using v8::Value; // The config binding is used to provide an internal view of compile or runtime @@ -21,32 +19,6 @@ using v8::Value; // alternative to dropping additional properties onto the process object as // has been the practice previously in node.cc. -#define READONLY_BOOLEAN_PROPERTY(str) \ - do { \ - target->DefineOwnProperty(context, \ - FIXED_ONE_BYTE_STRING(isolate, str), \ - True(isolate), ReadOnly).FromJust(); \ - } while (0) - -#define READONLY_STRING_PROPERTY(obj, str, val) \ - do { \ - (obj)->DefineOwnProperty(context, \ - FIXED_ONE_BYTE_STRING(isolate, str), \ - String::NewFromUtf8( \ - isolate, \ - val.data(), \ - v8::NewStringType::kNormal).ToLocalChecked(), \ - ReadOnly).FromJust(); \ - } while (0) - - -#define READONLY_PROPERTY(obj, name, value) \ - do { \ - obj->DefineOwnProperty(env->context(), \ - FIXED_ONE_BYTE_STRING(isolate, name), \ - value, ReadOnly).FromJust(); \ - } while (0) - static void Initialize(Local target, Local unused, Local context) { @@ -54,26 +26,26 @@ static void Initialize(Local target, Isolate* isolate = env->isolate(); #ifdef NODE_FIPS_MODE - READONLY_BOOLEAN_PROPERTY("fipsMode"); + READONLY_TRUE_PROPERTY(target, "fipsMode"); // TODO(addaleax): Use options parser variable instead. if (per_process_opts->force_fips_crypto) - READONLY_BOOLEAN_PROPERTY("fipsForced"); + READONLY_TRUE_PROPERTY(target, "fipsForced"); #endif #ifdef NODE_HAVE_I18N_SUPPORT - READONLY_BOOLEAN_PROPERTY("hasIntl"); + READONLY_TRUE_PROPERTY(target, "hasIntl"); #ifdef NODE_HAVE_SMALL_ICU - READONLY_BOOLEAN_PROPERTY("hasSmallICU"); + READONLY_TRUE_PROPERTY(target, "hasSmallICU"); #endif // NODE_HAVE_SMALL_ICU #if NODE_USE_V8_PLATFORM - READONLY_BOOLEAN_PROPERTY("hasTracing"); + READONLY_TRUE_PROPERTY(target, "hasTracing"); #endif #if !defined(NODE_WITHOUT_NODE_OPTIONS) - READONLY_BOOLEAN_PROPERTY("hasNodeOptions"); + READONLY_TRUE_PROPERTY(target, "hasNodeOptions"); #endif // TODO(addaleax): This seems to be an unused, private API. Remove it? @@ -83,12 +55,12 @@ static void Initialize(Local target, #endif // NODE_HAVE_I18N_SUPPORT if (env->options()->preserve_symlinks) - READONLY_BOOLEAN_PROPERTY("preserveSymlinks"); + READONLY_TRUE_PROPERTY(target, "preserveSymlinks"); if (env->options()->preserve_symlinks_main) - READONLY_BOOLEAN_PROPERTY("preserveSymlinksMain"); + READONLY_TRUE_PROPERTY(target, "preserveSymlinksMain"); if (env->options()->experimental_modules) { - READONLY_BOOLEAN_PROPERTY("experimentalModules"); + READONLY_TRUE_PROPERTY(target, "experimentalModules"); const std::string& userland_loader = env->options()->userland_loader; if (!userland_loader.empty()) { READONLY_STRING_PROPERTY(target, "userLoader", userland_loader); @@ -96,22 +68,22 @@ static void Initialize(Local target, } if (env->options()->experimental_vm_modules) - READONLY_BOOLEAN_PROPERTY("experimentalVMModules"); + READONLY_TRUE_PROPERTY(target, "experimentalVMModules"); if (env->options()->experimental_worker) - READONLY_BOOLEAN_PROPERTY("experimentalWorker"); + READONLY_TRUE_PROPERTY(target, "experimentalWorker"); if (env->options()->experimental_repl_await) - READONLY_BOOLEAN_PROPERTY("experimentalREPLAwait"); + READONLY_TRUE_PROPERTY(target, "experimentalREPLAwait"); if (env->options()->pending_deprecation) - READONLY_BOOLEAN_PROPERTY("pendingDeprecation"); + READONLY_TRUE_PROPERTY(target, "pendingDeprecation"); if (env->options()->expose_internals) - READONLY_BOOLEAN_PROPERTY("exposeInternals"); + READONLY_TRUE_PROPERTY(target, "exposeInternals"); if (env->abort_on_uncaught_exception()) - READONLY_BOOLEAN_PROPERTY("shouldAbortOnUncaughtException"); + READONLY_TRUE_PROPERTY(target, "shouldAbortOnUncaughtException"); READONLY_PROPERTY(target, "bits", diff --git a/src/util.h b/src/util.h index 9dd39fe1860b8e..9bf8bb05823c51 100644 --- a/src/util.h +++ b/src/util.h @@ -479,6 +479,31 @@ template inline v8::MaybeLocal ToV8Value(v8::Local context, const std::unordered_map& map); +// These macros expects a `Isolate* isolate` and a `Local context` +// to be in the scope. +#define READONLY_PROPERTY(obj, name, value) \ + do { \ + obj->DefineOwnProperty( \ + context, FIXED_ONE_BYTE_STRING(isolate, name), value, v8::ReadOnly) \ + .FromJust(); \ + } while (0) + +#define READONLY_DONT_ENUM_PROPERTY(obj, name, var) \ + do { \ + obj->DefineOwnProperty( \ + context, \ + OneByteString(isolate, name), \ + var, \ + static_cast(v8::ReadOnly | v8::DontEnum)) \ + .FromJust(); \ + } while (0) + +#define READONLY_TRUE_PROPERTY(obj, name) \ + READONLY_PROPERTY(obj, name, True(isolate)) + +#define READONLY_STRING_PROPERTY(obj, name, str) \ + READONLY_PROPERTY(obj, name, ToV8Value(context, str).ToLocalChecked()) + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS