IntegerIndexedElementSet.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var IsValidIntegerIndex = require('./IsValidIntegerIndex');
  4. var SetValueInBuffer = require('./SetValueInBuffer');
  5. var ToBigInt = require('./ToBigInt');
  6. var ToNumber = require('./ToNumber');
  7. var typedArrayBuffer = require('typed-array-buffer');
  8. var typedArrayByteOffset = require('typed-array-byte-offset');
  9. var whichTypedArray = require('which-typed-array');
  10. var tableTAO = require('./tables/typed-array-objects');
  11. // https://262.ecma-international.org/12.0/#sec-integerindexedelementset
  12. module.exports = function IntegerIndexedElementSet(O, index, value) {
  13. if (typeof index !== 'number') {
  14. throw new $TypeError('Assertion failed: `index` must be a Number');
  15. }
  16. var arrayTypeName = whichTypedArray(O); // step 4.b
  17. if (!arrayTypeName) {
  18. throw new $TypeError('Assertion failed: `O` must be a TypedArray'); // step 1
  19. }
  20. var contentType = arrayTypeName === 'BigInt64Array' || arrayTypeName === 'BigUint64Array' ? 'BigInt' : 'Number';
  21. var numValue = contentType === 'BigInt' ? ToBigInt(value) : ToNumber(value); // steps 2 - 3
  22. if (IsValidIntegerIndex(O, index)) { // step 4
  23. var offset = typedArrayByteOffset(O); // step 4.a
  24. var elementType = tableTAO.name['$' + arrayTypeName]; // step 4.e
  25. var elementSize = tableTAO.size['$' + elementType]; // step 4.c
  26. var indexedPosition = (index * elementSize) + offset; // step 4.d
  27. SetValueInBuffer(typedArrayBuffer(O), indexedPosition, elementType, numValue, true, 'Unordered'); // step 4.e
  28. }
  29. // 5. Return NormalCompletion(undefined)
  30. };