Skip to content

Commit d8e9e05

Browse files
authored
util: fix formatting of objects with built-in Symbol.toPrimitive
Fixes: #57818 PR-URL: #57832 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 33d8e03 commit d8e9e05

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

doc/api/util.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ corresponding argument. Supported specifiers are:
436436

437437
* `%s`: `String` will be used to convert all values except `BigInt`, `Object`
438438
and `-0`. `BigInt` values will be represented with an `n` and Objects that
439-
have no user defined `toString` function are inspected using `util.inspect()`
439+
have neither a user defined `toString` function nor `Symbol.toPrimitive` function are inspected using `util.inspect()`
440440
with options `{ depth: 0, colors: false, compact: 3 }`.
441441
* `%d`: `Number` will be used to convert all values except `BigInt` and
442442
`Symbol`.

lib/internal/util/inspect.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -2161,27 +2161,32 @@ function hasBuiltInToString(value) {
21612161
value = proxyTarget;
21622162
}
21632163

2164-
// Check if value has a custom Symbol.toPrimitive transformation.
2165-
if (typeof value[SymbolToPrimitive] === 'function') {
2166-
return false;
2167-
}
2164+
let hasOwnToString = ObjectPrototypeHasOwnProperty;
2165+
let hasOwnToPrimitive = ObjectPrototypeHasOwnProperty;
21682166

2169-
// Count objects that have no `toString` function as built-in.
2167+
// Count objects without `toString` and `Symbol.toPrimitive` function as built-in.
21702168
if (typeof value.toString !== 'function') {
2171-
return true;
2172-
}
2173-
2174-
// The object has a own `toString` property. Thus it's not not a built-in one.
2175-
if (ObjectPrototypeHasOwnProperty(value, 'toString')) {
2169+
if (typeof value[SymbolToPrimitive] !== 'function') {
2170+
return true;
2171+
} else if (ObjectPrototypeHasOwnProperty(value, SymbolToPrimitive)) {
2172+
return false;
2173+
}
2174+
hasOwnToString = returnFalse;
2175+
} else if (ObjectPrototypeHasOwnProperty(value, 'toString')) {
2176+
return false;
2177+
} else if (typeof value[SymbolToPrimitive] !== 'function') {
2178+
hasOwnToPrimitive = returnFalse;
2179+
} else if (ObjectPrototypeHasOwnProperty(value, SymbolToPrimitive)) {
21762180
return false;
21772181
}
21782182

2179-
// Find the object that has the `toString` property as own property in the
2180-
// prototype chain.
2183+
// Find the object that has the `toString` property or `Symbol.toPrimitive` property
2184+
// as own property in the prototype chain.
21812185
let pointer = value;
21822186
do {
21832187
pointer = ObjectGetPrototypeOf(pointer);
2184-
} while (!ObjectPrototypeHasOwnProperty(pointer, 'toString'));
2188+
} while (!hasOwnToString(pointer, 'toString') &&
2189+
!hasOwnToPrimitive(pointer, SymbolToPrimitive));
21852190

21862191
// Check closer if the object is a built-in.
21872192
const descriptor = ObjectGetOwnPropertyDescriptor(pointer, 'constructor');
@@ -2190,6 +2195,10 @@ function hasBuiltInToString(value) {
21902195
builtInObjects.has(descriptor.value.name);
21912196
}
21922197

2198+
function returnFalse() {
2199+
return false;
2200+
}
2201+
21932202
const firstErrorLine = (error) => StringPrototypeSplit(error.message, '\n', 1)[0];
21942203
let CIRCULAR_ERROR_MESSAGE;
21952204
function tryStringify(arg) {

test/parallel/test-util-format.js

+62
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,68 @@ assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
290290
assert.strictEqual(util.format('%s', objectWithToPrimitive + ''), 'default context');
291291
}
292292

293+
// built-in toPrimitive is the same behavior as inspect
294+
{
295+
const date = new Date('2023-10-01T00:00:00Z');
296+
assert.strictEqual(util.format('%s', date), util.inspect(date));
297+
298+
const symbol = Symbol('foo');
299+
assert.strictEqual(util.format('%s', symbol), util.inspect(symbol));
300+
}
301+
302+
// Prototype chain handling for toString
303+
{
304+
function hasToStringButNoToPrimitive() {}
305+
306+
hasToStringButNoToPrimitive.prototype.toString = function() {
307+
return 'hasToStringButNoToPrimitive';
308+
};
309+
310+
let obj = new hasToStringButNoToPrimitive();
311+
assert.strictEqual(util.format('%s', obj.toString()), 'hasToStringButNoToPrimitive');
312+
313+
function inheritsFromHasToStringButNoToPrimitive() {}
314+
Object.setPrototypeOf(inheritsFromHasToStringButNoToPrimitive.prototype,
315+
hasToStringButNoToPrimitive.prototype);
316+
obj = new inheritsFromHasToStringButNoToPrimitive();
317+
assert.strictEqual(util.format('%s', obj.toString()), 'hasToStringButNoToPrimitive');
318+
}
319+
320+
// Prototype chain handling for Symbol.toPrimitive
321+
{
322+
function hasToPrimitiveButNoToString() {}
323+
324+
hasToPrimitiveButNoToString.prototype[Symbol.toPrimitive] = function() {
325+
return 'hasToPrimitiveButNoToString';
326+
};
327+
328+
let obj = new hasToPrimitiveButNoToString();
329+
assert.strictEqual(util.format('%s', obj[Symbol.toPrimitive]()), 'hasToPrimitiveButNoToString');
330+
function inheritsFromHasToPrimitiveButNoToString() {}
331+
Object.setPrototypeOf(inheritsFromHasToPrimitiveButNoToString.prototype,
332+
hasToPrimitiveButNoToString.prototype);
333+
obj = new inheritsFromHasToPrimitiveButNoToString();
334+
assert.strictEqual(util.format('%s', obj[Symbol.toPrimitive]()), 'hasToPrimitiveButNoToString');
335+
}
336+
337+
// Prototype chain handling for both toString and Symbol.toPrimitive
338+
{
339+
function hasBothToStringAndToPrimitive() {}
340+
hasBothToStringAndToPrimitive.prototype.toString = function() {
341+
return 'toString';
342+
};
343+
hasBothToStringAndToPrimitive.prototype[Symbol.toPrimitive] = function() {
344+
return 'toPrimitive';
345+
};
346+
let obj = new hasBothToStringAndToPrimitive();
347+
assert.strictEqual(util.format('%s', obj.toString()), 'toString');
348+
function inheritsFromHasBothToStringAndToPrimitive() {}
349+
Object.setPrototypeOf(inheritsFromHasBothToStringAndToPrimitive.prototype,
350+
hasBothToStringAndToPrimitive.prototype);
351+
obj = new inheritsFromHasBothToStringAndToPrimitive();
352+
assert.strictEqual(util.format('%s', obj.toString()), 'toString');
353+
}
354+
293355
// JSON format specifier
294356
assert.strictEqual(util.format('%j'), '%j');
295357
assert.strictEqual(util.format('%j', 42), '42');

0 commit comments

Comments
 (0)