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 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 'use strict'; | 7 'use strict'; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var GlobalArray = global.Array; | 14 var GlobalArray = global.Array; |
15 var GlobalSymbol = global.Symbol; | 15 var GlobalSymbol = global.Symbol; |
16 | 16 |
17 var GetIterator; | 17 var GetIterator; |
18 var GetMethod; | 18 var GetMethod; |
19 var MathMax; | 19 var MathMax; |
20 var MathMin; | 20 var MathMin; |
21 var ObjectIsFrozen; | 21 var ObjectIsFrozen; |
| 22 var ObjectDefineProperty; |
22 | 23 |
23 utils.Import(function(from) { | 24 utils.Import(function(from) { |
24 GetIterator = from.GetIterator; | 25 GetIterator = from.GetIterator; |
25 GetMethod = from.GetMethod; | 26 GetMethod = from.GetMethod; |
26 MathMax = from.MathMax; | 27 MathMax = from.MathMax; |
27 MathMin = from.MathMin; | 28 MathMin = from.MathMin; |
28 ObjectIsFrozen = from.ObjectIsFrozen; | 29 ObjectIsFrozen = from.ObjectIsFrozen; |
| 30 ObjectDefineProperty = from.ObjectDefineProperty; |
29 }); | 31 }); |
30 | 32 |
31 // ------------------------------------------------------------------- | 33 // ------------------------------------------------------------------- |
32 | 34 |
33 function InnerArrayCopyWithin(target, start, end, array, length) { | 35 function InnerArrayCopyWithin(target, start, end, array, length) { |
34 target = TO_INTEGER(target); | 36 target = TO_INTEGER(target); |
35 var to; | 37 var to; |
36 if (target < 0) { | 38 if (target < 0) { |
37 to = MathMax(length + target, 0); | 39 to = MathMax(length + target, 0); |
38 } else { | 40 } else { |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 // ES6, draft 04-05-14, section 22.1.3.6 | 186 // ES6, draft 04-05-14, section 22.1.3.6 |
185 function ArrayFill(value, start, end) { | 187 function ArrayFill(value, start, end) { |
186 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); | 188 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); |
187 | 189 |
188 var array = $toObject(this); | 190 var array = $toObject(this); |
189 var length = TO_UINT32(array.length); | 191 var length = TO_UINT32(array.length); |
190 | 192 |
191 return InnerArrayFill(value, start, end, array, length); | 193 return InnerArrayFill(value, start, end, array, length); |
192 } | 194 } |
193 | 195 |
| 196 function AddArrayElement(constructor, array, i, value) { |
| 197 if (constructor === GlobalArray) { |
| 198 %AddElement(array, i, value); |
| 199 } else { |
| 200 ObjectDefineProperty(array, i, { |
| 201 value: value, writable: true, configurable: true, enumerable: true |
| 202 }); |
| 203 } |
| 204 } |
| 205 |
194 // ES6, draft 10-14-14, section 22.1.2.1 | 206 // ES6, draft 10-14-14, section 22.1.2.1 |
195 function ArrayFrom(arrayLike, mapfn, receiver) { | 207 function ArrayFrom(arrayLike, mapfn, receiver) { |
196 var items = $toObject(arrayLike); | 208 var items = $toObject(arrayLike); |
197 var mapping = !IS_UNDEFINED(mapfn); | 209 var mapping = !IS_UNDEFINED(mapfn); |
198 | 210 |
199 if (mapping) { | 211 if (mapping) { |
200 if (!IS_SPEC_FUNCTION(mapfn)) { | 212 if (!IS_SPEC_FUNCTION(mapfn)) { |
201 throw MakeTypeError(kCalledNonCallable, mapfn); | 213 throw MakeTypeError(kCalledNonCallable, mapfn); |
202 } else if (%IsSloppyModeFunction(mapfn)) { | 214 } else if (%IsSloppyModeFunction(mapfn)) { |
203 if (IS_NULL(receiver)) { | 215 if (IS_NULL(receiver)) { |
(...skipping 27 matching lines...) Expand all Loading... |
231 result.length = k; | 243 result.length = k; |
232 return result; | 244 return result; |
233 } | 245 } |
234 | 246 |
235 nextValue = next.value; | 247 nextValue = next.value; |
236 if (mapping) { | 248 if (mapping) { |
237 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | 249 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); |
238 } else { | 250 } else { |
239 mappedValue = nextValue; | 251 mappedValue = nextValue; |
240 } | 252 } |
241 // TODO(verwaest): This should redefine rather than adding. | 253 AddArrayElement(this, result, k, mappedValue); |
242 %AddElement(result, k++, mappedValue); | 254 k++; |
243 } | 255 } |
244 } else { | 256 } else { |
245 var len = $toLength(items.length); | 257 var len = $toLength(items.length); |
246 result = %IsConstructor(this) ? new this(len) : new GlobalArray(len); | 258 result = %IsConstructor(this) ? new this(len) : new GlobalArray(len); |
247 | 259 |
248 for (k = 0; k < len; ++k) { | 260 for (k = 0; k < len; ++k) { |
249 nextValue = items[k]; | 261 nextValue = items[k]; |
250 if (mapping) { | 262 if (mapping) { |
251 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); | 263 mappedValue = %_CallFunction(receiver, nextValue, k, mapfn); |
252 } else { | 264 } else { |
253 mappedValue = nextValue; | 265 mappedValue = nextValue; |
254 } | 266 } |
255 // TODO(verwaest): This should redefine rather than adding. | 267 AddArrayElement(this, result, k, mappedValue); |
256 %AddElement(result, k, mappedValue); | |
257 } | 268 } |
258 | 269 |
259 result.length = k; | 270 result.length = k; |
260 return result; | 271 return result; |
261 } | 272 } |
262 } | 273 } |
263 | 274 |
264 // ES6, draft 05-22-14, section 22.1.2.3 | 275 // ES6, draft 05-22-14, section 22.1.2.3 |
265 function ArrayOf() { | 276 function ArrayOf() { |
266 var length = %_ArgumentsLength(); | 277 var length = %_ArgumentsLength(); |
267 var constructor = this; | 278 var constructor = this; |
268 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 279 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
269 var array = %IsConstructor(constructor) ? new constructor(length) : []; | 280 var array = %IsConstructor(constructor) ? new constructor(length) : []; |
270 for (var i = 0; i < length; i++) { | 281 for (var i = 0; i < length; i++) { |
271 // TODO(verwaest): This should redefine rather than adding. | 282 AddArrayElement(constructor, array, i, %_Arguments(i)); |
272 %AddElement(array, i, %_Arguments(i)); | |
273 } | 283 } |
274 array.length = length; | 284 array.length = length; |
275 return array; | 285 return array; |
276 } | 286 } |
277 | 287 |
278 // ------------------------------------------------------------------- | 288 // ------------------------------------------------------------------- |
279 | 289 |
280 utils.InstallConstants(GlobalSymbol, [ | 290 utils.InstallConstants(GlobalSymbol, [ |
281 // TODO(dslomov, caitp): Move to symbol.js when shipping | 291 // TODO(dslomov, caitp): Move to symbol.js when shipping |
282 "isConcatSpreadable", symbolIsConcatSpreadable | 292 "isConcatSpreadable", symbolIsConcatSpreadable |
(...skipping 24 matching lines...) Expand all Loading... |
307 | 317 |
308 utils.Export(function(to) { | 318 utils.Export(function(to) { |
309 to.ArrayFrom = ArrayFrom; | 319 to.ArrayFrom = ArrayFrom; |
310 to.InnerArrayCopyWithin = InnerArrayCopyWithin; | 320 to.InnerArrayCopyWithin = InnerArrayCopyWithin; |
311 to.InnerArrayFill = InnerArrayFill; | 321 to.InnerArrayFill = InnerArrayFill; |
312 to.InnerArrayFind = InnerArrayFind; | 322 to.InnerArrayFind = InnerArrayFind; |
313 to.InnerArrayFindIndex = InnerArrayFindIndex; | 323 to.InnerArrayFindIndex = InnerArrayFindIndex; |
314 }); | 324 }); |
315 | 325 |
316 }) | 326 }) |
OLD | NEW |