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

Side by Side Diff: src/array.js

Issue 555173002: Array.prototype.sort: Unchecked calls to hasOwnProperty and push and sort (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed nits Created 6 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/array-sort.js » ('j') | test/mjsunit/array-sort.js » ('J')
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 "use strict"; 5 "use strict";
6 6
7 // This file relies on the fact that the following declarations have been made 7 // This file relies on the fact that the following declarations have been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 } 856 }
857 } 857 }
858 a[j + 1] = element; 858 a[j + 1] = element;
859 } 859 }
860 }; 860 };
861 861
862 var GetThirdIndex = function(a, from, to) { 862 var GetThirdIndex = function(a, from, to) {
863 var t_array = []; 863 var t_array = [];
864 // Use both 'from' and 'to' to determine the pivot candidates. 864 // Use both 'from' and 'to' to determine the pivot candidates.
865 var increment = 200 + ((to - from) & 15); 865 var increment = 200 + ((to - from) & 15);
866 for (var i = from + 1; i < to - 1; i += increment) { 866 for (var i = from + 1, j = 0; i < to - 1; i += increment, j++) {
867 t_array.push([i, a[i]]); 867 t_array[j] = [i, a[i]];
868 } 868 }
869 t_array.sort(function(a, b) { 869 %_CallFunction(t_array, function(a, b) {
870 return %_CallFunction(receiver, a[1], b[1], comparefn) } ); 870 return %_CallFunction(receiver, a[1], b[1], comparefn);
871 }, ArraySort);
871 var third_index = t_array[t_array.length >> 1][0]; 872 var third_index = t_array[t_array.length >> 1][0];
872 return third_index; 873 return third_index;
873 } 874 }
874 875
875 var QuickSort = function QuickSort(a, from, to) { 876 var QuickSort = function QuickSort(a, from, to) {
876 var third_index = 0; 877 var third_index = 0;
877 while (true) { 878 while (true) {
878 // Insertion sort is faster for short arrays. 879 // Insertion sort is faster for short arrays.
879 if (to - from <= 10) { 880 if (to - from <= 10) {
880 InsertionSort(a, from, to); 881 InsertionSort(a, from, to);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 // to obj itself, if obj has holes. Return one more than the maximal index 963 // to obj itself, if obj has holes. Return one more than the maximal index
963 // of a prototype property. 964 // of a prototype property.
964 var CopyFromPrototype = function CopyFromPrototype(obj, length) { 965 var CopyFromPrototype = function CopyFromPrototype(obj, length) {
965 var max = 0; 966 var max = 0;
966 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { 967 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) {
967 var indices = %GetArrayKeys(proto, length); 968 var indices = %GetArrayKeys(proto, length);
968 if (IS_NUMBER(indices)) { 969 if (IS_NUMBER(indices)) {
969 // It's an interval. 970 // It's an interval.
970 var proto_length = indices; 971 var proto_length = indices;
971 for (var i = 0; i < proto_length; i++) { 972 for (var i = 0; i < proto_length; i++) {
972 if (!obj.hasOwnProperty(i) && proto.hasOwnProperty(i)) { 973 if (!%_CallFunction(obj, i, ObjectHasOwnProperty) &&
arv (Not doing code reviews) 2014/09/15 18:05:28 Maybe use a helper or a macro for these? macro HA
974 %_CallFunction(proto, i, ObjectHasOwnProperty)) {
973 obj[i] = proto[i]; 975 obj[i] = proto[i];
974 if (i >= max) { max = i + 1; } 976 if (i >= max) { max = i + 1; }
975 } 977 }
976 } 978 }
977 } else { 979 } else {
978 for (var i = 0; i < indices.length; i++) { 980 for (var i = 0; i < indices.length; i++) {
979 var index = indices[i]; 981 var index = indices[i];
980 if (!IS_UNDEFINED(index) && 982 if (!IS_UNDEFINED(index) &&
981 !obj.hasOwnProperty(index) && proto.hasOwnProperty(index)) { 983 !%_CallFunction(obj, index, ObjectHasOwnProperty) &&
984 %_CallFunction(proto, index, ObjectHasOwnProperty)) {
982 obj[index] = proto[index]; 985 obj[index] = proto[index];
983 if (index >= max) { max = index + 1; } 986 if (index >= max) { max = index + 1; }
984 } 987 }
985 } 988 }
986 } 989 }
987 } 990 }
988 return max; 991 return max;
989 }; 992 };
990 993
991 // Set a value of "undefined" on all indices in the range from..to 994 // Set a value of "undefined" on all indices in the range from..to
992 // where a prototype of obj has an element. I.e., shadow all prototype 995 // where a prototype of obj has an element. I.e., shadow all prototype
993 // elements in that range. 996 // elements in that range.
994 var ShadowPrototypeElements = function(obj, from, to) { 997 var ShadowPrototypeElements = function(obj, from, to) {
995 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { 998 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) {
996 var indices = %GetArrayKeys(proto, to); 999 var indices = %GetArrayKeys(proto, to);
997 if (IS_NUMBER(indices)) { 1000 if (IS_NUMBER(indices)) {
998 // It's an interval. 1001 // It's an interval.
999 var proto_length = indices; 1002 var proto_length = indices;
1000 for (var i = from; i < proto_length; i++) { 1003 for (var i = from; i < proto_length; i++) {
1001 if (proto.hasOwnProperty(i)) { 1004 if (%_CallFunction(proto, i, ObjectHasOwnProperty)) {
1002 obj[i] = UNDEFINED; 1005 obj[i] = UNDEFINED;
1003 } 1006 }
1004 } 1007 }
1005 } else { 1008 } else {
1006 for (var i = 0; i < indices.length; i++) { 1009 for (var i = 0; i < indices.length; i++) {
1007 var index = indices[i]; 1010 var index = indices[i];
1008 if (!IS_UNDEFINED(index) && from <= index && 1011 if (!IS_UNDEFINED(index) && from <= index &&
1009 proto.hasOwnProperty(index)) { 1012 %_CallFunction(proto, index, ObjectHasOwnProperty)) {
1010 obj[index] = UNDEFINED; 1013 obj[index] = UNDEFINED;
1011 } 1014 }
1012 } 1015 }
1013 } 1016 }
1014 } 1017 }
1015 }; 1018 };
1016 1019
1017 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) { 1020 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) {
1018 // Copy defined elements from the end to fill in all holes and undefineds 1021 // Copy defined elements from the end to fill in all holes and undefineds
1019 // in the beginning of the array. Write undefineds and holes at the end 1022 // in the beginning of the array. Write undefineds and holes at the end
1020 // after loop is finished. 1023 // after loop is finished.
1021 var first_undefined = 0; 1024 var first_undefined = 0;
1022 var last_defined = length - 1; 1025 var last_defined = length - 1;
1023 var num_holes = 0; 1026 var num_holes = 0;
1024 while (first_undefined < last_defined) { 1027 while (first_undefined < last_defined) {
1025 // Find first undefined element. 1028 // Find first undefined element.
1026 while (first_undefined < last_defined && 1029 while (first_undefined < last_defined &&
1027 !IS_UNDEFINED(obj[first_undefined])) { 1030 !IS_UNDEFINED(obj[first_undefined])) {
1028 first_undefined++; 1031 first_undefined++;
1029 } 1032 }
1030 // Maintain the invariant num_holes = the number of holes in the original 1033 // Maintain the invariant num_holes = the number of holes in the original
1031 // array with indices <= first_undefined or > last_defined. 1034 // array with indices <= first_undefined or > last_defined.
1032 if (!obj.hasOwnProperty(first_undefined)) { 1035 if (!%_CallFunction(obj, first_undefined, ObjectHasOwnProperty)) {
1033 num_holes++; 1036 num_holes++;
1034 } 1037 }
1035 1038
1036 // Find last defined element. 1039 // Find last defined element.
1037 while (first_undefined < last_defined && 1040 while (first_undefined < last_defined &&
1038 IS_UNDEFINED(obj[last_defined])) { 1041 IS_UNDEFINED(obj[last_defined])) {
1039 if (!obj.hasOwnProperty(last_defined)) { 1042 if (!%_CallFunction(obj, last_defined, ObjectHasOwnProperty)) {
1040 num_holes++; 1043 num_holes++;
1041 } 1044 }
1042 last_defined--; 1045 last_defined--;
1043 } 1046 }
1044 if (first_undefined < last_defined) { 1047 if (first_undefined < last_defined) {
1045 // Fill in hole or undefined. 1048 // Fill in hole or undefined.
1046 obj[first_undefined] = obj[last_defined]; 1049 obj[first_undefined] = obj[last_defined];
1047 obj[last_defined] = UNDEFINED; 1050 obj[last_defined] = UNDEFINED;
1048 } 1051 }
1049 } 1052 }
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 )); 1549 ));
1547 1550
1548 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1551 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1549 "join", getFunction("join", ArrayJoin), 1552 "join", getFunction("join", ArrayJoin),
1550 "pop", getFunction("pop", ArrayPop), 1553 "pop", getFunction("pop", ArrayPop),
1551 "push", getFunction("push", ArrayPush) 1554 "push", getFunction("push", ArrayPush)
1552 )); 1555 ));
1553 } 1556 }
1554 1557
1555 SetUpArray(); 1558 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/array-sort.js » ('j') | test/mjsunit/array-sort.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698