Skip to content

Commit

Permalink
[Fix] ES2024+: GetViewByteLength, TypedArrayByteLength, `TypedA…
Browse files Browse the repository at this point in the history
…rrayLength`: properly handle resizable arrays
  • Loading branch information
ljharb committed Jan 21, 2025
1 parent b39976e commit bfe049d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
6 changes: 4 additions & 2 deletions 2024/GetViewByteLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ module.exports = function GetViewByteLength(viewRecord) {

var view = viewRecord['[[Object]]']; // step 2

var viewByteLength = dataViewByteLength(view); // view.[[ByteLength]]
var isFixed = IsFixedLengthArrayBuffer(dataViewBuffer(view));

var viewByteLength = isFixed ? dataViewByteLength(view) : 'AUTO'; // view.[[ByteLength]]
if (viewByteLength !== 'AUTO') {
return viewByteLength; // step 3
}

if (IsFixedLengthArrayBuffer(dataViewBuffer(view))) {
if (isFixed) {
throw new $TypeError('Assertion failed: DataView’s ArrayBuffer is not fixed length'); // step 4
}

Expand Down
6 changes: 5 additions & 1 deletion 2024/TypedArrayByteLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

var $TypeError = require('es-errors/type');

var IsFixedLengthArrayBuffer = require('./IsFixedLengthArrayBuffer');
var IsTypedArrayOutOfBounds = require('./IsTypedArrayOutOfBounds');
var TypedArrayElementSize = require('./TypedArrayElementSize');
var TypedArrayLength = require('./TypedArrayLength');

var isTypedArrayWithBufferWitnessRecord = require('../helpers/records/typed-array-with-buffer-witness-record');

var typedArrayByffer = require('typed-array-buffer');
var typedArrayByteLength = require('typed-array-byte-length');

// https://262.ecma-international.org/15.0/#sec-typedarraybytelength
Expand All @@ -28,7 +30,9 @@ module.exports = function TypedArrayByteLength(taRecord) {

var O = taRecord['[[Object]]']; // step 4

var byteLength = typedArrayByteLength(O);
var isFixed = IsFixedLengthArrayBuffer(typedArrayByffer(O));

var byteLength = isFixed ? typedArrayByteLength(O) : 'AUTO';
if (byteLength !== 'AUTO') {
return byteLength; // step 5
}
Expand Down
8 changes: 5 additions & 3 deletions 2024/TypedArrayLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var typedArrayBuffer = require('typed-array-buffer');
var typedArrayByteOffset = require('typed-array-byte-offset');
var typedArrayLength = require('typed-array-length');

// http://www.ecma-international.org/ecma-262/15.0/#sec-typedarraylength
// https://www.ecma-international.org/ecma-262/15.0/#sec-typedarraylength

module.exports = function TypedArrayLength(taRecord) {
if (!isTypedArrayWithBufferWitnessRecord(taRecord)) {
Expand All @@ -26,12 +26,14 @@ module.exports = function TypedArrayLength(taRecord) {

var O = taRecord['[[Object]]']; // step 2

var length = typedArrayLength(O);
var isFixed = IsFixedLengthArrayBuffer(typedArrayBuffer(O));

var length = isFixed ? typedArrayLength(O) : 'AUTO';
if (length !== 'AUTO') {
return length; // step 3
}

if (IsFixedLengthArrayBuffer(typedArrayBuffer(O))) {
if (isFixed) {
throw new $TypeError('Assertion failed: array buffer is not fixed length'); // step 4
}

Expand Down
47 changes: 45 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18007,9 +18007,52 @@ var es2024 = function ES2024(ES, ops, expectedMissing, skips) {

// TODO: actual TA byteLength auto, but not fixed length? (may not be possible)

tat.test('non-fixed length, return floor((byteLength - byteOffset) / elementSize)', { todo: 'blocked on native resizable ABs/growable SABs' });
var elementSize = elementSizes['$' + type];

tat.test(
'non-fixed length, return floor((byteLength - byteOffset) / elementSize)',
{ skip: !('resizable' in ArrayBuffer.prototype) },
function (tsat) {
var rab = new ArrayBuffer(17, { maxByteLength: 64 });
var arr = new TA(rab, 8);
record = ES.MakeTypedArrayWithBufferWitnessRecord(arr, 'UNORDERED');

tsat.equal(
ES.TypedArrayLength(record),
Math.floor((17 - 8) / elementSize),
type + ' + resizable AB: has expected length'
);

tsat.end();
}
);

tat.test(
'non-fixed length, detached throws',
{ skip: !('resizable' in ArrayBuffer.prototype) || !canDetach },
function (tsat) {
var rab = new ArrayBuffer(17, { maxByteLength: 64 });
var arr = new TA(rab, 8);
record = ES.MakeTypedArrayWithBufferWitnessRecord(arr, 'UNORDERED');

ES.DetachArrayBuffer(rab);

tat.test('non-fixed length, detached throws', { todo: 'blocked on native resizable ABs/growable SABs' });
tsat['throws'](
function () { ES.TypedArrayLength(record); },
TypeError,
'detached RAB with a non-detached TAWBR throws'
);

record = ES.MakeTypedArrayWithBufferWitnessRecord(arr, 'UNORDERED');
tsat['throws'](
function () { ES.TypedArrayLength(record); },
TypeError,
'detached RAB with a detached TAWBR throws'
);

tsat.end();
}
);
});
});

Expand Down

0 comments on commit bfe049d

Please sign in to comment.