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