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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Array = global.Array; | 9 // var $Array = global.Array; |
10 | 10 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 if (mapping) { | 135 if (mapping) { |
136 if (!IS_SPEC_FUNCTION(mapfn)) { | 136 if (!IS_SPEC_FUNCTION(mapfn)) { |
137 throw MakeTypeError('called_non_callable', [ mapfn ]); | 137 throw MakeTypeError('called_non_callable', [ mapfn ]); |
138 } else if (IS_NULL_OR_UNDEFINED(receiver)) { | 138 } else if (IS_NULL_OR_UNDEFINED(receiver)) { |
139 receiver = %GetDefaultReceiver(mapfn) || receiver; | 139 receiver = %GetDefaultReceiver(mapfn) || receiver; |
140 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) { | 140 } else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) { |
141 receiver = ToObject(receiver); | 141 receiver = ToObject(receiver); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 var iterable = ToIterable(items); | 145 var iterable = GetMethod(items, symbolIterator); |
146 var k; | 146 var k; |
147 var result; | 147 var result; |
148 var mappedValue; | 148 var mappedValue; |
149 var nextValue; | 149 var nextValue; |
150 | 150 |
151 if (!IS_UNDEFINED(iterable)) { | 151 if (!IS_UNDEFINED(iterable)) { |
152 result = %IsConstructor(this) ? new this() : []; | 152 result = %IsConstructor(this) ? new this() : []; |
153 | 153 |
| 154 var iterator = GetIterator(items, iterable); |
| 155 |
154 k = 0; | 156 k = 0; |
155 for (nextValue of items) { | 157 while (true) { |
156 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | 158 var next = iterator.next(); |
157 else mappedValue = nextValue; | 159 |
| 160 if (!IS_OBJECT(next)) { |
| 161 throw MakeTypeError("iterator_result_not_an_object", [next]); |
| 162 } |
| 163 |
| 164 if (next.done) { |
| 165 result.length = k; |
| 166 return result; |
| 167 } |
| 168 |
| 169 nextValue = next.value; |
| 170 if (mapping) { |
| 171 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); |
| 172 } else { |
| 173 mappedValue = nextValue; |
| 174 } |
158 %AddElement(result, k++, mappedValue, NONE); | 175 %AddElement(result, k++, mappedValue, NONE); |
159 } | 176 } |
160 | |
161 result.length = k; | |
162 return result; | |
163 } else { | 177 } else { |
164 var len = ToLength(items.length); | 178 var len = ToLength(items.length); |
165 result = %IsConstructor(this) ? new this(len) : new $Array(len); | 179 result = %IsConstructor(this) ? new this(len) : new $Array(len); |
166 | 180 |
167 for (k = 0; k < len; ++k) { | 181 for (k = 0; k < len; ++k) { |
168 nextValue = items[k]; | 182 nextValue = items[k]; |
169 if (mapping) mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | 183 if (mapping) { |
170 else mappedValue = nextValue; | 184 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); |
| 185 } else { |
| 186 mappedValue = nextValue; |
| 187 } |
171 %AddElement(result, k, mappedValue, NONE); | 188 %AddElement(result, k, mappedValue, NONE); |
172 } | 189 } |
173 | 190 |
174 result.length = k; | 191 result.length = k; |
175 return result; | 192 return result; |
176 } | 193 } |
177 } | 194 } |
178 | 195 |
179 // ES6, draft 05-22-14, section 22.1.2.3 | 196 // ES6, draft 05-22-14, section 22.1.2.3 |
180 function ArrayOf() { | 197 function ArrayOf() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 232 |
216 // Set up the non-enumerable functions on the Array prototype object. | 233 // Set up the non-enumerable functions on the Array prototype object. |
217 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 234 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
218 "find", ArrayFind, | 235 "find", ArrayFind, |
219 "findIndex", ArrayFindIndex, | 236 "findIndex", ArrayFindIndex, |
220 "fill", ArrayFill | 237 "fill", ArrayFill |
221 )); | 238 )); |
222 } | 239 } |
223 | 240 |
224 HarmonyArrayExtendArrayPrototype(); | 241 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |