Skip to content

Commit

Permalink
util: fix formatting of objects with SIMD enabled
Browse files Browse the repository at this point in the history
When SIMD is enabled, `util.format` couldn’t display objects
(with at least 1 key) because the formatter function got
overridden.

PR-URL: #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>
  • Loading branch information
addaleax committed Aug 2, 2016
1 parent 80b10b4 commit 1b24b37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['{', '}'];
Expand All @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-util-inspect-simd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 1b24b37

Please sign in to comment.