OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var $arrayValues; | 5 var $arrayValues; |
6 | 6 |
7 (function(global, utils) { | 7 (function(global, utils) { |
8 | 8 |
9 "use strict"; | 9 "use strict"; |
10 | 10 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 function CreateArrayIterator(array, kind) { | 55 function CreateArrayIterator(array, kind) { |
56 var object = TO_OBJECT(array); | 56 var object = TO_OBJECT(array); |
57 var iterator = new ArrayIterator; | 57 var iterator = new ArrayIterator; |
58 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object); | 58 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object); |
59 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0); | 59 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0); |
60 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind); | 60 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind); |
61 return iterator; | 61 return iterator; |
62 } | 62 } |
63 | 63 |
64 | 64 |
65 // 15.19.4.3.4 CreateItrResultObject | |
66 function CreateIteratorResultObject(value, done) { | |
67 return {value: value, done: done}; | |
68 } | |
69 | |
70 | |
71 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator] | 65 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator] |
72 function ArrayIteratorIterator() { | 66 function ArrayIteratorIterator() { |
73 return this; | 67 return this; |
74 } | 68 } |
75 | 69 |
76 | 70 |
77 // 15.4.5.2.2 ArrayIterator.prototype.next( ) | 71 // ES6 section 22.1.5.2.1 %ArrayIteratorPrototype%.next( ) |
78 function ArrayIteratorNext() { | 72 function ArrayIteratorNext() { |
79 var iterator = this; | 73 var iterator = this; |
| 74 var value = UNDEFINED; |
| 75 var done = true; |
80 | 76 |
81 if (!IS_SPEC_OBJECT(iterator) || | 77 if (!IS_SPEC_OBJECT(iterator) || |
82 !HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) { | 78 !HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) { |
83 throw MakeTypeError(kIncompatibleMethodReceiver, | 79 throw MakeTypeError(kIncompatibleMethodReceiver, |
84 'Array Iterator.prototype.next', this); | 80 'Array Iterator.prototype.next', this); |
85 } | 81 } |
86 | 82 |
87 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol); | 83 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol); |
88 if (IS_UNDEFINED(array)) { | 84 if (!IS_UNDEFINED(array)) { |
89 return CreateIteratorResultObject(UNDEFINED, true); | 85 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol); |
| 86 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol); |
| 87 var length = TO_UINT32(array.length); |
| 88 |
| 89 // "sparse" is never used. |
| 90 |
| 91 if (index >= length) { |
| 92 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED); |
| 93 } else { |
| 94 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1); |
| 95 |
| 96 if (itemKind == ITERATOR_KIND_VALUES) { |
| 97 value = array[index]; |
| 98 } else if (itemKind == ITERATOR_KIND_ENTRIES) { |
| 99 value = [index, array[index]]; |
| 100 } else { |
| 101 value = index; |
| 102 } |
| 103 done = false; |
| 104 } |
90 } | 105 } |
91 | 106 |
92 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol); | 107 return %_CreateIterResultObject(value, done); |
93 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol); | |
94 var length = TO_UINT32(array.length); | |
95 | |
96 // "sparse" is never used. | |
97 | |
98 if (index >= length) { | |
99 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED); | |
100 return CreateIteratorResultObject(UNDEFINED, true); | |
101 } | |
102 | |
103 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1); | |
104 | |
105 if (itemKind == ITERATOR_KIND_VALUES) { | |
106 return CreateIteratorResultObject(array[index], false); | |
107 } | |
108 | |
109 if (itemKind == ITERATOR_KIND_ENTRIES) { | |
110 return CreateIteratorResultObject([index, array[index]], false); | |
111 } | |
112 | |
113 return CreateIteratorResultObject(index, false); | |
114 } | 108 } |
115 | 109 |
116 | 110 |
117 function ArrayEntries() { | 111 function ArrayEntries() { |
118 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES); | 112 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES); |
119 } | 113 } |
120 | 114 |
121 | 115 |
122 function ArrayValues() { | 116 function ArrayValues() { |
123 return CreateArrayIterator(this, ITERATOR_KIND_VALUES); | 117 return CreateArrayIterator(this, ITERATOR_KIND_VALUES); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 %AddNamedProperty(GlobalNAME.prototype, 'keys', ArrayKeys, DONT_ENUM); | 154 %AddNamedProperty(GlobalNAME.prototype, 'keys', ArrayKeys, DONT_ENUM); |
161 %AddNamedProperty(GlobalNAME.prototype, iteratorSymbol, ArrayValues, | 155 %AddNamedProperty(GlobalNAME.prototype, iteratorSymbol, ArrayValues, |
162 DONT_ENUM); | 156 DONT_ENUM); |
163 endmacro | 157 endmacro |
164 | 158 |
165 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | 159 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) |
166 | 160 |
167 // ------------------------------------------------------------------- | 161 // ------------------------------------------------------------------- |
168 // Exports | 162 // Exports |
169 | 163 |
170 utils.Export(function(to) { | |
171 to.ArrayIteratorCreateResultObject = CreateIteratorResultObject; | |
172 }); | |
173 | |
174 $arrayValues = ArrayValues; | 164 $arrayValues = ArrayValues; |
175 | 165 |
176 %InstallToContext(["array_values_iterator", ArrayValues]); | 166 %InstallToContext(["array_values_iterator", ArrayValues]); |
177 | 167 |
178 }) | 168 }) |
OLD | NEW |