Skip to content

Commit 95b07ea

Browse files
committed
lib: add TypedArray to the primordials
This also makes it possible to use Symbol methods and getters.
1 parent c1ee70e commit 95b07ea

File tree

4 files changed

+48
-58
lines changed

4 files changed

+48
-58
lines changed

lib/buffer.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ const {
3434
ObjectCreate,
3535
ObjectDefineProperties,
3636
ObjectDefineProperty,
37-
ObjectGetOwnPropertyDescriptor,
38-
ObjectGetPrototypeOf,
3937
ObjectSetPrototypeOf,
4038
SymbolSpecies,
4139
SymbolToPrimitive,
42-
Uint8ArrayPrototype,
40+
TypedArrayPrototypeByteLength,
41+
TypedArrayPrototypeFill,
4342
} = primordials;
4443

4544
const {
@@ -107,13 +106,6 @@ const {
107106
addBufferPrototypeMethods
108107
} = require('internal/buffer');
109108

110-
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype);
111-
112-
const TypedArrayProto_byteLength =
113-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
114-
'byteLength').get;
115-
const TypedArrayFill = TypedArrayPrototype.fill;
116-
117109
FastBuffer.prototype.constructor = Buffer;
118110
Buffer.prototype = FastBuffer.prototype;
119111
addBufferPrototypeMethods(Buffer.prototype);
@@ -581,7 +573,7 @@ Buffer.concat = function concat(list, length) {
581573
// Zero-fill the remaining bytes if the specified `length` was more than
582574
// the actual total length, i.e. if we have some remaining allocated bytes
583575
// there were not initialized.
584-
TypedArrayFill.call(buffer, 0, pos, length);
576+
TypedArrayPrototypeFill(buffer, 0, pos, length);
585577
}
586578

