mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
 
var $TypeError = require('es-errors/type');
 
var IsDetachedBuffer = require('./IsDetachedBuffer');
var TypedArrayElementSize = require('./TypedArrayElementSize');
 
var isTypedArrayWithBufferWitnessRecord = require('../helpers/records/typed-array-with-buffer-witness-record');
 
var typedArrayBuffer = require('typed-array-buffer');
var typedArrayByteOffset = require('typed-array-byte-offset');
var typedArrayLength = require('typed-array-length');
 
// https://tc39.es/ecma262/#sec-istypedarrayoutofbounds
 
module.exports = function IsTypedArrayOutOfBounds(taRecord) {
    if (!isTypedArrayWithBufferWitnessRecord(taRecord)) {
        throw new $TypeError('Assertion failed: `taRecord` must be a TypedArray With Buffer Witness Record');
    }
 
    var O = taRecord['[[Object]]']; // step 1
 
    var bufferByteLength = taRecord['[[CachedBufferByteLength]]']; // step 2
 
    if (IsDetachedBuffer(typedArrayBuffer(O)) && bufferByteLength !== 'DETACHED') {
        throw new $TypeError('Assertion failed: typed array is detached only if the byte length is ~DETACHED~'); // step 3
    }
 
    if (bufferByteLength === 'DETACHED') {
        return true; // step 4
    }
 
    var byteOffsetStart = typedArrayByteOffset(O); // step 5
 
    var byteOffsetEnd;
    var length = typedArrayLength(O);
    // TODO: probably use package for array length
    // seems to apply when TA is backed by a resizable/growable AB
    if (length === 'AUTO') { // step 6
        byteOffsetEnd = bufferByteLength; // step 6.a
    } else {
        var elementSize = TypedArrayElementSize(O); // step 7.a
 
        byteOffsetEnd = byteOffsetStart + (length * elementSize); // step 7.b
    }
 
    if (byteOffsetStart > bufferByteLength || byteOffsetEnd > bufferByteLength) {
        return true; // step 8
    }
 
    // 9. NOTE: 0-length TypedArrays are not considered out-of-bounds.
 
    return false; // step 10
};