1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

fix(ext/console): fix inspecting large ArrayBuffers (#19373)

This commit is contained in:
ud2 2023-06-06 17:06:00 +08:00 committed by Bartek Iwańczuk
parent 311f416c25
commit 05282308ff
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 48 additions and 16 deletions

View file

@ -2193,6 +2193,31 @@ Deno.test(function inspectEmptyUint8Array() {
); );
}); });
Deno.test(function inspectLargeArrayBuffer() {
const arrayBuffer = new ArrayBuffer(2 ** 32 + 1);
assertEquals(
Deno.inspect(arrayBuffer),
`ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 4294967197 more bytes>,
byteLength: 4294967297
}`,
);
structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
assertEquals(
Deno.inspect(arrayBuffer),
"ArrayBuffer { (detached), byteLength: 0 }",
);
const sharedArrayBuffer = new SharedArrayBuffer(2 ** 32 + 1);
assertEquals(
Deno.inspect(sharedArrayBuffer),
`SharedArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 4294967197 more bytes>,
byteLength: 4294967297
}`,
);
});
Deno.test(function inspectStringAbbreviation() { Deno.test(function inspectStringAbbreviation() {
const LONG_STRING = const LONG_STRING =
"This is a really long string which will be abbreviated with ellipsis."; "This is a really long string which will be abbreviated with ellipsis.";

View file

@ -248,6 +248,17 @@ defineColorAlias("doubleunderline", "doubleUnderline");
// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.bytelength // https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.bytelength
let _getSharedArrayBufferByteLength; let _getSharedArrayBufferByteLength;
function getSharedArrayBufferByteLength(value) {
// TODO(kt3k): add SharedArrayBuffer to primordials
_getSharedArrayBufferByteLength ??= ObjectGetOwnPropertyDescriptor(
// deno-lint-ignore prefer-primordials
SharedArrayBuffer.prototype,
"byteLength",
).get;
return FunctionPrototypeCall(_getSharedArrayBufferByteLength, value);
}
function isObjectLike(value) { function isObjectLike(value) {
return value !== null && typeof value === "object"; return value !== null && typeof value === "object";
} }
@ -428,15 +439,8 @@ export function isSetIterator(
export function isSharedArrayBuffer( export function isSharedArrayBuffer(
value, value,
) { ) {
// TODO(kt3k): add SharedArrayBuffer to primordials
_getSharedArrayBufferByteLength ??= ObjectGetOwnPropertyDescriptor(
// deno-lint-ignore prefer-primordials
SharedArrayBuffer.prototype,
"byteLength",
).get;
try { try {
FunctionPrototypeCall(_getSharedArrayBufferByteLength, value); getSharedArrayBufferByteLength(value);
return true; return true;
} catch { } catch {
return false; return false;
@ -1608,7 +1612,7 @@ const hexSliceLookupTable = function () {
}(); }();
function hexSlice(buf, start, end) { function hexSlice(buf, start, end) {
const len = buf.length; const len = TypedArrayPrototypeGetLength(buf);
if (!start || start < 0) { if (!start || start < 0) {
start = 0; start = 0;
} }
@ -1624,21 +1628,24 @@ function hexSlice(buf, start, end) {
const arrayBufferRegExp = new SafeRegExp("(.{2})", "g"); const arrayBufferRegExp = new SafeRegExp("(.{2})", "g");
function formatArrayBuffer(ctx, value) { function formatArrayBuffer(ctx, value) {
let valLen;
try {
valLen = ArrayBufferPrototypeGetByteLength(value);
} catch {
valLen = getSharedArrayBufferByteLength(value);
}
const len = MathMin(MathMax(0, ctx.maxArrayLength), valLen);
let buffer; let buffer;
try { try {
buffer = new Uint8Array(value); buffer = new Uint8Array(value, 0, len);
} catch { } catch {
return [ctx.stylize("(detached)", "special")]; return [ctx.stylize("(detached)", "special")];
} }
let str = StringPrototypeTrim( let str = StringPrototypeTrim(
StringPrototypeReplace( StringPrototypeReplace(hexSlice(buffer), arrayBufferRegExp, "$1 "),
hexSlice(buffer, 0, MathMin(ctx.maxArrayLength, buffer.length)),
arrayBufferRegExp,
"$1 ",
),
); );
const remaining = buffer.length - ctx.maxArrayLength; const remaining = valLen - len;
if (remaining > 0) { if (remaining > 0) {
str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`; str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`;
} }