ValidateAtomicAccess.js 1.4 KB

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