IntegerIndexedElementGet.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var GetValueFromBuffer = require('./GetValueFromBuffer');
  4. var IsValidIntegerIndex = require('./IsValidIntegerIndex');
  5. var TypedArrayElementSize = require('./TypedArrayElementSize');
  6. var TypedArrayElementType = require('./TypedArrayElementType');
  7. var isTypedArray = require('is-typed-array');
  8. var typedArrayBuffer = require('typed-array-buffer');
  9. var typedArrayByteOffset = require('typed-array-byte-offset');
  10. // https://262.ecma-international.org/13.0/#sec-integerindexedelementget
  11. module.exports = function IntegerIndexedElementGet(O, index) {
  12. if (!isTypedArray(O)) {
  13. throw new $TypeError('Assertion failed: `O` must be a TypedArray');
  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 1
  20. }
  21. var offset = typedArrayByteOffset(O); // step 2
  22. var elementSize = TypedArrayElementSize(O); // step 3
  23. var indexedPosition = (index * elementSize) + offset; // step 4
  24. var elementType = TypedArrayElementType(O); // step 5
  25. return GetValueFromBuffer(typedArrayBuffer(O), indexedPosition, elementType, true, 'Unordered'); // step 11
  26. };