ValidateAtomicAccess.js 1.1 KB

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