iterator-create-proxy.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var create = require('../internals/object-create');
  4. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  5. var defineBuiltIns = require('../internals/define-built-ins');
  6. var wellKnownSymbol = require('../internals/well-known-symbol');
  7. var InternalStateModule = require('../internals/internal-state');
  8. var getMethod = require('../internals/get-method');
  9. var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
  10. var createIterResultObject = require('../internals/create-iter-result-object');
  11. var iteratorClose = require('../internals/iterator-close');
  12. var ITERATOR_HELPER = 'IteratorHelper';
  13. var WRAP_FOR_VALID_ITERATOR = 'WrapForValidIterator';
  14. var setInternalState = InternalStateModule.set;
  15. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  16. var createIteratorProxyPrototype = function (IS_ITERATOR) {
  17. var ITERATOR_PROXY = IS_ITERATOR ? WRAP_FOR_VALID_ITERATOR : ITERATOR_HELPER;
  18. var getInternalState = InternalStateModule.getterFor(ITERATOR_PROXY);
  19. var IteratorProxyPrototype = defineBuiltIns(create(IteratorPrototype), {
  20. next: function next() {
  21. var state = getInternalState(this);
  22. // for simplification:
  23. // for `%WrapForValidIteratorPrototype%.next` our `nextHandler` returns `IterResultObject`
  24. // for `%IteratorHelperPrototype%.next` - just a value
  25. if (IS_ITERATOR) return state.nextHandler();
  26. try {
  27. var result = state.done ? undefined : state.nextHandler();
  28. return createIterResultObject(result, state.done);
  29. } catch (error) {
  30. state.done = true;
  31. throw error;
  32. }
  33. },
  34. 'return': function () {
  35. var state = getInternalState(this);
  36. var iterator = state.iterator;
  37. state.done = true;
  38. if (IS_ITERATOR) {
  39. var returnMethod = getMethod(iterator, 'return');
  40. return returnMethod ? call(returnMethod, iterator) : createIterResultObject(undefined, true);
  41. }
  42. if (state.inner) try {
  43. iteratorClose(state.inner.iterator, 'return');
  44. } catch (error) {
  45. return iteratorClose(iterator, 'throw', error);
  46. }
  47. iteratorClose(iterator, 'return');
  48. return createIterResultObject(undefined, true);
  49. }
  50. });
  51. if (!IS_ITERATOR) {
  52. createNonEnumerableProperty(IteratorProxyPrototype, TO_STRING_TAG, 'Iterator Helper');
  53. }
  54. return IteratorProxyPrototype;
  55. };
  56. var IteratorHelperPrototype = createIteratorProxyPrototype(false);
  57. var WrapForValidIteratorPrototype = createIteratorProxyPrototype(true);
  58. module.exports = function (nextHandler, IS_ITERATOR) {
  59. var ITERATOR_PROXY = IS_ITERATOR ? WRAP_FOR_VALID_ITERATOR : ITERATOR_HELPER;
  60. var IteratorProxy = function Iterator(record, state) {
  61. if (state) {
  62. state.iterator = record.iterator;
  63. state.next = record.next;
  64. } else state = record;
  65. state.type = ITERATOR_PROXY;
  66. state.nextHandler = nextHandler;
  67. state.counter = 0;
  68. state.done = false;
  69. setInternalState(this, state);
  70. };
  71. IteratorProxy.prototype = IS_ITERATOR ? WrapForValidIteratorPrototype : IteratorHelperPrototype;
  72. return IteratorProxy;
  73. };