1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9
* SUMMARY: Array.prototype.sort() should not (re-)define .length
10
* See http://bugzilla.mozilla.org/show_bug.cgi?id=130451
12
* From the ECMA-262 Edition 3 Final spec:
14
* NOTE: The sort function is intentionally generic; it does not require that
15
* its |this| value be an Array object. Therefore, it can be transferred to
16
* other kinds of objects for use as a method. Whether the sort function can
17
* be applied successfully to a host object is implementation-dependent.
19
* The interesting parts of this testcase are the contrasting expectations for
20
* Brendan's test below, when applied to Array objects vs. non-Array objects.
23
//-----------------------------------------------------------------------------
25
var BUGNUMBER = 130451;
26
var summary = 'Array.prototype.sort() should not (re-)define .length';
30
var actualvalues = [];
32
var expectedvalues = [];
34
var cmp = new Function();
38
* First: test Array.prototype.sort() on Array objects
40
status = inSection(1);
42
cmp = function(x,y) {return x-y;};
43
actual = arr.sort(cmp).length;
47
status = inSection(2);
49
cmp = function(x,y) {return y-x;};
50
actual = arr.sort(cmp).length;
54
status = inSection(3);
56
cmp = function(x,y) {return x-y;};
58
actual = arr.sort(cmp).length;
63
* This test is by Brendan. Setting arr.length to
64
* 2 and then 4 should cause elements to be deleted.
67
cmp = function(x,y) {return x-y;};
70
status = inSection(4);
75
status = inSection(5);
80
status = inSection(6);
86
status = inSection(7);
89
expect = '0,1,,'; //<---- see how 2,3 have been lost
95
* Now test Array.prototype.sort() on non-Array objects
97
status = inSection(8);
98
var obj = new Object();
99
obj.sort = Array.prototype.sort;
105
cmp = function(x,y) {return x-y;};
106
actual = obj.sort(cmp).length;
112
* Here again is Brendan's test. Unlike the array case
113
* above, the setting of obj.length to 2 and then 4
114
* should NOT cause elements to be deleted
117
obj.sort = Array.prototype.sort;
123
cmp = function(x,y) {return x-y;};
124
obj.sort(cmp); //<---- this is what triggered the buggy behavior below
125
obj.join = Array.prototype.join;
127
status = inSection(9);
132
status = inSection(10);
137
status = inSection(11);
144
* Before this bug was fixed, |actual| held the value '0,1,,'
145
* as in the Array-object case at top. This bug only occurred
146
* if Array.prototype.sort() had been applied to |obj|,
147
* as we have done higher up.
149
status = inSection(12);
158
//-----------------------------------------------------------------------------
160
//-----------------------------------------------------------------------------
166
statusitems[UBound] = status;
167
actualvalues[UBound] = actual;
168
expectedvalues[UBound] = expect;
176
printBugNumber(BUGNUMBER);
177
printStatus(summary);
179
for (var i=0; i<UBound; i++)
181
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);