From 70cc5da0f11a024cf5be1ff20fd885556c1d2153 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 14 May 2018 17:30:27 +0200 Subject: lib,src: use V8 API for collection inspection Use a new public V8 API for inspecting weak collections and collection iterators, rather than using V8-internal functions to achieve this. This currently comes with a slight modification of the output for inspecting iterators generated by `Set().entries()`. Fixes: https://github.com/nodejs/node/issues/20409 PR-URL: https://github.com/nodejs/node/pull/20719 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- lib/util.js | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'lib/util.js') diff --git a/lib/util.js b/lib/util.js index bd7a98694b..f358adefdb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -30,18 +30,12 @@ const { const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; -const { - previewMapIterator, - previewSetIterator, - previewWeakMap, - previewWeakSet -} = require('internal/v8'); - const { getPromiseDetails, getProxyDetails, kPending, kRejected, + previewEntries } = process.binding('util'); const { internalBinding } = require('internal/bootstrap/loaders'); @@ -912,7 +906,7 @@ function formatMap(ctx, value, recurseTimes, keys) { function formatWeakSet(ctx, value, recurseTimes, keys) { const maxArrayLength = Math.max(ctx.maxArrayLength, 0); - const entries = previewWeakSet(value, maxArrayLength + 1); + 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) @@ -929,16 +923,14 @@ function formatWeakSet(ctx, value, recurseTimes, keys) { function formatWeakMap(ctx, value, recurseTimes, keys) { const maxArrayLength = Math.max(ctx.maxArrayLength, 0); - const entries = previewWeakMap(value, maxArrayLength + 1); - // Entries exist as [key1, val1, key2, val2, ...] - const remainder = entries.length / 2 > maxArrayLength; - const len = entries.length / 2 - (remainder ? 1 : 0); + const entries = previewEntries(value).slice(0, maxArrayLength + 1); + const remainder = entries.length > maxArrayLength; + const len = entries.length - (remainder ? 1 : 0); const maxLength = Math.min(maxArrayLength, len); let output = new Array(maxLength); for (var i = 0; i < len; i++) { - const pos = i * 2; - output[i] = `${formatValue(ctx, entries[pos], recurseTimes)} => ` + - formatValue(ctx, entries[pos + 1], recurseTimes); + output[i] = `${formatValue(ctx, entries[i][0], recurseTimes)} => ` + + formatValue(ctx, entries[i][1], 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). @@ -950,9 +942,9 @@ function formatWeakMap(ctx, value, recurseTimes, keys) { return output; } -function formatCollectionIterator(preview, ctx, value, recurseTimes, keys) { +function formatCollectionIterator(ctx, value, recurseTimes, keys) { const output = []; - for (const entry of preview(value)) { + for (const entry of previewEntries(value)) { if (ctx.maxArrayLength === output.length) { output.push('... more items'); break; @@ -966,13 +958,11 @@ function formatCollectionIterator(preview, ctx, value, recurseTimes, keys) { } function formatMapIterator(ctx, value, recurseTimes, keys) { - return formatCollectionIterator(previewMapIterator, ctx, value, recurseTimes, - keys); + return formatCollectionIterator(ctx, value, recurseTimes, keys); } function formatSetIterator(ctx, value, recurseTimes, keys) { - return formatCollectionIterator(previewSetIterator, ctx, value, recurseTimes, - keys); + return formatCollectionIterator(ctx, value, recurseTimes, keys); } function formatPromise(ctx, value, recurseTimes, keys) { -- cgit v1.2.3