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