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//-----------------------------------------------------------------------------
59var UBound = 0;
60var bug = 104077;
61var summary = "Just testing that we don't crash on with/finally/return -";
62var status = '';
63var statusitems = [];
64var actual = '';
65var actualvalues = [];
66var expect= '';
67var expectedvalues = [];
68
69
70function 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
86status = inSection(1);
87var obj = new Object();
88obj.arg1 = 1;
89obj.arg2 = 2;
90actual = addValues(obj);
91expect = 3;
92captureThis();
93
94
95
96function 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
116status = inSection(2);
117actual = tryThis();
118expect = 6;
119captureThis();
120
121
122
123function 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
146status = inSection(3);
147actual = myTest(null);
148expect = 1;
149captureThis();
150
151
152
153function 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
185status = inSection(4);
186obj = new Object();
187obj.arg1 = 1;
188obj.arg2 = 2;
189obj.arg3 = new Object();
190obj.arg3.a = 10;
191obj.arg3.b = 20;
192actual = addValues_2(obj);
193expect = 6;
194captureThis();
195
196
197
198status = inSection(5);
199try
200{
201  throw new A();
202}
203catch(e)
204{
205}
206finally
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}
221expect = 'b';
222captureThis();
223
224
225
226
227function 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
271status = inSection(6);
272actual = testfunc(0);
273expect = 100;
274captureThis();
275
276status = inSection(7);
277actual = testfunc();
278expect = 100;
279captureThis();
280
281
282
283
284function 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
308status = inSection(8);
309actual = entry_menu();
310expect = 100;
311captureThis();
312
313
314
315
316function 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
365status = inSection(9);
366obj = new Object();
367obj.arg1 = 1;
368obj.arg2 = 2;
369obj.arg3 = new Object();
370obj.arg3.a = 10;
371obj.arg3.b = 20;
372actual = addValues_3(obj);
373expect = 8;
374captureThis();
375
376
377
378
379function 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
433status = inSection(10);
434obj = new Object();
435obj.arg1 = 1;
436obj.arg2 = 2;
437obj.arg3 = new Object();
438obj.arg3.a = 10;
439obj.arg3.b = 20;
440actual = addValues_4(obj);
441expect = 8;
442captureThis();
443
444
445
446
447function 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
496status = inSection(11);
497obj = new Object();
498obj.arg1 = 1;
499obj.arg2 = 2;
500obj.arg3 = new Object();
501obj.arg3.a = 10;
502obj.arg3.b = 20;
503actual = addValues_5(obj);
504expect = 8;
505captureThis();
506
507
508
509
510function 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
530status = inSection(12);
531obj = {p:43};
532actual = testObj(obj);
533expect = 999;
534captureThis();
535
536
537
538/*
539 * Next two cases are from http://bugzilla.mozilla.org/show_bug.cgi?id=120571
540 */
541function 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.
556print(a120571);
557
558// Now test that we have a non-null value for a120571.toString()
559status = inSection(13);
560try
561{
562  actual = a120571.toString().match(/continue/)[0];
563}
564catch(e)
565{
566  actual = 'FAILED! Did not find "continue" in function body';
567}
568expect = 'continue';
569captureThis();
570
571
572
573
574function 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.
589print(b);
590
591// Now test that we have a non-null value for b.toString()
592status = inSection(14);
593try
594{
595  actual = b.toString().match(/continue/)[0];
596}
597catch(e)
598{
599  actual = 'FAILED! Did not find "continue" in function body';
600}
601expect = 'continue';
602captureThis();
603
604
605
606
607
608//-----------------------------------------------------------------------------
609test();
610//-----------------------------------------------------------------------------
611
612
613
614function captureThis()
615{
616  statusitems[UBound] = status;
617  actualvalues[UBound] = actual;
618  expectedvalues[UBound] = expect;
619  UBound++;
620}
621
622
623function 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}
636