mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
util: fix formatting of objects with built-in Symbol.toPrimitive
Fixes: https://github.com/nodejs/node/issues/57818 PR-URL: https://github.com/nodejs/node/pull/57832 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
33d8e03d9d
commit
d8e9e05a27
@ -436,7 +436,7 @@ corresponding argument. Supported specifiers are:
|
||||
|
||||
* `%s`: `String` will be used to convert all values except `BigInt`, `Object`
|
||||
and `-0`. `BigInt` values will be represented with an `n` and Objects that
|
||||
have no user defined `toString` function are inspected using `util.inspect()`
|
||||
have neither a user defined `toString` function nor `Symbol.toPrimitive` function are inspected using `util.inspect()`
|
||||
with options `{ depth: 0, colors: false, compact: 3 }`.
|
||||
* `%d`: `Number` will be used to convert all values except `BigInt` and
|
||||
`Symbol`.
|
||||
|
||||
@ -2161,27 +2161,32 @@ function hasBuiltInToString(value) {
|
||||
value = proxyTarget;
|
||||
}
|
||||
|
||||
// Check if value has a custom Symbol.toPrimitive transformation.
|
||||
if (typeof value[SymbolToPrimitive] === 'function') {
|
||||
return false;
|
||||
}
|
||||
let hasOwnToString = ObjectPrototypeHasOwnProperty;
|
||||
let hasOwnToPrimitive = ObjectPrototypeHasOwnProperty;
|
||||
|
||||
// Count objects that have no `toString` function as built-in.
|
||||
// Count objects without `toString` and `Symbol.toPrimitive` function as built-in.
|
||||
if (typeof value.toString !== 'function') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// The object has a own `toString` property. Thus it's not not a built-in one.
|
||||
if (ObjectPrototypeHasOwnProperty(value, 'toString')) {
|
||||
if (typeof value[SymbolToPrimitive] !== 'function') {
|
||||
return true;
|
||||
} else if (ObjectPrototypeHasOwnProperty(value, SymbolToPrimitive)) {
|
||||
return false;
|
||||
}
|
||||
hasOwnToString = returnFalse;
|
||||
} else if (ObjectPrototypeHasOwnProperty(value, 'toString')) {
|
||||
return false;
|
||||
} else if (typeof value[SymbolToPrimitive] !== 'function') {
|
||||
hasOwnToPrimitive = returnFalse;
|
||||
} else if (ObjectPrototypeHasOwnProperty(value, SymbolToPrimitive)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find the object that has the `toString` property as own property in the
|
||||
// prototype chain.
|
||||
// Find the object that has the `toString` property or `Symbol.toPrimitive` property
|
||||
// as own property in the prototype chain.
|
||||
let pointer = value;
|
||||
do {
|
||||
pointer = ObjectGetPrototypeOf(pointer);
|
||||
} while (!ObjectPrototypeHasOwnProperty(pointer, 'toString'));
|
||||
} while (!hasOwnToString(pointer, 'toString') &&
|
||||
!hasOwnToPrimitive(pointer, SymbolToPrimitive));
|
||||
|
||||
// Check closer if the object is a built-in.
|
||||
const descriptor = ObjectGetOwnPropertyDescriptor(pointer, 'constructor');
|
||||
@ -2190,6 +2195,10 @@ function hasBuiltInToString(value) {
|
||||
builtInObjects.has(descriptor.value.name);
|
||||
}
|
||||
|
||||
function returnFalse() {
|
||||
return false;
|
||||
}
|
||||
|
||||
const firstErrorLine = (error) => StringPrototypeSplit(error.message, '\n', 1)[0];
|
||||
let CIRCULAR_ERROR_MESSAGE;
|
||||
function tryStringify(arg) {
|
||||
|
||||
@ -290,6 +290,68 @@ assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
|
||||
assert.strictEqual(util.format('%s', objectWithToPrimitive + ''), 'default context');
|
||||
}
|
||||
|
||||
// built-in toPrimitive is the same behavior as inspect
|
||||
{
|
||||
const date = new Date('2023-10-01T00:00:00Z');
|
||||
assert.strictEqual(util.format('%s', date), util.inspect(date));
|
||||
|
||||
const symbol = Symbol('foo');
|
||||
assert.strictEqual(util.format('%s', symbol), util.inspect(symbol));
|
||||
}
|
||||
|
||||
// Prototype chain handling for toString
|
||||
{
|
||||
function hasToStringButNoToPrimitive() {}
|
||||
|
||||
hasToStringButNoToPrimitive.prototype.toString = function() {
|
||||
return 'hasToStringButNoToPrimitive';
|
||||
};
|
||||
|
||||
let obj = new hasToStringButNoToPrimitive();
|
||||
assert.strictEqual(util.format('%s', obj.toString()), 'hasToStringButNoToPrimitive');
|
||||
|
||||
function inheritsFromHasToStringButNoToPrimitive() {}
|
||||
Object.setPrototypeOf(inheritsFromHasToStringButNoToPrimitive.prototype,
|
||||
hasToStringButNoToPrimitive.prototype);
|
||||
obj = new inheritsFromHasToStringButNoToPrimitive();
|
||||
assert.strictEqual(util.format('%s', obj.toString()), 'hasToStringButNoToPrimitive');
|
||||
}
|
||||
|
||||
// Prototype chain handling for Symbol.toPrimitive
|
||||
{
|
||||
function hasToPrimitiveButNoToString() {}
|
||||
|
||||
hasToPrimitiveButNoToString.prototype[Symbol.toPrimitive] = function() {
|
||||
return 'hasToPrimitiveButNoToString';
|
||||
};
|
||||
|
||||
let obj = new hasToPrimitiveButNoToString();
|
||||
assert.strictEqual(util.format('%s', obj[Symbol.toPrimitive]()), 'hasToPrimitiveButNoToString');
|
||||
function inheritsFromHasToPrimitiveButNoToString() {}
|
||||
Object.setPrototypeOf(inheritsFromHasToPrimitiveButNoToString.prototype,
|
||||
hasToPrimitiveButNoToString.prototype);
|
||||
obj = new inheritsFromHasToPrimitiveButNoToString();
|
||||
assert.strictEqual(util.format('%s', obj[Symbol.toPrimitive]()), 'hasToPrimitiveButNoToString');
|
||||
}
|
||||
|
||||
// Prototype chain handling for both toString and Symbol.toPrimitive
|
||||
{
|
||||
function hasBothToStringAndToPrimitive() {}
|
||||
hasBothToStringAndToPrimitive.prototype.toString = function() {
|
||||
return 'toString';
|
||||
};
|
||||
hasBothToStringAndToPrimitive.prototype[Symbol.toPrimitive] = function() {
|
||||
return 'toPrimitive';
|
||||
};
|
||||
let obj = new hasBothToStringAndToPrimitive();
|
||||
assert.strictEqual(util.format('%s', obj.toString()), 'toString');
|
||||
function inheritsFromHasBothToStringAndToPrimitive() {}
|
||||
Object.setPrototypeOf(inheritsFromHasBothToStringAndToPrimitive.prototype,
|
||||
hasBothToStringAndToPrimitive.prototype);
|
||||
obj = new inheritsFromHasBothToStringAndToPrimitive();
|
||||
assert.strictEqual(util.format('%s', obj.toString()), 'toString');
|
||||
}
|
||||
|
||||
// JSON format specifier
|
||||
assert.strictEqual(util.format('%j'), '%j');
|
||||
assert.strictEqual(util.format('%j', 42), '42');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user