Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add prototype support for boxed primitives #27351

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,6 @@ function getPrefix(constructor, tag, fallback) {
return `${constructor} `;
}

const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor);

// Look up the keys of the object.
function getKeys(value, showHidden) {
let keys;
Expand Down Expand Up @@ -709,31 +707,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
braces[0] = `[${tag}] {`;
formatter = formatNamespaceObject;
} else if (isBoxedPrimitive(value)) {
let type;
if (isNumberObject(value)) {
base = `[Number: ${getBoxedValue(NumberPrototype.valueOf(value))}]`;
type = 'number';
} else if (isStringObject(value)) {
base = `[String: ${
getBoxedValue(StringPrototype.valueOf(value), ctx)
}]`;
type = 'string';
// For boxed Strings, we have to remove the 0-n indexed entries,
// since they just noisy up the output and are redundant
// Make boxed primitive Strings look like such
keys = keys.slice(value.length);
} else if (isBooleanObject(value)) {
base = `[Boolean: ${getBoxedValue(BooleanPrototype.valueOf(value))}]`;
type = 'boolean';
} else if (isBigIntObject(value)) {
base = `[BigInt: ${getBoxedValue(BigIntPrototype.valueOf(value))}]`;
type = 'bigint';
} else {
base = `[Symbol: ${getBoxedValue(SymbolPrototype.valueOf(value))}]`;
type = 'symbol';
}
base = getBoxedBase(value, ctx, keys, constructor, tag);
if (keys.length === 0) {
return ctx.stylize(base, type);
return base;
}
} else {
// The input prototype got manipulated. Special handle these. We have to
Expand Down Expand Up @@ -818,6 +794,46 @@ function getIteratorBraces(type, tag) {
return [`[${tag}] {`, '}'];
}

function getBoxedBase(value, ctx, keys, constructor, tag) {
let fn;
let type;
if (isNumberObject(value)) {
fn = NumberPrototype;
type = 'Number';
} else if (isStringObject(value)) {
fn = StringPrototype;
type = 'String';
// For boxed Strings, we have to remove the 0-n indexed entries,
// since they just noisy up the output and are redundant
// Make boxed primitive Strings look like such
keys.splice(0, value.length);
} else if (isBooleanObject(value)) {
fn = BooleanPrototype;
type = 'Boolean';
} else if (isBigIntObject(value)) {
fn = BigIntPrototype;
type = 'BigInt';
} else {
fn = SymbolPrototype;
type = 'Symbol';
}
let base = `[${type}`;
if (type !== constructor) {
if (constructor === null) {
base += ' (null prototype)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Not a blocking comment.. But my thoughts..For objects, null prototype is showed as [Object: null prototype] {}, looks like for primitives, we show as [Boolean (null prototype): true] (like in test), may be we can show like [Boolean: null prototype] true, so that reading is consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that as well but I think it would be less obvious that it's a boxed primitive in that case (obviously, e.g., 5 is a number, so it might also just stand for extra information?).

The inconsistency is not ideal but this is likely going to be a super rare case anyway.

} else {
base += ` (${constructor})`;
}
}
base += `: ${formatPrimitive(stylizeNoColor, fn.valueOf(value), ctx)}]`;
if (tag !== '' && tag !== constructor) {
base += ` [${tag}]`;
}
if (keys.length !== 0 || ctx.stylize === stylizeNoColor)
return base;
return ctx.stylize(base, type.toLowerCase());
}

function formatError(err, constructor, tag, ctx) {
// TODO(BridgeAR): Always show the error code if present.
let stack = err.stack || ErrorPrototype.toString(err);
Expand Down
16 changes: 14 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,21 @@ assert.strictEqual(
'[Symbol: Symbol(test)]'
);
assert.strictEqual(util.inspect(new Boolean(false)), '[Boolean: false]');
assert.strictEqual(util.inspect(new Boolean(true)), '[Boolean: true]');
assert.strictEqual(
util.inspect(Object.setPrototypeOf(new Boolean(true), null)),
'[Boolean (null prototype): true]'
);
assert.strictEqual(util.inspect(new Number(0)), '[Number: 0]');
assert.strictEqual(util.inspect(new Number(-0)), '[Number: -0]');
assert.strictEqual(
util.inspect(
Object.defineProperty(
Object.setPrototypeOf(new Number(-0), Array.prototype),
Symbol.toStringTag,
{ value: 'Foobar' }
)
),
'[Number (Array): -0] [Foobar]'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆 😮

);
assert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]');
assert.strictEqual(util.inspect(new Number(13.37)), '[Number: 13.37]');

Expand Down