Skip to content

Commit 8b95729

Browse files
committed
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 <daeyeon.dev@gmail.com>
1 parent db88483 commit 8b95729

File tree

7 files changed

+60
-47
lines changed

7 files changed

+60
-47
lines changed

lib/buffer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ const {
6767
kStringMaxLength
6868
} = internalBinding('buffer');
6969
const {
70-
getOwnNonIndexProperties,
71-
propertyFilter: {
70+
constants: {
7271
ALL_PROPERTIES,
73-
ONLY_ENUMERABLE
72+
ONLY_ENUMERABLE,
7473
},
74+
getOwnNonIndexProperties,
7575
} = internalBinding('util');
7676
const {
7777
customInspectSymbol,

lib/internal/bootstrap/node.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ const {
8080
validateInteger,
8181
} = require('internal/validators');
8282
const {
83+
constants: {
84+
kExitCode,
85+
kExiting,
86+
kHasExitCode,
87+
},
8388
privateSymbols: {
8489
exit_info_private_symbol,
8590
},
86-
kExitCode,
87-
kExiting,
88-
kHasExitCode,
8991
} = internalBinding('util');
9092

9193
setupProcessObject();

lib/internal/util/comparisons.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ const {
4646
isFloat64Array,
4747
} = types;
4848
const {
49-
getOwnNonIndexProperties,
50-
propertyFilter: {
49+
constants: {
5150
ONLY_ENUMERABLE,
52-
SKIP_SYMBOLS
53-
}
51+
SKIP_SYMBOLS,
52+
},
53+
getOwnNonIndexProperties,
5454
} = internalBinding('util');
5555

5656
const kStrict = true;

lib/internal/util/inspect.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ const {
9797
} = primordials;
9898

9999
const {
100+
constants: {
101+
ALL_PROPERTIES,
102+
ONLY_ENUMERABLE,
103+
kPending,
104+
kRejected,
105+
},
100106
getOwnNonIndexProperties,
101107
getPromiseDetails,
102108
getProxyDetails,
103-
kPending,
104-
kRejected,
105109
previewEntries,
106110
getConstructorName: internalGetConstructorName,
107111
getExternalValue,
108-
propertyFilter: {
109-
ALL_PROPERTIES,
110-
ONLY_ENUMERABLE
111-
}
112112
} = internalBinding('util');
113113

114114
const {

lib/internal/webstreams/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ const {
4040
} = require('util');
4141

4242
const {
43+
constants: {
44+
kPending,
45+
},
4346
getPromiseDetails,
44-
kPending,
4547
} = internalBinding('util');
4648

4749
const assert = require('internal/assert');

lib/repl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ const {
166166
setupReverseSearch,
167167
} = require('internal/repl/utils');
168168
const {
169-
getOwnNonIndexProperties,
170-
propertyFilter: {
169+
constants: {
171170
ALL_PROPERTIES,
172-
SKIP_SYMBOLS
173-
}
171+
SKIP_SYMBOLS,
172+
},
173+
getOwnNonIndexProperties,
174174
} = internalBinding('util');
175175
const {
176176
startSigintWatchdog,

src/node_util.cc

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using v8::Isolate;
2323
using v8::KeyCollectionMode;
2424
using v8::Local;
2525
using v8::Object;
26+
using v8::ObjectTemplate;
2627
using v8::ONLY_CONFIGURABLE;
2728
using v8::ONLY_ENUMERABLE;
2829
using v8::ONLY_WRITABLE;
@@ -379,27 +380,45 @@ void Initialize(Local<Object> target,
379380
.Check();
380381
}
381382

382-
#define V(name) \
383-
target->Set(context, \
384-
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
385-
Integer::New(env->isolate(), Promise::PromiseState::name)) \
386-
.FromJust()
387-
V(kPending);
388-
V(kFulfilled);
389-
V(kRejected);
383+
{
384+
Local<ObjectTemplate> tmpl = ObjectTemplate::New(isolate);
385+
#define V(name) \
386+
tmpl->Set(FIXED_ONE_BYTE_STRING(isolate, #name), \
387+
Integer::New(isolate, Promise::PromiseState::name));
388+
389+
V(kPending);
390+
V(kFulfilled);
391+
V(kRejected);
392+
#undef V
393+
394+
#define V(name) \
395+
tmpl->Set(FIXED_ONE_BYTE_STRING(isolate, #name), \
396+
Integer::New(isolate, Environment::ExitInfoField::name));
397+
398+
V(kExiting);
399+
V(kExitCode);
400+
V(kHasExitCode);
390401
#undef V
391402

392403
#define V(name) \
393-
target \
394-
->Set(context, \
395-
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
396-
Integer::New(env->isolate(), Environment::ExitInfoField::name)) \
397-
.FromJust()
398-
V(kExiting);
399-
V(kExitCode);
400-
V(kHasExitCode);
404+
tmpl->Set(FIXED_ONE_BYTE_STRING(isolate, #name), \
405+
Integer::New(isolate, PropertyFilter::name));
406+
407+
V(ALL_PROPERTIES);
408+
V(ONLY_WRITABLE);
409+
V(ONLY_ENUMERABLE);
410+
V(ONLY_CONFIGURABLE);
411+
V(SKIP_STRINGS);
412+
V(SKIP_SYMBOLS);
401413
#undef V
402414

415+
target
416+
->Set(context,
417+
env->constants_string(),
418+
tmpl->NewInstance(context).ToLocalChecked())
419+
.Check();
420+
}
421+
403422
SetMethodNoSideEffect(
404423
context, target, "getPromiseDetails", GetPromiseDetails);
405424
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
@@ -413,16 +432,6 @@ void Initialize(Local<Object> target,
413432

414433
SetMethod(
415434
context, target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
416-
Local<Object> constants = Object::New(env->isolate());
417-
NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
418-
NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
419-
NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
420-
NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
421-
NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
422-
NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
423-
target->Set(context,
424-
FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
425-
constants).Check();
426435

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

0 commit comments

Comments
 (0)