Skip to content

Commit 96fdc40

Browse files
committed
util: Handle null prototype on inspect
1 parent 709b3b1 commit 96fdc40

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

test/parallel/test-util-inspect.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,15 @@ assert.strictEqual(
261261
name: { value: 'Tim', enumerable: true },
262262
hidden: { value: 'secret' }
263263
}), { showHidden: true }),
264-
"{ name: 'Tim', [hidden]: 'secret' }"
264+
"[Object: null prototype] { name: 'Tim', [hidden]: 'secret' }"
265265
);
266266

267267
assert.strictEqual(
268268
util.inspect(Object.create(null, {
269269
name: { value: 'Tim', enumerable: true },
270270
hidden: { value: 'secret' }
271271
})),
272-
"{ name: 'Tim' }"
272+
"[Object: null prototype] { name: 'Tim' }"
273273
);
274274

275275
// Dynamic properties.
@@ -505,11 +505,17 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
505505
set: function() {}
506506
}
507507
});
508-
assert.strictEqual(util.inspect(getter, true), '{ [a]: [Getter] }');
509-
assert.strictEqual(util.inspect(setter, true), '{ [b]: [Setter] }');
508+
assert.strictEqual(
509+
util.inspect(getter, true),
510+
'[Object: null prototype] { [a]: [Getter] }'
511+
);
512+
assert.strictEqual(
513+
util.inspect(setter, true),
514+
'[Object: null prototype] { [b]: [Setter] }'
515+
);
510516
assert.strictEqual(
511517
util.inspect(getterAndSetter, true),
512-
'{ [c]: [Getter/Setter] }'
518+
'[Object: null prototype] { [c]: [Getter/Setter] }'
513519
);
514520
}
515521

@@ -1084,7 +1090,7 @@ if (typeof Symbol !== 'undefined') {
10841090

10851091
{
10861092
const x = Object.create(null);
1087-
assert.strictEqual(util.inspect(x), '{}');
1093+
assert.strictEqual(util.inspect(x), '[Object: null prototype] {}');
10881094
}
10891095

10901096
{
@@ -1224,7 +1230,7 @@ util.inspect(process);
12241230

12251231
assert.strictEqual(util.inspect(
12261232
Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } })),
1227-
'[foo] {}');
1233+
'[Object: null prototype] [foo] {}');
12281234

12291235
assert.strictEqual(util.inspect(new Foo()), "Foo [bar] { foo: 'bar' }");
12301236

@@ -1574,20 +1580,12 @@ assert.strictEqual(util.inspect('"\''), '`"\'`');
15741580
// eslint-disable-next-line no-template-curly-in-string
15751581
assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
15761582

1577-
// Verify the output in case the value has no prototype.
1578-
// Sadly, these cases can not be fully inspected :(
1579-
[
1580-
[/a/, '/undefined/undefined'],
1581-
[new DataView(new ArrayBuffer(2)),
1582-
'DataView {\n byteLength: undefined,\n byteOffset: undefined,\n ' +
1583-
'buffer: undefined }'],
1584-
[new SharedArrayBuffer(2), 'SharedArrayBuffer { byteLength: undefined }']
1585-
].forEach(([value, expected]) => {
1583+
{
15861584
assert.strictEqual(
1587-
util.inspect(Object.setPrototypeOf(value, null)),
1588-
expected
1585+
util.inspect(Object.setPrototypeOf(/a/, null)),
1586+
'/undefined/undefined'
15891587
);
1590-
});
1588+
}
15911589

15921590
// Verify that throwing in valueOf and having no prototype still produces nice
15931591
// results.
@@ -1623,6 +1621,39 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
16231621
}
16241622
});
16251623
assert.strictEqual(util.inspect(value), expected);
1624+
value.foo = 'bar';
1625+
assert.notStrictEqual(util.inspect(value), expected);
1626+
delete value.foo;
1627+
value[Symbol('foo')] = 'yeah';
1628+
assert.notStrictEqual(util.inspect(value), expected);
1629+
});
1630+
1631+
[
1632+
[[1, 3, 4], '[Array: null prototype] [ 1, 3, 4 ]'],
1633+
[new Set([1, 2]), '[Set: null prototype] { 1, 2 }'],
1634+
[new Map([[1, 2]]), '[Map: null prototype] { 1 => 2 }'],
1635+
[new Promise((resolve) => setTimeout(resolve, 10)),
1636+
'[Promise: null prototype] { <pending> }'],
1637+
[new WeakSet(), '[WeakSet: null prototype] { <items unknown> }'],
1638+
[new WeakMap(), '[WeakMap: null prototype] { <items unknown> }'],
1639+
[new Uint8Array(2), '[Uint8Array: null prototype] [ 0, 0 ]'],
1640+
[new Uint16Array(2), '[Uint16Array: null prototype] [ 0, 0 ]'],
1641+
[new Uint32Array(2), '[Uint32Array: null prototype] [ 0, 0 ]'],
1642+
[new Int8Array(2), '[Int8Array: null prototype] [ 0, 0 ]'],
1643+
[new Int16Array(2), '[Int16Array: null prototype] [ 0, 0 ]'],
1644+
[new Int32Array(2), '[Int32Array: null prototype] [ 0, 0 ]'],
1645+
[new Float32Array(2), '[Float32Array: null prototype] [ 0, 0 ]'],
1646+
[new Float64Array(2), '[Float64Array: null prototype] [ 0, 0 ]'],
1647+
[new BigInt64Array(2), '[BigInt64Array: null prototype] [ 0n, 0n ]'],
1648+
[new BigUint64Array(2), '[BigUint64Array: null prototype] [ 0n, 0n ]'],
1649+
[new ArrayBuffer(16), '[ArrayBuffer: null prototype] ' +
1650+
'{ byteLength: undefined }'],
1651+
[new DataView(new ArrayBuffer(16)),
1652+
'[DataView: null prototype] {\n byteLength: undefined,\n ' +
1653+
'byteOffset: undefined,\n buffer: undefined }'],
1654+
[new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' +
1655+
'{ byteLength: undefined }']
1656+
].forEach(([value, expected]) => {
16261657
assert.strictEqual(
16271658
util.inspect(Object.setPrototypeOf(value, null)),
16281659
expected

0 commit comments

Comments
 (0)