Skip to content

Commit b5c51fd

Browse files
evanlucasjasnell
authored andcommitted
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: #3119 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 546e833 commit b5c51fd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/util.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ function formatValue(ctx, value, recurseTimes) {
292292
var base = '', empty = false, braces, formatter;
293293

294294
if (Array.isArray(value)) {
295-
if (constructor === Array)
295+
// We can't use `constructor === Array` because this could
296+
// have come from a Debug context.
297+
// Otherwise, an Array will print "Array [...]".
298+
if (constructor && constructor.name === 'Array')
296299
constructor = null;
297300
braces = ['[', ']'];
298301
empty = value.length === 0;

test/parallel/test-util-inspect.js

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');
2323
assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
2424
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
2525

26+
// test for Array constructor in different context
27+
const Debug = require('vm').runInDebugContext('Debug');
28+
var map = new Map();
29+
map.set(1, 2);
30+
var mirror = Debug.MakeMirror(map.entries(), true);
31+
var vals = mirror.preview();
32+
var valsOutput = [];
33+
for (let o of vals) {
34+
valsOutput.push(o);
35+
}
36+
37+
assert.strictEqual(util.inspect(valsOutput), '[ [ 1, 2 ] ]');
38+
2639
// test for property descriptors
2740
var getter = Object.create(null, {
2841
a: {

0 commit comments

Comments
 (0)