ValidateAtomicAccess.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 typedArrayByteOffset = require('typed-array-byte-offset');
  8. var typedArrayLength = require('typed-array-length');
  9. var whichTypedArray = require('which-typed-array');
  10. var table60 = {
  11. __proto__: null,
  12. $Int8Array: 1,
  13. $Uint8Array: 1,
  14. $Uint8ClampedArray: 1,
  15. $Int16Array: 2,
  16. $Uint16Array: 2,
  17. $Int32Array: 4,
  18. $Uint32Array: 4,
  19. $BigInt64Array: 8,
  20. $BigUint64Array: 8,
  21. $Float32Array: 4,
  22. $Float64Array: 8
  23. };
  24. // https://262.ecma-international.org/12.0/#sec-validateatomicaccess
  25. module.exports = function ValidateAtomicAccess(typedArray, requestIndex) {
  26. if (!isTypedArray(typedArray)) {
  27. throw new $TypeError('Assertion failed: `typedArray` must be a TypedArray'); // step 1
  28. }
  29. var length = typedArrayLength(typedArray); // step 2
  30. var accessIndex = ToIndex(requestIndex); // step 3
  31. /*
  32. // this assertion can never be reached
  33. if (!(accessIndex >= 0)) {
  34. throw new $TypeError('Assertion failed: accessIndex >= 0'); // step 4
  35. }
  36. */
  37. if (accessIndex >= length) {
  38. throw new $RangeError('index out of range'); // step 5
  39. }
  40. var arrayTypeName = whichTypedArray(typedArray); // step 6
  41. var elementSize = table60['$' + arrayTypeName]; // step 7
  42. var offset = typedArrayByteOffset(typedArray); // step 8
  43. return (accessIndex * elementSize) + offset; // step 9
  44. };