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
19 changes: 17 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
noIterator = true;
}
}
let isDV;
if (noIterator) {
keys = getKeys(value, ctx.showHidden);
braces = ['{', '}'];
Expand Down Expand Up @@ -1046,6 +1047,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
braces[0] = `${prefix}{`;
ArrayPrototypeUnshift(keys, 'byteLength');
} else if (isDataView(value)) {
isDV = true;
braces[0] = `${getPrefix(constructor, tag, 'DataView')}{`;
// .buffer goes last, it's not a primitive like the others.
ArrayPrototypeUnshift(keys, 'byteLength', 'byteOffset', 'buffer');
Expand Down Expand Up @@ -1099,6 +1101,20 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
try {
output = formatter(ctx, value, recurseTimes);
for (i = 0; i < keys.length; i++) {
isDV ??= isDataView(value);
if (
isDV && (keys[i] === 'byteLength' || keys[i] === 'byteOffset')
) {
try {
new Uint8Array(value.buffer);
} catch {
ArrayPrototypePush(
output,
formatValue(ctx, value.buffer.byteLength),
);
continue;
}
}
ArrayPrototypePush(
output,
formatProperty(ctx, value, recurseTimes, keys[i], extrasType),
Expand Down Expand Up @@ -1994,8 +2010,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
original = value) {
let name, str;
let extra = ' ';
desc ||= ObjectGetOwnPropertyDescriptor(value, key) ||
{ value: value[key], enumerable: true };
desc ||= ObjectGetOwnPropertyDescriptor(value, key) || { value: value[key], enumerable: true };
if (desc.value !== undefined) {
const diff = (ctx.compact !== true || type !== kObjectType) ? 2 : 3;
ctx.indentationLvl += diff;
Expand Down
19 changes: 15 additions & 4 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,20 @@ assert.doesNotMatch(

{
const ab = new ArrayBuffer(42);
const dv = new DataView(ab);

assert.strictEqual(ab.byteLength, 42);
new MessageChannel().port1.postMessage(ab, [ ab ]);
assert.strictEqual(ab.byteLength, 0);
assert.strictEqual(util.inspect(ab),
'ArrayBuffer { (detached), byteLength: 0 }');
assert.strictEqual(
util.inspect(ab),
'ArrayBuffer { (detached), byteLength: 0 }',
);

assert.strictEqual(
util.inspect(dv),
'DataView { 0, 0, buffer: ArrayBuffer { (detached), byteLength: 0 } }',
);
}

// Truncate output for ArrayBuffers using plural or singular bytes
Expand Down Expand Up @@ -294,15 +303,17 @@ assert.doesNotMatch(
});

// Now check that declaring a TypedArray in a different context works the same.
[ Float32Array,
[
Float32Array,
Float64Array,
Int16Array,
Int32Array,
Int8Array,
Uint16Array,
Uint32Array,
Uint8Array,
Uint8ClampedArray ].forEach((constructor) => {
Uint8ClampedArray,
].forEach((constructor) => {
const length = 2;
const byteLength = length * constructor.BYTES_PER_ELEMENT;
const array = vm.runInNewContext(
Expand Down
Loading