Skip to content

Commit 1b24b37

Browse files
committed
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: #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>
1 parent 80b10b4 commit 1b24b37

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/util.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,13 @@ function formatValue(ctx, value, recurseTimes) {
487487
'byteOffset',
488488
'buffer');
489489
}
490-
} else if (simdFormatters &&
491-
typeof value.constructor === 'function' &&
492-
(formatter = simdFormatters.get(value.constructor))) {
493-
braces = ['[', ']'];
494490
} else {
495491
var promiseInternals = inspectPromise(value);
496492
if (promiseInternals) {
497493
braces = ['{', '}'];
498494
formatter = formatPromise;
499495
} else {
496+
let maybeSimdFormatter;
500497
if (binding.isMapIterator(value)) {
501498
constructor = { name: 'MapIterator' };
502499
braces = ['{', '}'];
@@ -507,6 +504,11 @@ function formatValue(ctx, value, recurseTimes) {
507504
braces = ['{', '}'];
508505
empty = false;
509506
formatter = formatCollectionIterator;
507+
} else if (simdFormatters &&
508+
typeof constructor === 'function' &&
509+
(maybeSimdFormatter = simdFormatters.get(constructor))) {
510+
braces = ['[', ']'];
511+
formatter = maybeSimdFormatter;
510512
} else {
511513
// Unset the constructor to prevent "Object {...}" for ordinary objects.
512514
if (constructor && constructor.name === 'Object')

test/parallel/test-util-format.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const symbol = Symbol('foo');
77
assert.equal(util.format(), '');
88
assert.equal(util.format(''), '');
99
assert.equal(util.format([]), '[]');
10+
assert.equal(util.format([0]), '[ 0 ]');
1011
assert.equal(util.format({}), '{}');
12+
assert.equal(util.format({foo: 42}), '{ foo: 42 }');
1113
assert.equal(util.format(null), 'null');
1214
assert.equal(util.format(true), 'true');
1315
assert.equal(util.format(false), 'false');

test/parallel/test-util-inspect-simd.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ if (typeof SIMD.Uint8x16 === 'function') {
5959
inspect(SIMD.Uint8x16()),
6060
'Uint8x16 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]');
6161
}
62+
63+
// Tests from test-inspect.js that should not fail with --harmony_simd.
64+
assert.strictEqual(inspect([]), '[]');
65+
assert.strictEqual(inspect([0]), '[ 0 ]');
66+
assert.strictEqual(inspect({}), '{}');
67+
assert.strictEqual(inspect({foo: 42}), '{ foo: 42 }');
68+
assert.strictEqual(inspect(null), 'null');
69+
assert.strictEqual(inspect(true), 'true');
70+
assert.strictEqual(inspect(false), 'false');

0 commit comments

Comments
 (0)