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