ab5d729056bd532c3cdf178b32ab95aa1cca5535c5b893664d803cd0fe631442e0c7b5d516d6012d0d633dfb71b257cac6c380d8cc363600c1194069e6d347 1.9 KB

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