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