source: imaps-frontend/node_modules/es-abstract/2020/IntegerIndexedElementSet.js@ d565449

main
Last change on this file since d565449 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 $TypeError = require('es-errors/type');
4
5var IsDetachedBuffer = require('./IsDetachedBuffer');
6var IsValidIntegerIndex = require('./IsValidIntegerIndex');
7var SetValueInBuffer = require('./SetValueInBuffer');
8var ToBigInt = require('./ToBigInt');
9var ToNumber = require('./ToNumber');
10
11var typedArrayBuffer = require('typed-array-buffer');
12var typedArrayByteOffset = require('typed-array-byte-offset');
13var typedArrayLength = require('typed-array-length');
14var whichTypedArray = require('which-typed-array');
15
16var tableTAO = require('./tables/typed-array-objects');
17
18// https://262.ecma-international.org/11.0/#sec-integerindexedelementset
19
20module.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};
Note: See TracBrowser for help on using the repository browser.