IntegerIndexedElementSet.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var IsDetachedBuffer = require('./IsDetachedBuffer');
  4. var IsInteger = require('./IsInteger');
  5. var SetValueInBuffer = require('./SetValueInBuffer');
  6. var ToNumber = require('./ToNumber');
  7. var isNegativeZero = require('is-negative-zero');
  8. var typedArrayBuffer = require('typed-array-buffer');
  9. var typedArrayByteOffset = require('typed-array-byte-offset');
  10. var typedArrayLength = require('typed-array-length');
  11. var whichTypedArray = require('which-typed-array');
  12. var tableTAO = require('./tables/typed-array-objects');
  13. // https://262.ecma-international.org/8.0/#sec-integerindexedelementset
  14. module.exports = function IntegerIndexedElementSet(O, index, value) {
  15. if (typeof index !== 'number') {
  16. throw new $TypeError('`index` must be a Number'); // step 1
  17. }
  18. var arrayTypeName = whichTypedArray(O); // step 12
  19. if (!arrayTypeName) {
  20. throw new $TypeError('`O` must be a TypedArray'); // step 2
  21. }
  22. var numValue = ToNumber(value); // step 3
  23. var buffer = typedArrayBuffer(O); // step 5
  24. if (IsDetachedBuffer(buffer)) {
  25. throw new $TypeError('`O` has a detached buffer'); // step 6
  26. }
  27. if (!IsInteger(index) || isNegativeZero(index)) {
  28. return false; // steps 7 - 8
  29. }
  30. var length = typedArrayLength(O); // step 9
  31. if (index < 0 || index >= length) {
  32. return false; // step 10
  33. }
  34. var offset = typedArrayByteOffset(O); // step 11
  35. var elementType = tableTAO.name['$' + arrayTypeName]; // step 15
  36. var elementSize = tableTAO.size['$' + elementType]; // step 13
  37. var indexedPosition = (index * elementSize) + offset; // step 14
  38. SetValueInBuffer(buffer, indexedPosition, elementType, numValue, true, 'Unordered'); // step 16
  39. return true; // step 17
  40. };