Skip to content

Commit 0a25934

Browse files
committed
[Fix] ES2024+: ArrayBufferByteLength: return the byte length for SABs, not NaN
1 parent 12f1331 commit 0a25934

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

2024/ArrayBufferByteLength.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var isArrayBuffer = require('is-array-buffer');
1010
var isSharedArrayBuffer = require('is-shared-array-buffer');
1111
var arrayBufferByteLength = require('array-buffer-byte-length');
1212

13+
var callBound = require('call-bound');
14+
var $sabByteLength = callBound('SharedArrayBuffer.prototype.byteLength', true);
15+
1316
var isGrowable = false; // TODO: support this
1417

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

37-
return arrayBufferByteLength(arrayBuffer);
40+
return isSAB ? $sabByteLength(arrayBuffer) : arrayBufferByteLength(arrayBuffer);
3841
};

test/tests.js

+46
Original file line numberDiff line numberDiff line change
@@ -15048,6 +15048,52 @@ var es2024 = function ES2024(ES, ops, expectedMissing, skips) {
1504815048
t.end();
1504915049
});
1505015050

15051+
test('ArrayBufferByteLength', function (t) {
15052+
var order = 'UNORDERED';
15053+
15054+
forEach([].concat(
15055+
v.primitives,
15056+
v.objects
15057+
), function (nonAB) {
15058+
t['throws'](
15059+
function () { ES.ArrayBufferByteLength(nonAB, order); },
15060+
TypeError,
15061+
debug(nonAB) + ' is not an ArrayBuffer'
15062+
);
15063+
});
15064+
15065+
t.test('ArrayBuffers supported', { skip: typeof ArrayBuffer !== 'function' }, function (st) {
15066+
var ab = new ArrayBuffer(8);
15067+
st['throws'](
15068+
function () { ES.ArrayBufferByteLength(ab, 'not a valid order'); },
15069+
TypeError,
15070+
'invalid order enum value throws'
15071+
);
15072+
15073+
st.equal(ES.ArrayBufferByteLength(ab, order), 8, 'fixed length ArrayBuffer is fixed length');
15074+
15075+
st.end();
15076+
});
15077+
15078+
t.test('SharedArrayBuffers supported', { skip: typeof SharedArrayBuffer !== 'function' }, function (st) {
15079+
var sab = new SharedArrayBuffer(1);
15080+
st.equal(ES.ArrayBufferByteLength(sab, order), 1, 'fixed length SharedArrayBuffer is fixed length');
15081+
15082+
st.test('growable SABs', { skip: !('grow' in SharedArrayBuffer.prototype) }, function (s2t) {
15083+
var gsab = new SharedArrayBuffer(0, { maxByteLength: 64 });
15084+
s2t.equal(ES.ArrayBufferByteLength(gsab, order), 0, 'growable SharedArrayBuffer has initial length');
15085+
15086+
gsab.grow(8);
15087+
15088+
s2t.equal(ES.ArrayBufferByteLength(gsab, order), 8, 'growable SharedArrayBuffer has initial length');
15089+
15090+
s2t.end();
15091+
});
15092+
15093+
st.end();
15094+
});
15095+
});
15096+
1505115097
test('ArrayBufferCopyAndDetach', function (t) {
1505215098
forEach([].concat(
1505315099
v.primitives,

0 commit comments

Comments
 (0)