Skip to content

Commit fa40366

Browse files
Trotttargos
authored andcommitted
util: simplify constructor retrieval in inspect()
Instead of looping through a list of typed arrays, use TypedArrayPrototypeGetSymbolToStringTag. PR-URL: #36466 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1b4984d commit fa40366

File tree

1 file changed

+3
-44
lines changed

1 file changed

+3
-44
lines changed

lib/internal/util/inspect.js

+3-44
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33
const {
44
Array,
55
ArrayIsArray,
6-
BigInt64Array,
76
BigIntPrototypeValueOf,
8-
BigUint64Array,
97
BooleanPrototypeValueOf,
108
DatePrototypeGetTime,
119
DatePrototypeToISOString,
1210
DatePrototypeToString,
1311
ErrorPrototypeToString,
14-
Float32Array,
15-
Float64Array,
1612
FunctionPrototypeCall,
1713
FunctionPrototypeToString,
18-
Int8Array,
19-
Int16Array,
20-
Int32Array,
2114
JSONStringify,
2215
Map,
2316
MapPrototypeGetSize,
@@ -59,10 +52,8 @@ const {
5952
SymbolIterator,
6053
SymbolToStringTag,
6154
TypedArrayPrototypeGetLength,
62-
Uint16Array,
63-
Uint32Array,
55+
TypedArrayPrototypeGetSymbolToStringTag,
6456
Uint8Array,
65-
Uint8ClampedArray,
6657
uncurryThis,
6758
} = primordials;
6859

@@ -120,17 +111,6 @@ const {
120111
isNumberObject,
121112
isBooleanObject,
122113
isBigIntObject,
123-
isUint8Array,
124-
isUint8ClampedArray,
125-
isUint16Array,
126-
isUint32Array,
127-
isInt8Array,
128-
isInt16Array,
129-
isInt32Array,
130-
isFloat32Array,
131-
isFloat64Array,
132-
isBigInt64Array,
133-
isBigUint64Array
134114
} = require('internal/util/types');
135115

136116
const assert = require('internal/assert');
@@ -712,26 +692,6 @@ function formatProxy(ctx, proxy, recurseTimes) {
712692
ctx, res, '', ['Proxy [', ']'], kArrayExtrasType, recurseTimes);
713693
}
714694

715-
function findTypedConstructor(value) {
716-
for (const [check, clazz] of [
717-
[isUint8Array, Uint8Array],
718-
[isUint8ClampedArray, Uint8ClampedArray],
719-
[isUint16Array, Uint16Array],
720-
[isUint32Array, Uint32Array],
721-
[isInt8Array, Int8Array],
722-
[isInt16Array, Int16Array],
723-
[isInt32Array, Int32Array],
724-
[isFloat32Array, Float32Array],
725-
[isFloat64Array, Float64Array],
726-
[isBigInt64Array, BigInt64Array],
727-
[isBigUint64Array, BigUint64Array]
728-
]) {
729-
if (check(value)) {
730-
return clazz;
731-
}
732-
}
733-
}
734-
735695
// Note: using `formatValue` directly requires the indentation level to be
736696
// corrected by setting `ctx.indentationLvL += diff` and then to decrease the
737697
// value afterwards again.
@@ -879,10 +839,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
879839
let bound = value;
880840
let fallback = '';
881841
if (constructor === null) {
882-
const constr = findTypedConstructor(value);
883-
fallback = constr.name;
842+
fallback = TypedArrayPrototypeGetSymbolToStringTag(value);
884843
// Reconstruct the array information.
885-
bound = new constr(value);
844+
bound = new primordials[fallback](value);
886845
}
887846
const size = TypedArrayPrototypeGetLength(value);
888847
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);

0 commit comments

Comments
 (0)