IntegerIndexedElementGet.js 1.5 KB

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