GeezerVison is showing github.com — util: display constructor when inspecting objects · nodejs/node@ee9dd68
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/util.js +28-3Lines changed: 28 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -167,6 +167,22 @@ function arrayToHash(array) { | 167 | 167 | } | 168 | 168 |
| 169 | 169 |
| | 170 | +function getConstructorOf(obj) { | | 171 | + while (obj) { | | 172 | + var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor'); | | 173 | + if (descriptor !== undefined && | | 174 | + typeof descriptor.value === 'function' && | | 175 | + descriptor.value.name !== '') { | | 176 | + return descriptor.value; | | 177 | + } | | 178 | + | | 179 | + obj = Object.getPrototypeOf(obj); | | 180 | + } | | 181 | + | | 182 | + return null; | | 183 | +} | | 184 | + | | 185 | + | 170 | 186 | function inspectPromise(p) { | 171 | 187 | Debug = Debug || require('vm').runInDebugContext('Debug'); | 172 | 188 | var mirror = Debug.MakeMirror(p, true); | @@ -260,14 +276,17 @@ function formatValue(ctx, value, recurseTimes) { | 260 | 276 | } | 261 | 277 | } | 262 | 278 |
| | 279 | + var constructor = getConstructorOf(value); | 263 | 280 | var base = '', empty = false, braces, formatter; | 264 | 281 |
| 265 | 282 | if (Array.isArray(value)) { | | 283 | + if (constructor === Array) | | 284 | + constructor = null; | 266 | 285 | braces = ['[', ']']; | 267 | 286 | empty = value.length === 0; | 268 | 287 | formatter = formatArray; | 269 | 288 | } else if (value instanceof Set) { | 270 | | - braces = ['Set {', '}']; | | 289 | + braces = ['{', '}']; | 271 | 290 | // With `showHidden`, `length` will display as a hidden property for | 272 | 291 | // arrays. For consistency's sake, do the same for `size`, even though this | 273 | 292 | // property isn't selected by Object.getOwnPropertyNames(). | @@ -276,7 +295,7 @@ function formatValue(ctx, value, recurseTimes) { | 276 | 295 | empty = value.size === 0; | 277 | 296 | formatter = formatSet; | 278 | 297 | } else if (value instanceof Map) { | 279 | | - braces = ['Map {', '}']; | | 298 | + braces = ['{', '}']; | 280 | 299 | // Ditto. | 281 | 300 | if (ctx.showHidden) | 282 | 301 | keys.unshift('size'); | @@ -286,9 +305,11 @@ function formatValue(ctx, value, recurseTimes) { | 286 | 305 | // Only create a mirror if the object superficially looks like a Promise. | 287 | 306 | var promiseInternals = value instanceof Promise && inspectPromise(value); | 288 | 307 | if (promiseInternals) { | 289 | | - braces = ['Promise {', '}']; | | 308 | + braces = ['{', '}']; | 290 | 309 | formatter = formatPromise; | 291 | 310 | } else { | | 311 | + if (constructor === Object) | | 312 | + constructor = null; | 292 | 313 | braces = ['{', '}']; | 293 | 314 | empty = true; // No other data than keys. | 294 | 315 | formatter = formatObject; | @@ -336,6 +357,10 @@ function formatValue(ctx, value, recurseTimes) { | 336 | 357 | base = ' ' + '[Boolean: ' + formatted + ']'; | 337 | 358 | } | 338 | 359 |
| | 360 | + // Add constructor name if available | | 361 | + if (base === '' && constructor) | | 362 | + braces[0] = constructor.name + ' ' + braces[0]; | | 363 | + | 339 | 364 | if (empty === true) { | 340 | 365 | return braces[0] + base + braces[1]; | 341 | 366 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sys.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ assert.equal(new Date('2010-02-14T12:48:40+01:00').toString(), | 17 | 17 | assert.equal("'\\n\\u0001'", common.inspect('\n\u0001')); | 18 | 18 |
| 19 | 19 | assert.equal('[]', common.inspect([])); | 20 | | -assert.equal('{}', common.inspect(Object.create([]))); | | 20 | +assert.equal('Array {}', common.inspect(Object.create([]))); | 21 | 21 | assert.equal('[ 1, 2 ]', common.inspect([1, 2])); | 22 | 22 | assert.equal('[ 1, [ 2, 3 ] ]', common.inspect([1, [2, 3]])); | 23 | 23 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +42-1Lines changed: 42 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,7 +61,7 @@ assert.ok(ex.indexOf('[message]') != -1); | 61 | 61 |
| 62 | 62 | // GH-1941 | 63 | 63 | // should not throw: | 64 | | -assert.equal(util.inspect(Object.create(Date.prototype)), '{}'); | | 64 | +assert.equal(util.inspect(Object.create(Date.prototype)), 'Date {}'); | 65 | 65 |
| 66 | 66 | // GH-1944 | 67 | 67 | assert.doesNotThrow(function() { | @@ -306,3 +306,44 @@ checkAlignment(function() { | 306 | 306 | }()); | 307 | 307 | checkAlignment(new Set(big_array)); | 308 | 308 | checkAlignment(new Map(big_array.map(function(y) { return [y, null]; }))); | | 309 | + | | 310 | + | | 311 | +// Test display of constructors | | 312 | + | | 313 | +class ObjectSubclass {} | | 314 | +class ArraySubclass extends Array {} | | 315 | +class SetSubclass extends Set {} | | 316 | +class MapSubclass extends Map {} | | 317 | +class PromiseSubclass extends Promise {} | | 318 | + | | 319 | +var x = new ObjectSubclass(); | | 320 | +x.foo = 42; | | 321 | +assert.equal(util.inspect(x), | | 322 | + 'ObjectSubclass { foo: 42 }'); | | 323 | +assert.equal(util.inspect(new ArraySubclass(1, 2, 3)), | | 324 | + 'ArraySubclass [ 1, 2, 3 ]'); | | 325 | +assert.equal(util.inspect(new SetSubclass([1, 2, 3])), | | 326 | + 'SetSubclass { 1, 2, 3 }'); | | 327 | +assert.equal(util.inspect(new MapSubclass([['foo', 42]])), | | 328 | + 'MapSubclass { \'foo\' => 42 }'); | | 329 | +assert.equal(util.inspect(new PromiseSubclass(function() {})), | | 330 | + 'PromiseSubclass { <pending> }'); | | 331 | + | | 332 | +// Corner cases. | | 333 | +var x = { constructor: 42 }; | | 334 | +assert.equal(util.inspect(x), '{ constructor: 42 }'); | | 335 | + | | 336 | +var x = {}; | | 337 | +Object.defineProperty(x, 'constructor', { | | 338 | + get: function() { | | 339 | + throw new Error('should not access constructor'); | | 340 | + }, | | 341 | + enumerable: true | | 342 | +}); | | 343 | +assert.equal(util.inspect(x), '{ constructor: [Getter] }'); | | 344 | + | | 345 | +var x = new (function() {}); | | 346 | +assert.equal(util.inspect(x), '{}'); | | 347 | + | | 348 | +var x = Object.create(null); | | 349 | +assert.equal(util.inspect(x), '{}'); |
|