ValidateAtomicAccess.js 950 B

123456789101112131415161718192021222324252627282930313233343536
  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 isTypedArray = require('is-typed-array');
  7. var typedArrayLength = require('typed-array-length');
  8. // https://262.ecma-international.org/8.0/#sec-validateatomicaccess
  9. module.exports = function ValidateAtomicAccess(typedArray, requestIndex) {
  10. if (!isTypedArray(typedArray)) {
  11. throw new $TypeError('Assertion failed: `typedArray` must be a TypedArray'); // step 1
  12. }
  13. var accessIndex = ToIndex(requestIndex); // step 2
  14. var length = typedArrayLength(typedArray); // step 3
  15. /*
  16. // this assertion can never be reached
  17. if (!(accessIndex >= 0)) {
  18. throw new $TypeError('Assertion failed: accessIndex >= 0'); // step 4
  19. }
  20. */
  21. if (accessIndex >= length) {
  22. throw new $RangeError('index out of range'); // step 5
  23. }
  24. return accessIndex; // step 6
  25. };