util: simplify constructor retrieval in inspect()

Instead of looping through a list of typed arrays, use
TypedArrayPrototypeGetSymbolToStringTag.

PR-URL: https://github.com/nodejs/node/pull/36466
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott authored and Michaël Zasso committed 2020-12-21 18:24:25 +01:00
1 parent 1b4984de98
commit fa40366276
1 file changed
+3 -44
+3 -44
View File
@@ -3,21 +3,14 @@
const {
Array,
ArrayIsArray,
BigInt64Array,
BigIntPrototypeValueOf,
BigUint64Array,
BooleanPrototypeValueOf,
DatePrototypeGetTime,
DatePrototypeToISOString,
DatePrototypeToString,
ErrorPrototypeToString,
Float32Array,
Float64Array,
FunctionPrototypeCall,
FunctionPrototypeToString,
Int8Array,
Int16Array,
Int32Array,
JSONStringify,
Map,
MapPrototypeGetSize,
@@ -59,10 +52,8 @@ const {
SymbolIterator,
SymbolToStringTag,
TypedArrayPrototypeGetLength,
Uint16Array,
Uint32Array,
TypedArrayPrototypeGetSymbolToStringTag,
Uint8Array,
Uint8ClampedArray,
uncurryThis,
} = primordials;
@@ -120,17 +111,6 @@ const {
isNumberObject,
isBooleanObject,
isBigIntObject,
isUint8Array,
isUint8ClampedArray,
isUint16Array,
isUint32Array,
isInt8Array,
isInt16Array,
isInt32Array,
isFloat32Array,
isFloat64Array,
isBigInt64Array,
isBigUint64Array
} = require('internal/util/types');
const assert = require('internal/assert');
@@ -712,26 +692,6 @@ function formatProxy(ctx, proxy, recurseTimes) {
ctx, res, '', ['Proxy [', ']'], kArrayExtrasType, recurseTimes);
}
function findTypedConstructor(value) {
for (const [check, clazz] of [
[isUint8Array, Uint8Array],
[isUint8ClampedArray, Uint8ClampedArray],
[isUint16Array, Uint16Array],
[isUint32Array, Uint32Array],
[isInt8Array, Int8Array],
[isInt16Array, Int16Array],
[isInt32Array, Int32Array],
[isFloat32Array, Float32Array],
[isFloat64Array, Float64Array],
[isBigInt64Array, BigInt64Array],
[isBigUint64Array, BigUint64Array]
]) {
if (check(value)) {
return clazz;
}
}
}
// Note: using `formatValue` directly requires the indentation level to be
// corrected by setting `ctx.indentationLvL += diff` and then to decrease the
// value afterwards again.
@@ -879,10 +839,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
let bound = value;
let fallback = '';
if (constructor === null) {
const constr = findTypedConstructor(value);
fallback = constr.name;
fallback = TypedArrayPrototypeGetSymbolToStringTag(value);
// Reconstruct the array information.
bound = new constr(value);
bound = new primordials[fallback](value);
}
const size = TypedArrayPrototypeGetLength(value);
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);