OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 $arrayConcat; | 5 var $arrayConcat; |
6 var $arrayJoin; | 6 var $arrayJoin; |
7 var $arrayPush; | 7 var $arrayPush; |
8 var $arrayPop; | 8 var $arrayPop; |
9 var $arrayShift; | 9 var $arrayShift; |
10 var $arraySlice; | 10 var $arraySlice; |
11 var $arraySplice; | 11 var $arraySplice; |
12 var $arrayUnshift; | 12 var $arrayUnshift; |
13 var $innerArrayForEach; | 13 var $innerArrayForEach; |
14 var $innerArrayEvery; | 14 var $innerArrayEvery; |
| 15 var $innerArrayFilter; |
15 var $innerArrayIndexOf; | 16 var $innerArrayIndexOf; |
16 var $innerArrayLastIndexOf; | 17 var $innerArrayLastIndexOf; |
| 18 var $innerArrayMap; |
17 var $innerArrayReverse; | 19 var $innerArrayReverse; |
| 20 var $innerArraySome; |
18 var $innerArraySort; | 21 var $innerArraySort; |
19 | 22 |
20 (function(global, shared, exports) { | 23 (function(global, shared, exports) { |
21 | 24 |
22 "use strict"; | 25 "use strict"; |
23 | 26 |
24 %CheckIsBootstrapping(); | 27 %CheckIsBootstrapping(); |
25 | 28 |
26 // ------------------------------------------------------------------- | 29 // ------------------------------------------------------------------- |
27 // Imports | 30 // Imports |
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1157 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); | 1160 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); |
1158 | 1161 |
1159 var length = TO_UINT32(this.length); | 1162 var length = TO_UINT32(this.length); |
1160 return %_CallFunction(this, length, comparefn, InnerArraySort); | 1163 return %_CallFunction(this, length, comparefn, InnerArraySort); |
1161 } | 1164 } |
1162 | 1165 |
1163 | 1166 |
1164 // The following functions cannot be made efficient on sparse arrays while | 1167 // The following functions cannot be made efficient on sparse arrays while |
1165 // preserving the semantics, since the calls to the receiver function can add | 1168 // preserving the semantics, since the calls to the receiver function can add |
1166 // or delete elements from the array. | 1169 // or delete elements from the array. |
1167 function ArrayFilter(f, receiver) { | 1170 function InnerArrayFilter(f, receiver, array, length) { |
1168 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter"); | |
1169 | |
1170 // Pull out the length so that modifications to the length in the | |
1171 // loop will not affect the looping and side effects are visible. | |
1172 var array = $toObject(this); | |
1173 var length = $toUint32(array.length); | |
1174 | |
1175 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); | 1171 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
1176 var needs_wrapper = false; | 1172 var needs_wrapper = false; |
1177 if (IS_NULL(receiver)) { | 1173 if (IS_NULL(receiver)) { |
1178 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; | 1174 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; |
1179 } else if (!IS_UNDEFINED(receiver)) { | 1175 } else if (!IS_UNDEFINED(receiver)) { |
1180 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 1176 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
1181 } | 1177 } |
1182 | 1178 |
1183 var result = new GlobalArray(); | |
1184 var accumulator = new InternalArray(); | 1179 var accumulator = new InternalArray(); |
1185 var accumulator_length = 0; | 1180 var accumulator_length = 0; |
1186 var is_array = IS_ARRAY(array); | 1181 var is_array = IS_ARRAY(array); |
1187 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); | 1182 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); |
1188 for (var i = 0; i < length; i++) { | 1183 for (var i = 0; i < length; i++) { |
1189 if (HAS_INDEX(array, i, is_array)) { | 1184 if (HAS_INDEX(array, i, is_array)) { |
1190 var element = array[i]; | 1185 var element = array[i]; |
1191 // Prepare break slots for debugger step in. | 1186 // Prepare break slots for debugger step in. |
1192 if (stepping) %DebugPrepareStepInIfStepping(f); | 1187 if (stepping) %DebugPrepareStepInIfStepping(f); |
1193 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; | 1188 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; |
1194 if (%_CallFunction(new_receiver, element, i, array, f)) { | 1189 if (%_CallFunction(new_receiver, element, i, array, f)) { |
1195 accumulator[accumulator_length++] = element; | 1190 accumulator[accumulator_length++] = element; |
1196 } | 1191 } |
1197 } | 1192 } |
1198 } | 1193 } |
| 1194 return accumulator; |
| 1195 } |
| 1196 |
| 1197 function ArrayFilter(f, receiver) { |
| 1198 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter"); |
| 1199 |
| 1200 // Pull out the length so that modifications to the length in the |
| 1201 // loop will not affect the looping and side effects are visible. |
| 1202 var array = $toObject(this); |
| 1203 var length = $toUint32(array.length); |
| 1204 var accumulator = InnerArrayFilter(f, receiver, array, length); |
| 1205 var result = new GlobalArray(); |
1199 %MoveArrayContents(accumulator, result); | 1206 %MoveArrayContents(accumulator, result); |
1200 return result; | 1207 return result; |
1201 } | 1208 } |
1202 | 1209 |
1203 function InnerArrayForEach(f, receiver, array, length) { | 1210 function InnerArrayForEach(f, receiver, array, length) { |
1204 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); | 1211 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
1205 var needs_wrapper = false; | 1212 var needs_wrapper = false; |
1206 if (IS_NULL(receiver)) { | 1213 if (IS_NULL(receiver)) { |
1207 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; | 1214 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; |
1208 } else if (!IS_UNDEFINED(receiver)) { | 1215 } else if (!IS_UNDEFINED(receiver)) { |
(...skipping 17 matching lines...) Expand all Loading... |
1226 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); | 1233 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); |
1227 | 1234 |
1228 // Pull out the length so that modifications to the length in the | 1235 // Pull out the length so that modifications to the length in the |
1229 // loop will not affect the looping and side effects are visible. | 1236 // loop will not affect the looping and side effects are visible. |
1230 var array = $toObject(this); | 1237 var array = $toObject(this); |
1231 var length = TO_UINT32(array.length); | 1238 var length = TO_UINT32(array.length); |
1232 InnerArrayForEach(f, receiver, array, length); | 1239 InnerArrayForEach(f, receiver, array, length); |
1233 } | 1240 } |
1234 | 1241 |
1235 | 1242 |
1236 // Executes the function once for each element present in the | 1243 function InnerArraySome(f, receiver, array, length) { |
1237 // array until it finds one where callback returns true. | |
1238 function ArraySome(f, receiver) { | |
1239 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); | |
1240 | |
1241 // Pull out the length so that modifications to the length in the | |
1242 // loop will not affect the looping and side effects are visible. | |
1243 var array = $toObject(this); | |
1244 var length = TO_UINT32(array.length); | |
1245 | |
1246 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); | 1244 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
1247 var needs_wrapper = false; | 1245 var needs_wrapper = false; |
1248 if (IS_NULL(receiver)) { | 1246 if (IS_NULL(receiver)) { |
1249 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; | 1247 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; |
1250 } else if (!IS_UNDEFINED(receiver)) { | 1248 } else if (!IS_UNDEFINED(receiver)) { |
1251 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 1249 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
1252 } | 1250 } |
1253 | 1251 |
1254 var is_array = IS_ARRAY(array); | 1252 var is_array = IS_ARRAY(array); |
1255 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); | 1253 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); |
1256 for (var i = 0; i < length; i++) { | 1254 for (var i = 0; i < length; i++) { |
1257 if (HAS_INDEX(array, i, is_array)) { | 1255 if (HAS_INDEX(array, i, is_array)) { |
1258 var element = array[i]; | 1256 var element = array[i]; |
1259 // Prepare break slots for debugger step in. | 1257 // Prepare break slots for debugger step in. |
1260 if (stepping) %DebugPrepareStepInIfStepping(f); | 1258 if (stepping) %DebugPrepareStepInIfStepping(f); |
1261 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; | 1259 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; |
1262 if (%_CallFunction(new_receiver, element, i, array, f)) return true; | 1260 if (%_CallFunction(new_receiver, element, i, array, f)) return true; |
1263 } | 1261 } |
1264 } | 1262 } |
1265 return false; | 1263 return false; |
1266 } | 1264 } |
1267 | 1265 |
1268 | 1266 |
| 1267 // Executes the function once for each element present in the |
| 1268 // array until it finds one where callback returns true. |
| 1269 function ArraySome(f, receiver) { |
| 1270 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); |
| 1271 |
| 1272 // Pull out the length so that modifications to the length in the |
| 1273 // loop will not affect the looping and side effects are visible. |
| 1274 var array = $toObject(this); |
| 1275 var length = TO_UINT32(array.length); |
| 1276 return InnerArraySome(f, receiver, array, length); |
| 1277 } |
| 1278 |
| 1279 |
1269 function InnerArrayEvery(f, receiver, array, length) { | 1280 function InnerArrayEvery(f, receiver, array, length) { |
1270 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); | 1281 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
1271 var needs_wrapper = false; | 1282 var needs_wrapper = false; |
1272 if (IS_NULL(receiver)) { | 1283 if (IS_NULL(receiver)) { |
1273 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; | 1284 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; |
1274 } else if (!IS_UNDEFINED(receiver)) { | 1285 } else if (!IS_UNDEFINED(receiver)) { |
1275 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 1286 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
1276 } | 1287 } |
1277 | 1288 |
1278 var is_array = IS_ARRAY(array); | 1289 var is_array = IS_ARRAY(array); |
(...skipping 14 matching lines...) Expand all Loading... |
1293 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); | 1304 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); |
1294 | 1305 |
1295 // Pull out the length so that modifications to the length in the | 1306 // Pull out the length so that modifications to the length in the |
1296 // loop will not affect the looping and side effects are visible. | 1307 // loop will not affect the looping and side effects are visible. |
1297 var array = $toObject(this); | 1308 var array = $toObject(this); |
1298 var length = TO_UINT32(array.length); | 1309 var length = TO_UINT32(array.length); |
1299 return InnerArrayEvery(f, receiver, array, length); | 1310 return InnerArrayEvery(f, receiver, array, length); |
1300 } | 1311 } |
1301 | 1312 |
1302 | 1313 |
1303 function ArrayMap(f, receiver) { | 1314 function InnerArrayMap(f, receiver, array, length) { |
1304 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); | |
1305 | |
1306 // Pull out the length so that modifications to the length in the | |
1307 // loop will not affect the looping and side effects are visible. | |
1308 var array = $toObject(this); | |
1309 var length = TO_UINT32(array.length); | |
1310 | |
1311 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); | 1315 if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
1312 var needs_wrapper = false; | 1316 var needs_wrapper = false; |
1313 if (IS_NULL(receiver)) { | 1317 if (IS_NULL(receiver)) { |
1314 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; | 1318 if (%IsSloppyModeFunction(f)) receiver = UNDEFINED; |
1315 } else if (!IS_UNDEFINED(receiver)) { | 1319 } else if (!IS_UNDEFINED(receiver)) { |
1316 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); | 1320 needs_wrapper = SHOULD_CREATE_WRAPPER(f, receiver); |
1317 } | 1321 } |
1318 | 1322 |
1319 var result = new GlobalArray(); | |
1320 var accumulator = new InternalArray(length); | 1323 var accumulator = new InternalArray(length); |
1321 var is_array = IS_ARRAY(array); | 1324 var is_array = IS_ARRAY(array); |
1322 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); | 1325 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f); |
1323 for (var i = 0; i < length; i++) { | 1326 for (var i = 0; i < length; i++) { |
1324 if (HAS_INDEX(array, i, is_array)) { | 1327 if (HAS_INDEX(array, i, is_array)) { |
1325 var element = array[i]; | 1328 var element = array[i]; |
1326 // Prepare break slots for debugger step in. | 1329 // Prepare break slots for debugger step in. |
1327 if (stepping) %DebugPrepareStepInIfStepping(f); | 1330 if (stepping) %DebugPrepareStepInIfStepping(f); |
1328 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; | 1331 var new_receiver = needs_wrapper ? $toObject(receiver) : receiver; |
1329 accumulator[i] = %_CallFunction(new_receiver, element, i, array, f); | 1332 accumulator[i] = %_CallFunction(new_receiver, element, i, array, f); |
1330 } | 1333 } |
1331 } | 1334 } |
| 1335 return accumulator; |
| 1336 } |
| 1337 |
| 1338 |
| 1339 function ArrayMap(f, receiver) { |
| 1340 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); |
| 1341 |
| 1342 // Pull out the length so that modifications to the length in the |
| 1343 // loop will not affect the looping and side effects are visible. |
| 1344 var array = $toObject(this); |
| 1345 var length = TO_UINT32(array.length); |
| 1346 var accumulator = InnerArrayMap(f, receiver, array, length); |
| 1347 var result = new GlobalArray(); |
1332 %MoveArrayContents(accumulator, result); | 1348 %MoveArrayContents(accumulator, result); |
1333 return result; | 1349 return result; |
1334 } | 1350 } |
1335 | 1351 |
1336 | 1352 |
1337 // For .indexOf, we don't need to pass in the number of arguments | 1353 // For .indexOf, we don't need to pass in the number of arguments |
1338 // at the callsite since ToInteger(undefined) == 0; however, for | 1354 // at the callsite since ToInteger(undefined) == 0; however, for |
1339 // .lastIndexOf, we need to pass it, since the behavior for passing | 1355 // .lastIndexOf, we need to pass it, since the behavior for passing |
1340 // undefined is 0 but for not including the argument is length-1. | 1356 // undefined is 0 but for not including the argument is length-1. |
1341 function InnerArrayIndexOf(element, index, length) { | 1357 function InnerArrayIndexOf(element, index, length) { |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1629 | 1645 |
1630 $arrayConcat = ArrayConcatJS; | 1646 $arrayConcat = ArrayConcatJS; |
1631 $arrayJoin = ArrayJoin; | 1647 $arrayJoin = ArrayJoin; |
1632 $arrayPush = ArrayPush; | 1648 $arrayPush = ArrayPush; |
1633 $arrayPop = ArrayPop; | 1649 $arrayPop = ArrayPop; |
1634 $arrayShift = ArrayShift; | 1650 $arrayShift = ArrayShift; |
1635 $arraySlice = ArraySlice; | 1651 $arraySlice = ArraySlice; |
1636 $arraySplice = ArraySplice; | 1652 $arraySplice = ArraySplice; |
1637 $arrayUnshift = ArrayUnshift; | 1653 $arrayUnshift = ArrayUnshift; |
1638 | 1654 |
| 1655 $innerArrayEvery = InnerArrayEvery; |
| 1656 $innerArrayFilter = InnerArrayFilter; |
1639 $innerArrayForEach = InnerArrayForEach; | 1657 $innerArrayForEach = InnerArrayForEach; |
1640 $innerArrayEvery = InnerArrayEvery; | |
1641 $innerArrayIndexOf = InnerArrayIndexOf; | 1658 $innerArrayIndexOf = InnerArrayIndexOf; |
1642 $innerArrayLastIndexOf = InnerArrayLastIndexOf; | 1659 $innerArrayLastIndexOf = InnerArrayLastIndexOf; |
| 1660 $innerArrayMap = InnerArrayMap; |
1643 $innerArrayReverse = InnerArrayReverse; | 1661 $innerArrayReverse = InnerArrayReverse; |
| 1662 $innerArraySome = InnerArraySome; |
1644 $innerArraySort = InnerArraySort; | 1663 $innerArraySort = InnerArraySort; |
1645 | 1664 |
1646 }); | 1665 }); |
OLD | NEW |