Skip to content

Commit 5daeac6

Browse files
ExE-Bosstargos
authored andcommitted
lib: add uncurried accessor properties to primordials
Closes: #32127 PR-URL: #36329 Fixes: #32127 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0db9101 commit 5daeac6

File tree

4 files changed

+58
-60
lines changed

4 files changed

+58
-60
lines changed

lib/buffer.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535
ObjectCreate,
3636
ObjectDefineProperties,
3737
ObjectDefineProperty,
38-
ObjectGetOwnPropertyDescriptor,
3938
ObjectSetPrototypeOf,
4039
StringPrototypeCharCodeAt,
4140
StringPrototypeReplace,
@@ -44,12 +43,11 @@ const {
4443
StringPrototypeTrim,
4544
SymbolSpecies,
4645
SymbolToPrimitive,
47-
TypedArrayPrototype,
46+
TypedArrayPrototypeGetByteLength,
4847
TypedArrayPrototypeFill,
4948
TypedArrayPrototypeSet,
5049
Uint8Array,
5150
Uint8ArrayPrototype,
52-
uncurryThis,
5351
} = primordials;
5452

5553
const {
@@ -118,10 +116,6 @@ const {
118116
addBufferPrototypeMethods
119117
} = require('internal/buffer');
120118

121-
const TypedArrayProto_byteLength = uncurryThis(
122-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
123-
'byteLength').get);
124-
125119
FastBuffer.prototype.constructor = Buffer;
126120
Buffer.prototype = FastBuffer.prototype;
127121
addBufferPrototypeMethods(Buffer.prototype);
@@ -1031,7 +1025,7 @@ function _fill(buf, value, offset, end, encoding) {
10311025

10321026
if (typeof value === 'number') {
10331027
// OOB check
1034-
const byteLen = TypedArrayProto_byteLength(buf);
1028+
const byteLen = TypedArrayPrototypeGetByteLength(buf);
10351029
const fillLength = end - offset;
10361030
if (offset > end || fillLength + offset > byteLen)
10371031
throw new ERR_BUFFER_OUT_OF_BOUNDS();

lib/internal/per_context/primordials.js

+37-18
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,63 @@ function copyProps(src, dest) {
3030
}
3131
}
3232

33+
function getNewKey(key) {
34+
return typeof key === 'symbol' ?
35+
`Symbol${key.description[7].toUpperCase()}${key.description.slice(8)}` :
36+
`${key[0].toUpperCase()}${key.slice(1)}`;
37+
}
38+
39+
function copyAccessor(dest, prefix, key, { enumerable, get, set }) {
40+
Reflect.defineProperty(dest, `${prefix}Get${key}`, {
41+
value: uncurryThis(get),
42+
enumerable
43+
});
44+
if (set !== undefined) {
45+
Reflect.defineProperty(dest, `${prefix}Set${key}`, {
46+
value: uncurryThis(set),
47+
enumerable
48+
});
49+
}
50+
}
51+
3352
function copyPropsRenamed(src, dest, prefix) {
3453
for (const key of Reflect.ownKeys(src)) {
35-
if (typeof key === 'string') {
36-
Reflect.defineProperty(
37-
dest,
38-
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
39-
Reflect.getOwnPropertyDescriptor(src, key));
54+
const newKey = getNewKey(key);
55+
const desc = Reflect.getOwnPropertyDescriptor(src, key);
56+
if ('get' in desc) {
57+
copyAccessor(dest, prefix, newKey, desc);
58+
} else {
59+
Reflect.defineProperty(dest, `${prefix}${newKey}`, desc);
4060
}
4161
}
4262
}
4363

4464
function copyPropsRenamedBound(src, dest, prefix) {
4565
for (const key of Reflect.ownKeys(src)) {
46-
if (typeof key === 'string') {
47-
const desc = Reflect.getOwnPropertyDescriptor(src, key);
66+
const newKey = getNewKey(key);
67+
const desc = Reflect.getOwnPropertyDescriptor(src, key);
68+
if ('get' in desc) {
69+
copyAccessor(dest, prefix, newKey, desc);
70+
} else {
4871
if (typeof desc.value === 'function') {
4972
desc.value = desc.value.bind(src);
5073
}
51-
Reflect.defineProperty(
52-
dest,
53-
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
54-
desc
55-
);
74+
Reflect.defineProperty(dest, `${prefix}${newKey}`, desc);
5675
}
5776
}
5877
}
5978

6079
function copyPrototype(src, dest, prefix) {
6180
for (const key of Reflect.ownKeys(src)) {
62-
if (typeof key === 'string') {
63-
const desc = Reflect.getOwnPropertyDescriptor(src, key);
81+
const newKey = getNewKey(key);
82+
const desc = Reflect.getOwnPropertyDescriptor(src, key);
83+
if ('get' in desc) {
84+
copyAccessor(dest, prefix, newKey, desc);
85+
} else {
6486
if (typeof desc.value === 'function') {
6587
desc.value = uncurryThis(desc.value);
6688
}
67-
Reflect.defineProperty(
68-
dest,
69-
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
70-
desc);
89+
Reflect.defineProperty(dest, `${prefix}${newKey}`, desc);
7190
}
7291
}
7392
}

lib/internal/util/inspect.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {
2020
Int32Array,
2121
JSONStringify,
2222
Map,
23-
MapPrototype,
23+
MapPrototypeGetSize,
2424
MapPrototypeEntries,
2525
MathFloor,
2626
MathMax,
@@ -50,15 +50,15 @@ const {
5050
RegExp,
5151
RegExpPrototypeToString,
5252
Set,
53-
SetPrototype,
53+
SetPrototypeGetSize,
5454
SetPrototypeValues,
5555
String,
5656
StringPrototypeValueOf,
5757
SymbolPrototypeToString,
5858
SymbolPrototypeValueOf,
5959
SymbolIterator,
6060
SymbolToStringTag,
61-
TypedArrayPrototype,
61+
TypedArrayPrototypeGetLength,
6262
Uint16Array,
6363
Uint32Array,
6464
Uint8Array,
@@ -137,13 +137,6 @@ const assert = require('internal/assert');
137137

138138
const { NativeModule } = require('internal/bootstrap/loaders');
139139

140-
const setSizeGetter = uncurryThis(
141-
ObjectGetOwnPropertyDescriptor(SetPrototype, 'size').get);
142-
const mapSizeGetter = uncurryThis(
143-
ObjectGetOwnPropertyDescriptor(MapPrototype, 'size').get);
144-
const typedArraySizeGetter = uncurryThis(
145-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, 'length').get);
146-
147140
let hexSlice;
148141

149142
const builtInObjects = new Set(
@@ -862,7 +855,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
862855
extrasType = kArrayExtrasType;
863856
formatter = formatArray;
864857
} else if (isSet(value)) {
865-
const size = setSizeGetter(value);
858+
const size = SetPrototypeGetSize(value);
866859
const prefix = getPrefix(constructor, tag, 'Set', `(${size})`);
867860
keys = getKeys(value, ctx.showHidden);
868861
formatter = constructor !== null ?
@@ -872,7 +865,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
872865
return `${prefix}{}`;
873866
braces = [`${prefix}{`, '}'];
874867
} else if (isMap(value)) {
875-
const size = mapSizeGetter(value);
868+
const size = MapPrototypeGetSize(value);
876869
const prefix = getPrefix(constructor, tag, 'Map', `(${size})`);
877870
keys = getKeys(value, ctx.showHidden);
878871
formatter = constructor !== null ?
@@ -891,7 +884,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
891884
// Reconstruct the array information.
892885
bound = new constr(value);
893886
}
894-
const size = typedArraySizeGetter(value);
887+
const size = TypedArrayPrototypeGetLength(value);
895888
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);
896889
braces = [`${prefix}[`, ']'];
897890
if (value.length === 0 && keys.length === 0 && !ctx.showHidden)

lib/internal/util/types.js

+13-21
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,55 @@
22

33
const {
44
ArrayBufferIsView,
5-
ObjectGetOwnPropertyDescriptor,
6-
SymbolToStringTag,
7-
TypedArrayPrototype,
8-
uncurryThis,
5+
TypedArrayPrototypeGetSymbolToStringTag,
96
} = primordials;
107

11-
const TypedArrayProto_toStringTag =
12-
uncurryThis(
13-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
14-
SymbolToStringTag).get);
15-
168
function isTypedArray(value) {
17-
return TypedArrayProto_toStringTag(value) !== undefined;
9+
return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined;
1810
}
1911

2012
function isUint8Array(value) {
21-
return TypedArrayProto_toStringTag(value) === 'Uint8Array';
13+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
2214
}
2315

2416
function isUint8ClampedArray(value) {
25-
return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
17+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8ClampedArray';
2618
}
2719

2820
function isUint16Array(value) {
29-
return TypedArrayProto_toStringTag(value) === 'Uint16Array';
21+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint16Array';
3022
}
3123

3224
function isUint32Array(value) {
33-
return TypedArrayProto_toStringTag(value) === 'Uint32Array';
25+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint32Array';
3426
}
3527

3628
function isInt8Array(value) {
37-
return TypedArrayProto_toStringTag(value) === 'Int8Array';
29+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int8Array';
3830
}
3931

4032
function isInt16Array(value) {
41-
return TypedArrayProto_toStringTag(value) === 'Int16Array';
33+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int16Array';
4234
}
4335

4436
function isInt32Array(value) {
45-
return TypedArrayProto_toStringTag(value) === 'Int32Array';
37+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Int32Array';
4638
}
4739

4840
function isFloat32Array(value) {
49-
return TypedArrayProto_toStringTag(value) === 'Float32Array';
41+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float32Array';
5042
}
5143

5244
function isFloat64Array(value) {
53-
return TypedArrayProto_toStringTag(value) === 'Float64Array';
45+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Float64Array';
5446
}
5547

5648
function isBigInt64Array(value) {
57-
return TypedArrayProto_toStringTag(value) === 'BigInt64Array';
49+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigInt64Array';
5850
}
5951

6052
function isBigUint64Array(value) {
61-
return TypedArrayProto_toStringTag(value) === 'BigUint64Array';
53+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigUint64Array';
6254
}
6355

6456
module.exports = {

0 commit comments

Comments
 (0)