Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Side by Side Diff: src/array.js

Issue 1148513002: Implement %TypedArray%.prototype.sort (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rebase Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 $innerArrayReverse; 15 var $innerArrayReverse;
16 var $innerArraySort;
16 17
17 (function(global, shared, exports) { 18 (function(global, shared, exports) {
18 19
19 "use strict"; 20 "use strict";
20 21
21 %CheckIsBootstrapping(); 22 %CheckIsBootstrapping();
22 23
23 // ------------------------------------------------------------------- 24 // -------------------------------------------------------------------
24 // Imports 25 // Imports
25 26
(...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 while (arguments_index < arguments_length) { 861 while (arguments_index < arguments_length) {
861 array[i++] = %_Arguments(arguments_index++); 862 array[i++] = %_Arguments(arguments_index++);
862 } 863 }
863 array.length = len - del_count + num_elements_to_add; 864 array.length = len - del_count + num_elements_to_add;
864 865
865 // Return the deleted elements. 866 // Return the deleted elements.
866 return deleted_elements; 867 return deleted_elements;
867 } 868 }
868 869
869 870
870 function ArraySort(comparefn) { 871 function InnerArraySort(length, comparefn) {
871 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
872
873 // In-place QuickSort algorithm. 872 // In-place QuickSort algorithm.
874 // For short (length <= 22) arrays, insertion sort is used for efficiency. 873 // For short (length <= 22) arrays, insertion sort is used for efficiency.
875 874
876 if (!IS_SPEC_FUNCTION(comparefn)) { 875 if (!IS_SPEC_FUNCTION(comparefn)) {
877 comparefn = function (x, y) { 876 comparefn = function (x, y) {
878 if (x === y) return 0; 877 if (x === y) return 0;
879 if (%_IsSmi(x) && %_IsSmi(y)) { 878 if (%_IsSmi(x) && %_IsSmi(y)) {
880 return %SmiLexicographicCompare(x, y); 879 return %SmiLexicographicCompare(x, y);
881 } 880 }
882 x = $toString(x); 881 x = $toString(x);
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 obj[i] = UNDEFINED; 1106 obj[i] = UNDEFINED;
1108 } else { 1107 } else {
1109 delete obj[i]; 1108 delete obj[i];
1110 } 1109 }
1111 } 1110 }
1112 1111
1113 // Return the number of defined elements. 1112 // Return the number of defined elements.
1114 return first_undefined; 1113 return first_undefined;
1115 }; 1114 };
1116 1115
1117 var length = TO_UINT32(this.length);
1118 if (length < 2) return this; 1116 if (length < 2) return this;
1119 1117
1120 var is_array = IS_ARRAY(this); 1118 var is_array = IS_ARRAY(this);
1121 var max_prototype_element; 1119 var max_prototype_element;
1122 if (!is_array) { 1120 if (!is_array) {
1123 // For compatibility with JSC, we also sort elements inherited from 1121 // For compatibility with JSC, we also sort elements inherited from
1124 // the prototype chain on non-Array objects. 1122 // the prototype chain on non-Array objects.
1125 // We do this by copying them to this object and sorting only 1123 // We do this by copying them to this object and sorting only
1126 // own elements. This is not very efficient, but sorting with 1124 // own elements. This is not very efficient, but sorting with
1127 // inherited elements happens very, very rarely, if at all. 1125 // inherited elements happens very, very rarely, if at all.
(...skipping 18 matching lines...) Expand all
1146 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) { 1144 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) {
1147 // For compatibility with JSC, we shadow any elements in the prototype 1145 // For compatibility with JSC, we shadow any elements in the prototype
1148 // chain that has become exposed by sort moving a hole to its position. 1146 // chain that has become exposed by sort moving a hole to its position.
1149 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element); 1147 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element);
1150 } 1148 }
1151 1149
1152 return this; 1150 return this;
1153 } 1151 }
1154 1152
1155 1153
1154 function ArraySort(comparefn) {
1155 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
1156
1157 var length = TO_UINT32(this.length);
1158 return %_CallFunction(this, length, comparefn, InnerArraySort);
1159 }
1160
1161
1156 // The following functions cannot be made efficient on sparse arrays while 1162 // The following functions cannot be made efficient on sparse arrays while
1157 // preserving the semantics, since the calls to the receiver function can add 1163 // preserving the semantics, since the calls to the receiver function can add
1158 // or delete elements from the array. 1164 // or delete elements from the array.
1159 function ArrayFilter(f, receiver) { 1165 function ArrayFilter(f, receiver) {
1160 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter"); 1166 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
1161 1167
1162 // Pull out the length so that modifications to the length in the 1168 // Pull out the length so that modifications to the length in the
1163 // loop will not affect the looping and side effects are visible. 1169 // loop will not affect the looping and side effects are visible.
1164 var array = $toObject(this); 1170 var array = $toObject(this);
1165 var length = $toUint32(array.length); 1171 var length = $toUint32(array.length);
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1609 $arrayPush = ArrayPush; 1615 $arrayPush = ArrayPush;
1610 $arrayPop = ArrayPop; 1616 $arrayPop = ArrayPop;
1611 $arrayShift = ArrayShift; 1617 $arrayShift = ArrayShift;
1612 $arraySlice = ArraySlice; 1618 $arraySlice = ArraySlice;
1613 $arraySplice = ArraySplice; 1619 $arraySplice = ArraySplice;
1614 $arrayUnshift = ArrayUnshift; 1620 $arrayUnshift = ArrayUnshift;
1615 1621
1616 $innerArrayForEach = InnerArrayForEach; 1622 $innerArrayForEach = InnerArrayForEach;
1617 $innerArrayEvery = InnerArrayEvery; 1623 $innerArrayEvery = InnerArrayEvery;
1618 $innerArrayReverse = InnerArrayReverse; 1624 $innerArrayReverse = InnerArrayReverse;
1625 $innerArraySort = InnerArraySort;
1619 1626
1620 }); 1627 });
OLDNEW
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698