Skip to content

Commit

Permalink
util: update set iterator entries inspection
Browse files Browse the repository at this point in the history
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: #25941
Fixes: #24629
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and rvagg committed Feb 28, 2019
1 parent 206e4b0 commit 4bf58ac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
15 changes: 5 additions & 10 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
Local<Array> entries;
if (!args[0].As<Object>()->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);

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }');
Expand Down Expand Up @@ -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 ]'],
Expand Down

0 comments on commit 4bf58ac

Please sign in to comment.