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