ValidateAtomicAccess.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 typedArrayByteOffset = require('typed-array-byte-offset');
  7. var typedArrayLength = require('typed-array-length');
  8. var whichTypedArray = require('which-typed-array');
  9. var tableTAO = require('./tables/typed-array-objects');
  10. // https://262.ecma-international.org/12.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'); // step 1
  14. }
  15. var length = typedArrayLength(typedArray); // step 2
  16. var accessIndex = ToIndex(requestIndex); // step 3
  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 5
  25. }
  26. var arrayTypeName = whichTypedArray(typedArray); // step 6
  27. var taType = tableTAO.name['$' + arrayTypeName];
  28. var elementSize = tableTAO.size['$' + taType]; // step 7
  29. var offset = typedArrayByteOffset(typedArray); // step 8
  30. return (accessIndex * elementSize) + offset; // step 9
  31. };