1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var IsValidIntegerIndex = require('./IsValidIntegerIndex');
|
---|
6 | var SetValueInBuffer = require('./SetValueInBuffer');
|
---|
7 | var ToBigInt = require('./ToBigInt');
|
---|
8 | var ToNumber = require('./ToNumber');
|
---|
9 |
|
---|
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/12.0/#sec-integerindexedelementset
|
---|
17 |
|
---|
18 | module.exports = function IntegerIndexedElementSet(O, index, value) {
|
---|
19 | if (typeof index !== 'number') {
|
---|
20 | throw new $TypeError('Assertion failed: `index` must be a Number');
|
---|
21 | }
|
---|
22 |
|
---|
23 | var arrayTypeName = whichTypedArray(O); // step 4.b
|
---|
24 | if (!arrayTypeName) {
|
---|
25 | throw new $TypeError('Assertion failed: `O` must be a TypedArray'); // step 1
|
---|
26 | }
|
---|
27 |
|
---|
28 | var contentType = arrayTypeName === 'BigInt64Array' || arrayTypeName === 'BigUint64Array' ? 'BigInt' : 'Number';
|
---|
29 | var numValue = contentType === 'BigInt' ? ToBigInt(value) : ToNumber(value); // steps 2 - 3
|
---|
30 |
|
---|
31 | if (IsValidIntegerIndex(O, index)) { // step 4
|
---|
32 | var offset = typedArrayByteOffset(O); // step 4.a
|
---|
33 |
|
---|
34 | var elementType = tableTAO.name['$' + arrayTypeName]; // step 4.e
|
---|
35 |
|
---|
36 | var elementSize = tableTAO.size['$' + elementType]; // step 4.c
|
---|
37 |
|
---|
38 | var indexedPosition = (index * elementSize) + offset; // step 4.d
|
---|
39 |
|
---|
40 | SetValueInBuffer(typedArrayBuffer(O), indexedPosition, elementType, numValue, true, 'Unordered'); // step 4.e
|
---|
41 | }
|
---|
42 |
|
---|
43 | // 5. Return NormalCompletion(undefined)
|
---|
44 | };
|
---|