ValidateAtomicAccess.js 1.6 KB

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