UNPKG

3.02 kBJavaScriptView Raw
1'use strict';
2var call = require('../internals/function-call');
3var create = require('../internals/object-create');
4var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
5var defineBuiltIns = require('../internals/define-built-ins');
6var wellKnownSymbol = require('../internals/well-known-symbol');
7var InternalStateModule = require('../internals/internal-state');
8var getMethod = require('../internals/get-method');
9var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
10var createIterResultObject = require('../internals/create-iter-result-object');
11var iteratorClose = require('../internals/iterator-close');
12
13var TO_STRING_TAG = wellKnownSymbol('toStringTag');
14var ITERATOR_HELPER = 'IteratorHelper';
15var WRAP_FOR_VALID_ITERATOR = 'WrapForValidIterator';
16var setInternalState = InternalStateModule.set;
17
18var 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 // for simplification:
25 // for `%WrapForValidIteratorPrototype%.next` our `nextHandler` returns `IterResultObject`
26 // for `%IteratorHelperPrototype%.next` - just a value
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
55var WrapForValidIteratorPrototype = createIteratorProxyPrototype(true);
56var IteratorHelperPrototype = createIteratorProxyPrototype(false);
57
58createNonEnumerableProperty(IteratorHelperPrototype, TO_STRING_TAG, 'Iterator Helper');
59
60module.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};