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

Side by Side Diff: src/array.js

Issue 99272: Made sort on non-arrays also affect elements on the prototype, for JSC compatability. (Closed)
Patch Set: Added one more test Created 11 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/runtime.cc » ('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 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 691 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 a[i] = a[high_start]; 702 a[i] = a[high_start];
703 a[high_start] = element; 703 a[high_start] = element;
704 } else { // order == 0 704 } else { // order == 0
705 i++; 705 i++;
706 } 706 }
707 } 707 }
708 QuickSort(a, from, low_end); 708 QuickSort(a, from, low_end);
709 QuickSort(a, high_start, to); 709 QuickSort(a, high_start, to);
710 } 710 }
711 711
712 // Copies elements in the range 0..length from obj's prototype chain
713 // to obj itself, if obj has holes. Returns one more than the maximal index
714 // of a prototype property.
715 function CopyFromPrototype(obj, length) {
716 var max = 0;
717 for (var o = obj.__proto__; o; o = o.__proto__) {
Erik Corry 2009/05/01 09:58:55 Can't we call o proto or something? These 1-lette
718 var indices = %GetArrayKeys(o, length);
719 if (indices.length > 0) {
720 if (indices[0] == -1) {
721 // It's an interval
Erik Corry 2009/05/01 09:58:55 Missing full stop.
722 var proto_length = indices[1];
723 for (var i = 0; i < proto_length; i++) {
724 if (!obj.hasOwnProperty(i) && o.hasOwnProperty(i)) {
725 obj[i] = o[i];
726 if (i >= max) { max = i + 1; }
727 }
728 }
729 } else {
730 for (var i = 0; i < indices.length; i++) {
731 var index = indices[i];
732 if (!IS_UNDEFINED(index) &&
733 !obj.hasOwnProperty(index) && o.hasOwnProperty(index)) {
734 obj[index] = o[index];
735 if (index >= max) { max = index + 1; }
736 }
737 }
738 }
739 }
740 }
741 return max;
742 }
743
744 // Set a value of "undefined" on all indices in the range from..to
745 // where a prototype of obj has an element. I.e., shadow all prototype
746 // elements in that range.
747 function ShadowPrototypeElements(obj, from, to) {
748 for (var o = obj.__proto__; o; o = o.__proto__) {
Erik Corry 2009/05/01 09:58:55 And here.
749 var indices = %GetArrayKeys(o, to);
750 if (indices.length > 0) {
751 if (indices[0] == -1) {
752 // It's an interval
Erik Corry 2009/05/01 09:58:55 And here.
753 var proto_length = indices[1];
754 if (proto_length > from) {
Erik Corry 2009/05/01 09:58:55 >= perhaps?
Lasse Reichstein 2009/05/01 10:05:39 Wouldn't make a difference. The next line is a loo
755 for (var i = from; i < proto_length; i++) {
756 if (o.hasOwnProperty(i)) {
757 obj[i] = void 0;
758 }
759 }
760 }
761 } else {
762 for (var i = 0; i < indices.length; i++) {
763 var index = indices[i];
764 if (!IS_UNDEFINED(index) && from <= index &&
765 o.hasOwnProperty(index)) {
766 obj[index] = void 0;
767 }
768 }
769 }
770 }
771 }
772 }
773
712 var length = ToUint32(this.length); 774 var length = ToUint32(this.length);
713 if (length < 2) return this; 775 if (length < 2) return this;
714 776
777 var is_array = IS_ARRAY(this);
778 if (!is_array) {
779 // For compatibility with JSC, we also sort elements inherited from
780 // the prototype chain on non-Array objects.
781 // We do this by copying them to this object and sorting only
782 // local elements. This is not very efficient, but sorting with
783 // inherited elements happens very, very rarely, if at all.
784 // The specification allows "implementation dependent" behavior
785 // if an element on the prototype chain has an element that
786 // might interact with sorting.
787 var max_prototype_element = CopyFromPrototype(this, length);
Erik Corry 2009/05/01 09:58:55 I think the var declaration should be hoisted out
788 }
789
715 var num_non_undefined = %RemoveArrayHoles(this, length); 790 var num_non_undefined = %RemoveArrayHoles(this, length);
716 791
717 QuickSort(this, 0, num_non_undefined); 792 QuickSort(this, 0, num_non_undefined);
718 793
794 if (!is_array && (num_non_undefined + 1 < max_prototype_element)) {
795 // For compatibility with JSC, we shadow any elements in the prototype
796 // chain that has become exposed by sort moving a hole to its position.
797 ShadowPrototypeElements(this, num_non_undefined, max_prototype_element);
798 }
799
719 return this; 800 return this;
720 } 801 }
721 802
722 803
723 // The following functions cannot be made efficient on sparse arrays while 804 // The following functions cannot be made efficient on sparse arrays while
724 // preserving the semantics, since the calls to the receiver function can add 805 // preserving the semantics, since the calls to the receiver function can add
725 // or delete elements from the array. 806 // or delete elements from the array.
726 function ArrayFilter(f, receiver) { 807 function ArrayFilter(f, receiver) {
727 if (!IS_FUNCTION(f)) { 808 if (!IS_FUNCTION(f)) {
728 throw MakeTypeError('called_non_callable', [ f ]); 809 throw MakeTypeError('called_non_callable', [ f ]);
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 ArrayIndexOf: 1, 1049 ArrayIndexOf: 1,
969 ArrayLastIndexOf: 1, 1050 ArrayLastIndexOf: 1,
970 ArrayPush: 1, 1051 ArrayPush: 1,
971 ArrayReduce: 1, 1052 ArrayReduce: 1,
972 ArrayReduceRight: 1 1053 ArrayReduceRight: 1
973 }); 1054 });
974 } 1055 }
975 1056
976 1057
977 SetupArray(); 1058 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698