Skip to content

Commit d14c3e6

Browse files
BridgeARantsmartian
authored andcommitted
util: improve prototype detection in .inspect()
This makes sure the `null` prototype is always detected properly.
1 parent 96fdc40 commit d14c3e6

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

test/parallel/test-util-inspect.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
const common = require('../common');
2626
const assert = require('assert');
2727
const { internalBinding } = require('internal/test/binding');
28-
const { JSStream } = internalBinding('js_stream');
28+
const JSStream = process.binding('js_stream').JSStream;
2929
const util = require('util');
3030
const vm = require('vm');
3131
const { previewEntries } = internalBinding('util');
@@ -1737,3 +1737,30 @@ assert.strictEqual(
17371737
'[ 3, 2, 1, [Symbol(a)]: false, [Symbol(b)]: true, a: 1, b: 2, c: 3 ]'
17381738
);
17391739
}
1740+
1741+
// Manipulate the prototype to one that we can not handle.
1742+
{
1743+
let obj = { a: true };
1744+
let value = (function() { return function() {}; })();
1745+
Object.setPrototypeOf(value, null);
1746+
Object.setPrototypeOf(obj, value);
1747+
assert.strictEqual(util.inspect(obj), '{ a: true }');
1748+
1749+
obj = { a: true };
1750+
value = [];
1751+
Object.setPrototypeOf(value, null);
1752+
Object.setPrototypeOf(obj, value);
1753+
assert.strictEqual(util.inspect(obj), '{ a: true }');
1754+
}
1755+
1756+
// Check that the fallback always works.
1757+
{
1758+
const obj = new Set([1, 2]);
1759+
const iterator = obj[Symbol.iterator];
1760+
Object.setPrototypeOf(obj, null);
1761+
Object.defineProperty(obj, Symbol.iterator, {
1762+
value: iterator,
1763+
configurable: true
1764+
});
1765+
assert.strictEqual(util.inspect(obj), '[Set: null prototype] { 1, 2 }');
1766+
}

0 commit comments

Comments
 (0)