Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b5c51fd

Browse filesBrowse 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 <info@bnoordhuis.nl>
1 parent 546e833 commit b5c51fd
Copy full SHA for b5c51fd

File tree

Expand file treeCollapse file tree

2 files changed

+17
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-1
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+4-1Lines changed: 4 additions & 1 deletion
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;
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+13Lines changed: 13 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.