Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 1009 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1020 } | 1020 } |
| 1021 if (IS_NULL_OR_UNDEFINED(receiver)) { | 1021 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 1022 receiver = %GetDefaultReceiver(f) || receiver; | 1022 receiver = %GetDefaultReceiver(f) || receiver; |
| 1023 } else if (!IS_SPEC_OBJECT(receiver)) { | 1023 } else if (!IS_SPEC_OBJECT(receiver)) { |
| 1024 receiver = ToObject(receiver); | 1024 receiver = ToObject(receiver); |
| 1025 } | 1025 } |
| 1026 | 1026 |
| 1027 var result = new $Array(); | 1027 var result = new $Array(); |
| 1028 var accumulator = new InternalArray(); | 1028 var accumulator = new InternalArray(); |
| 1029 var accumulator_length = 0; | 1029 var accumulator_length = 0; |
| 1030 for (var i = 0; i < length; i++) { | 1030 if (%DebugStepIntoBuiltinCallback(f)) { |
|
Søren Gjesse
2012/04/20 08:33:25
Maybe rename DebugStepIntoBuiltinCallback to somet
Søren Gjesse
2012/04/20 08:33:25
The only thing I don't like here is the code dupli
Søren Gjesse
2012/04/20 08:33:25
An important thing performance wise is that it sho
Yang
2012/04/20 11:06:54
This would be great, but I have no idea how to che
Yang
2012/04/20 11:06:54
I don't see anything simple either. The one way I
| |
| 1031 if (i in array) { | 1031 for (var i = 0; i < length; i++) { |
| 1032 var element = array[i]; | 1032 if (i in array) { |
| 1033 if (%_CallFunction(receiver, element, i, array, f)) { | 1033 var element = array[i]; |
| 1034 accumulator[accumulator_length++] = element; | 1034 // Prepare break slots for debugger step in. |
| 1035 %PrepareBreakSlotsForCallback(f); | |
|
Søren Gjesse
2012/04/20 08:33:25
I think the name here i somewhat misleading. Maybe
Yang
2012/04/20 11:06:54
Done.
| |
| 1036 if (%_CallFunction(receiver, element, i, array, f)) { | |
| 1037 accumulator[accumulator_length++] = element; | |
| 1038 } | |
| 1039 } | |
| 1040 } | |
| 1041 } else { | |
| 1042 for (var i = 0; i < length; i++) { | |
| 1043 if (i in array) { | |
| 1044 var element = array[i]; | |
| 1045 if (%_CallFunction(receiver, element, i, array, f)) { | |
| 1046 accumulator[accumulator_length++] = element; | |
| 1047 } | |
| 1035 } | 1048 } |
| 1036 } | 1049 } |
| 1037 } | 1050 } |
| 1038 %MoveArrayContents(accumulator, result); | 1051 %MoveArrayContents(accumulator, result); |
| 1039 return result; | 1052 return result; |
| 1040 } | 1053 } |
| 1041 | 1054 |
| 1042 | 1055 |
| 1043 function ArrayForEach(f, receiver) { | 1056 function ArrayForEach(f, receiver) { |
| 1044 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1057 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1045 throw MakeTypeError("called_on_null_or_undefined", | 1058 throw MakeTypeError("called_on_null_or_undefined", |
| 1046 ["Array.prototype.forEach"]); | 1059 ["Array.prototype.forEach"]); |
| 1047 } | 1060 } |
| 1048 | 1061 |
| 1049 // Pull out the length so that modifications to the length in the | 1062 // Pull out the length so that modifications to the length in the |
| 1050 // loop will not affect the looping and side effects are visible. | 1063 // loop will not affect the looping and side effects are visible. |
| 1051 var array = ToObject(this); | 1064 var array = ToObject(this); |
| 1052 var length = TO_UINT32(array.length); | 1065 var length = TO_UINT32(array.length); |
| 1053 | 1066 |
| 1054 if (!IS_SPEC_FUNCTION(f)) { | 1067 if (!IS_SPEC_FUNCTION(f)) { |
| 1055 throw MakeTypeError('called_non_callable', [ f ]); | 1068 throw MakeTypeError('called_non_callable', [ f ]); |
| 1056 } | 1069 } |
| 1057 if (IS_NULL_OR_UNDEFINED(receiver)) { | 1070 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 1058 receiver = %GetDefaultReceiver(f) || receiver; | 1071 receiver = %GetDefaultReceiver(f) || receiver; |
| 1059 } else if (!IS_SPEC_OBJECT(receiver)) { | 1072 } else if (!IS_SPEC_OBJECT(receiver)) { |
| 1060 receiver = ToObject(receiver); | 1073 receiver = ToObject(receiver); |
| 1061 } | 1074 } |
| 1062 | 1075 if (%DebugStepIntoBuiltinCallback(f)) { |
| 1063 for (var i = 0; i < length; i++) { | 1076 for (var i = 0; i < length; i++) { |
| 1064 if (i in array) { | 1077 if (i in array) { |
| 1065 var element = array[i]; | 1078 var element = array[i]; |
| 1066 %_CallFunction(receiver, element, i, array, f); | 1079 // Prepare break slots for debugger step in. |
| 1080 %PrepareBreakSlotsForCallback(f); | |
| 1081 %_CallFunction(receiver, element, i, array, f); | |
| 1082 } | |
| 1083 } | |
| 1084 } else { | |
| 1085 for (var i = 0; i < length; i++) { | |
| 1086 if (i in array) { | |
| 1087 var element = array[i]; | |
| 1088 %_CallFunction(receiver, element, i, array, f); | |
| 1089 } | |
| 1067 } | 1090 } |
| 1068 } | 1091 } |
| 1069 } | 1092 } |
| 1070 | 1093 |
| 1071 | 1094 |
| 1072 // Executes the function once for each element present in the | 1095 // Executes the function once for each element present in the |
| 1073 // array until it finds one where callback returns true. | 1096 // array until it finds one where callback returns true. |
| 1074 function ArraySome(f, receiver) { | 1097 function ArraySome(f, receiver) { |
| 1075 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1098 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1076 throw MakeTypeError("called_on_null_or_undefined", | 1099 throw MakeTypeError("called_on_null_or_undefined", |
| 1077 ["Array.prototype.some"]); | 1100 ["Array.prototype.some"]); |
| 1078 } | 1101 } |
| 1079 | 1102 |
| 1080 // Pull out the length so that modifications to the length in the | 1103 // Pull out the length so that modifications to the length in the |
| 1081 // loop will not affect the looping and side effects are visible. | 1104 // loop will not affect the looping and side effects are visible. |
| 1082 var array = ToObject(this); | 1105 var array = ToObject(this); |
| 1083 var length = TO_UINT32(array.length); | 1106 var length = TO_UINT32(array.length); |
| 1084 | 1107 |
| 1085 if (!IS_SPEC_FUNCTION(f)) { | 1108 if (!IS_SPEC_FUNCTION(f)) { |
| 1086 throw MakeTypeError('called_non_callable', [ f ]); | 1109 throw MakeTypeError('called_non_callable', [ f ]); |
| 1087 } | 1110 } |
| 1088 if (IS_NULL_OR_UNDEFINED(receiver)) { | 1111 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 1089 receiver = %GetDefaultReceiver(f) || receiver; | 1112 receiver = %GetDefaultReceiver(f) || receiver; |
| 1090 } else if (!IS_SPEC_OBJECT(receiver)) { | 1113 } else if (!IS_SPEC_OBJECT(receiver)) { |
| 1091 receiver = ToObject(receiver); | 1114 receiver = ToObject(receiver); |
| 1092 } | 1115 } |
| 1093 | 1116 |
| 1094 for (var i = 0; i < length; i++) { | 1117 if (%DebugStepIntoBuiltinCallback(f)) { |
| 1095 if (i in array) { | 1118 for (var i = 0; i < length; i++) { |
| 1096 var element = array[i]; | 1119 if (i in array) { |
| 1097 if (%_CallFunction(receiver, element, i, array, f)) return true; | 1120 var element = array[i]; |
| 1121 // Prepare break slots for debugger step in. | |
| 1122 %PrepareBreakSlotsForCallback(f); | |
| 1123 if (%_CallFunction(receiver, element, i, array, f)) return true; | |
| 1124 } | |
| 1125 } | |
| 1126 } else { | |
| 1127 for (var i = 0; i < length; i++) { | |
| 1128 if (i in array) { | |
| 1129 var element = array[i]; | |
| 1130 if (%_CallFunction(receiver, element, i, array, f)) return true; | |
| 1131 } | |
| 1098 } | 1132 } |
| 1099 } | 1133 } |
| 1100 return false; | 1134 return false; |
| 1101 } | 1135 } |
| 1102 | 1136 |
| 1103 | 1137 |
| 1104 function ArrayEvery(f, receiver) { | 1138 function ArrayEvery(f, receiver) { |
| 1105 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1139 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1106 throw MakeTypeError("called_on_null_or_undefined", | 1140 throw MakeTypeError("called_on_null_or_undefined", |
| 1107 ["Array.prototype.every"]); | 1141 ["Array.prototype.every"]); |
| 1108 } | 1142 } |
| 1109 | 1143 |
| 1110 // Pull out the length so that modifications to the length in the | 1144 // Pull out the length so that modifications to the length in the |
| 1111 // loop will not affect the looping and side effects are visible. | 1145 // loop will not affect the looping and side effects are visible. |
| 1112 var array = ToObject(this); | 1146 var array = ToObject(this); |
| 1113 var length = TO_UINT32(array.length); | 1147 var length = TO_UINT32(array.length); |
| 1114 | 1148 |
| 1115 if (!IS_SPEC_FUNCTION(f)) { | 1149 if (!IS_SPEC_FUNCTION(f)) { |
| 1116 throw MakeTypeError('called_non_callable', [ f ]); | 1150 throw MakeTypeError('called_non_callable', [ f ]); |
| 1117 } | 1151 } |
| 1118 if (IS_NULL_OR_UNDEFINED(receiver)) { | 1152 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 1119 receiver = %GetDefaultReceiver(f) || receiver; | 1153 receiver = %GetDefaultReceiver(f) || receiver; |
| 1120 } else if (!IS_SPEC_OBJECT(receiver)) { | 1154 } else if (!IS_SPEC_OBJECT(receiver)) { |
| 1121 receiver = ToObject(receiver); | 1155 receiver = ToObject(receiver); |
| 1122 } | 1156 } |
| 1123 | 1157 |
| 1124 for (var i = 0; i < length; i++) { | 1158 if (%DebugStepIntoBuiltinCallback(f)) { |
| 1125 if (i in array) { | 1159 for (var i = 0; i < length; i++) { |
| 1126 var element = array[i]; | 1160 if (i in array) { |
| 1127 if (!%_CallFunction(receiver, element, i, array, f)) return false; | 1161 var element = array[i]; |
| 1162 // Prepare break slots for debugger step in. | |
| 1163 %PrepareBreakSlotsForCallback(f); | |
| 1164 if (!%_CallFunction(receiver, element, i, array, f)) return false; | |
| 1165 } | |
| 1166 } | |
| 1167 } else { | |
| 1168 for (var i = 0; i < length; i++) { | |
| 1169 if (i in array) { | |
| 1170 var element = array[i]; | |
| 1171 if (!%_CallFunction(receiver, element, i, array, f)) return false; | |
| 1172 } | |
| 1128 } | 1173 } |
| 1129 } | 1174 } |
| 1130 return true; | 1175 return true; |
| 1131 } | 1176 } |
| 1132 | 1177 |
| 1133 function ArrayMap(f, receiver) { | 1178 function ArrayMap(f, receiver) { |
| 1134 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1179 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1135 throw MakeTypeError("called_on_null_or_undefined", | 1180 throw MakeTypeError("called_on_null_or_undefined", |
| 1136 ["Array.prototype.map"]); | 1181 ["Array.prototype.map"]); |
| 1137 } | 1182 } |
| 1138 | 1183 |
| 1139 // Pull out the length so that modifications to the length in the | 1184 // Pull out the length so that modifications to the length in the |
| 1140 // loop will not affect the looping and side effects are visible. | 1185 // loop will not affect the looping and side effects are visible. |
| 1141 var array = ToObject(this); | 1186 var array = ToObject(this); |
| 1142 var length = TO_UINT32(array.length); | 1187 var length = TO_UINT32(array.length); |
| 1143 | 1188 |
| 1144 if (!IS_SPEC_FUNCTION(f)) { | 1189 if (!IS_SPEC_FUNCTION(f)) { |
| 1145 throw MakeTypeError('called_non_callable', [ f ]); | 1190 throw MakeTypeError('called_non_callable', [ f ]); |
| 1146 } | 1191 } |
| 1147 if (IS_NULL_OR_UNDEFINED(receiver)) { | 1192 if (IS_NULL_OR_UNDEFINED(receiver)) { |
| 1148 receiver = %GetDefaultReceiver(f) || receiver; | 1193 receiver = %GetDefaultReceiver(f) || receiver; |
| 1149 } else if (!IS_SPEC_OBJECT(receiver)) { | 1194 } else if (!IS_SPEC_OBJECT(receiver)) { |
| 1150 receiver = ToObject(receiver); | 1195 receiver = ToObject(receiver); |
| 1151 } | 1196 } |
| 1152 | 1197 |
| 1153 var result = new $Array(); | 1198 var result = new $Array(); |
| 1154 var accumulator = new InternalArray(length); | 1199 var accumulator = new InternalArray(length); |
| 1155 for (var i = 0; i < length; i++) { | 1200 if (%DebugStepIntoBuiltinCallback(f)) { |
| 1156 if (i in array) { | 1201 for (var i = 0; i < length; i++) { |
| 1157 var element = array[i]; | 1202 if (i in array) { |
| 1158 accumulator[i] = %_CallFunction(receiver, element, i, array, f); | 1203 var element = array[i]; |
| 1204 // Prepare break slots for debugger step in. | |
| 1205 %PrepareBreakSlotsForCallback(f); | |
| 1206 accumulator[i] = %_CallFunction(receiver, element, i, array, f); | |
| 1207 } | |
| 1208 } | |
| 1209 } else { | |
| 1210 for (var i = 0; i < length; i++) { | |
| 1211 if (i in array) { | |
| 1212 var element = array[i]; | |
| 1213 accumulator[i] = %_CallFunction(receiver, element, i, array, f); | |
| 1214 } | |
| 1159 } | 1215 } |
| 1160 } | 1216 } |
| 1161 %MoveArrayContents(accumulator, result); | 1217 %MoveArrayContents(accumulator, result); |
| 1162 return result; | 1218 return result; |
| 1163 } | 1219 } |
| 1164 | 1220 |
| 1165 | 1221 |
| 1166 function ArrayIndexOf(element, index) { | 1222 function ArrayIndexOf(element, index) { |
| 1167 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1223 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1168 throw MakeTypeError("called_on_null_or_undefined", | 1224 throw MakeTypeError("called_on_null_or_undefined", |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1304 current = array[i]; | 1360 current = array[i]; |
| 1305 if (!IS_UNDEFINED(current) || i in array) { | 1361 if (!IS_UNDEFINED(current) || i in array) { |
| 1306 i++; | 1362 i++; |
| 1307 break find_initial; | 1363 break find_initial; |
| 1308 } | 1364 } |
| 1309 } | 1365 } |
| 1310 throw MakeTypeError('reduce_no_initial', []); | 1366 throw MakeTypeError('reduce_no_initial', []); |
| 1311 } | 1367 } |
| 1312 | 1368 |
| 1313 var receiver = %GetDefaultReceiver(callback); | 1369 var receiver = %GetDefaultReceiver(callback); |
| 1314 for (; i < length; i++) { | 1370 |
| 1315 if (i in array) { | 1371 if (%DebugStepIntoBuiltinCallback(callback)) { |
| 1316 var element = array[i]; | 1372 for (; i < length; i++) { |
| 1317 current = %_CallFunction(receiver, current, element, i, array, callback); | 1373 if (i in array) { |
| 1374 var element = array[i]; | |
| 1375 // Prepare break slots for debugger step in. | |
| 1376 %PrepareBreakSlotsForCallback(callback); | |
| 1377 current = | |
| 1378 %_CallFunction(receiver, current, element, i, array, callback); | |
| 1379 } | |
| 1380 } | |
| 1381 } else { | |
| 1382 for (; i < length; i++) { | |
| 1383 if (i in array) { | |
| 1384 var element = array[i]; | |
| 1385 current = | |
| 1386 %_CallFunction(receiver, current, element, i, array, callback); | |
| 1387 } | |
| 1318 } | 1388 } |
| 1319 } | 1389 } |
| 1320 return current; | 1390 return current; |
| 1321 } | 1391 } |
| 1322 | 1392 |
| 1323 function ArrayReduceRight(callback, current) { | 1393 function ArrayReduceRight(callback, current) { |
| 1324 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1394 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1325 throw MakeTypeError("called_on_null_or_undefined", | 1395 throw MakeTypeError("called_on_null_or_undefined", |
| 1326 ["Array.prototype.reduceRight"]); | 1396 ["Array.prototype.reduceRight"]); |
| 1327 } | 1397 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1341 current = array[i]; | 1411 current = array[i]; |
| 1342 if (!IS_UNDEFINED(current) || i in array) { | 1412 if (!IS_UNDEFINED(current) || i in array) { |
| 1343 i--; | 1413 i--; |
| 1344 break find_initial; | 1414 break find_initial; |
| 1345 } | 1415 } |
| 1346 } | 1416 } |
| 1347 throw MakeTypeError('reduce_no_initial', []); | 1417 throw MakeTypeError('reduce_no_initial', []); |
| 1348 } | 1418 } |
| 1349 | 1419 |
| 1350 var receiver = %GetDefaultReceiver(callback); | 1420 var receiver = %GetDefaultReceiver(callback); |
| 1351 for (; i >= 0; i--) { | 1421 |
| 1352 if (i in array) { | 1422 if (%DebugStepIntoBuiltinCallback(callback)) { |
| 1353 var element = array[i]; | 1423 for (; i >= 0; i--) { |
| 1354 current = %_CallFunction(receiver, current, element, i, array, callback); | 1424 if (i in array) { |
| 1425 var element = array[i]; | |
| 1426 // Prepare break slots for debugger step in. | |
| 1427 %PrepareBreakSlotsForCallback(callback); | |
| 1428 current = | |
| 1429 %_CallFunction(receiver, current, element, i, array, callback); | |
| 1430 } | |
| 1431 } | |
| 1432 } else { | |
| 1433 for (; i >= 0; i--) { | |
| 1434 if (i in array) { | |
| 1435 var element = array[i]; | |
| 1436 current = | |
| 1437 %_CallFunction(receiver, current, element, i, array, callback); | |
| 1438 } | |
| 1355 } | 1439 } |
| 1356 } | 1440 } |
| 1357 return current; | 1441 return current; |
| 1358 } | 1442 } |
| 1359 | 1443 |
| 1360 // ES5, 15.4.3.2 | 1444 // ES5, 15.4.3.2 |
| 1361 function ArrayIsArray(obj) { | 1445 function ArrayIsArray(obj) { |
| 1362 return IS_ARRAY(obj); | 1446 return IS_ARRAY(obj); |
| 1363 } | 1447 } |
| 1364 | 1448 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1422 // exposed to user code. | 1506 // exposed to user code. |
| 1423 // Adding only the functions that are actually used. | 1507 // Adding only the functions that are actually used. |
| 1424 SetUpLockedPrototype(InternalArray, $Array(), $Array( | 1508 SetUpLockedPrototype(InternalArray, $Array(), $Array( |
| 1425 "join", getFunction("join", ArrayJoin), | 1509 "join", getFunction("join", ArrayJoin), |
| 1426 "pop", getFunction("pop", ArrayPop), | 1510 "pop", getFunction("pop", ArrayPop), |
| 1427 "push", getFunction("push", ArrayPush) | 1511 "push", getFunction("push", ArrayPush) |
| 1428 )); | 1512 )); |
| 1429 } | 1513 } |
| 1430 | 1514 |
| 1431 SetUpArray(); | 1515 SetUpArray(); |
| OLD | NEW |