Skip to content

Commit

Permalink
[Fix] ES2024+: ArrayBufferByteLength: return the byte length for …
Browse files Browse the repository at this point in the history
…SABs, not NaN
  • Loading branch information
ljharb committed Jan 19, 2025
1 parent 12f1331 commit 0a25934
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 2024/ArrayBufferByteLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var isArrayBuffer = require('is-array-buffer');
var isSharedArrayBuffer = require('is-shared-array-buffer');
var arrayBufferByteLength = require('array-buffer-byte-length');

var callBound = require('call-bound');
var $sabByteLength = callBound('SharedArrayBuffer.prototype.byteLength', true);

var isGrowable = false; // TODO: support this

module.exports = function ArrayBufferByteLength(arrayBuffer, order) {
Expand All @@ -34,5 +37,5 @@ module.exports = function ArrayBufferByteLength(arrayBuffer, order) {
throw new $TypeError('Assertion failed: `arrayBuffer` must not be detached'); // step 2
}

return arrayBufferByteLength(arrayBuffer);
return isSAB ? $sabByteLength(arrayBuffer) : arrayBufferByteLength(arrayBuffer);
};
46 changes: 46 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15048,6 +15048,52 @@ var es2024 = function ES2024(ES, ops, expectedMissing, skips) {
t.end();
});

test('ArrayBufferByteLength', function (t) {
var order = 'UNORDERED';

forEach([].concat(
v.primitives,
v.objects
), function (nonAB) {
t['throws'](
function () { ES.ArrayBufferByteLength(nonAB, order); },
TypeError,
debug(nonAB) + ' is not an ArrayBuffer'
);
});

t.test('ArrayBuffers supported', { skip: typeof ArrayBuffer !== 'function' }, function (st) {
var ab = new ArrayBuffer(8);
st['throws'](
function () { ES.ArrayBufferByteLength(ab, 'not a valid order'); },
TypeError,
'invalid order enum value throws'
);

st.equal(ES.ArrayBufferByteLength(ab, order), 8, 'fixed length ArrayBuffer is fixed length');

st.end();
});

t.test('SharedArrayBuffers supported', { skip: typeof SharedArrayBuffer !== 'function' }, function (st) {
var sab = new SharedArrayBuffer(1);
st.equal(ES.ArrayBufferByteLength(sab, order), 1, 'fixed length SharedArrayBuffer is fixed length');

st.test('growable SABs', { skip: !('grow' in SharedArrayBuffer.prototype) }, function (s2t) {
var gsab = new SharedArrayBuffer(0, { maxByteLength: 64 });
s2t.equal(ES.ArrayBufferByteLength(gsab, order), 0, 'growable SharedArrayBuffer has initial length');

gsab.grow(8);

s2t.equal(ES.ArrayBufferByteLength(gsab, order), 8, 'growable SharedArrayBuffer has initial length');

s2t.end();
});

st.end();
});
});

test('ArrayBufferCopyAndDetach', function (t) {
forEach([].concat(
v.primitives,
Expand Down

0 comments on commit 0a25934

Please sign in to comment.