Skip to content

Commit 0fbd4b1

Browse files
committed
util: improve iterator inspect output
1) So far extra keys on an (Set|Map)Iterator were ignored. Those will now be visible. 2) Improve the performance of showing (Set|Map)Iterator by using the cloned iterator instead of copying all entries first. 3) So far the output was strictly limited to up to 100 entries. The limit will now depend on `maxArrayLength` instead (that default is set to 100 as well) and the output indicates that more entries exist than visible. PR-URL: #19259 Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a101631 commit 0fbd4b1

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

lib/internal/bootstrap/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@
471471
// functions lazily (unless --nolazy is set) so we need to do this
472472
// before we turn off --allow_natives_syntax again.
473473
const v8 = NativeModule.require('internal/v8');
474-
v8.previewMapIterator(new Map().entries(), 1);
475-
v8.previewSetIterator(new Set().entries(), 1);
474+
v8.previewMapIterator(new Map().entries());
475+
v8.previewSetIterator(new Set().entries());
476476
// Disable --allow_natives_syntax again unless it was explicitly
477477
// specified on the command line.
478478
const re = /^--allow[-_]natives[-_]syntax$/;

lib/internal/v8.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict';
22

3-
function take(it, n) {
4-
const result = [];
5-
for (const e of it) {
6-
if (--n < 0)
7-
break;
8-
result.push(e);
9-
}
10-
return result;
11-
}
3+
// This file provides access to some of V8's native runtime functions. See
4+
// https://github.com/v8/v8/wiki/Built-in-functions for further information
5+
// about their implementation.
6+
// They have to be loaded before anything else to make sure we deactivate them
7+
// before executing any other code. Gaining access is achieved by using a
8+
// specific flag that is used internally in the startup phase.
129

13-
function previewMapIterator(it, n) {
14-
return take(%MapIteratorClone(it), n);
10+
// Clone the provided Map Iterator.
11+
function previewMapIterator(it) {
12+
return %MapIteratorClone(it);
1513
}
1614

17-
function previewSetIterator(it, n) {
18-
return take(%SetIteratorClone(it), n);
15+
// Clone the provided Set Iterator.
16+
function previewSetIterator(it) {
17+
return %SetIteratorClone(it);
1918
}
2019

21-
module.exports = { previewMapIterator, previewSetIterator };
20+
module.exports = {
21+
previewMapIterator,
22+
previewSetIterator
23+
};

lib/util.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ const {
3030
const { TextDecoder, TextEncoder } = require('internal/encoding');
3131
const { isBuffer } = require('buffer').Buffer;
3232

33-
const { previewMapIterator, previewSetIterator } = require('internal/v8');
33+
const {
34+
previewMapIterator,
35+
previewSetIterator
36+
} = require('internal/v8');
3437

3538
const {
3639
getPromiseDetails,
@@ -836,25 +839,29 @@ function formatMap(ctx, value, recurseTimes, keys) {
836839
return output;
837840
}
838841

839-
function formatCollectionIterator(preview, ctx, value, recurseTimes,
840-
visibleKeys, keys) {
841-
const nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
842-
const vals = preview(value, 100);
842+
function formatCollectionIterator(preview, ctx, value, recurseTimes, keys) {
843843
const output = [];
844-
for (const o of vals) {
845-
output.push(formatValue(ctx, o, nextRecurseTimes));
844+
for (const entry of preview(value)) {
845+
if (ctx.maxArrayLength === output.length) {
846+
output.push('... more items');
847+
break;
848+
}
849+
output.push(formatValue(ctx, entry, recurseTimes));
850+
}
851+
for (var n = 0; n < keys.length; n++) {
852+
output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0));
846853
}
847854
return output;
848855
}
849856

850-
function formatMapIterator(ctx, value, recurseTimes, visibleKeys, keys) {
857+
function formatMapIterator(ctx, value, recurseTimes, keys) {
851858
return formatCollectionIterator(previewMapIterator, ctx, value, recurseTimes,
852-
visibleKeys, keys);
859+
keys);
853860
}
854861

855-
function formatSetIterator(ctx, value, recurseTimes, visibleKeys, keys) {
862+
function formatSetIterator(ctx, value, recurseTimes, keys) {
856863
return formatCollectionIterator(previewSetIterator, ctx, value, recurseTimes,
857-
visibleKeys, keys);
864+
keys);
858865
}
859866

860867
function formatPromise(ctx, value, recurseTimes, keys) {

test/parallel/test-util-inspect.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
448448
{
449449
const map = new Map();
450450
map.set(1, 2);
451-
const vals = previewMapIterator(map.entries(), 100);
451+
const vals = previewMapIterator(map.entries());
452452
const valsOutput = [];
453453
for (const o of vals) {
454454
valsOutput.push(o);
@@ -924,6 +924,10 @@ if (typeof Symbol !== 'undefined') {
924924
const keys = map.keys();
925925
assert.strictEqual(util.inspect(keys), '[Map Iterator] { \'foo\' }');
926926
assert.strictEqual(util.inspect(keys), '[Map Iterator] { \'foo\' }');
927+
keys.extra = true;
928+
assert.strictEqual(
929+
util.inspect(keys, { maxArrayLength: 0 }),
930+
'[Map Iterator] { ... more items, extra: true }');
927931
}
928932

929933
// Test Set iterators.
@@ -937,6 +941,10 @@ if (typeof Symbol !== 'undefined') {
937941
const keys = aSet.keys();
938942
assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }');
939943
assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }');
944+
keys.extra = true;
945+
assert.strictEqual(
946+
util.inspect(keys, { maxArrayLength: 1 }),
947+
'[Set Iterator] { 1, ... more items, extra: true }');
940948
}
941949

942950
// Test alignment of items in container.

0 commit comments

Comments
 (0)