From 0a07201ca1e637f021d22c06e00e193193eaa3d5 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 25 Jul 2016 09:06:26 +0200 Subject: [PATCH] util: fix formatting of objects with SIMD enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Michaël Zasso --- lib/util.js | 10 ++++++---- test/parallel/test-util-format.js | 2 ++ test/parallel/test-util-inspect-simd.js | 9 +++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/util.js b/lib/util.js index bb588cf475e236..93313d443f4d05 100644 --- a/lib/util.js +++ b/lib/util.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') diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index f3702ef2f05547..b8977e57b5139c 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -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'); diff --git a/test/parallel/test-util-inspect-simd.js b/test/parallel/test-util-inspect-simd.js index 752ea3d0ce677c..ec4ccc1875a817 100644 --- a/test/parallel/test-util-inspect-simd.js +++ b/test/parallel/test-util-inspect-simd.js @@ -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');