From b02b3c8315567a193663285bcf4781d0b044b57f Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Thu, 24 Nov 2022 11:09:29 +0900 Subject: [PATCH] src,lib: group properties used as constants from `util` binding 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 PR-URL: https://github.com/nodejs/node/pull/45539 Backport-PR-URL: https://github.com/nodejs/node/pull/45674 Reviewed-By: Darshan Sen Reviewed-By: Chengzhong Wu Reviewed-By: Minwoo Jung Reviewed-By: Joyee Cheung --- lib/buffer.js | 6 ++-- lib/internal/util/comparisons.js | 8 +++--- lib/internal/util/inspect.js | 12 ++++---- lib/internal/webstreams/util.js | 4 ++- lib/repl.js | 8 +++--- src/node_util.cc | 48 ++++++++++++++++++++------------ 6 files changed, 50 insertions(+), 36 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 898bc5032e5f8d..08aa52b3883ea1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -67,11 +67,11 @@ const { kStringMaxLength } = internalBinding('buffer'); const { - getOwnNonIndexProperties, - propertyFilter: { + constants: { ALL_PROPERTIES, - ONLY_ENUMERABLE + ONLY_ENUMERABLE, }, + getOwnNonIndexProperties, } = internalBinding('util'); const { customInspectSymbol, diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index c126bd6346dae7..e41fcf2daf92a0 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -46,11 +46,11 @@ const { isFloat64Array, } = types; const { - getOwnNonIndexProperties, - propertyFilter: { + constants: { ONLY_ENUMERABLE, - SKIP_SYMBOLS - } + SKIP_SYMBOLS, + }, + getOwnNonIndexProperties, } = internalBinding('util'); const kStrict = true; diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 7f9d33a7f03497..6174d62bb11643 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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 { diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index 1cf2d3a1874b10..5e0a75d2b30265 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -40,8 +40,10 @@ const { } = require('util'); const { + constants: { + kPending, + }, getPromiseDetails, - kPending, } = internalBinding('util'); const assert = require('internal/assert'); diff --git a/lib/repl.js b/lib/repl.js index f2996d660d8d6f..124d4781498cc4 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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, diff --git a/src/node_util.cc b/src/node_util.cc index 4d6c757c2fe422..ecb591698a4e82 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -387,16 +387,38 @@ void Initialize(Local 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 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, "getPromiseDetails", GetPromiseDetails); SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); @@ -410,16 +432,6 @@ void Initialize(Local target, SetMethod( context, target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer); - Local 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 should_abort_on_uncaught_toggle = FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");