| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 } | 1091 } |
| 1082 } | 1092 } |
| 1083 return current; | 1093 return current; |
| 1084 } | 1094 } |
| 1085 | 1095 |
| 1086 // ES5, 15.4.3.2 | 1096 // ES5, 15.4.3.2 |
| 1087 function ArrayIsArray(obj) { | 1097 function ArrayIsArray(obj) { |
| 1088 return IS_ARRAY(obj); | 1098 return IS_ARRAY(obj); |
| 1089 } | 1099 } |
| 1090 | 1100 |
| 1091 // ------------------------------------------------------------------- | |
| 1092 | |
| 1093 | |
| 1094 function UpdateFunctionLengths(lengths) { | |
| 1095 for (var key in lengths) { | |
| 1096 %FunctionSetLength(this[key], lengths[key]); | |
| 1097 } | |
| 1098 } | |
| 1099 | |
| 1100 | 1101 |
| 1101 // ------------------------------------------------------------------- | 1102 // ------------------------------------------------------------------- |
| 1102 function SetupArray() { | 1103 function SetupArray() { |
| 1103 // Setup non-enumerable constructor property on the Array.prototype | 1104 // Setup non-enumerable constructor property on the Array.prototype |
| 1104 // object. | 1105 // object. |
| 1105 %SetProperty($Array.prototype, "constructor", $Array, DONT_ENUM); | 1106 %SetProperty($Array.prototype, "constructor", $Array, DONT_ENUM); |
| 1106 | 1107 |
| 1107 // Setup non-enumerable functions on the Array object. | 1108 // Setup non-enumerable functions on the Array object. |
| 1108 InstallFunctions($Array, DONT_ENUM, $Array( | 1109 InstallFunctions($Array, DONT_ENUM, $Array( |
| 1109 "isArray", ArrayIsArray | 1110 "isArray", ArrayIsArray |
| 1110 )); | 1111 )); |
| 1111 | 1112 |
| 1113 var specialFunctions = %SpecialArrayFunctions({}); |
| 1114 |
| 1115 function getFunction(name, jsBuiltin, len) { |
| 1116 var f = jsBuiltin; |
| 1117 if (specialFunctions.hasOwnProperty(name)) { |
| 1118 f = specialFunctions[name]; |
| 1119 } |
| 1120 if (!IS_UNDEFINED(len)) { |
| 1121 %FunctionSetLength(f, len); |
| 1122 } |
| 1123 return f; |
| 1124 } |
| 1125 |
| 1112 // Setup non-enumerable functions of the Array.prototype object and | 1126 // Setup non-enumerable functions of the Array.prototype object and |
| 1113 // set their names. | 1127 // set their names. |
| 1128 // Manipulate the length of some of the functions to meet |
| 1129 // expectations set by ECMA-262 or Mozilla. |
| 1114 InstallFunctionsOnHiddenPrototype($Array.prototype, DONT_ENUM, $Array( | 1130 InstallFunctionsOnHiddenPrototype($Array.prototype, DONT_ENUM, $Array( |
| 1115 "toString", ArrayToString, | 1131 "toString", getFunction("toString", ArrayToString), |
| 1116 "toLocaleString", ArrayToLocaleString, | 1132 "toLocaleString", getFunction("toLocaleString", ArrayToLocaleString), |
| 1117 "join", ArrayJoin, | 1133 "join", getFunction("join", ArrayJoin), |
| 1118 "pop", ArrayPop, | 1134 "pop", getFunction("pop", ArrayPop), |
| 1119 "push", ArrayPush, | 1135 "push", getFunction("push", ArrayPush, 1), |
| 1120 "concat", ArrayConcat, | 1136 "concat", getFunction("concat", ArrayConcat, 1), |
| 1121 "reverse", ArrayReverse, | 1137 "reverse", getFunction("reverse", ArrayReverse), |
| 1122 "shift", ArrayShift, | 1138 "shift", getFunction("shift", ArrayShift), |
| 1123 "unshift", ArrayUnshift, | 1139 "unshift", getFunction("unshift", ArrayUnshift, 1), |
| 1124 "slice", ArraySlice, | 1140 "slice", getFunction("slice", ArraySlice, 2), |
| 1125 "splice", ArraySplice, | 1141 "splice", getFunction("splice", ArraySplice, 2), |
| 1126 "sort", ArraySort, | 1142 "sort", getFunction("sort", ArraySort), |
| 1127 "filter", ArrayFilter, | 1143 "filter", getFunction("filter", ArrayFilter, 1), |
| 1128 "forEach", ArrayForEach, | 1144 "forEach", getFunction("forEach", ArrayForEach, 1), |
| 1129 "some", ArraySome, | 1145 "some", getFunction("some", ArraySome, 1), |
| 1130 "every", ArrayEvery, | 1146 "every", getFunction("every", ArrayEvery, 1), |
| 1131 "map", ArrayMap, | 1147 "map", getFunction("map", ArrayMap, 1), |
| 1132 "indexOf", ArrayIndexOf, | 1148 "indexOf", getFunction("indexOf", ArrayIndexOf, 1), |
| 1133 "lastIndexOf", ArrayLastIndexOf, | 1149 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
| 1134 "reduce", ArrayReduce, | 1150 "reduce", getFunction("reduce", ArrayReduce, 1), |
| 1135 "reduceRight", ArrayReduceRight | 1151 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
| 1136 )); | 1152 )); |
| 1137 | 1153 |
| 1138 // Manipulate the length of some of the functions to meet | |
| 1139 // expectations set by ECMA-262 or Mozilla. | |
| 1140 UpdateFunctionLengths({ | |
| 1141 ArrayFilter: 1, | |
| 1142 ArrayForEach: 1, | |
| 1143 ArraySome: 1, | |
| 1144 ArrayEvery: 1, | |
| 1145 ArrayMap: 1, | |
| 1146 ArrayIndexOf: 1, | |
| 1147 ArrayLastIndexOf: 1, | |
| 1148 ArrayPush: 1, | |
| 1149 ArrayReduce: 1, | |
| 1150 ArrayReduceRight: 1 | |
| 1151 }); | |
| 1152 | |
| 1153 %FinishArrayPrototypeSetup($Array.prototype); | 1154 %FinishArrayPrototypeSetup($Array.prototype); |
| 1154 } | 1155 } |
| 1155 | 1156 |
| 1156 | 1157 |
| 1157 SetupArray(); | 1158 SetupArray(); |
| OLD | NEW |