From 4bf58ac13dc9421cabac936bb702a75da916716e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 5 Feb 2019 04:17:52 +0100 Subject: [PATCH] util: update set iterator entries inspection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The inspection output for Set#entries() was wrong so far as it did not return an array as it should have. That was a bug in V8 that is now fixed and the code in Node.js has to be updated accordingly. PR-URL: https://github.com/nodejs/node/pull/25941 Fixes: https://github.com/nodejs/node/issues/24629 Reviewed-By: Michaƫl Zasso --- lib/internal/util/inspect.js | 15 +++++---------- src/node_util.cc | 2 +- test/parallel/test-util-inspect.js | 5 +++-- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 0f81ab5d0296e9..31b9e37ef71137 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -594,11 +594,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { } else if (isMapIterator(value)) { keys = getKeys(value, ctx.showHidden); braces = [`[${tag}] {`, '}']; - formatter = formatMapIterator; + formatter = formatIterator; } else if (isSetIterator(value)) { keys = getKeys(value, ctx.showHidden); braces = [`[${tag}] {`, '}']; - formatter = formatSetIterator; + formatter = formatIterator; } else { noIterator = true; } @@ -730,10 +730,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { } if (isMapIterator(value)) { braces = [`[${tag || 'Map Iterator'}] {`, '}']; - formatter = formatMapIterator; + formatter = formatIterator; } else if (isSetIterator(value)) { braces = [`[${tag || 'Set Iterator'}] {`, '}']; - formatter = formatSetIterator; + formatter = formatIterator; // Handle other regular objects again. } else if (keys.length === 0) { if (isExternal(value)) @@ -1090,12 +1090,7 @@ function formatWeakMap(ctx, value, recurseTimes) { return formatMapIterInner(ctx, recurseTimes, entries, kWeak); } -function formatSetIterator(ctx, value, recurseTimes) { - const entries = previewEntries(value); - return formatSetIterInner(ctx, recurseTimes, entries, kIterator); -} - -function formatMapIterator(ctx, value, recurseTimes) { +function formatIterator(ctx, value, recurseTimes) { const [entries, isKeyValue] = previewEntries(value, true); if (isKeyValue) { return formatMapIterInner(ctx, recurseTimes, entries, kMapEntries); diff --git a/src/node_util.cc b/src/node_util.cc index 4cca0cbb72aed0..f2c008c797d61b 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -100,7 +100,7 @@ static void PreviewEntries(const FunctionCallbackInfo& args) { Local entries; if (!args[0].As()->PreviewEntries(&is_key_value).ToLocal(&entries)) return; - // Fast path for WeakMap, WeakSet and Set iterators. + // Fast path for WeakMap and WeakSet. if (args.Length() == 1) return args.GetReturnValue().Set(entries); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 76f8caa31a88b8..a9afdbf8af049f 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -995,7 +995,8 @@ if (typeof Symbol !== 'undefined') { const aSet = new Set([1, 3]); assert.strictEqual(util.inspect(aSet.keys()), '[Set Iterator] { 1, 3 }'); assert.strictEqual(util.inspect(aSet.values()), '[Set Iterator] { 1, 3 }'); - assert.strictEqual(util.inspect(aSet.entries()), '[Set Iterator] { 1, 3 }'); + assert.strictEqual(util.inspect(aSet.entries()), + '[Set Iterator] { [ 1, 1 ], [ 3, 3 ] }'); // Make sure the iterator doesn't get consumed. const keys = aSet.keys(); assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }'); @@ -1609,7 +1610,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); [{ a: 5 }, '{ a: 5 }'], [new Set([1, 2]), 'Set { 1, 2 }'], [new Map([[1, 2]]), 'Map { 1 => 2 }'], - [new Set([1, 2]).entries(), '[Set Iterator] { 1, 2 }'], + [new Set([1, 2]).entries(), '[Set Iterator] { [ 1, 1 ], [ 2, 2 ] }'], [new Map([[1, 2]]).keys(), '[Map Iterator] { 1 }'], [new Date(2000), '1970-01-01T00:00:02.000Z'], [new Uint8Array(2), 'Uint8Array [ 0, 0 ]'],