Skip to content

Commit be9cd3e

Browse files
authored
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 <[email protected]> PR-URL: #45539 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 3ebe753 commit be9cd3e

File tree

7 files changed

+65
-47
lines changed

7 files changed

+65
-47
lines changed

lib/buffer.js

+3-3
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

+5-3
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

+4-4
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

+6-6
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

+3-1
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

+4-4
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

+40-26
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;
@@ -364,7 +365,7 @@ void Initialize(Local<Object> target,
364365
Isolate* isolate = env->isolate();
365366

366367
{
367-
Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate);
368+
Local<ObjectTemplate> tmpl = ObjectTemplate::New(isolate);
368369
#define V(PropertyName, _) \
369370
tmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #PropertyName), \
370371
env->PropertyName());
@@ -379,27 +380,50 @@ 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<Object> constants = Object::New(isolate);
385+
#define V(name) \
386+
constants \
387+
->Set(context, \
388+
FIXED_ONE_BYTE_STRING(isolate, #name), \
389+
Integer::New(isolate, Promise::PromiseState::name)) \
390+
.Check();
391+
392+
V(kPending);
393+
V(kFulfilled);
394+
V(kRejected);
390395
#undef V
391396

392397
#define V(name) \
393-
target \
398+
constants \
394399
->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);
400+
FIXED_ONE_BYTE_STRING(isolate, #name), \
401+
Integer::New(isolate, Environment::ExitInfoField::name)) \
402+
.Check();
403+
404+
V(kExiting);
405+
V(kExitCode);
406+
V(kHasExitCode);
401407
#undef V
402408

409+
#define V(name) \
410+
constants \
411+
->Set(context, \
412+
FIXED_ONE_BYTE_STRING(isolate, #name), \
413+
Integer::New(isolate, PropertyFilter::name)) \
414+
.Check();
415+
416+
V(ALL_PROPERTIES);
417+
V(ONLY_WRITABLE);
418+
V(ONLY_ENUMERABLE);
419+
V(ONLY_CONFIGURABLE);
420+
V(SKIP_STRINGS);
421+
V(SKIP_SYMBOLS);
422+
#undef V
423+
424+
target->Set(context, env->constants_string(), constants).Check();
425+
}
426+
403427
SetMethodNoSideEffect(
404428
context, target, "getPromiseDetails", GetPromiseDetails);
405429
SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails);
@@ -413,16 +437,6 @@ void Initialize(Local<Object> target,
413437

414438
SetMethod(
415439
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();
426440

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

0 commit comments

Comments
 (0)