ValidateAtomicAccess.js 900 B

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