ValidateAtomicAccess.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 TypedArrayLength = require('./TypedArrayLength');
  7. var isTypedArrayWithBufferWitnessRecord = require('../helpers/records/typed-array-with-buffer-witness-record');
  8. var typedArrayByteOffset = require('typed-array-byte-offset');
  9. // https://262.ecma-international.org/15.0/#sec-validateatomicaccess
  10. module.exports = function ValidateAtomicAccess(taRecord, requestIndex) {
  11. if (!isTypedArrayWithBufferWitnessRecord(taRecord)) {
  12. throw new $TypeError('Assertion failed: `taRecord` must be a TypedArray With Buffer Witness Record');
  13. }
  14. var length = TypedArrayLength(taRecord); // 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 typedArray = taRecord['[[Object]]']; // step 5
  26. var elementSize = TypedArrayElementSize(typedArray); // step 6
  27. var offset = typedArrayByteOffset(typedArray); // step 7
  28. return (accessIndex * elementSize) + offset; // step 8
  29. };