IntegerIndexedElementGet.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var GetValueFromBuffer = require('./GetValueFromBuffer');
  4. var IsValidIntegerIndex = require('./IsValidIntegerIndex');
  5. var typedArrayBuffer = require('typed-array-buffer');
  6. var typedArrayByteOffset = require('typed-array-byte-offset');
  7. var whichTypedArray = require('which-typed-array');
  8. var tableTAO = require('./tables/typed-array-objects');
  9. // https://262.ecma-international.org/12.0/#sec-integerindexedelementget
  10. module.exports = function IntegerIndexedElementGet(O, index) {
  11. var arrayTypeName = whichTypedArray(O); // step 4
  12. if (!arrayTypeName) {
  13. throw new $TypeError('Assertion failed: `O` must be a TypedArray'); // step 1
  14. }
  15. if (typeof index !== 'number') {
  16. throw new $TypeError('Assertion failed: `index` must be a Number');
  17. }
  18. if (!IsValidIntegerIndex(O, index)) {
  19. return void undefined; // step 2
  20. }
  21. var offset = typedArrayByteOffset(O); // step 3
  22. var elementType = tableTAO.name['$' + arrayTypeName]; // step 7
  23. var elementSize = tableTAO.size['$' + elementType]; // step 5
  24. var indexedPosition = (index * elementSize) + offset; // step 6
  25. return GetValueFromBuffer(typedArrayBuffer(O), indexedPosition, elementType, true, 'Unordered'); // step 11
  26. };