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