util: fix check for Array constructor
In the event an Array is created in a Debug context, the constructor will be Array, but !== Array. This adds a check constructor.name === 'Array' to handle edge cases like that. PR-URL: https://github.com/nodejs/node/pull/3119 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
77a10ed05f
commit
089d688617
@ -292,7 +292,10 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
var base = '', empty = false, braces, formatter;
|
var base = '', empty = false, braces, formatter;
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
if (constructor === Array)
|
// We can't use `constructor === Array` because this could
|
||||||
|
// have come from a Debug context.
|
||||||
|
// Otherwise, an Array will print "Array [...]".
|
||||||
|
if (constructor && constructor.name === 'Array')
|
||||||
constructor = null;
|
constructor = null;
|
||||||
braces = ['[', ']'];
|
braces = ['[', ']'];
|
||||||
empty = value.length === 0;
|
empty = value.length === 0;
|
||||||
|
@ -23,6 +23,19 @@ assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');
|
|||||||
assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
|
assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
|
||||||
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
|
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
|
||||||
|
|
||||||
|
// test for Array constructor in different context
|
||||||
|
const Debug = require('vm').runInDebugContext('Debug');
|
||||||
|
var map = new Map();
|
||||||
|
map.set(1, 2);
|
||||||
|
var mirror = Debug.MakeMirror(map.entries(), true);
|
||||||
|
var vals = mirror.preview();
|
||||||
|
var valsOutput = [];
|
||||||
|
for (let o of vals) {
|
||||||
|
valsOutput.push(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.strictEqual(util.inspect(valsOutput), '[ [ 1, 2 ] ]');
|
||||||
|
|
||||||
// test for property descriptors
|
// test for property descriptors
|
||||||
var getter = Object.create(null, {
|
var getter = Object.create(null, {
|
||||||
a: {
|
a: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user