From fa40366276ffa619a9e384fb66a630fbdc4ff34c Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 9 Dec 2020 21:33:28 -0800 Subject: [PATCH] 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 Reviewed-By: Franziska Hinkelmann Reviewed-By: Daijiro Wachi Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 47 +++--------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index d606ad430151c3..17ca3f3d2b39ce 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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})`);