587579
return buffer;
@@ -1020,12 +1012,12 @@ function _fill(buf, value, offset, end, encoding) {
10201012

10211013
if (typeof value === 'number') {
10221014
// OOB check
1023-
const byteLen = TypedArrayProto_byteLength.call(buf);
1015+
const byteLen = TypedArrayPrototypeByteLength(buf);
10241016
const fillLength = end - offset;
10251017
if (offset > end || fillLength + offset > byteLen)
10261018
throw new ERR_BUFFER_OUT_OF_BOUNDS();
10271019

1028-
TypedArrayFill.call(buf, value, offset, end);
1020+
TypedArrayPrototypeFill(buf, value, offset, end);
10291021
} else {
10301022
const res = bindingFill(buf, value, offset, end, encoding);
10311023
if (res < 0) {

lib/internal/per_context/primordials.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,22 @@ function copyPropsRenamedBound(src, dest, prefix) {
6565

6666
function copyPrototype(src, dest, prefix) {
6767
for (const key of Reflect.ownKeys(src)) {
68-
if (typeof key === 'string') {
69-
const desc = Reflect.getOwnPropertyDescriptor(src, key);
70-
if (typeof desc.value === 'function') {
71-
desc.value = uncurryThis(desc.value);
72-
}
73-
Reflect.defineProperty(
74-
dest,
75-
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
76-
desc);
68+
let newKey;
69+
if (typeof key === 'symbol') {
70+
const name = key.description.slice(7);
71+
newKey = `${prefix}Symbol${name[0].toUpperCase()}${name.slice(1)}`;
72+
} else {
73+
newKey = `${prefix}${key[0].toUpperCase()}${key.slice(1)}`;
7774
}
75+
const desc = Reflect.getOwnPropertyDescriptor(src, key);
76+
if (typeof desc.value === 'function') {
77+
desc.value = uncurryThis(desc.value);
78+
} else if (typeof desc.get === 'function') {
79+
desc.value = uncurryThis(desc.get);
80+
delete desc.get;
81+
delete desc.set;
82+
}
83+
Reflect.defineProperty(dest, newKey, desc);
7884
}
7985
}
8086

@@ -163,5 +169,14 @@ primordials.SafePromise = makeSafe(
163169
copyPrototype(original.prototype, primordials, `${name}Prototype`);
164170
});
165171

172+
// Create copies of objects that are not immediately available on `globalThis`.
173+
[
174+
['TypedArray', Object.getPrototypeOf(Uint8Array)]
175+
].forEach(([name, original]) => {
176+
primordials[name] = original;
177+
copyPropsRenamed(original, primordials, name);
178+
copyPrototype(original.prototype, primordials, `${name}Prototype`);
179+
});
180+
166181
Object.setPrototypeOf(primordials, null);
167182
Object.freeze(primordials);

lib/internal/util/inspect.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const {
1414
Float32Array,
1515
JSONStringify,
1616
Map,
17-
MapPrototype,
1817
MapPrototypeEntries,
18+
MapPrototypeSize,
1919
MathFloor,
2020
MathMax,
2121
MathMin,
@@ -39,13 +39,14 @@ const {
3939
RegExp,
4040
RegExpPrototypeToString,
4141
Set,
42-
SetPrototype,
42+
SetPrototypeSize,
4343
SetPrototypeValues,
4444
StringPrototypeValueOf,
4545
SymbolPrototypeToString,
4646
SymbolPrototypeValueOf,
4747
SymbolIterator,
4848
SymbolToStringTag,
49+
TypedArrayPrototypeLength,
4950
Uint16Array,
5051
uncurryThis,
5152
} = primordials;
@@ -120,14 +121,6 @@ const assert = require('internal/assert');
120121

121122
const { NativeModule } = require('internal/bootstrap/loaders');
122123

123-
const setSizeGetter = uncurryThis(
124-
ObjectGetOwnPropertyDescriptor(SetPrototype, 'size').get);
125-
const mapSizeGetter = uncurryThis(
126-
ObjectGetOwnPropertyDescriptor(MapPrototype, 'size').get);
127-
const typedArraySizeGetter = uncurryThis(
128-
ObjectGetOwnPropertyDescriptor(
129-
ObjectGetPrototypeOf(Uint8Array.prototype), 'length').get);
130-
131124
let hexSlice;
132125

133126
const builtInObjects = new Set(
@@ -802,7 +795,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
802795
extrasType = kArrayExtrasType;
803796
formatter = formatArray;
804797
} else if (isSet(value)) {
805-
const size = setSizeGetter(value);
798+
const size = SetPrototypeSize(value);
806799
const prefix = getPrefix(constructor, tag, 'Set', `(${size})`);
807800
keys = getKeys(value, ctx.showHidden);
808801
formatter = constructor !== null ?
@@ -812,7 +805,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
812805
return `${prefix}{}`;
813806
braces = [`${prefix}{`, '}'];
814807
} else if (isMap(value)) {
815-
const size = mapSizeGetter(value);
808+
const size = MapPrototypeSize(value);
816809
const prefix = getPrefix(constructor, tag, 'Map', `(${size})`);
817810
keys = getKeys(value, ctx.showHidden);
818811
formatter = constructor !== null ?
@@ -831,7 +824,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
831824
// Reconstruct the array information.
832825
bound = new constr(value);
833826
}
834-
const size = typedArraySizeGetter(value);
827+
const size = TypedArrayPrototypeLength(value);
835828
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);
836829
braces = [`${prefix}[`, ']'];
837830
if (value.length === 0 && keys.length === 0 && !ctx.showHidden)

lib/internal/util/types.js

+13-23
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,55 @@
22

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

11-
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8Array.prototype);
12-
13-
const TypedArrayProto_toStringTag =
14-
uncurryThis(
15-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
16-
SymbolToStringTag).get);
17-
188
function isTypedArray(value) {
19-
return TypedArrayProto_toStringTag(value) !== undefined;
9+
return TypedArrayPrototypeSymbolToStringTag(value) !== undefined;
2010
}
2111

2212
function isUint8Array(value) {
23-
return TypedArrayProto_toStringTag(value) === 'Uint8Array';
13+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Uint8Array';
2414
}
2515

2616
function isUint8ClampedArray(value) {
27-
return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
17+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Uint8ClampedArray';
2818
}
2919

3020
function isUint16Array(value) {
31-
return TypedArrayProto_toStringTag(value) === 'Uint16Array';
21+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Uint16Array';
3222
}
3323

3424
function isUint32Array(value) {
35-
return TypedArrayProto_toStringTag(value) === 'Uint32Array';
25+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Uint32Array';
3626
}
3727

3828
function isInt8Array(value) {
39-
return TypedArrayProto_toStringTag(value) === 'Int8Array';
29+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Int8Array';
4030
}
4131

4232
function isInt16Array(value) {
43-
return TypedArrayProto_toStringTag(value) === 'Int16Array';
33+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Int16Array';
4434
}
4535

4636
function isInt32Array(value) {
47-
return TypedArrayProto_toStringTag(value) === 'Int32Array';
37+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Int32Array';
4838
}
4939

5040
function isFloat32Array(value) {
51-
return TypedArrayProto_toStringTag(value) === 'Float32Array';
41+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Float32Array';
5242
}
5343

5444
function isFloat64Array(value) {
55-
return TypedArrayProto_toStringTag(value) === 'Float64Array';
45+
return TypedArrayPrototypeSymbolToStringTag(value) === 'Float64Array';
5646
}
5747

5848
function isBigInt64Array(value) {
59-
return TypedArrayProto_toStringTag(value) === 'BigInt64Array';
49+
return TypedArrayPrototypeSymbolToStringTag(value) === 'BigInt64Array';
6050
}
6151

6252
function isBigUint64Array(value) {
63-
return TypedArrayProto_toStringTag(value) === 'BigUint64Array';
53+
return TypedArrayPrototypeSymbolToStringTag(value) === 'BigUint64Array';
6454
}
6555

6656
module.exports = {

0 commit comments

Comments
 (0)