Skip to content

Commit

Permalink
util: improve prototype detection in .inspect()
Browse files Browse the repository at this point in the history
This makes sure the `null` prototype is always detected properly.
  • Loading branch information
BridgeAR authored and antsmartian committed Sep 27, 2018
1 parent 96fdc40 commit d14c3e6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
const common = require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const { JSStream } = internalBinding('js_stream');
const JSStream = process.binding('js_stream').JSStream;
const util = require('util');
const vm = require('vm');
const { previewEntries } = internalBinding('util');
Expand Down Expand Up @@ -1737,3 +1737,30 @@ assert.strictEqual(
'[ 3, 2, 1, [Symbol(a)]: false, [Symbol(b)]: true, a: 1, b: 2, c: 3 ]'
);
}

// Manipulate the prototype to one that we can not handle.
{
let obj = { a: true };
let value = (function() { return function() {}; })();
Object.setPrototypeOf(value, null);
Object.setPrototypeOf(obj, value);
assert.strictEqual(util.inspect(obj), '{ a: true }');

obj = { a: true };
value = [];
Object.setPrototypeOf(value, null);
Object.setPrototypeOf(obj, value);
assert.strictEqual(util.inspect(obj), '{ a: true }');
}

// Check that the fallback always works.
{
const obj = new Set([1, 2]);
const iterator = obj[Symbol.iterator];
Object.setPrototypeOf(obj, null);
Object.defineProperty(obj, Symbol.iterator, {
value: iterator,
configurable: true
});
assert.strictEqual(util.inspect(obj), '[Set: null prototype] { 1, 2 }');
}

0 comments on commit d14c3e6

Please sign in to comment.