iterator-create-proxy.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 TO_STRING_TAG = wellKnownSymbol('toStringTag');
  13. var ITERATOR_HELPER = 'IteratorHelper';
  14. var WRAP_FOR_VALID_ITERATOR = 'WrapForValidIterator';
  15. var setInternalState = InternalStateModule.set;
  16. var createIteratorProxyPrototype = function (IS_ITERATOR) {
  17. var getInternalState = InternalStateModule.getterFor(IS_ITERATOR ? WRAP_FOR_VALID_ITERATOR : ITERATOR_HELPER);
  18. return defineBuiltIns(create(IteratorPrototype), {
  19. next: function next() {
  20. var state = getInternalState(this);
  21. // for simplification:
  22. // for `%WrapForValidIteratorPrototype%.next` our `nextHandler` returns `IterResultObject`
  23. // for `%IteratorHelperPrototype%.next` - just a value
  24. if (IS_ITERATOR) return state.nextHandler();
  25. try {
  26. var result = state.done ? undefined : state.nextHandler();
  27. return createIterResultObject(result, state.done);
  28. } catch (error) {
  29. state.done = true;
  30. throw error;
  31. }
  32. },
  33. 'return': function () {
  34. var state = getInternalState(this);
  35. var iterator = state.iterator;
  36. state.done = true;
  37. if (IS_ITERATOR) {
  38. var returnMethod = getMethod(iterator, 'return');
  39. return returnMethod ? call(returnMethod, iterator) : createIterResultObject(undefined, true);
  40. }
  41. if (state.inner) try {
  42. iteratorClose(state.inner.iterator, 'normal');
  43. } catch (error) {
  44. return iteratorClose(iterator, 'throw', error);
  45. }
  46. if (iterator) iteratorClose(iterator, 'normal');
  47. return createIterResultObject(undefined, true);
  48. }
  49. });
  50. };
  51. var WrapForValidIteratorPrototype = createIteratorProxyPrototype(true);
  52. var IteratorHelperPrototype = createIteratorProxyPrototype(false);
  53. createNonEnumerableProperty(IteratorHelperPrototype, TO_STRING_TAG, 'Iterator Helper');
  54. module.exports = function (nextHandler, IS_ITERATOR) {
  55. var IteratorProxy = function Iterator(record, state) {
  56. if (state) {
  57. state.iterator = record.iterator;
  58. state.next = record.next;
  59. } else state = record;
  60. state.type = IS_ITERATOR ? WRAP_FOR_VALID_ITERATOR : ITERATOR_HELPER;
  61. state.nextHandler = nextHandler;
  62. state.counter = 0;
  63. state.done = false;
  64. setInternalState(this, state);
  65. };
  66. IteratorProxy.prototype = IS_ITERATOR ? WrapForValidIteratorPrototype : IteratorHelperPrototype;
  67. return IteratorProxy;
  68. };