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