source: imaps-frontend/node_modules/es-abstract/2015/IntegerIndexedElementGet.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.8 KB
Line 
1'use strict';
2
3var $SyntaxError = require('es-errors/syntax');
4var $TypeError = require('es-errors/type');
5
6var GetValueFromBuffer = require('./GetValueFromBuffer');
7var IsDetachedBuffer = require('./IsDetachedBuffer');
8var IsInteger = require('./IsInteger');
9
10var isNegativeZero = require('../helpers/isNegativeZero');
11
12var typedArrayLength = require('typed-array-length');
13var typedArrayBuffer = require('typed-array-buffer');
14var typedArrayByteOffset = require('typed-array-byte-offset');
15var whichTypedArray = require('which-typed-array');
16
17var tableTAO = require('./tables/typed-array-objects');
18
19// https://262.ecma-international.org/6.0/#sec-integerindexedelementget
20
21module.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); // step 14
58};
Note: See TracBrowser for help on using the repository browser.