diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-11-30 07:33:20 +0800 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-12-03 18:16:36 -0800 |
commit | 77618817d0e8210506f8e6e739ac7bf469122179 (patch) | |
tree | 443872c13297cf192ef4260cc9682e548610e014 | |
parent | ff7d053ae6b7f620516049e5c9bd1b6ab348835f (diff) | |
download | node-new-77618817d0e8210506f8e6e739ac7bf469122179.tar.gz |
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 <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
-rw-r--r-- | src/node.cc | 31 | ||||
-rw-r--r-- | src/node_config.cc | 58 | ||||
-rw-r--r-- | src/util.h | 25 |
3 files changed, 43 insertions, 71 deletions
diff --git a/src/node.cc b/src/node.cc index 24fe3a71e9..dc7d8e21e5 100644 --- a/src/node.cc +++ b/src/node.cc @@ -123,7 +123,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; @@ -145,8 +144,6 @@ using v8::None; using v8::Nothing; using v8::Object; using v8::ObjectTemplate; -using v8::PropertyAttribute; -using v8::ReadOnly; using v8::Script; using v8::ScriptOrigin; using v8::SealHandleScope; @@ -917,31 +914,12 @@ static Local<Object> GetFeatures(Environment* env) { static void DebugProcess(const FunctionCallbackInfo<Value>& args); static void DebugEnd(const FunctionCallbackInfo<Value>& 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<PropertyAttribute>(ReadOnly|DontEnum)) \ - .FromJust(); \ - } while (0) - -} // anonymous namespace - void SetupProcessObject(Environment* env, const std::vector<std::string>& args, const std::vector<std::string>& exec_args) { - HandleScope scope(env->isolate()); + Isolate* isolate = env->isolate(); + HandleScope scope(isolate); + Local<Context> context = env->context(); Local<Object> process = env->process_object(); @@ -1282,9 +1260,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 c3592dc3e8..6b7f4e62b7 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<Object> target, Local<Value> unused, Local<Context> context) { @@ -54,26 +26,26 @@ static void Initialize(Local<Object> 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<Object> 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<Object> 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 9dd39fe186..9bf8bb0582 100644 --- a/src/util.h +++ b/src/util.h @@ -479,6 +479,31 @@ template <typename T, typename U> inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, const std::unordered_map<T, U>& map); +// These macros expects a `Isolate* isolate` and a `Local<Context> 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::PropertyAttribute>(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 |