Skip to content

Commit c93e267

Browse files
mscdexrvagg
authored andcommitted
util: fix constructor/instanceof checks
These new checks are similar to the one introduced in 089d688, but for other types of objects. Specifically, if an object was created in a different context, the constructor object will not be the same as the constructor object in the current context, so we have to compare constructor names instead. PR-URL: #3385 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent aaeced9 commit c93e267

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/util.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ function ensureDebugIsInitialized() {
198198

199199
function inspectPromise(p) {
200200
ensureDebugIsInitialized();
201+
// Only create a mirror if the object is a Promise.
201202
if (!binding.isPromise(p))
202203
return null;
203204
const mirror = Debug.MakeMirror(p, true);
@@ -292,16 +293,19 @@ function formatValue(ctx, value, recurseTimes) {
292293
var constructor = getConstructorOf(value);
293294
var base = '', empty = false, braces, formatter;
294295

296+
// We can't compare constructors for various objects using a comparison like
297+
// `constructor === Array` because the object could have come from a different
298+
// context and thus the constructor won't match. Instead we check the
299+
// constructor names (including those up the prototype chain where needed) to
300+
// determine object types.
295301
if (Array.isArray(value)) {
296-
// We can't use `constructor === Array` because this could
297-
// have come from a Debug context.
298-
// Otherwise, an Array will print "Array [...]".
302+
// Unset the constructor to prevent "Array [...]" for ordinary arrays.
299303
if (constructor && constructor.name === 'Array')
300304
constructor = null;
301305
braces = ['[', ']'];
302306
empty = value.length === 0;
303307
formatter = formatArray;
304-
} else if (value instanceof Set) {
308+
} else if (objectToString(value) === '[object Set]') {
305309
braces = ['{', '}'];
306310
// With `showHidden`, `length` will display as a hidden property for
307311
// arrays. For consistency's sake, do the same for `size`, even though this
@@ -310,16 +314,15 @@ function formatValue(ctx, value, recurseTimes) {
310314
keys.unshift('size');
311315
empty = value.size === 0;
312316
formatter = formatSet;
313-
} else if (value instanceof Map) {
317+
} else if (objectToString(value) === '[object Map]') {
314318
braces = ['{', '}'];
315319
// Ditto.
316320
if (ctx.showHidden)
317321
keys.unshift('size');
318322
empty = value.size === 0;
319323
formatter = formatMap;
320324
} else {
321-
// Only create a mirror if the object superficially looks like a Promise.
322-
var promiseInternals = value instanceof Promise && inspectPromise(value);
325+
var promiseInternals = inspectPromise(value);
323326
if (promiseInternals) {
324327
braces = ['{', '}'];
325328
formatter = formatPromise;
@@ -335,7 +338,8 @@ function formatValue(ctx, value, recurseTimes) {
335338
empty = false;
336339
formatter = formatCollectionIterator;
337340
} else {
338-
if (constructor === Object)
341+
// Unset the constructor to prevent "Object {...}" for ordinary objects.
342+
if (constructor && constructor.name === 'Object')
339343
constructor = null;
340344
braces = ['{', '}'];
341345
empty = true; // No other data than keys.

test/parallel/test-util-inspect.js

+10
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ for (const o of vals) {
141141

142142
assert.strictEqual(util.inspect(valsOutput), '[ [ 1, 2 ] ]');
143143

144+
// test for other constructors in different context
145+
var obj = require('vm').runInNewContext('(function(){return {}})()', {});
146+
assert.strictEqual(util.inspect(obj), '{}');
147+
obj = require('vm').runInNewContext('var m=new Map();m.set(1,2);m', {});
148+
assert.strictEqual(util.inspect(obj), 'Map { 1 => 2 }');
149+
obj = require('vm').runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
150+
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
151+
obj = require('vm').runInNewContext('fn=function(){};new Promise(fn,fn)', {});
152+
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
153+
144154
// test for property descriptors
145155
var getter = Object.create(null, {
146156
a: {

0 commit comments

Comments
 (0)