Browse Source
util: fix formatting of objects with SIMD enabled
When SIMD is enabled, `util.format` couldn’t display objects
(with at least 1 key) because the formatter function got
overridden.
PR-URL: https://github.com/nodejs/node/pull/7864
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
v7.x
Anna Henningsen
9 years ago
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
3 changed files with
17 additions and
4 deletions
lib/util.js
test/parallel/test-util-format.js
test/parallel/test-util-inspect-simd.js
@ -487,16 +487,13 @@ function formatValue(ctx, value, recurseTimes) {
'byteOffset' ,
'buffer' ) ;
}
} else if ( simdFormatters &&
typeof value . constructor === 'function' &&
( formatter = simdFormatters . get ( value . constructor ) ) ) {
braces = [ '[' , ']' ] ;
} else {
var promiseInternals = inspectPromise ( value ) ;
if ( promiseInternals ) {
braces = [ '{' , '}' ] ;
formatter = formatPromise ;
} else {
let maybeSimdFormatter ;
if ( binding . isMapIterator ( value ) ) {
constructor = { name : 'MapIterator' } ;
braces = [ '{' , '}' ] ;
@ -507,6 +504,11 @@ function formatValue(ctx, value, recurseTimes) {
braces = [ '{' , '}' ] ;
empty = false ;
formatter = formatCollectionIterator ;
} else if ( simdFormatters &&
typeof constructor === 'function' &&
( maybeSimdFormatter = simdFormatters . get ( constructor ) ) ) {
braces = [ '[' , ']' ] ;
formatter = maybeSimdFormatter ;
} else {
// Unset the constructor to prevent "Object {...}" for ordinary objects.
if ( constructor && constructor . name === 'Object' )
@ -7,7 +7,9 @@ const symbol = Symbol('foo');
assert . equal ( util . format ( ) , '' ) ;
assert . equal ( util . format ( '' ) , '' ) ;
assert . equal ( util . format ( [ ] ) , '[]' ) ;
assert . equal ( util . format ( [ 0 ] ) , '[ 0 ]' ) ;
assert . equal ( util . format ( { } ) , '{}' ) ;
assert . equal ( util . format ( { foo : 42 } ) , '{ foo: 42 }' ) ;
assert . equal ( util . format ( null ) , 'null' ) ;
assert . equal ( util . format ( true ) , 'true' ) ;
assert . equal ( util . format ( false ) , 'false' ) ;
@ -59,3 +59,12 @@ if (typeof SIMD.Uint8x16 === 'function') {
inspect ( SIMD . Uint8x16 ( ) ) ,
'Uint8x16 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]' ) ;
}
// Tests from test-inspect.js that should not fail with --harmony_simd.
assert . strictEqual ( inspect ( [ ] ) , '[]' ) ;
assert . strictEqual ( inspect ( [ 0 ] ) , '[ 0 ]' ) ;
assert . strictEqual ( inspect ( { } ) , '{}' ) ;
assert . strictEqual ( inspect ( { foo : 42 } ) , '{ foo: 42 }' ) ;
assert . strictEqual ( inspect ( null ) , 'null' ) ;
assert . strictEqual ( inspect ( true ) , 'true' ) ;
assert . strictEqual ( inspect ( false ) , 'false' ) ;