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: #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 committed Nov 29, 2022
1 parent e7a5b33 commit 0567596
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 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: 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
48 changes: 30 additions & 18 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,38 @@ void Initialize(Local<Object> target,
}
#undef V

#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) \
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, "getHiddenValue", GetHiddenValue);
SetMethod(context, target, "setHiddenValue", SetHiddenValue);
SetMethodNoSideEffect(
Expand All @@ -439,16 +461,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 0567596

Please sign in to comment.