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 var $arrayConcat; | 5 var $arrayConcat; |
6 var $arrayJoin; | 6 var $arrayJoin; |
7 var $arrayPush; | 7 var $arrayPush; |
8 var $arrayPop; | 8 var $arrayPop; |
9 var $arrayShift; | 9 var $arrayShift; |
10 var $arraySlice; | 10 var $arraySlice; |
11 var $arraySplice; | 11 var $arraySplice; |
12 var $arrayUnshift; | 12 var $arrayUnshift; |
13 var $innerArrayForEach; | 13 var $innerArrayForEach; |
14 var $innerArrayEvery; | 14 var $innerArrayEvery; |
| 15 var $innerArraySort; |
15 | 16 |
16 (function(global, shared, exports) { | 17 (function(global, shared, exports) { |
17 | 18 |
18 "use strict"; | 19 "use strict"; |
19 | 20 |
20 %CheckIsBootstrapping(); | 21 %CheckIsBootstrapping(); |
21 | 22 |
22 // ------------------------------------------------------------------- | 23 // ------------------------------------------------------------------- |
23 // Imports | 24 // Imports |
24 | 25 |
(...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 while (arguments_index < arguments_length) { | 855 while (arguments_index < arguments_length) { |
855 array[i++] = %_Arguments(arguments_index++); | 856 array[i++] = %_Arguments(arguments_index++); |
856 } | 857 } |
857 array.length = len - del_count + num_elements_to_add; | 858 array.length = len - del_count + num_elements_to_add; |
858 | 859 |
859 // Return the deleted elements. | 860 // Return the deleted elements. |
860 return deleted_elements; | 861 return deleted_elements; |
861 } | 862 } |
862 | 863 |
863 | 864 |
864 function ArraySort(comparefn) { | 865 function InnerArraySort(length, comparefn) { |
865 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); | |
866 | |
867 // In-place QuickSort algorithm. | 866 // In-place QuickSort algorithm. |
868 // For short (length <= 22) arrays, insertion sort is used for efficiency. | 867 // For short (length <= 22) arrays, insertion sort is used for efficiency. |
869 | 868 |
870 if (!IS_SPEC_FUNCTION(comparefn)) { | 869 if (!IS_SPEC_FUNCTION(comparefn)) { |
871 comparefn = function (x, y) { | 870 comparefn = function (x, y) { |
872 if (x === y) return 0; | 871 if (x === y) return 0; |
873 if (%_IsSmi(x) && %_IsSmi(y)) { | 872 if (%_IsSmi(x) && %_IsSmi(y)) { |
874 return %SmiLexicographicCompare(x, y); | 873 return %SmiLexicographicCompare(x, y); |
875 } | 874 } |
876 x = $toString(x); | 875 x = $toString(x); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 obj[i] = UNDEFINED; | 1100 obj[i] = UNDEFINED; |
1102 } else { | 1101 } else { |
1103 delete obj[i]; | 1102 delete obj[i]; |
1104 } | 1103 } |
1105 } | 1104 } |
1106 | 1105 |
1107 // Return the number of defined elements. | 1106 // Return the number of defined elements. |
1108 return first_undefined; | 1107 return first_undefined; |
1109 }; | 1108 }; |
1110 | 1109 |
1111 var length = TO_UINT32(this.length); | |
1112 if (length < 2) return this; | 1110 if (length < 2) return this; |
1113 | 1111 |
1114 var is_array = IS_ARRAY(this); | 1112 var is_array = IS_ARRAY(this); |
1115 var max_prototype_element; | 1113 var max_prototype_element; |
1116 if (!is_array) { | 1114 if (!is_array) { |
1117 // For compatibility with JSC, we also sort elements inherited from | 1115 // For compatibility with JSC, we also sort elements inherited from |
1118 // the prototype chain on non-Array objects. | 1116 // the prototype chain on non-Array objects. |
1119 // We do this by copying them to this object and sorting only | 1117 // We do this by copying them to this object and sorting only |
1120 // own elements. This is not very efficient, but sorting with | 1118 // own elements. This is not very efficient, but sorting with |
1121 // inherited elements happens very, very rarely, if at all. | 1119 // inherited elements happens very, very rarely, if at all. |
(...skipping 18 matching lines...) Expand all Loading... |
1140 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) { | 1138 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) { |
1141 // For compatibility with JSC, we shadow any elements in the prototype | 1139 // For compatibility with JSC, we shadow any elements in the prototype |
1142 // chain that has become exposed by sort moving a hole to its position. | 1140 // chain that has become exposed by sort moving a hole to its position. |
1143 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element); | 1141 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element); |
1144 } | 1142 } |
1145 | 1143 |
1146 return this; | 1144 return this; |
1147 } | 1145 } |
1148 | 1146 |
1149 | 1147 |
| 1148 function ArraySort(comparefn) { |
| 1149 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort"); |
| 1150 |
| 1151 var length = TO_UINT32(this.length); |
| 1152 return %_CallFunction(this, length, comparefn, InnerArraySort); |
| 1153 } |
| 1154 |
| 1155 |
1150 // The following functions cannot be made efficient on sparse arrays while | 1156 // The following functions cannot be made efficient on sparse arrays while |
1151 // preserving the semantics, since the calls to the receiver function can add | 1157 // preserving the semantics, since the calls to the receiver function can add |
1152 // or delete elements from the array. | 1158 // or delete elements from the array. |
1153 function ArrayFilter(f, receiver) { | 1159 function ArrayFilter(f, receiver) { |
1154 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter"); | 1160 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter"); |
1155 | 1161 |
1156 // Pull out the length so that modifications to the length in the | 1162 // Pull out the length so that modifications to the length in the |
1157 // loop will not affect the looping and side effects are visible. | 1163 // loop will not affect the looping and side effects are visible. |
1158 var array = $toObject(this); | 1164 var array = $toObject(this); |
1159 var length = $toUint32(array.length); | 1165 var length = $toUint32(array.length); |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1602 $arrayJoin = ArrayJoin; | 1608 $arrayJoin = ArrayJoin; |
1603 $arrayPush = ArrayPush; | 1609 $arrayPush = ArrayPush; |
1604 $arrayPop = ArrayPop; | 1610 $arrayPop = ArrayPop; |
1605 $arrayShift = ArrayShift; | 1611 $arrayShift = ArrayShift; |
1606 $arraySlice = ArraySlice; | 1612 $arraySlice = ArraySlice; |
1607 $arraySplice = ArraySplice; | 1613 $arraySplice = ArraySplice; |
1608 $arrayUnshift = ArrayUnshift; | 1614 $arrayUnshift = ArrayUnshift; |
1609 | 1615 |
1610 $innerArrayForEach = InnerArrayForEach; | 1616 $innerArrayForEach = InnerArrayForEach; |
1611 $innerArrayEvery = InnerArrayEvery; | 1617 $innerArrayEvery = InnerArrayEvery; |
| 1618 $innerArraySort = InnerArraySort; |
1612 | 1619 |
1613 }); | 1620 }); |
OLD | NEW |