1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | // https://tc39.es/ecma262/#sec-arraybufferbytelength
|
---|
6 |
|
---|
7 | var IsDetachedBuffer = require('./IsDetachedBuffer');
|
---|
8 |
|
---|
9 | var isArrayBuffer = require('is-array-buffer');
|
---|
10 | var isSharedArrayBuffer = require('is-shared-array-buffer');
|
---|
11 | var arrayBufferByteLength = require('array-buffer-byte-length');
|
---|
12 |
|
---|
13 | var isGrowable = false; // TODO: support this
|
---|
14 |
|
---|
15 | module.exports = function ArrayBufferByteLength(arrayBuffer, order) {
|
---|
16 | var isSAB = isSharedArrayBuffer(arrayBuffer);
|
---|
17 | if (!isArrayBuffer(arrayBuffer) && !isSAB) {
|
---|
18 | throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
|
---|
19 | }
|
---|
20 | if (order !== 'SEQ-CST' && order !== 'UNORDERED') {
|
---|
21 | throw new $TypeError('Assertion failed: `order` must be ~SEQ-CST~ or ~UNORDERED~');
|
---|
22 | }
|
---|
23 |
|
---|
24 | // 1. If IsSharedArrayBuffer(arrayBuffer) is true and arrayBuffer has an [[ArrayBufferByteLengthData]] internal slot, then
|
---|
25 | // TODO: see if IsFixedLengthArrayBuffer can be used here in the spec instead
|
---|
26 | if (isSAB && isGrowable) { // step 1
|
---|
27 | // a. Let bufferByteLengthBlock be arrayBuffer.[[ArrayBufferByteLengthData]].
|
---|
28 | // b. Let rawLength be GetRawBytesFromSharedBlock(bufferByteLengthBlock, 0, BIGUINT64, true, order).
|
---|
29 | // c. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record.
|
---|
30 | // d. Return ℝ(RawBytesToNumeric(BIGUINT64, rawLength, isLittleEndian)).
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (IsDetachedBuffer(arrayBuffer)) {
|
---|
34 | throw new $TypeError('Assertion failed: `arrayBuffer` must not be detached'); // step 2
|
---|
35 | }
|
---|
36 |
|
---|
37 | return arrayBufferByteLength(arrayBuffer);
|
---|
38 | };
|
---|