| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent aaeced9 commit c93e267
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -198,6 +198,7 @@ function ensureDebugIsInitialized() { | |||
| 198 | 198 | ||
| 199 | 199 | function inspectPromise(p) { | |
| 200 | 200 | ensureDebugIsInitialized(); | |
| 201 | + // Only create a mirror if the object is a Promise. | ||
| 201 | 202 | if (!binding.isPromise(p)) | |
| 202 | 203 | return null; | |
| 203 | 204 | const mirror = Debug.MakeMirror(p, true); | |
@@ -292,16 +293,19 @@ function formatValue(ctx, value, recurseTimes) { | |||
| 292 | 293 | var constructor = getConstructorOf(value); | |
| 293 | 294 | var base = '', empty = false, braces, formatter; | |
| 294 | 295 | ||
| 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. | ||
| 295 | 301 | 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. | ||
| 299 | 303 | if (constructor && constructor.name === 'Array') | |
| 300 | 304 | constructor = null; | |
| 301 | 305 | braces = ['[', ']']; | |
| 302 | 306 | empty = value.length === 0; | |
| 303 | 307 | formatter = formatArray; | |
| 304 | - } else if (value instanceof Set) { | ||
| 308 | + } else if (objectToString(value) === '[object Set]') { | ||
| 305 | 309 | braces = ['{', '}']; | |
| 306 | 310 | // With `showHidden`, `length` will display as a hidden property for | |
| 307 | 311 | // arrays. For consistency's sake, do the same for `size`, even though this | |
@@ -310,16 +314,15 @@ function formatValue(ctx, value, recurseTimes) { | |||
| 310 | 314 | keys.unshift('size'); | |
| 311 | 315 | empty = value.size === 0; | |
| 312 | 316 | formatter = formatSet; | |
| 313 | - } else if (value instanceof Map) { | ||
| 317 | + } else if (objectToString(value) === '[object Map]') { | ||
| 314 | 318 | braces = ['{', '}']; | |
| 315 | 319 | // Ditto. | |
| 316 | 320 | if (ctx.showHidden) | |
| 317 | 321 | keys.unshift('size'); | |
| 318 | 322 | empty = value.size === 0; | |
| 319 | 323 | formatter = formatMap; | |
| 320 | 324 | } 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); | ||
| 323 | 326 | if (promiseInternals) { | |
| 324 | 327 | braces = ['{', '}']; | |
| 325 | 328 | formatter = formatPromise; | |
@@ -335,7 +338,8 @@ function formatValue(ctx, value, recurseTimes) { | |||
| 335 | 338 | empty = false; | |
| 336 | 339 | formatter = formatCollectionIterator; | |
| 337 | 340 | } else { | |
| 338 | - if (constructor === Object) | ||
| 341 | + // Unset the constructor to prevent "Object {...}" for ordinary objects. | ||
| 342 | + if (constructor && constructor.name === 'Object') | ||
| 339 | 343 | constructor = null; | |
| 340 | 344 | braces = ['{', '}']; | |
| 341 | 345 | empty = true; // No other data than keys. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -141,6 +141,16 @@ for (const o of vals) { | |||
| 141 | 141 | ||
| 142 | 142 | assert.strictEqual(util.inspect(valsOutput), '[ [ 1, 2 ] ]'); | |
| 143 | 143 | ||
| 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 | + | ||
| 144 | 154 | // test for property descriptors | |
| 145 | 155 | var getter = Object.create(null, { | |
| 146 | 156 | a: { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments