OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 if (length == 0) return -1; | 957 if (length == 0) return -1; |
958 if (IS_UNDEFINED(index)) { | 958 if (IS_UNDEFINED(index)) { |
959 index = 0; | 959 index = 0; |
960 } else { | 960 } else { |
961 index = TO_INTEGER(index); | 961 index = TO_INTEGER(index); |
962 // If index is negative, index from the end of the array. | 962 // If index is negative, index from the end of the array. |
963 if (index < 0) index = length + index; | 963 if (index < 0) index = length + index; |
964 // If index is still negative, search the entire array. | 964 // If index is still negative, search the entire array. |
965 if (index < 0) index = 0; | 965 if (index < 0) index = 0; |
966 } | 966 } |
| 967 var min = index; |
| 968 var max = length; |
| 969 if (UseSparseVariant(this, length, true)) { |
| 970 var intervals = %GetArrayKeys(this, length); |
| 971 if (intervals.length == 2 && intervals[0] < 0) { |
| 972 // A single interval. |
| 973 var intervalMin = -(intervals[0] + 1); |
| 974 var intervalMax = intervalMin + intervals[1]; |
| 975 min = MAX(min, intervalMin); |
| 976 max = intervalMax; // Capped by length already. |
| 977 // Fall through to loop below. |
| 978 } else { |
| 979 if (intervals.length == 0) return -1; |
| 980 // Get all the keys in sorted order. |
| 981 var sortedKeys = GetSortedArrayKeys(this, intervals); |
| 982 var n = sortedKeys.length; |
| 983 var i = 0; |
| 984 while (i < n && sortedKeys[i] < index) i++; |
| 985 while (i < n) { |
| 986 var key = sortedKeys[i]; |
| 987 if (!IS_UNDEFINED(key) && this[key] === element) return key; |
| 988 i++; |
| 989 } |
| 990 return -1; |
| 991 } |
| 992 } |
967 // Lookup through the array. | 993 // Lookup through the array. |
968 if (!IS_UNDEFINED(element)) { | 994 if (!IS_UNDEFINED(element)) { |
969 for (var i = index; i < length; i++) { | 995 for (var i = min; i < max; i++) { |
970 if (this[i] === element) return i; | 996 if (this[i] === element) return i; |
971 } | 997 } |
972 return -1; | 998 return -1; |
973 } | 999 } |
974 for (var i = index; i < length; i++) { | 1000 // Lookup through the array. |
| 1001 for (var i = min; i < max; i++) { |
975 if (IS_UNDEFINED(this[i]) && i in this) { | 1002 if (IS_UNDEFINED(this[i]) && i in this) { |
976 return i; | 1003 return i; |
977 } | 1004 } |
978 } | 1005 } |
979 return -1; | 1006 return -1; |
980 } | 1007 } |
981 | 1008 |
982 | 1009 |
983 function ArrayLastIndexOf(element, index) { | 1010 function ArrayLastIndexOf(element, index) { |
984 var length = TO_UINT32(this.length); | 1011 var length = TO_UINT32(this.length); |
985 if (length == 0) return -1; | 1012 if (length == 0) return -1; |
986 if (%_ArgumentsLength() < 2) { | 1013 if (%_ArgumentsLength() < 2) { |
987 index = length - 1; | 1014 index = length - 1; |
988 } else { | 1015 } else { |
989 index = TO_INTEGER(index); | 1016 index = TO_INTEGER(index); |
990 // If index is negative, index from end of the array. | 1017 // If index is negative, index from end of the array. |
991 if (index < 0) index = length + index; | 1018 if (index < 0) index += length; |
992 // If index is still negative, do not search the array. | 1019 // If index is still negative, do not search the array. |
993 if (index < 0) index = -1; | 1020 if (index < 0) return -1; |
994 else if (index >= length) index = length - 1; | 1021 else if (index >= length) index = length - 1; |
995 } | 1022 } |
| 1023 var min = 0; |
| 1024 var max = index; |
| 1025 if (UseSparseVariant(this, length, true)) { |
| 1026 var intervals = %GetArrayKeys(this, index + 1); |
| 1027 if (intervals.length == 2 && intervals[0] < 0) { |
| 1028 // A single interval. |
| 1029 var intervalMin = -(intervals[0] + 1); |
| 1030 var intervalMax = intervalMin + intervals[1]; |
| 1031 min = MAX(min, intervalMin); |
| 1032 max = intervalMax; // Capped by index already. |
| 1033 // Fall through to loop below. |
| 1034 } else { |
| 1035 if (intervals.length == 0) return -1; |
| 1036 // Get all the keys in sorted order. |
| 1037 var sortedKeys = GetSortedArrayKeys(this, intervals); |
| 1038 var i = sortedKeys.length - 1; |
| 1039 while (i >= 0) { |
| 1040 var key = sortedKeys[i]; |
| 1041 if (!IS_UNDEFINED(key) && this[key] === element) return key; |
| 1042 i--; |
| 1043 } |
| 1044 return -1; |
| 1045 } |
| 1046 } |
996 // Lookup through the array. | 1047 // Lookup through the array. |
997 if (!IS_UNDEFINED(element)) { | 1048 if (!IS_UNDEFINED(element)) { |
998 for (var i = index; i >= 0; i--) { | 1049 for (var i = max; i >= min; i--) { |
999 if (this[i] === element) return i; | 1050 if (this[i] === element) return i; |
1000 } | 1051 } |
1001 return -1; | 1052 return -1; |
1002 } | 1053 } |
1003 for (var i = index; i >= 0; i--) { | 1054 for (var i = max; i >= min; i--) { |
1004 if (IS_UNDEFINED(this[i]) && i in this) { | 1055 if (IS_UNDEFINED(this[i]) && i in this) { |
1005 return i; | 1056 return i; |
1006 } | 1057 } |
1007 } | 1058 } |
1008 return -1; | 1059 return -1; |
1009 } | 1060 } |
1010 | 1061 |
1011 | 1062 |
1012 function ArrayReduce(callback, current) { | 1063 function ArrayReduce(callback, current) { |
1013 if (!IS_FUNCTION(callback)) { | 1064 if (!IS_FUNCTION(callback)) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1171 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
1121 "reduce", getFunction("reduce", ArrayReduce, 1), | 1172 "reduce", getFunction("reduce", ArrayReduce, 1), |
1122 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1173 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
1123 )); | 1174 )); |
1124 | 1175 |
1125 %FinishArrayPrototypeSetup($Array.prototype); | 1176 %FinishArrayPrototypeSetup($Array.prototype); |
1126 } | 1177 } |
1127 | 1178 |
1128 | 1179 |
1129 SetupArray(); | 1180 SetupArray(); |
OLD | NEW |