es.array.push.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var toObject = require('../internals/to-object');
  4. var lengthOfArrayLike = require('../internals/length-of-array-like');
  5. var setArrayLength = require('../internals/array-set-length');
  6. var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
  7. var fails = require('../internals/fails');
  8. var INCORRECT_TO_LENGTH = fails(function () {
  9. return [].push.call({ length: 0x100000000 }, 1) !== 4294967297;
  10. });
  11. // V8 and Safari <= 15.4, FF < 23 throws InternalError
  12. // https://bugs.chromium.org/p/v8/issues/detail?id=12681
  13. var SILENT_ON_NON_WRITABLE_LENGTH = !function () {
  14. try {
  15. // eslint-disable-next-line es/no-object-defineproperty -- safe
  16. Object.defineProperty([], 'length', { writable: false }).push();
  17. } catch (error) {
  18. return error instanceof TypeError;
  19. }
  20. }();
  21. // `Array.prototype.push` method
  22. // https://tc39.es/ecma262/#sec-array.prototype.push
  23. $({ target: 'Array', proto: true, arity: 1, forced: INCORRECT_TO_LENGTH || SILENT_ON_NON_WRITABLE_LENGTH }, {
  24. // eslint-disable-next-line no-unused-vars -- required for `.length`
  25. push: function push(item) {
  26. var O = toObject(this);
  27. var len = lengthOfArrayLike(O);
  28. var argCount = arguments.length;
  29. doesNotExceedSafeInteger(len + argCount);
  30. for (var i = 0; i < argCount; i++) {
  31. O[len] = arguments[i];
  32. len++;
  33. }
  34. setArrayLength(O, len);
  35. return len;
  36. }
  37. });