ValidateAtomicAccess.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $RangeError = GetIntrinsic('%RangeError%');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var ToIndex = require('./ToIndex');
  6. var TypedArrayElementSize = require('./TypedArrayElementSize');
  7. var isTypedArray = require('is-typed-array');
  8. var typedArrayByteOffset = require('typed-array-byte-offset');
  9. var typedArrayLength = require('typed-array-length');
  10. // https://262.ecma-international.org/13.0/#sec-validateatomicaccess
  11. module.exports = function ValidateAtomicAccess(typedArray, requestIndex) {
  12. if (!isTypedArray(typedArray)) {
  13. throw new $TypeError('Assertion failed: `typedArray` must be a TypedArray');
  14. }
  15. var length = typedArrayLength(typedArray); // step 1
  16. var accessIndex = ToIndex(requestIndex); // step 2
  17. /*
  18. // this assertion can never be reached
  19. if (!(accessIndex >= 0)) {
  20. throw new $TypeError('Assertion failed: accessIndex >= 0'); // step 4
  21. }
  22. */
  23. if (accessIndex >= length) {
  24. throw new $RangeError('index out of range'); // step 4
  25. }
  26. var elementSize = TypedArrayElementSize(typedArray); // step 5
  27. var offset = typedArrayByteOffset(typedArray); // step 6
  28. return (accessIndex * elementSize) + offset; // step 7
  29. };