js/src/builtin/Iterator.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:281f0ca8a125
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 function IteratorIdentity() {
6 return this;
7 }
8
9 var LegacyIteratorWrapperMap = new std_WeakMap();
10
11 function LegacyIteratorNext(arg) {
12 var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this);
13 try {
14 return { value: iter.next(arg), done: false };
15 } catch (e) {
16 if (e instanceof std_StopIteration)
17 return { value: undefined, done: true };
18 throw e;
19 }
20 }
21
22 function LegacyIteratorThrow(exn) {
23 var iter = callFunction(std_WeakMap_get, LegacyIteratorWrapperMap, this);
24 try {
25 return { value: iter.throw(exn), done: false };
26 } catch (e) {
27 if (e instanceof std_StopIteration)
28 return { value: undefined, done: true };
29 throw e;
30 }
31 }
32
33 function LegacyIterator(iter) {
34 callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter);
35 }
36
37 function LegacyGeneratorIterator(iter) {
38 callFunction(std_WeakMap_set, LegacyIteratorWrapperMap, this, iter);
39 }
40
41 var LegacyIteratorsInitialized = std_Object_create(null);
42
43 function InitLegacyIterators() {
44 var props = std_Object_create(null);
45
46 props.next = std_Object_create(null);
47 props.next.value = LegacyIteratorNext;
48 props.next.enumerable = false;
49 props.next.configurable = true;
50 props.next.writable = true;
51
52 props[std_iterator] = std_Object_create(null);
53 props[std_iterator].value = IteratorIdentity;
54 props[std_iterator].enumerable = false;
55 props[std_iterator].configurable = true;
56 props[std_iterator].writable = true;
57
58 var LegacyIteratorProto = std_Object_create(GetIteratorPrototype(), props);
59 MakeConstructible(LegacyIterator, LegacyIteratorProto);
60
61 props.throw = std_Object_create(null);
62 props.throw.value = LegacyIteratorThrow;
63 props.throw.enumerable = false;
64 props.throw.configurable = true;
65 props.throw.writable = true;
66
67 var LegacyGeneratorIteratorProto = std_Object_create(GetIteratorPrototype(), props);
68 MakeConstructible(LegacyGeneratorIterator, LegacyGeneratorIteratorProto);
69
70 LegacyIteratorsInitialized.initialized = true;
71 }
72
73 function NewLegacyIterator(iter, wrapper) {
74 if (!LegacyIteratorsInitialized.initialized)
75 InitLegacyIterators();
76
77 return new wrapper(iter);
78 }
79
80 function LegacyIteratorShim() {
81 return NewLegacyIterator(ToObject(this), LegacyIterator);
82 }
83
84 function LegacyGeneratorIteratorShim() {
85 return NewLegacyIterator(ToObject(this), LegacyGeneratorIterator);
86 }

mercurial