1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is JavaScript Engine testing utilities.
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2002
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s): brendan@mozilla.org, pschwartau@netscape.com
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the NPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the NPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK *****
38 * SUMMARY: Array.prototype.sort() should not (re-)define .length
39 * See http://bugzilla.mozilla.org/show_bug.cgi?id=130451
41 * From the ECMA-262 Edition 3 Final spec:
43 * NOTE: The sort function is intentionally generic; it does not require that
44 * its |this| value be an Array object. Therefore, it can be transferred to
45 * other kinds of objects for use as a method. Whether the sort function can
46 * be applied successfully to a host object is implementation-dependent.
48 * The interesting parts of this testcase are the contrasting expectations for
49 * Brendan's test below, when applied to Array objects vs. non-Array objects.
52 //-----------------------------------------------------------------------------
55 var summary = 'Array.prototype.sort() should not (re-)define .length';
59 var actualvalues = [];
61 var expectedvalues = [];
63 var cmp = new Function();
67 * First: test Array.prototype.sort() on Array objects
69 status = inSection(1);
71 cmp = function(x,y) {return x-y;};
72 actual = arr.sort(cmp).length;
76 status = inSection(2);
78 cmp = function(x,y) {return y-x;};
79 actual = arr.sort(cmp).length;
83 status = inSection(3);
85 cmp = function(x,y) {return x-y;};
87 actual = arr.sort(cmp).length;
92 * This test is by Brendan. Setting arr.length to
93 * 2 and then 4 should cause elements to be deleted.
96 cmp = function(x,y) {return x-y;};
99 status = inSection(4);
104 status = inSection(5);
109 status = inSection(6);
115 status = inSection(7);
118 expect = '0,1,,'; //<---- see how 2,3 have been lost
124 * Now test Array.prototype.sort() on non-Array objects
126 status = inSection(8);
127 var obj = new Object();
128 obj.sort = Array.prototype.sort;
134 cmp = function(x,y) {return x-y;};
135 actual = obj.sort(cmp).length;
141 * Here again is Brendan's test. Unlike the array case
142 * above, the setting of obj.length to 2 and then 4
143 * should NOT cause elements to be deleted
146 obj.sort = Array.prototype.sort;
152 cmp = function(x,y) {return x-y;};
153 obj.sort(cmp); //<---- this is what triggered the buggy behavior below
154 obj.join = Array.prototype.join;
156 status = inSection(9);
161 status = inSection(10);
166 status = inSection(11);
173 * Before this bug was fixed, |actual| held the value '0,1,,'
174 * as in the Array-object case at top. This bug only occurred
175 * if Array.prototype.sort() had been applied to |obj|,
176 * as we have done higher up.
178 status = inSection(12);
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
195 statusitems[UBound] = status;
196 actualvalues[UBound] = actual;
197 expectedvalues[UBound] = expect;
206 printStatus(summary);
208 for (var i=0; i<UBound; i++)
210 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);