es.array.push.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 <= 121 and Safari <= 15.4; FF < 23 throws InternalError
  12. // https://bugs.chromium.org/p/v8/issues/detail?id=12681
  13. var properErrorOnNonWritableLength = 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. var FORCED = INCORRECT_TO_LENGTH || !properErrorOnNonWritableLength();
  22. // `Array.prototype.push` method
  23. // https://tc39.es/ecma262/#sec-array.prototype.push
  24. $({ target: 'Array', proto: true, arity: 1, forced: FORCED }, {
  25. // eslint-disable-next-line no-unused-vars -- required for `.length`
  26. push: function push(item) {
  27. var O = toObject(this);
  28. var len = lengthOfArrayLike(O);
  29. var argCount = arguments.length;
  30. doesNotExceedSafeInteger(len + argCount);
  31. for (var i = 0; i < argCount; i++) {
  32. O[len] = arguments[i];
  33. len++;
  34. }
  35. setArrayLength(O, len);
  36. return len;
  37. }
  38. });