@@ -92,3 +92,123 @@ try {
92
92
assert ( e . message === "foo" ) ;
93
93
assert ( e instanceof ReferenceError ) ;
94
94
}
95
+
96
+ // Checking behavior when this value is undefined
97
+ var obj = { sort : Array . prototype . sort } ;
98
+
99
+ try {
100
+ obj . sort . call ( undefined , function ( ) { } ) ;
101
+ assert ( false ) ;
102
+ } catch ( e ) {
103
+ assert ( e instanceof TypeError ) ;
104
+ }
105
+
106
+ // Checking behavior when length's valueOf throws exception
107
+ var len = { } ;
108
+ Object . defineProperty ( len , 'valueOf' , { 'get' : function ( ) { throw new ReferenceError ( "foo" ) ; } } ) ;
109
+ var obj = { sort : Array . prototype . sort , length : len } ;
110
+
111
+ try {
112
+ obj . sort ( ) ;
113
+ assert ( false ) ;
114
+ } catch ( e ) {
115
+ assert ( e . message === 'foo' ) ;
116
+ assert ( e instanceof ReferenceError ) ;
117
+ }
118
+
119
+ // Checking behavior when unable to get elements
120
+ var obj = { sort : Array . prototype . sort , length : 2 } ;
121
+ Object . defineProperty ( obj , '0' , { 'get' : function ( ) { throw new ReferenceError ( "foo" ) ; } } ) ;
122
+ Object . defineProperty ( obj , '1' , { 'get' : function ( ) { throw new ReferenceError ( "bar" ) ; } } ) ;
123
+
124
+ try {
125
+ obj . sort ( ) ;
126
+ assert ( false ) ;
127
+ } catch ( e ) {
128
+ assert ( e . message === "foo" ) ;
129
+ assert ( e instanceof ReferenceError ) ;
130
+ }
131
+
132
+ // Checking behavior when array is non-extensible while sorting
133
+ var arr = [ 1 , 0 ] ;
134
+
135
+ try {
136
+ arr . sort ( function ( ) { Object . freeze ( arr ) } ) ;
137
+ assert ( false ) ;
138
+ } catch ( e ) {
139
+ assert ( e instanceof TypeError ) ;
140
+ }
141
+
142
+ // Checking behavior when unable to delete property
143
+ var obj = { sort : Array . prototype . sort , '0' : 2 , '1' : 1 , length : 4 } ;
144
+ Object . defineProperty ( obj , '3' , function ( ) { } ) ;
145
+
146
+ try {
147
+ obj . sort ( ) ;
148
+ assert ( false ) ;
149
+ }
150
+ catch ( e ) {
151
+ assert ( e instanceof TypeError ) ;
152
+ }
153
+
154
+ // Checking behavior when unable to get the last element
155
+ var arr = [ 1 , 2 , ] ;
156
+ Object . defineProperty ( arr , '2' , { 'get' : function ( ) { throw new ReferenceError ( "foo" ) ; } } ) ;
157
+
158
+ try {
159
+ arr . sort ( ) ;
160
+ assert ( false ) ;
161
+ }
162
+ catch ( e ) {
163
+ assert ( e . message === 'foo' ) ;
164
+ assert ( e instanceof ReferenceError ) ;
165
+ }
166
+
167
+ // Checking behavior when lhs_value throws exception at comparefn
168
+ f = function ( ) { throw new ReferenceError ( 'foo' ) ; } ;
169
+ obj = { 'toString' : f } ;
170
+ arr = [ obj , 1 ] ;
171
+
172
+ try {
173
+ arr . sort ( ) ;
174
+ assert ( false ) ;
175
+ }
176
+ catch ( e ) {
177
+ assert ( e . message === 'foo' ) ;
178
+ assert ( e instanceof ReferenceError ) ;
179
+ }
180
+
181
+ // Checking behavior when rhs_value throws exception at comparefn
182
+ f = function ( ) { throw new ReferenceError ( 'foo' ) ; } ;
183
+ obj = { 'toString' : f } ;
184
+ arr = [ 1 , obj ] ;
185
+
186
+ try {
187
+ arr . sort ( ) ;
188
+ assert ( false ) ;
189
+ }
190
+ catch ( e ) {
191
+ assert ( e . message === 'foo' ) ;
192
+ assert ( e instanceof ReferenceError ) ;
193
+ }
194
+
195
+ // Sorting when array elements are the same string
196
+ arr = [ 'foo' , 'foo' ] ;
197
+ arr . sort ( ) ;
198
+
199
+ assert ( arr [ 0 ] === 'foo' ) ;
200
+ assert ( arr [ 1 ] === 'foo' ) ;
201
+
202
+ // Checking behavior when comparefn's call value cannot be converted to number
203
+ obj = { } ;
204
+ Object . defineProperty ( obj , 'toString' , function ( ) { } ) ;
205
+ f = function ( ) { return obj } ;
206
+ arr = [ 1 , 2 ] ;
207
+
208
+ try {
209
+ arr . sort ( f ) ;
210
+ assert ( false ) ;
211
+ }
212
+ catch ( e ) {
213
+ assert ( e instanceof TypeError ) ;
214
+ }
0 commit comments