OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Reduce functions. | 148 // Reduce functions. |
149 var reducing_functions = | 149 var reducing_functions = |
150 [Array.prototype.reduce, | 150 [Array.prototype.reduce, |
151 Array.prototype.reduceRight]; | 151 Array.prototype.reduceRight]; |
152 | 152 |
153 // Test that all natives using the ToObject call throw the right exception. | 153 // Test that all natives using the ToObject call throw the right exception. |
154 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | 154 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
155 // Sanity check that all functions are correct | 155 // Sanity check that all functions are correct |
156 assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function"); | 156 assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function"); |
157 | 157 |
| 158 var exception = false; |
158 try { | 159 try { |
159 // We call all functions with no parameters, which means that essential | 160 // We call all functions with no parameters, which means that essential |
160 // parameters will have the undefined value. | 161 // parameters will have the undefined value. |
161 // The test for whether the "this" value is null or undefined is always | 162 // The test for whether the "this" value is null or undefined is always |
162 // performed before access to the other parameters, so even if the | 163 // performed before access to the other parameters, so even if the |
163 // undefined value is an invalid argument value, it mustn't change | 164 // undefined value is an invalid argument value, it mustn't change |
164 // the result of the test. | 165 // the result of the test. |
165 should_throw_on_null_and_undefined[i].call(null); | 166 should_throw_on_null_and_undefined[i].call(null); |
166 assertUnreachable(); | |
167 } catch (e) { | 167 } catch (e) { |
| 168 exception = true; |
168 assertTrue("called_on_null_or_undefined" == e.type || | 169 assertTrue("called_on_null_or_undefined" == e.type || |
169 "null_to_object" == e.type); | 170 "null_to_object" == e.type); |
170 } | 171 } |
| 172 assertTrue(exception); |
171 | 173 |
| 174 exception = false; |
172 try { | 175 try { |
173 should_throw_on_null_and_undefined[i].call(undefined); | 176 should_throw_on_null_and_undefined[i].call(undefined); |
174 assertUnreachable(); | |
175 } catch (e) { | 177 } catch (e) { |
| 178 exception = true; |
176 assertTrue("called_on_null_or_undefined" == e.type || | 179 assertTrue("called_on_null_or_undefined" == e.type || |
177 "null_to_object" == e.type); | 180 "null_to_object" == e.type); |
178 } | 181 } |
| 182 assertTrue(exception); |
179 | 183 |
| 184 exception = false; |
180 try { | 185 try { |
181 should_throw_on_null_and_undefined[i].apply(null); | 186 should_throw_on_null_and_undefined[i].apply(null); |
182 assertUnreachable(); | |
183 } catch (e) { | 187 } catch (e) { |
| 188 exception = true; |
184 assertTrue("called_on_null_or_undefined" == e.type || | 189 assertTrue("called_on_null_or_undefined" == e.type || |
185 "null_to_object" == e.type); | 190 "null_to_object" == e.type); |
186 } | 191 } |
| 192 assertTrue(exception); |
187 | 193 |
| 194 exception = false; |
188 try { | 195 try { |
189 should_throw_on_null_and_undefined[i].apply(undefined); | 196 should_throw_on_null_and_undefined[i].apply(undefined); |
190 assertUnreachable(); | |
191 } catch (e) { | 197 } catch (e) { |
| 198 exception = true; |
192 assertTrue("called_on_null_or_undefined" == e.type || | 199 assertTrue("called_on_null_or_undefined" == e.type || |
193 "null_to_object" == e.type); | 200 "null_to_object" == e.type); |
194 } | 201 } |
| 202 assertTrue(exception); |
195 } | 203 } |
196 | 204 |
197 // Test that all natives that are non generic throw on null and undefined. | 205 // Test that all natives that are non generic throw on null and undefined. |
198 for (var i = 0; i < non_generic.length; i++) { | 206 for (var i = 0; i < non_generic.length; i++) { |
199 // Sanity check that all functions are correct | 207 // Sanity check that all functions are correct |
200 assertEquals(typeof(non_generic[i]), "function"); | 208 assertEquals(typeof(non_generic[i]), "function"); |
| 209 |
| 210 exception = false; |
201 try { | 211 try { |
202 non_generic[i].call(null); | 212 non_generic[i].call(null); |
203 assertUnreachable(); | |
204 } catch (e) { | 213 } catch (e) { |
| 214 exception = true; |
205 assertTrue(e instanceof TypeError); | 215 assertTrue(e instanceof TypeError); |
206 } | 216 } |
| 217 assertTrue(exception); |
207 | 218 |
| 219 exception = false; |
208 try { | 220 try { |
209 non_generic[i].call(null); | 221 non_generic[i].call(null); |
210 assertUnreachable(); | |
211 } catch (e) { | 222 } catch (e) { |
| 223 exception = true; |
212 assertTrue(e instanceof TypeError); | 224 assertTrue(e instanceof TypeError); |
213 } | 225 } |
| 226 assertTrue(exception); |
214 | 227 |
| 228 exception = false; |
215 try { | 229 try { |
216 non_generic[i].apply(null); | 230 non_generic[i].apply(null); |
217 assertUnreachable(); | |
218 } catch (e) { | 231 } catch (e) { |
| 232 exception = true; |
219 assertTrue(e instanceof TypeError); | 233 assertTrue(e instanceof TypeError); |
220 } | 234 } |
| 235 assertTrue(exception); |
221 | 236 |
| 237 exception = false; |
222 try { | 238 try { |
223 non_generic[i].apply(null); | 239 non_generic[i].apply(null); |
224 assertUnreachable(); | |
225 } catch (e) { | 240 } catch (e) { |
| 241 exception = true; |
226 assertTrue(e instanceof TypeError); | 242 assertTrue(e instanceof TypeError); |
227 } | 243 } |
| 244 assertTrue(exception); |
228 } | 245 } |
229 | 246 |
230 | 247 |
231 // Test that we still throw when calling with thisArg null or undefined | 248 // Test that we still throw when calling with thisArg null or undefined |
232 // through an array mapping function. | 249 // through an array mapping function. |
233 var array = [1,2,3,4,5]; | 250 var array = [1,2,3,4,5]; |
234 for (var j = 0; j < mapping_functions.length; j++) { | 251 for (var j = 0; j < mapping_functions.length; j++) { |
235 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | 252 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
| 253 exception = false; |
236 try { | 254 try { |
237 mapping_functions[j].call(array, | 255 mapping_functions[j].call(array, |
238 should_throw_on_null_and_undefined[i], | 256 should_throw_on_null_and_undefined[i], |
239 null); | 257 null); |
240 assertUnreachable(); | |
241 } catch (e) { | 258 } catch (e) { |
| 259 exception = true; |
242 assertTrue("called_on_null_or_undefined" == e.type || | 260 assertTrue("called_on_null_or_undefined" == e.type || |
243 "null_to_object" == e.type); | 261 "null_to_object" == e.type); |
244 } | 262 } |
| 263 assertTrue(exception); |
245 | 264 |
| 265 exception = false; |
246 try { | 266 try { |
247 mapping_functions[j].call(array, | 267 mapping_functions[j].call(array, |
248 should_throw_on_null_and_undefined[i], | 268 should_throw_on_null_and_undefined[i], |
249 undefined); | 269 undefined); |
250 assertUnreachable(); | |
251 } catch (e) { | 270 } catch (e) { |
| 271 exception = true; |
252 assertTrue("called_on_null_or_undefined" == e.type || | 272 assertTrue("called_on_null_or_undefined" == e.type || |
253 "null_to_object" == e.type); | 273 "null_to_object" == e.type); |
254 } | 274 } |
| 275 assertTrue(exception); |
255 } | 276 } |
256 } | 277 } |
257 | 278 |
258 for (var j = 0; j < mapping_functions.length; j++) { | 279 for (var j = 0; j < mapping_functions.length; j++) { |
259 for (var i = 0; i < non_generic.length; i++) { | 280 for (var i = 0; i < non_generic.length; i++) { |
| 281 exception = false; |
260 try { | 282 try { |
261 mapping_functions[j].call(array, | 283 mapping_functions[j].call(array, |
262 non_generic[i], | 284 non_generic[i], |
263 null); | 285 null); |
264 assertUnreachable(); | |
265 } catch (e) { | 286 } catch (e) { |
| 287 exception = true; |
266 assertTrue(e instanceof TypeError); | 288 assertTrue(e instanceof TypeError); |
267 } | 289 } |
| 290 assertTrue(exception); |
268 | 291 |
| 292 exception = false; |
269 try { | 293 try { |
270 mapping_functions[j].call(array, | 294 mapping_functions[j].call(array, |
271 non_generic[i], | 295 non_generic[i], |
272 undefined); | 296 undefined); |
273 assertUnreachable(); | |
274 } catch (e) { | 297 } catch (e) { |
| 298 exception = true; |
275 assertTrue(e instanceof TypeError); | 299 assertTrue(e instanceof TypeError); |
276 } | 300 } |
| 301 assertTrue(exception); |
277 } | 302 } |
278 } | 303 } |
279 | 304 |
280 | 305 |
281 // Reduce functions do a call with null as this argument. | 306 // Reduce functions do a call with null as this argument. |
282 for (var j = 0; j < reducing_functions.length; j++) { | 307 for (var j = 0; j < reducing_functions.length; j++) { |
283 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { | 308 for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) { |
| 309 exception = false; |
284 try { | 310 try { |
285 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); | 311 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); |
286 assertUnreachable(); | |
287 } catch (e) { | 312 } catch (e) { |
| 313 exception = true; |
288 assertTrue("called_on_null_or_undefined" == e.type || | 314 assertTrue("called_on_null_or_undefined" == e.type || |
289 "null_to_object" == e.type); | 315 "null_to_object" == e.type); |
290 } | 316 } |
| 317 assertTrue(exception); |
291 | 318 |
| 319 exception = false; |
292 try { | 320 try { |
293 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); | 321 reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]); |
294 assertUnreachable(); | |
295 } catch (e) { | 322 } catch (e) { |
| 323 exception = true; |
296 assertTrue("called_on_null_or_undefined" == e.type || | 324 assertTrue("called_on_null_or_undefined" == e.type || |
297 "null_to_object" == e.type); | 325 "null_to_object" == e.type); |
298 } | 326 } |
| 327 assertTrue(exception); |
299 } | 328 } |
300 } | 329 } |
301 | 330 |
302 for (var j = 0; j < reducing_functions.length; j++) { | 331 for (var j = 0; j < reducing_functions.length; j++) { |
303 for (var i = 0; i < non_generic.length; i++) { | 332 for (var i = 0; i < non_generic.length; i++) { |
| 333 exception = false; |
304 try { | 334 try { |
305 reducing_functions[j].call(array, non_generic[i]); | 335 reducing_functions[j].call(array, non_generic[i]); |
306 assertUnreachable(); | |
307 } catch (e) { | 336 } catch (e) { |
| 337 exception = true; |
308 assertTrue(e instanceof TypeError); | 338 assertTrue(e instanceof TypeError); |
309 } | 339 } |
| 340 assertTrue(exception); |
310 | 341 |
| 342 exception = false; |
311 try { | 343 try { |
312 reducing_functions[j].call(array, non_generic[i]); | 344 reducing_functions[j].call(array, non_generic[i]); |
313 assertUnreachable(); | |
314 } catch (e) { | 345 } catch (e) { |
| 346 exception = true; |
315 assertTrue(e instanceof TypeError); | 347 assertTrue(e instanceof TypeError); |
316 } | 348 } |
| 349 assertTrue(exception); |
317 } | 350 } |
318 } | 351 } |
319 | 352 |
320 | 353 |
321 // Object.prototype.toString() | 354 // Object.prototype.toString() |
322 assertEquals(Object.prototype.toString.call(null), | 355 assertEquals(Object.prototype.toString.call(null), |
323 '[object Null]') | 356 '[object Null]') |
324 | 357 |
325 assertEquals(Object.prototype.toString.call(undefined), | 358 assertEquals(Object.prototype.toString.call(undefined), |
326 '[object Undefined]') | 359 '[object Undefined]') |
OLD | NEW |