es.iterator.constructor.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var anInstance = require('../internals/an-instance');
  5. var anObject = require('../internals/an-object');
  6. var isCallable = require('../internals/is-callable');
  7. var getPrototypeOf = require('../internals/object-get-prototype-of');
  8. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  9. var createProperty = require('../internals/create-property');
  10. var fails = require('../internals/fails');
  11. var hasOwn = require('../internals/has-own-property');
  12. var wellKnownSymbol = require('../internals/well-known-symbol');
  13. var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
  14. var DESCRIPTORS = require('../internals/descriptors');
  15. var IS_PURE = require('../internals/is-pure');
  16. var CONSTRUCTOR = 'constructor';
  17. var ITERATOR = 'Iterator';
  18. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  19. var $TypeError = TypeError;
  20. var NativeIterator = globalThis[ITERATOR];
  21. // FF56- have non-standard global helper `Iterator`
  22. var FORCED = IS_PURE
  23. || !isCallable(NativeIterator)
  24. || NativeIterator.prototype !== IteratorPrototype
  25. // FF44- non-standard `Iterator` passes previous tests
  26. || !fails(function () { NativeIterator({}); });
  27. var IteratorConstructor = function Iterator() {
  28. anInstance(this, IteratorPrototype);
  29. if (getPrototypeOf(this) === IteratorPrototype) throw new $TypeError('Abstract class Iterator not directly constructable');
  30. };
  31. var defineIteratorPrototypeAccessor = function (key, value) {
  32. if (DESCRIPTORS) {
  33. defineBuiltInAccessor(IteratorPrototype, key, {
  34. configurable: true,
  35. get: function () {
  36. return value;
  37. },
  38. set: function (replacement) {
  39. anObject(this);
  40. if (this === IteratorPrototype) throw new $TypeError("You can't redefine this property");
  41. if (hasOwn(this, key)) this[key] = replacement;
  42. else createProperty(this, key, replacement);
  43. }
  44. });
  45. } else IteratorPrototype[key] = value;
  46. };
  47. if (!hasOwn(IteratorPrototype, TO_STRING_TAG)) defineIteratorPrototypeAccessor(TO_STRING_TAG, ITERATOR);
  48. if (FORCED || !hasOwn(IteratorPrototype, CONSTRUCTOR) || IteratorPrototype[CONSTRUCTOR] === Object) {
  49. defineIteratorPrototypeAccessor(CONSTRUCTOR, IteratorConstructor);
  50. }
  51. IteratorConstructor.prototype = IteratorPrototype;
  52. // `Iterator` constructor
  53. // https://tc39.es/ecma262/#sec-iterator
  54. $({ global: true, constructor: true, forced: FORCED }, {
  55. Iterator: IteratorConstructor
  56. });