fecee361a7e23cf3ef4f70a9cc3257f6e849989dccf1a4ec566b39b889c3100d47ded183eb47654ebe4cecdbac58cc363499c93e876789aa232b20e2f216b1 1.4 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var arrayWith = require('../internals/array-with');
  3. var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
  4. var isBigIntArray = require('../internals/is-big-int-array');
  5. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  6. var toBigInt = require('../internals/to-big-int');
  7. var aTypedArray = ArrayBufferViewCore.aTypedArray;
  8. var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
  9. var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
  10. var PROPER_ORDER = !!function () {
  11. try {
  12. // eslint-disable-next-line no-throw-literal, es/no-typed-arrays, es/no-array-prototype-with -- required for testing
  13. new Int8Array(1)['with'](2, { valueOf: function () { throw 8; } });
  14. } catch (error) {
  15. // some early implementations, like WebKit, does not follow the final semantic
  16. // https://github.com/tc39/proposal-change-array-by-copy/pull/86
  17. return error === 8;
  18. }
  19. }();
  20. // `%TypedArray%.prototype.with` method
  21. // https://tc39.es/ecma262/#sec-%typedarray%.prototype.with
  22. exportTypedArrayMethod('with', { 'with': function (index, value) {
  23. var O = aTypedArray(this);
  24. var relativeIndex = toIntegerOrInfinity(index);
  25. var actualValue = isBigIntArray(O) ? toBigInt(value) : +value;
  26. return arrayWith(O, getTypedArrayConstructor(O), relativeIndex, actualValue);
  27. } }['with'], !PROPER_ORDER);