IntegerIndexedElementSet.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 TypedArrayElementSize = require('./TypedArrayElementSize');
  8. var TypedArrayElementType = require('./TypedArrayElementType');
  9. var typedArrayBuffer = require('typed-array-buffer');
  10. var typedArrayByteOffset = require('typed-array-byte-offset');
  11. var whichTypedArray = require('which-typed-array');
  12. // https://262.ecma-international.org/13.0/#sec-integerindexedelementset
  13. module.exports = function IntegerIndexedElementSet(O, index, value) {
  14. var arrayTypeName = whichTypedArray(O);
  15. if (!arrayTypeName) {
  16. throw new $TypeError('Assertion failed: `O` must be a TypedArray');
  17. }
  18. if (typeof index !== 'number') {
  19. throw new $TypeError('Assertion failed: `index` must be a Number');
  20. }
  21. var contentType = arrayTypeName === 'BigInt64Array' || arrayTypeName === 'BigUint64Array' ? 'BigInt' : 'Number';
  22. var numValue = contentType === 'BigInt' ? ToBigInt(value) : ToNumber(value); // steps 1 - 2
  23. if (IsValidIntegerIndex(O, index)) { // step 3
  24. var offset = typedArrayByteOffset(O); // step 3.a
  25. var elementSize = TypedArrayElementSize(O); // step 3.b
  26. var indexedPosition = (index * elementSize) + offset; // step 3.c
  27. var elementType = TypedArrayElementType(O); // step 3.d
  28. SetValueInBuffer(typedArrayBuffer(O), indexedPosition, elementType, numValue, true, 'Unordered'); // step 3.e
  29. }
  30. };