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 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 var length = this.length; | 987 var length = this.length; |
988 if (index == null) { | 988 if (index == null) { |
989 index = 0; | 989 index = 0; |
990 } else { | 990 } else { |
991 index = TO_INTEGER(index); | 991 index = TO_INTEGER(index); |
992 // If index is negative, index from the end of the array. | 992 // If index is negative, index from the end of the array. |
993 if (index < 0) index = length + index; | 993 if (index < 0) index = length + index; |
994 // If index is still negative, search the entire array. | 994 // If index is still negative, search the entire array. |
995 if (index < 0) index = 0; | 995 if (index < 0) index = 0; |
996 } | 996 } |
| 997 if (!IS_UNDEFINED(element)) { |
| 998 for (var i = index; i < length; i++) { |
| 999 if (this[i] === element) return i; |
| 1000 } |
| 1001 return -1; |
| 1002 } |
997 // Lookup through the array. | 1003 // Lookup through the array. |
998 for (var i = index; i < length; i++) { | 1004 for (var i = index; i < length; i++) { |
999 var current = this[i]; | 1005 if (IS_UNDEFINED(this[i]) && i in this) { |
1000 if (!IS_UNDEFINED(current) || i in this) { | 1006 return i; |
1001 if (current === element) return i; | |
1002 } | 1007 } |
1003 } | 1008 } |
1004 return -1; | 1009 return -1; |
1005 } | 1010 } |
1006 | 1011 |
1007 | 1012 |
1008 function ArrayLastIndexOf(element, index) { | 1013 function ArrayLastIndexOf(element, index) { |
1009 var length = this.length; | 1014 var length = this.length; |
1010 if (index == null) { | 1015 if (index == null) { |
1011 index = length - 1; | 1016 index = length - 1; |
1012 } else { | 1017 } else { |
1013 index = TO_INTEGER(index); | 1018 index = TO_INTEGER(index); |
1014 // If index is negative, index from end of the array. | 1019 // If index is negative, index from end of the array. |
1015 if (index < 0) index = length + index; | 1020 if (index < 0) index = length + index; |
1016 // If index is still negative, do not search the array. | 1021 // If index is still negative, do not search the array. |
1017 if (index < 0) index = -1; | 1022 if (index < 0) index = -1; |
1018 else if (index >= length) index = length - 1; | 1023 else if (index >= length) index = length - 1; |
1019 } | 1024 } |
1020 // Lookup through the array. | 1025 // Lookup through the array. |
| 1026 if (!IS_UNDEFINED(element)) { |
| 1027 for (var i = index; i >= 0; i--) { |
| 1028 if (this[i] === element) return i; |
| 1029 } |
| 1030 return -1; |
| 1031 } |
1021 for (var i = index; i >= 0; i--) { | 1032 for (var i = index; i >= 0; i--) { |
1022 var current = this[i]; | 1033 if (IS_UNDEFINED(this[i]) && i in this) { |
1023 if (!IS_UNDEFINED(current) || i in this) { | 1034 return i; |
1024 if (current === element) return i; | |
1025 } | 1035 } |
1026 } | 1036 } |
1027 return -1; | 1037 return -1; |
1028 } | 1038 } |
1029 | 1039 |
1030 | 1040 |
1031 function ArrayReduce(callback, current) { | 1041 function ArrayReduce(callback, current) { |
1032 if (!IS_FUNCTION(callback)) { | 1042 if (!IS_FUNCTION(callback)) { |
1033 throw MakeTypeError('called_non_callable', [callback]); | 1043 throw MakeTypeError('called_non_callable', [callback]); |
1034 } | 1044 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1149 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
1140 "reduce", getFunction("reduce", ArrayReduce, 1), | 1150 "reduce", getFunction("reduce", ArrayReduce, 1), |
1141 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1151 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
1142 )); | 1152 )); |
1143 | 1153 |
1144 %FinishArrayPrototypeSetup($Array.prototype); | 1154 %FinishArrayPrototypeSetup($Array.prototype); |
1145 } | 1155 } |
1146 | 1156 |
1147 | 1157 |
1148 SetupArray(); | 1158 SetupArray(); |
OLD | NEW |