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

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 80-column line limit 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 | src/macros.py » ('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 "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 (!HAS_OWN_PROPERTY(obj, i) && HAS_OWN_PROPERTY(proto, i)) {
973 obj[i] = proto[i]; 974 obj[i] = proto[i];
974 if (i >= max) { max = i + 1; } 975 if (i >= max) { max = i + 1; }
975 } 976 }
976 } 977 }
977 } else { 978 } else {
978 for (var i = 0; i < indices.length; i++) { 979 for (var i = 0; i < indices.length; i++) {
979 var index = indices[i]; 980 var index = indices[i];
980 if (!IS_UNDEFINED(index) && 981 if (!IS_UNDEFINED(index) && !HAS_OWN_PROPERTY(obj, index)
981 !obj.hasOwnProperty(index) && proto.hasOwnProperty(index)) { 982 && HAS_OWN_PROPERTY(proto, index)) {
982 obj[index] = proto[index]; 983 obj[index] = proto[index];
983 if (index >= max) { max = index + 1; } 984 if (index >= max) { max = index + 1; }
984 } 985 }
985 } 986 }
986 } 987 }
987 } 988 }
988 return max; 989 return max;
989 }; 990 };
990 991
991 // Set a value of "undefined" on all indices in the range from..to 992 // 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 993 // where a prototype of obj has an element. I.e., shadow all prototype
993 // elements in that range. 994 // elements in that range.
994 var ShadowPrototypeElements = function(obj, from, to) { 995 var ShadowPrototypeElements = function(obj, from, to) {
995 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { 996 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) {
996 var indices = %GetArrayKeys(proto, to); 997 var indices = %GetArrayKeys(proto, to);
997 if (IS_NUMBER(indices)) { 998 if (IS_NUMBER(indices)) {
998 // It's an interval. 999 // It's an interval.
999 var proto_length = indices; 1000 var proto_length = indices;
1000 for (var i = from; i < proto_length; i++) { 1001 for (var i = from; i < proto_length; i++) {
1001 if (proto.hasOwnProperty(i)) { 1002 if (HAS_OWN_PROPERTY(proto, i)) {
1002 obj[i] = UNDEFINED; 1003 obj[i] = UNDEFINED;
1003 } 1004 }
1004 } 1005 }
1005 } else { 1006 } else {
1006 for (var i = 0; i < indices.length; i++) { 1007 for (var i = 0; i < indices.length; i++) {
1007 var index = indices[i]; 1008 var index = indices[i];
1008 if (!IS_UNDEFINED(index) && from <= index && 1009 if (!IS_UNDEFINED(index) && from <= index &&
1009 proto.hasOwnProperty(index)) { 1010 HAS_OWN_PROPERTY(proto, index)) {
1010 obj[index] = UNDEFINED; 1011 obj[index] = UNDEFINED;
1011 } 1012 }
1012 } 1013 }
1013 } 1014 }
1014 } 1015 }
1015 }; 1016 };
1016 1017
1017 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) { 1018 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) {
1018 // Copy defined elements from the end to fill in all holes and undefineds 1019 // 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 1020 // in the beginning of the array. Write undefineds and holes at the end
1020 // after loop is finished. 1021 // after loop is finished.
1021 var first_undefined = 0; 1022 var first_undefined = 0;
1022 var last_defined = length - 1; 1023 var last_defined = length - 1;
1023 var num_holes = 0; 1024 var num_holes = 0;
1024 while (first_undefined < last_defined) { 1025 while (first_undefined < last_defined) {
1025 // Find first undefined element. 1026 // Find first undefined element.
1026 while (first_undefined < last_defined && 1027 while (first_undefined < last_defined &&
1027 !IS_UNDEFINED(obj[first_undefined])) { 1028 !IS_UNDEFINED(obj[first_undefined])) {
1028 first_undefined++; 1029 first_undefined++;
1029 } 1030 }
1030 // Maintain the invariant num_holes = the number of holes in the original 1031 // Maintain the invariant num_holes = the number of holes in the original
1031 // array with indices <= first_undefined or > last_defined. 1032 // array with indices <= first_undefined or > last_defined.
1032 if (!obj.hasOwnProperty(first_undefined)) { 1033 if (!HAS_OWN_PROPERTY(obj, first_undefined)) {
1033 num_holes++; 1034 num_holes++;
1034 } 1035 }
1035 1036
1036 // Find last defined element. 1037 // Find last defined element.
1037 while (first_undefined < last_defined && 1038 while (first_undefined < last_defined &&
1038 IS_UNDEFINED(obj[last_defined])) { 1039 IS_UNDEFINED(obj[last_defined])) {
1039 if (!obj.hasOwnProperty(last_defined)) { 1040 if (!HAS_OWN_PROPERTY(obj, last_defined)) {
1040 num_holes++; 1041 num_holes++;
1041 } 1042 }
1042 last_defined--; 1043 last_defined--;
1043 } 1044 }
1044 if (first_undefined < last_defined) { 1045 if (first_undefined < last_defined) {
1045 // Fill in hole or undefined. 1046 // Fill in hole or undefined.
1046 obj[first_undefined] = obj[last_defined]; 1047 obj[first_undefined] = obj[last_defined];
1047 obj[last_defined] = UNDEFINED; 1048 obj[last_defined] = UNDEFINED;
1048 } 1049 }
1049 } 1050 }
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 )); 1547 ));
1547 1548
1548 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1549 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1549 "join", getFunction("join", ArrayJoin), 1550 "join", getFunction("join", ArrayJoin),
1550 "pop", getFunction("pop", ArrayPop), 1551 "pop", getFunction("pop", ArrayPop),
1551 "push", getFunction("push", ArrayPush) 1552 "push", getFunction("push", ArrayPush)
1552 )); 1553 ));
1553 } 1554 }
1554 1555
1555 SetUpArray(); 1556 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | src/macros.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698