Skip to content

Commit

Permalink
util: simplify constructor retrieval in inspect()
Browse files Browse the repository at this point in the history
Instead of looping through a list of typed arrays, use
TypedArrayPrototypeGetSymbolToStringTag.

PR-URL: #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>
  • Loading branch information
Trott authored and targos committed Jun 11, 2021
1 parent ba19313 commit e113035
Showing 1 changed file with 3 additions and 44 deletions.
47 changes: 3 additions & 44 deletions lib/internal/util/inspect.js
Expand Up @@ -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,
Expand Down Expand Up @@ -59,10 +52,8 @@ const {
SymbolIterator,
SymbolToStringTag,
TypedArrayPrototypeGetLength,
Uint16Array,
Uint32Array,
TypedArrayPrototypeGetSymbolToStringTag,
Uint8Array,
Uint8ClampedArray,
uncurryThis,
} = primordials;

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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})`);
Expand Down

0 comments on commit e113035

Please sign in to comment.