initial import
[vuplus_webkit] / Source / JavaScriptCore / tests / mozilla / js1_5 / Regress / regress-104077.js
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
3 *
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/
8 *
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
12 * License.
13 *
14 * The Original Code is JavaScript Engine testing utilities.
15 *
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2001
18 * the Initial Developer. All Rights Reserved.
19 *
20 * Contributor(s): chwu@nortelnetworks.com, timeless@mac.com,
21 *                 brendan@mozilla.org, pschwartau@netscape.com
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the NPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the NPL, the GPL or the LGPL.
34 *
35 * ***** END LICENSE BLOCK *****
36 *
37 *
38 * Date: 10 October 2001
39 * SUMMARY: Regression test for Bugzilla bug 104077
40 * See http://bugzilla.mozilla.org/show_bug.cgi?id=104077
41 * "JS crash: with/finally/return"
42 *
43 * Also http://bugzilla.mozilla.org/show_bug.cgi?id=120571
44 * "JS crash: try/catch/continue."
45 *
46 * SpiderMonkey crashed on this code - it shouldn't.
47 *
48 * NOTE: the finally-blocks below should execute even if their try-blocks
49 * have return or throw statements in them:
50 *
51 * ------- Additional Comment #76 From Mike Shaver 2001-12-07 01:21 -------
52 * finally trumps return, and all other control-flow constructs that cause
53 * program execution to jump out of the try block: throw, break, etc.  Once you
54 * enter a try block, you will execute the finally block after leaving the try,
55 * regardless of what happens to make you leave the try.
56 *
57 */
58 //-----------------------------------------------------------------------------
59 var UBound = 0;
60 var bug = 104077;
61 var summary = "Just testing that we don't crash on with/finally/return -";
62 var status = '';
63 var statusitems = [];
64 var actual = '';
65 var actualvalues = [];
66 var expect= '';
67 var expectedvalues = [];
68
69
70 function addValues(obj)
71 {
72   var sum;
73   with (obj)
74   {
75     try
76     {
77       sum = arg1 + arg2;
78     }
79     finally
80     {
81       return sum;
82     }
83   }
84 }
85
86 status = inSection(1);
87 var obj = new Object();
88 obj.arg1 = 1;
89 obj.arg2 = 2;
90 actual = addValues(obj);
91 expect = 3;
92 captureThis();
93
94
95
96 function tryThis()
97 {
98   var sum = 4 ;
99   var i = 0;
100
101   while (sum < 10)
102   {
103     try
104     {
105      sum += 1;
106      i += 1;
107     }
108     finally
109     {
110      print("In finally case of tryThis() function");
111     }
112   }
113   return i;
114 }
115
116 status = inSection(2);
117 actual = tryThis();
118 expect = 6;
119 captureThis();
120
121
122
123 function myTest(x)
124 {
125   var obj = new Object();
126   var msg;
127
128   with (obj)
129   {
130     msg = (x != null) ? "NO" : "YES";
131     print("Is the provided argument to myTest() null? : " + msg);
132
133     try
134     {
135       throw "ZZZ";
136     }
137     catch(e)
138     {
139       print("Caught thrown exception = " + e);
140     }
141   }
142
143   return 1;
144 }
145
146 status = inSection(3);
147 actual = myTest(null);
148 expect = 1;
149 captureThis();
150
151
152
153 function addValues_2(obj)
154 {
155   var sum = 0;
156   with (obj)
157   {
158     try
159     {
160       sum = arg1 + arg2;
161       with (arg3)
162       {
163         while (sum < 10)
164         {
165           try 
166           {
167             if (sum > 5)
168               return sum;
169             sum += 1;
170           }
171           catch(e)
172           {
173             print('Caught an exception in addValues_2() function: ' + e);
174           }
175         }
176       }
177     }
178     finally
179     {
180       return sum;
181     }
182   }
183 }
184
185 status = inSection(4);
186 obj = new Object();
187 obj.arg1 = 1;
188 obj.arg2 = 2;
189 obj.arg3 = new Object();
190 obj.arg3.a = 10;
191 obj.arg3.b = 20;
192 actual = addValues_2(obj);
193 expect = 6;
194 captureThis();
195
196
197
198 status = inSection(5);
199 try
200 {
201   throw new A();
202 }
203 catch(e)
204 {
205 }
206 finally
207 {
208   try
209   {
210     throw new A();
211   }
212   catch(e)
213   {
214   }
215   finally
216   {
217     actual = 'a';
218   }
219   actual = 'b';
220 }
221 expect = 'b';
222 captureThis();
223
224
225
226
227 function testfunc(mode)
228 {
229   var obj = new Object();
230   with (obj)
231   {
232     var num = 100;
233     var str = "abc" ;
234
235     if (str == null)
236     {
237       try
238       {
239         throw "authentication.0";
240       }
241       catch(e)
242       {
243       }
244       finally
245       {
246       }
247
248       return num;
249     }
250     else
251     {
252       try
253       {
254         if (mode == 0)
255           throw "authentication.0";
256         else
257           mytest();
258       }
259       catch(e)
260       {
261       }
262       finally
263       {
264       }
265
266       return num;
267     }
268   }
269 }
270
271 status = inSection(6);
272 actual = testfunc(0);
273 expect = 100; 
274 captureThis();
275
276 status = inSection(7);
277 actual = testfunc();
278 expect = 100;  
279 captureThis();
280
281
282
283
284 function entry_menu()
285 {
286   var document = new Object();
287   var dialog = new Object();
288   var num = 100;
289
290   with (document)
291   {
292     with (dialog)
293     {
294       try
295       {
296         while (true)
297         {
298           return num;
299         }
300       }
301       finally
302       {
303       }
304     }
305   }
306 }
307
308 status = inSection(8);
309 actual = entry_menu();
310 expect = 100;
311 captureThis();
312
313
314
315
316 function addValues_3(obj)
317 {
318   var sum = 0;
319   
320   with (obj)
321   {
322     try
323     {
324       sum = arg1 + arg2;
325       with (arg3)
326       {
327         while (sum < 10)
328         {
329           try 
330           {
331             if (sum > 5)
332               return sum;
333             sum += 1;
334           }
335           catch (e)
336           {
337             sum += 1;
338             print(e);
339           }
340         }
341       }
342     }
343     finally
344     {
345       try 
346       { 
347         sum +=1;
348         print("In finally block of addValues_3() function: sum = " + sum);
349       } 
350       catch (e if e == 42) 
351       {
352         sum +=1;
353         print('In finally catch block of addValues_3() function: sum = ' + sum + ', e = ' + e);
354       } 
355       finally 
356       {
357         sum +=1;
358         print("In finally finally block of addValues_3() function: sum = " + sum);
359         return sum;
360       }
361     }
362   }
363 }
364
365 status = inSection(9);
366 obj = new Object();
367 obj.arg1 = 1;
368 obj.arg2 = 2;
369 obj.arg3 = new Object();
370 obj.arg3.a = 10;
371 obj.arg3.b = 20;
372 actual = addValues_3(obj);
373 expect = 8;
374 captureThis();
375
376
377
378
379 function addValues_4(obj)
380 {
381   var sum = 0;
382
383   with (obj)
384   {
385     try
386     {
387       sum = arg1 + arg2;
388       with (arg3)
389       {
390         while (sum < 10)
391         {
392           try 
393           {
394             if (sum > 5)
395               return sum;
396             sum += 1;
397           }
398           catch (e)
399           {
400             sum += 1;
401             print(e);
402           }
403         }
404       }
405     }
406     finally
407     {
408       try 
409       {
410         sum += 1;
411         print("In finally block of addValues_4() function: sum = " + sum);
412       }
413       catch (e if e == 42)
414       {
415         sum += 1;
416         print("In 1st finally catch block of addValues_4() function: sum = " + sum + ", e = " + e);
417       } 
418       catch (e if e == 43)
419       {
420         sum += 1;
421         print("In 2nd finally catch block of addValues_4() function: sum = " + sum + ", e = " + e);
422       }
423       finally
424       {
425         sum += 1;
426         print("In finally finally block of addValues_4() function: sum = " + sum);
427         return sum;
428       }
429     }
430   }
431 }
432
433 status = inSection(10);
434 obj = new Object();
435 obj.arg1 = 1;
436 obj.arg2 = 2;
437 obj.arg3 = new Object();
438 obj.arg3.a = 10;
439 obj.arg3.b = 20;
440 actual = addValues_4(obj);
441 expect = 8;
442 captureThis();
443
444
445
446
447 function addValues_5(obj)
448 {
449   var sum = 0;
450
451   with (obj)
452   {
453     try
454     {
455       sum = arg1 + arg2;
456       with (arg3)
457       {
458         while (sum < 10)
459         {
460           try 
461           {
462            if (sum > 5)
463              return sum;
464            sum += 1;
465           }
466           catch (e)
467           {
468             sum += 1;
469             print(e);
470           }
471         }
472       }
473     }
474     finally
475     {
476       try
477       {
478         sum += 1;
479         print("In finally block of addValues_5() function: sum = " + sum);
480       }
481       catch (e)
482       {
483         sum += 1;
484         print("In finally catch block of addValues_5() function: sum = " + sum + ", e = " + e);
485       }
486       finally
487       {
488         sum += 1;
489         print("In finally finally block of addValues_5() function: sum = " + sum);
490         return sum;
491       }
492     }
493   }
494 }
495
496 status = inSection(11);
497 obj = new Object();
498 obj.arg1 = 1;
499 obj.arg2 = 2;
500 obj.arg3 = new Object();
501 obj.arg3.a = 10;
502 obj.arg3.b = 20;
503 actual = addValues_5(obj);
504 expect = 8;
505 captureThis();
506
507
508
509
510 function testObj(obj)
511 {
512   var x = 42;
513
514   try
515   {
516     with (obj)
517     {
518       if (obj.p)
519         throw obj.p;
520       x = obj.q;
521     }
522   }
523   finally
524   {
525     print("in finally block of testObj() function");
526     return 999;
527   }
528 }
529
530 status = inSection(12);
531 obj = {p:43};
532 actual = testObj(obj);
533 expect = 999;
534 captureThis();
535
536
537
538 /*
539  * Next two cases are from http://bugzilla.mozilla.org/show_bug.cgi?id=120571
540  */
541 function a120571()
542 {
543   while(0)
544   {
545     try
546     {
547     }
548     catch(e)
549     {
550       continue;
551     }
552   }
553 }
554
555 // this caused a crash! Test to see that it doesn't now.
556 print(a120571);
557
558 // Now test that we have a non-null value for a120571.toString()
559 status = inSection(13);
560 try
561 {
562   actual = a120571.toString().match(/continue/)[0];
563 }
564 catch(e)
565 {
566   actual = 'FAILED! Did not find "continue" in function body';
567 }
568 expect = 'continue';
569 captureThis();
570
571
572
573
574 function b()
575 {
576   for(;;)
577   {
578     try
579     {
580     }
581     catch(e)
582     {
583       continue;
584     }
585   }
586 }
587
588 // this caused a crash!!! Test to see that it doesn't now.
589 print(b);
590
591 // Now test that we have a non-null value for b.toString()
592 status = inSection(14);
593 try
594 {
595   actual = b.toString().match(/continue/)[0];
596 }
597 catch(e)
598 {
599   actual = 'FAILED! Did not find "continue" in function body';
600 }
601 expect = 'continue';
602 captureThis();
603
604
605
606
607
608 //-----------------------------------------------------------------------------
609 test();
610 //-----------------------------------------------------------------------------
611
612
613
614 function captureThis()
615 {
616   statusitems[UBound] = status;
617   actualvalues[UBound] = actual;
618   expectedvalues[UBound] = expect;
619   UBound++;
620 }
621
622
623 function test()
624 {
625   enterFunc ('test');
626   printBugNumber (bug);
627   printStatus (summary);
628
629   for (var i=0; i<UBound; i++)
630   {
631     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
632   }
633
634   exitFunc ('test');
635 }