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