Skip to content

Commit

Permalink
src,lib: group properties used as constants from util binding
Browse files Browse the repository at this point in the history
Properties used as constants in `util` internal binding are
scattered. This suggests using an object holding all of them
for better maintenance.

Signed-off-by: Daeyeon Jeong <daeyeon.dev@gmail.com>
PR-URL: nodejs#45539
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
daeyeon authored and marco-ippolito committed Nov 24, 2022
1 parent af60389 commit 1e0aac3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 47 deletions.
6 changes: 3 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ const {
kStringMaxLength
} = internalBinding('buffer');
const {
getOwnNonIndexProperties,
propertyFilter: {
constants: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
ONLY_ENUMERABLE,
},
getOwnNonIndexProperties,
} = internalBinding('util');
const {
customInspectSymbol,
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ const {
validateInteger,
} = require('internal/validators');
const {
constants: {
kExitCode,
kExiting,
kHasExitCode,
},
privateSymbols: {
exit_info_private_symbol,
},
kExitCode,
kExiting,
kHasExitCode,
} = internalBinding('util');

setupProcessObject();
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ const {
isFloat64Array,
} = types;
const {
getOwnNonIndexProperties,
propertyFilter: {
constants: {
ONLY_ENUMERABLE,
SKIP_SYMBOLS
}
SKIP_SYMBOLS,
},
getOwnNonIndexProperties,
} = internalBinding('util');

const kStrict = true;
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ const {
} = primordials;

const {
constants: {
ALL_PROPERTIES,
ONLY_ENUMERABLE,
kPending,
kRejected,
},
getOwnNonIndexProperties,
getPromiseDetails,
getProxyDetails,
kPending,
kRejected,
previewEntries,
getConstructorName: internalGetConstructorName,
getExternalValue,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
}
} = internalBinding('util');

const {
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ const {
} = require('util');

const {
constants: {
kPending,
},
getPromiseDetails,
kPending,
} = internalBinding('util');

const assert = require('internal/assert');
Expand Down
8 changes: 4 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ const {
setupReverseSearch,
} = require('internal/repl/utils');
const {
getOwnNonIndexProperties,
propertyFilter: {
constants: {
ALL_PROPERTIES,
SKIP_SYMBOLS
}
SKIP_SYMBOLS,
},
getOwnNonIndexProperties,
} = internalBinding('util');
const {
startSigintWatchdog,
Expand Down
66 changes: 40 additions & 26 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using v8::Isolate;
using v8::KeyCollectionMode;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::ONLY_CONFIGURABLE;
using v8::ONLY_ENUMERABLE;
using v8::ONLY_WRITABLE;
Expand Down Expand Up @@ -364,7 +365,7 @@ void Initialize(Local<Object> target,
Isolate* isolate = env->isolate();

{
Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate);
Local<ObjectTemplate> tmpl = ObjectTemplate::New(isolate);
#define V(PropertyName, _) \
tmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #PropertyName), \
env->PropertyName());
Expand All @@ -379,27 +380,50 @@ void Initialize(Local<Object> target,
.Check();
}

#define V(name) \
target->Set(context, \
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Integer::New(env->isolate(), Promise::PromiseState::name)) \
.FromJust()
V(kPending);
V(kFulfilled);
V(kRejected);
{
Local<Object> constants = Object::New(isolate);
#define V(name) \
constants \
->Set(context, \
FIXED_ONE_BYTE_STRING(isolate, #name), \
Integer::New(isolate, Promise::PromiseState::name)) \
.Check();

V(kPending);
V(kFulfilled);
V(kRejected);
#undef V

#define V(name) \
target \
constants \
->Set(context, \
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Integer::New(env->isolate(), Environment::ExitInfoField::name)) \
.FromJust()
V(kExiting);
V(kExitCode);
V(kHasExitCode);
FIXED_ONE_BYTE_STRING(isolate, #name), \
Integer::New(isolate, Environment::ExitInfoField::name)) \
.Check();

V(kExiting);
V(kExitCode);
V(kHasExitCode);
#undef V

#define V(name) \
constants \
->Set(context, \
FIXED_ONE_BYTE_STRING(isolate, #name), \
Integer::New(isolate, PropertyFilter::name)) \
.Check();

V(ALL_PROPERTIES);
V(ONLY_WRITABLE);
V(ONLY_ENUMERABLE);
V(ONLY_CONFIGURABLE);
V(SKIP_STRINGS);
V(SKIP_SYMBOLS);
#undef V

target->Set(context, env->constants_string(), constants).Check();
}

SetMethodNoSideEffect(
context, target, "getPromiseDetails", GetPromiseDetails);
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
Expand All @@ -413,16 +437,6 @@ void Initialize(Local<Object> target,

SetMethod(
context, target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
Local<Object> constants = Object::New(env->isolate());
NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
constants).Check();

Local<String> should_abort_on_uncaught_toggle =
FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
Expand Down

0 comments on commit 1e0aac3

Please sign in to comment.