Skip to content

Commit

Permalink
src: move READONLY_* macros into util.h
Browse files Browse the repository at this point in the history
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: #24774
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
joyeecheung authored and BridgeAR committed Dec 5, 2018
1 parent 1f8787c commit 53b59b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 71 deletions.
31 changes: 3 additions & 28 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -954,31 +951,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();

Expand Down Expand Up @@ -1319,9 +1297,6 @@ void SetupProcessObject(Environment* env,
}


#undef READONLY_PROPERTY


void SignalExit(int signo) {
uv_tty_reset_mode();
#ifdef __FreeBSD__
Expand Down
58 changes: 15 additions & 43 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,40 @@ 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
// config options that are required internally by lib/*.js code. This is an
// 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) {
Environment* env = Environment::GetCurrent(context);
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?
Expand All @@ -83,35 +55,35 @@ 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);
}
}

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",
Expand Down
25 changes: 25 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53b59b4

Please sign in to comment.