util: improve display of iterators and weak entries by BridgeAR · Pull Request #20961 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/console.js
98 changes: 54 additions & 44 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ const meta = [
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '\\\\'
];
// Constants to map the iterator state.
const kWeak = 0;
const kIterator = 1;
const kMapEntries = 2;

function addQuotes(str, quotes) {
if (quotes === -1) {
Expand Down Expand Up @@ -1017,77 +1021,83 @@ function formatMap(ctx, value, recurseTimes, keys) {
return output;
}

function formatWeakSet(ctx, value, recurseTimes, keys) {
function formatSetIterInner(ctx, value, recurseTimes, keys, entries, state) {
const maxArrayLength = Math.max(ctx.maxArrayLength, 0);
const [ entries ] = previewEntries(value).slice(0, maxArrayLength + 1);
const maxLength = Math.min(maxArrayLength, entries.length);
let output = new Array(maxLength);
for (var i = 0; i < maxLength; ++i)
output[i] = formatValue(ctx, entries[i], recurseTimes);
// Sort all entries to have a halfway reliable output (if more entries than
// retrieved ones exist, we can not reliably return the same output).
output = output.sort();
if (entries.length > maxArrayLength)
output.push('... more items');
if (state === kWeak) {
// Sort all entries to have a halfway reliable output (if more entries than
// retrieved ones exist, we can not reliably return the same output).
output = output.sort();
}
const remaining = entries.length - maxLength;
if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
for (i = 0; i < keys.length; i++)
output.push(formatProperty(ctx, value, recurseTimes, keys[i], 0));
return output;
}

function formatWeakMap(ctx, value, recurseTimes, keys) {
function formatMapIterInner(ctx, value, recurseTimes, keys, entries, state) {
const maxArrayLength = Math.max(ctx.maxArrayLength, 0);
const [ entries ] = previewEntries(value).slice(0, (maxArrayLength + 1) * 2);
// Entries exist as [key1, val1, key2, val2, ...]
const remainder = entries.length / 2 > maxArrayLength;
const len = entries.length / 2 - (remainder ? 1 : 0);
const len = entries.length / 2;
const remaining = len - maxArrayLength;
const maxLength = Math.min(maxArrayLength, len);
let output = new Array(maxLength);
for (var i = 0; i < maxLength; i++) {
let start = '';
let end = '';
let middle = ' => ';
let i = 0;
if (state === kMapEntries) {
start = '[ ';
end = ' ]';
middle = ', ';
}
for (; i < maxLength; i++) {
const pos = i * 2;
output[i] = `${formatValue(ctx, entries[pos], recurseTimes)} => ` +
formatValue(ctx, entries[pos + 1], recurseTimes);
output[i] = `${start}${formatValue(ctx, entries[pos], recurseTimes)}` +
`${middle}${formatValue(ctx, entries[pos + 1], recurseTimes)}${end}`;
}
if (state === kWeak) {
// Sort all entries to have a halfway reliable output (if more entries
// than retrieved ones exist, we can not reliably return the same output).
output = output.sort();
}
if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
// Sort all entries to have a halfway reliable output (if more entries than
// retrieved ones exist, we can not reliably return the same output).
output = output.sort();
if (remainder > 0)
output.push('... more items');
for (i = 0; i < keys.length; i++)
output.push(formatProperty(ctx, value, recurseTimes, keys[i], 0));
return output;
}

function zip2(list) {
const ret = Array(list.length / 2);
for (var i = 0; i < ret.length; ++i)
ret[i] = [list[2 * i], list[2 * i + 1]];
return ret;
function formatWeakSet(ctx, value, recurseTimes, keys) {
const entries = previewEntries(value);
return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kWeak);
}

function formatCollectionIterator(ctx, value, recurseTimes, keys) {
const output = [];
var [ entries, isKeyValue ] = previewEntries(value);
if (isKeyValue)
entries = zip2(entries);
for (const entry of entries) {
if (ctx.maxArrayLength === output.length) {
output.push('... more items');
break;
}
output.push(formatValue(ctx, entry, recurseTimes));
}
for (var n = 0; n < keys.length; n++) {
output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0));
}
return output;
function formatWeakMap(ctx, value, recurseTimes, keys) {
const entries = previewEntries(value);
return formatMapIterInner(ctx, value, recurseTimes, keys, entries, kWeak);
}

function formatMapIterator(ctx, value, recurseTimes, keys) {
return formatCollectionIterator(ctx, value, recurseTimes, keys);
function formatSetIterator(ctx, value, recurseTimes, keys) {
const entries = previewEntries(value);
return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kIterator);
}

function formatSetIterator(ctx, value, recurseTimes, keys) {
return formatCollectionIterator(ctx, value, recurseTimes, keys);
function formatMapIterator(ctx, value, recurseTimes, keys) {
const [entries, isKeyValue] = previewEntries(value, true);
if (isKeyValue) {
return formatMapIterInner(
ctx, value, recurseTimes, keys, entries, kMapEntries);
}

return formatSetIterInner(ctx, value, recurseTimes, keys, entries, kIterator);
}

function formatPromise(ctx, value, recurseTimes, keys) {
Expand Down
3 changes: 3 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ 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.
if (args.Length() == 1)
return args.GetReturnValue().Set(entries);
Local<Array> ret = Array::New(env->isolate(), 2);
ret->Set(env->context(), 0, entries).FromJust();
ret->Set(env->context(), 1, v8::Boolean::New(env->isolate(), is_key_value))
Expand Down
49 changes: 26 additions & 23 deletions test/parallel/test-util-inspect.js