ValidateAtomicAccess.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $RangeError = GetIntrinsic('%RangeError%');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var callBound = require('call-bind/callBound');
  6. // node 0.10 doesn't have a prototype method
  7. var $byteOffset = callBound('%TypedArray.prototype.byteOffset%', true) || function (x) { return x.byteOffset; };
  8. var ToIndex = require('./ToIndex');
  9. var TypedArrayElementSize = require('./TypedArrayElementSize');
  10. var isTypedArray = require('is-typed-array');
  11. var typedArrayLength = require('typed-array-length');
  12. // https://262.ecma-international.org/13.0/#sec-validateatomicaccess
  13. module.exports = function ValidateAtomicAccess(typedArray, requestIndex) {
  14. if (!isTypedArray(typedArray)) {
  15. throw new $TypeError('Assertion failed: `typedArray` must be a TypedArray');
  16. }
  17. var length = typedArrayLength(typedArray); // step 1
  18. var accessIndex = ToIndex(requestIndex); // step 2
  19. /*
  20. // this assertion can never be reached
  21. if (!(accessIndex >= 0)) {
  22. throw new $TypeError('Assertion failed: accessIndex >= 0'); // step 4
  23. }
  24. */
  25. if (accessIndex >= length) {
  26. throw new $RangeError('index out of range'); // step 4
  27. }
  28. var elementSize = TypedArrayElementSize(typedArray); // step 5
  29. var offset = $byteOffset(typedArray); // step 6
  30. return (accessIndex * elementSize) + offset; // step 7
  31. };