| 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 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
| 6 | 6 |
| 7 "use strict"; | 7 "use strict"; |
| 8 | 8 |
| 9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
| 10 | 10 |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 323 } |
| 324 } | 324 } |
| 325 | 325 |
| 326 | 326 |
| 327 // This is part of the old simple-minded splice. We are using it either | 327 // This is part of the old simple-minded splice. We are using it either |
| 328 // because the receiver is not an array (so we have no choice) or because we | 328 // because the receiver is not an array (so we have no choice) or because we |
| 329 // know we are not deleting or moving a lot of elements. | 329 // know we are not deleting or moving a lot of elements. |
| 330 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { | 330 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { |
| 331 for (var i = 0; i < del_count; i++) { | 331 for (var i = 0; i < del_count; i++) { |
| 332 var index = start_i + i; | 332 var index = start_i + i; |
| 333 if (HAS_INDEX(array, index)) { | 333 if (index in array) { |
| 334 var current = array[index]; | 334 var current = array[index]; |
| 335 %CreateDataProperty(deleted_elements, i, current); | 335 %CreateDataProperty(deleted_elements, i, current); |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 | 340 |
| 341 function SimpleMove(array, start_i, del_count, len, num_additional_args) { | 341 function SimpleMove(array, start_i, del_count, len, num_additional_args) { |
| 342 if (num_additional_args !== del_count) { | 342 if (num_additional_args !== del_count) { |
| 343 // Move the existing elements after the elements to be deleted | 343 // Move the existing elements after the elements to be deleted |
| 344 // to the right position in the resulting array. | 344 // to the right position in the resulting array. |
| 345 if (num_additional_args > del_count) { | 345 if (num_additional_args > del_count) { |
| 346 for (var i = len - del_count; i > start_i; i--) { | 346 for (var i = len - del_count; i > start_i; i--) { |
| 347 var from_index = i + del_count - 1; | 347 var from_index = i + del_count - 1; |
| 348 var to_index = i + num_additional_args - 1; | 348 var to_index = i + num_additional_args - 1; |
| 349 if (HAS_INDEX(array, from_index)) { | 349 if (from_index in array) { |
| 350 array[to_index] = array[from_index]; | 350 array[to_index] = array[from_index]; |
| 351 } else { | 351 } else { |
| 352 delete array[to_index]; | 352 delete array[to_index]; |
| 353 } | 353 } |
| 354 } | 354 } |
| 355 } else { | 355 } else { |
| 356 for (var i = start_i; i < len - del_count; i++) { | 356 for (var i = start_i; i < len - del_count; i++) { |
| 357 var from_index = i + del_count; | 357 var from_index = i + del_count; |
| 358 var to_index = i + num_additional_args; | 358 var to_index = i + num_additional_args; |
| 359 if (HAS_INDEX(array, from_index)) { | 359 if (from_index in array) { |
| 360 array[to_index] = array[from_index]; | 360 array[to_index] = array[from_index]; |
| 361 } else { | 361 } else { |
| 362 delete array[to_index]; | 362 delete array[to_index]; |
| 363 } | 363 } |
| 364 } | 364 } |
| 365 for (var i = len; i > len - del_count + num_additional_args; i--) { | 365 for (var i = len; i > len - del_count + num_additional_args; i--) { |
| 366 delete array[i - 1]; | 366 delete array[i - 1]; |
| 367 } | 367 } |
| 368 } | 368 } |
| 369 } | 369 } |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 return InnerArraySort(array, length, comparefn); | 1047 return InnerArraySort(array, length, comparefn); |
| 1048 } | 1048 } |
| 1049 | 1049 |
| 1050 | 1050 |
| 1051 // The following functions cannot be made efficient on sparse arrays while | 1051 // The following functions cannot be made efficient on sparse arrays while |
| 1052 // preserving the semantics, since the calls to the receiver function can add | 1052 // preserving the semantics, since the calls to the receiver function can add |
| 1053 // or delete elements from the array. | 1053 // or delete elements from the array. |
| 1054 function InnerArrayFilter(f, receiver, array, length, result) { | 1054 function InnerArrayFilter(f, receiver, array, length, result) { |
| 1055 var result_length = 0; | 1055 var result_length = 0; |
| 1056 for (var i = 0; i < length; i++) { | 1056 for (var i = 0; i < length; i++) { |
| 1057 if (HAS_INDEX(array, i)) { | 1057 if (i in array) { |
| 1058 var element = array[i]; | 1058 var element = array[i]; |
| 1059 if (%_Call(f, receiver, element, i, array)) { | 1059 if (%_Call(f, receiver, element, i, array)) { |
| 1060 %CreateDataProperty(result, result_length, element); | 1060 %CreateDataProperty(result, result_length, element); |
| 1061 result_length++; | 1061 result_length++; |
| 1062 } | 1062 } |
| 1063 } | 1063 } |
| 1064 } | 1064 } |
| 1065 return result; | 1065 return result; |
| 1066 } | 1066 } |
| 1067 | 1067 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1078 var result = ArraySpeciesCreate(array, 0); | 1078 var result = ArraySpeciesCreate(array, 0); |
| 1079 return InnerArrayFilter(f, receiver, array, length, result); | 1079 return InnerArrayFilter(f, receiver, array, length, result); |
| 1080 } | 1080 } |
| 1081 | 1081 |
| 1082 | 1082 |
| 1083 function InnerArrayForEach(f, receiver, array, length) { | 1083 function InnerArrayForEach(f, receiver, array, length) { |
| 1084 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); | 1084 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 1085 | 1085 |
| 1086 if (IS_UNDEFINED(receiver)) { | 1086 if (IS_UNDEFINED(receiver)) { |
| 1087 for (var i = 0; i < length; i++) { | 1087 for (var i = 0; i < length; i++) { |
| 1088 if (HAS_INDEX(array, i)) { | 1088 if (i in array) { |
| 1089 var element = array[i]; | 1089 var element = array[i]; |
| 1090 f(element, i, array); | 1090 f(element, i, array); |
| 1091 } | 1091 } |
| 1092 } | 1092 } |
| 1093 } else { | 1093 } else { |
| 1094 for (var i = 0; i < length; i++) { | 1094 for (var i = 0; i < length; i++) { |
| 1095 if (HAS_INDEX(array, i)) { | 1095 if (i in array) { |
| 1096 var element = array[i]; | 1096 var element = array[i]; |
| 1097 %_Call(f, receiver, element, i, array); | 1097 %_Call(f, receiver, element, i, array); |
| 1098 } | 1098 } |
| 1099 } | 1099 } |
| 1100 } | 1100 } |
| 1101 } | 1101 } |
| 1102 | 1102 |
| 1103 | 1103 |
| 1104 function ArrayForEach(f, receiver) { | 1104 function ArrayForEach(f, receiver) { |
| 1105 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); | 1105 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach"); |
| 1106 | 1106 |
| 1107 // Pull out the length so that modifications to the length in the | 1107 // Pull out the length so that modifications to the length in the |
| 1108 // loop will not affect the looping and side effects are visible. | 1108 // loop will not affect the looping and side effects are visible. |
| 1109 var array = TO_OBJECT(this); | 1109 var array = TO_OBJECT(this); |
| 1110 var length = TO_LENGTH(array.length); | 1110 var length = TO_LENGTH(array.length); |
| 1111 InnerArrayForEach(f, receiver, array, length); | 1111 InnerArrayForEach(f, receiver, array, length); |
| 1112 } | 1112 } |
| 1113 | 1113 |
| 1114 | 1114 |
| 1115 function InnerArraySome(f, receiver, array, length) { | 1115 function InnerArraySome(f, receiver, array, length) { |
| 1116 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); | 1116 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 1117 | 1117 |
| 1118 for (var i = 0; i < length; i++) { | 1118 for (var i = 0; i < length; i++) { |
| 1119 if (HAS_INDEX(array, i)) { | 1119 if (i in array) { |
| 1120 var element = array[i]; | 1120 var element = array[i]; |
| 1121 if (%_Call(f, receiver, element, i, array)) return true; | 1121 if (%_Call(f, receiver, element, i, array)) return true; |
| 1122 } | 1122 } |
| 1123 } | 1123 } |
| 1124 return false; | 1124 return false; |
| 1125 } | 1125 } |
| 1126 | 1126 |
| 1127 | 1127 |
| 1128 // Executes the function once for each element present in the | 1128 // Executes the function once for each element present in the |
| 1129 // array until it finds one where callback returns true. | 1129 // array until it finds one where callback returns true. |
| 1130 function ArraySome(f, receiver) { | 1130 function ArraySome(f, receiver) { |
| 1131 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); | 1131 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some"); |
| 1132 | 1132 |
| 1133 // Pull out the length so that modifications to the length in the | 1133 // Pull out the length so that modifications to the length in the |
| 1134 // loop will not affect the looping and side effects are visible. | 1134 // loop will not affect the looping and side effects are visible. |
| 1135 var array = TO_OBJECT(this); | 1135 var array = TO_OBJECT(this); |
| 1136 var length = TO_LENGTH(array.length); | 1136 var length = TO_LENGTH(array.length); |
| 1137 return InnerArraySome(f, receiver, array, length); | 1137 return InnerArraySome(f, receiver, array, length); |
| 1138 } | 1138 } |
| 1139 | 1139 |
| 1140 | 1140 |
| 1141 function InnerArrayEvery(f, receiver, array, length) { | 1141 function InnerArrayEvery(f, receiver, array, length) { |
| 1142 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); | 1142 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 1143 | 1143 |
| 1144 for (var i = 0; i < length; i++) { | 1144 for (var i = 0; i < length; i++) { |
| 1145 if (HAS_INDEX(array, i)) { | 1145 if (i in array) { |
| 1146 var element = array[i]; | 1146 var element = array[i]; |
| 1147 if (!%_Call(f, receiver, element, i, array)) return false; | 1147 if (!%_Call(f, receiver, element, i, array)) return false; |
| 1148 } | 1148 } |
| 1149 } | 1149 } |
| 1150 return true; | 1150 return true; |
| 1151 } | 1151 } |
| 1152 | 1152 |
| 1153 function ArrayEvery(f, receiver) { | 1153 function ArrayEvery(f, receiver) { |
| 1154 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); | 1154 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every"); |
| 1155 | 1155 |
| 1156 // Pull out the length so that modifications to the length in the | 1156 // Pull out the length so that modifications to the length in the |
| 1157 // loop will not affect the looping and side effects are visible. | 1157 // loop will not affect the looping and side effects are visible. |
| 1158 var array = TO_OBJECT(this); | 1158 var array = TO_OBJECT(this); |
| 1159 var length = TO_LENGTH(array.length); | 1159 var length = TO_LENGTH(array.length); |
| 1160 return InnerArrayEvery(f, receiver, array, length); | 1160 return InnerArrayEvery(f, receiver, array, length); |
| 1161 } | 1161 } |
| 1162 | 1162 |
| 1163 | 1163 |
| 1164 function ArrayMap(f, receiver) { | 1164 function ArrayMap(f, receiver) { |
| 1165 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); | 1165 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map"); |
| 1166 | 1166 |
| 1167 // Pull out the length so that modifications to the length in the | 1167 // Pull out the length so that modifications to the length in the |
| 1168 // loop will not affect the looping and side effects are visible. | 1168 // loop will not affect the looping and side effects are visible. |
| 1169 var array = TO_OBJECT(this); | 1169 var array = TO_OBJECT(this); |
| 1170 var length = TO_LENGTH(array.length); | 1170 var length = TO_LENGTH(array.length); |
| 1171 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); | 1171 if (!IS_CALLABLE(f)) throw MakeTypeError(kCalledNonCallable, f); |
| 1172 var result = ArraySpeciesCreate(array, length); | 1172 var result = ArraySpeciesCreate(array, length); |
| 1173 for (var i = 0; i < length; i++) { | 1173 for (var i = 0; i < length; i++) { |
| 1174 if (HAS_INDEX(array, i)) { | 1174 if (i in array) { |
| 1175 var element = array[i]; | 1175 var element = array[i]; |
| 1176 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array)); | 1176 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array)); |
| 1177 } | 1177 } |
| 1178 } | 1178 } |
| 1179 return result; | 1179 return result; |
| 1180 } | 1180 } |
| 1181 | 1181 |
| 1182 | 1182 |
| 1183 // For .indexOf, we don't need to pass in the number of arguments | 1183 // For .indexOf, we don't need to pass in the number of arguments |
| 1184 // at the callsite since ToInteger(undefined) == 0; however, for | 1184 // at the callsite since ToInteger(undefined) == 0; however, for |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1306 | 1306 |
| 1307 | 1307 |
| 1308 function InnerArrayReduce(callback, current, array, length, argumentsLength) { | 1308 function InnerArrayReduce(callback, current, array, length, argumentsLength) { |
| 1309 if (!IS_CALLABLE(callback)) { | 1309 if (!IS_CALLABLE(callback)) { |
| 1310 throw MakeTypeError(kCalledNonCallable, callback); | 1310 throw MakeTypeError(kCalledNonCallable, callback); |
| 1311 } | 1311 } |
| 1312 | 1312 |
| 1313 var i = 0; | 1313 var i = 0; |
| 1314 find_initial: if (argumentsLength < 2) { | 1314 find_initial: if (argumentsLength < 2) { |
| 1315 for (; i < length; i++) { | 1315 for (; i < length; i++) { |
| 1316 if (HAS_INDEX(array, i)) { | 1316 if (i in array) { |
| 1317 current = array[i++]; | 1317 current = array[i++]; |
| 1318 break find_initial; | 1318 break find_initial; |
| 1319 } | 1319 } |
| 1320 } | 1320 } |
| 1321 throw MakeTypeError(kReduceNoInitial); | 1321 throw MakeTypeError(kReduceNoInitial); |
| 1322 } | 1322 } |
| 1323 | 1323 |
| 1324 for (; i < length; i++) { | 1324 for (; i < length; i++) { |
| 1325 if (HAS_INDEX(array, i)) { | 1325 if (i in array) { |
| 1326 var element = array[i]; | 1326 var element = array[i]; |
| 1327 current = callback(current, element, i, array); | 1327 current = callback(current, element, i, array); |
| 1328 } | 1328 } |
| 1329 } | 1329 } |
| 1330 return current; | 1330 return current; |
| 1331 } | 1331 } |
| 1332 | 1332 |
| 1333 | 1333 |
| 1334 function ArrayReduce(callback, current) { | 1334 function ArrayReduce(callback, current) { |
| 1335 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce"); | 1335 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce"); |
| 1336 | 1336 |
| 1337 // Pull out the length so that modifications to the length in the | 1337 // Pull out the length so that modifications to the length in the |
| 1338 // loop will not affect the looping and side effects are visible. | 1338 // loop will not affect the looping and side effects are visible. |
| 1339 var array = TO_OBJECT(this); | 1339 var array = TO_OBJECT(this); |
| 1340 var length = TO_LENGTH(array.length); | 1340 var length = TO_LENGTH(array.length); |
| 1341 return InnerArrayReduce(callback, current, array, length, | 1341 return InnerArrayReduce(callback, current, array, length, |
| 1342 arguments.length); | 1342 arguments.length); |
| 1343 } | 1343 } |
| 1344 | 1344 |
| 1345 | 1345 |
| 1346 function InnerArrayReduceRight(callback, current, array, length, | 1346 function InnerArrayReduceRight(callback, current, array, length, |
| 1347 argumentsLength) { | 1347 argumentsLength) { |
| 1348 if (!IS_CALLABLE(callback)) { | 1348 if (!IS_CALLABLE(callback)) { |
| 1349 throw MakeTypeError(kCalledNonCallable, callback); | 1349 throw MakeTypeError(kCalledNonCallable, callback); |
| 1350 } | 1350 } |
| 1351 | 1351 |
| 1352 var i = length - 1; | 1352 var i = length - 1; |
| 1353 find_initial: if (argumentsLength < 2) { | 1353 find_initial: if (argumentsLength < 2) { |
| 1354 for (; i >= 0; i--) { | 1354 for (; i >= 0; i--) { |
| 1355 if (HAS_INDEX(array, i)) { | 1355 if (i in array) { |
| 1356 current = array[i--]; | 1356 current = array[i--]; |
| 1357 break find_initial; | 1357 break find_initial; |
| 1358 } | 1358 } |
| 1359 } | 1359 } |
| 1360 throw MakeTypeError(kReduceNoInitial); | 1360 throw MakeTypeError(kReduceNoInitial); |
| 1361 } | 1361 } |
| 1362 | 1362 |
| 1363 for (; i >= 0; i--) { | 1363 for (; i >= 0; i--) { |
| 1364 if (HAS_INDEX(array, i)) { | 1364 if (i in array) { |
| 1365 var element = array[i]; | 1365 var element = array[i]; |
| 1366 current = callback(current, element, i, array); | 1366 current = callback(current, element, i, array); |
| 1367 } | 1367 } |
| 1368 } | 1368 } |
| 1369 return current; | 1369 return current; |
| 1370 } | 1370 } |
| 1371 | 1371 |
| 1372 | 1372 |
| 1373 function ArrayReduceRight(callback, current) { | 1373 function ArrayReduceRight(callback, current) { |
| 1374 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); | 1374 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1780 %InstallToContext([ | 1780 %InstallToContext([ |
| 1781 "array_pop", ArrayPop, | 1781 "array_pop", ArrayPop, |
| 1782 "array_push", ArrayPush, | 1782 "array_push", ArrayPush, |
| 1783 "array_shift", ArrayShift, | 1783 "array_shift", ArrayShift, |
| 1784 "array_splice", ArraySplice, | 1784 "array_splice", ArraySplice, |
| 1785 "array_slice", ArraySlice, | 1785 "array_slice", ArraySlice, |
| 1786 "array_unshift", ArrayUnshift, | 1786 "array_unshift", ArrayUnshift, |
| 1787 ]); | 1787 ]); |
| 1788 | 1788 |
| 1789 }); | 1789 }); |
| OLD | NEW |