Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
25 changes: 25 additions & 0 deletions cli/tests/unit/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
const LONG_STRING =
"This is a really long string which will be abbreviated with ellipsis.";
Expand Down
39 changes: 23 additions & 16 deletions ext/console/01_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 247,17 @@ defineColorAlias("doubleunderline", "doubleUnderline");
// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.bytelength
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) {
return value !== null && typeof value === "object";
}
Expand Down Expand Up @@ -427,15 438,8 @@ export function isSetIterator(
export function isSharedArrayBuffer(
value,
) {
// TODO(kt3k): add SharedArrayBuffer to primordials
_getSharedArrayBufferByteLength ??= ObjectGetOwnPropertyDescriptor(
// deno-lint-ignore prefer-primordials
SharedArrayBuffer.prototype,
"byteLength",
).get;

try {
FunctionPrototypeCall(_getSharedArrayBufferByteLength, value);
getSharedArrayBufferByteLength(value);
return true;
} catch {
return false;
Expand Down Expand Up @@ -1607,7 1611,7 @@ const hexSliceLookupTable = function () {
}();

function hexSlice(buf, start, end) {
const len = buf.length;
const len = TypedArrayPrototypeGetLength(buf);
if (!start || start < 0) {
start = 0;
}
Expand All @@ -1623,21 1627,24 @@ function hexSlice(buf, start, end) {

const arrayBufferRegExp = new SafeRegExp("(.{2})", "g");
function formatArrayBuffer(ctx, value) {
let valLen;
try {
valLen = ArrayBufferPrototypeGetByteLength(value);
} catch {
valLen = getSharedArrayBufferByteLength(value);
}
const len = MathMin(MathMax(0, ctx.maxArrayLength), valLen);
let buffer;
try {
buffer = new Uint8Array(value);
buffer = new Uint8Array(value, 0, len);
} catch {
return [ctx.stylize("(detached)", "special")];
}
let str = StringPrototypeTrim(
StringPrototypeReplace(
hexSlice(buffer, 0, MathMin(ctx.maxArrayLength, buffer.length)),
arrayBufferRegExp,
"$1 ",
),
StringPrototypeReplace(hexSlice(buffer), arrayBufferRegExp, "$1 "),
);

const remaining = buffer.length - ctx.maxArrayLength;
const remaining = valLen - len;
if (remaining > 0) {
str = ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`;
}
Expand Down