michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 10 October 2001 michael@0: * SUMMARY: Regression test for Bugzilla bug 104077 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=104077 michael@0: * "JS crash: with/finally/return" michael@0: * michael@0: * Also http://bugzilla.mozilla.org/show_bug.cgi?id=120571 michael@0: * "JS crash: try/catch/continue." michael@0: * michael@0: * SpiderMonkey crashed on this code - it shouldn't. michael@0: * michael@0: * NOTE: the finally-blocks below should execute even if their try-blocks michael@0: * have return or throw statements in them: michael@0: * michael@0: * ------- Additional Comment #76 From Mike Shaver 2001-12-07 01:21 ------- michael@0: * finally trumps return, and all other control-flow constructs that cause michael@0: * program execution to jump out of the try block: throw, break, etc. Once you michael@0: * enter a try block, you will execute the finally block after leaving the try, michael@0: * regardless of what happens to make you leave the try. michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 104077; michael@0: var summary = "Just testing that we don't crash on with/finally/return -"; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: michael@0: michael@0: function addValues(obj) michael@0: { michael@0: var sum; michael@0: with (obj) michael@0: { michael@0: try michael@0: { michael@0: sum = arg1 + arg2; michael@0: } michael@0: finally michael@0: { michael@0: return sum; michael@0: } michael@0: } michael@0: } michael@0: michael@0: status = inSection(1); michael@0: var obj = new Object(); michael@0: obj.arg1 = 1; michael@0: obj.arg2 = 2; michael@0: actual = addValues(obj); michael@0: expect = 3; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: function tryThis() michael@0: { michael@0: var sum = 4 ; michael@0: var i = 0; michael@0: michael@0: while (sum < 10) michael@0: { michael@0: try michael@0: { michael@0: sum += 1; michael@0: i += 1; michael@0: } michael@0: finally michael@0: { michael@0: print("In finally case of tryThis() function"); michael@0: } michael@0: } michael@0: return i; michael@0: } michael@0: michael@0: status = inSection(2); michael@0: actual = tryThis(); michael@0: expect = 6; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: function myTest(x) michael@0: { michael@0: var obj = new Object(); michael@0: var msg; michael@0: michael@0: with (obj) michael@0: { michael@0: msg = (x != null) ? "NO" : "YES"; michael@0: print("Is the provided argument to myTest() null? : " + msg); michael@0: michael@0: try michael@0: { michael@0: throw "ZZZ"; michael@0: } michael@0: catch(e) michael@0: { michael@0: print("Caught thrown exception = " + e); michael@0: } michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: status = inSection(3); michael@0: actual = myTest(null); michael@0: expect = 1; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: function addValues_2(obj) michael@0: { michael@0: var sum = 0; michael@0: with (obj) michael@0: { michael@0: try michael@0: { michael@0: sum = arg1 + arg2; michael@0: with (arg3) michael@0: { michael@0: while (sum < 10) michael@0: { michael@0: try michael@0: { michael@0: if (sum > 5) michael@0: return sum; michael@0: sum += 1; michael@0: } michael@0: catch(e) michael@0: { michael@0: print('Caught an exception in addValues_2() function: ' + e); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: finally michael@0: { michael@0: return sum; michael@0: } michael@0: } michael@0: } michael@0: michael@0: status = inSection(4); michael@0: obj = new Object(); michael@0: obj.arg1 = 1; michael@0: obj.arg2 = 2; michael@0: obj.arg3 = new Object(); michael@0: obj.arg3.a = 10; michael@0: obj.arg3.b = 20; michael@0: actual = addValues_2(obj); michael@0: expect = 6; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: status = inSection(5); michael@0: try michael@0: { michael@0: throw new A(); michael@0: } michael@0: catch(e) michael@0: { michael@0: } michael@0: finally michael@0: { michael@0: try michael@0: { michael@0: throw new A(); michael@0: } michael@0: catch(e) michael@0: { michael@0: } michael@0: finally michael@0: { michael@0: actual = 'a'; michael@0: } michael@0: actual = 'b'; michael@0: } michael@0: expect = 'b'; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: function testfunc(mode) michael@0: { michael@0: var obj = new Object(); michael@0: with (obj) michael@0: { michael@0: var num = 100; michael@0: var str = "abc" ; michael@0: michael@0: if (str == null) michael@0: { michael@0: try michael@0: { michael@0: throw "authentication.0"; michael@0: } michael@0: catch(e) michael@0: { michael@0: } michael@0: finally michael@0: { michael@0: } michael@0: michael@0: return num; michael@0: } michael@0: else michael@0: { michael@0: try michael@0: { michael@0: if (mode == 0) michael@0: throw "authentication.0"; michael@0: else michael@0: mytest(); michael@0: } michael@0: catch(e) michael@0: { michael@0: } michael@0: finally michael@0: { michael@0: } michael@0: michael@0: return num; michael@0: } michael@0: } michael@0: } michael@0: michael@0: status = inSection(6); michael@0: actual = testfunc(0); michael@0: expect = 100; michael@0: captureThis(); michael@0: michael@0: status = inSection(7); michael@0: actual = testfunc(); michael@0: expect = 100; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: function entry_menu() michael@0: { michael@0: var document = new Object(); michael@0: var dialog = new Object(); michael@0: var num = 100; michael@0: michael@0: with (document) michael@0: { michael@0: with (dialog) michael@0: { michael@0: try michael@0: { michael@0: while (true) michael@0: { michael@0: return num; michael@0: } michael@0: } michael@0: finally michael@0: { michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: status = inSection(8); michael@0: actual = entry_menu(); michael@0: expect = 100; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: function addValues_5(obj) michael@0: { michael@0: var sum = 0; michael@0: michael@0: with (obj) michael@0: { michael@0: try michael@0: { michael@0: sum = arg1 + arg2; michael@0: with (arg3) michael@0: { michael@0: while (sum < 10) michael@0: { michael@0: try michael@0: { michael@0: if (sum > 5) michael@0: return sum; michael@0: sum += 1; michael@0: } michael@0: catch (e) michael@0: { michael@0: sum += 1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: finally michael@0: { michael@0: try michael@0: { michael@0: sum += 1; michael@0: print("In finally block of addValues_5() function: sum = " + sum); michael@0: } michael@0: catch (e) michael@0: { michael@0: sum += 1; michael@0: print("In finally catch block of addValues_5() function: sum = " + sum + ", e = " + e); michael@0: } michael@0: finally michael@0: { michael@0: sum += 1; michael@0: print("In finally finally block of addValues_5() function: sum = " + sum); michael@0: return sum; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: status = inSection(11); michael@0: obj = new Object(); michael@0: obj.arg1 = 1; michael@0: obj.arg2 = 2; michael@0: obj.arg3 = new Object(); michael@0: obj.arg3.a = 10; michael@0: obj.arg3.b = 20; michael@0: actual = addValues_5(obj); michael@0: expect = 8; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: function testObj(obj) michael@0: { michael@0: var x = 42; michael@0: michael@0: try michael@0: { michael@0: with (obj) michael@0: { michael@0: if (obj.p) michael@0: throw obj.p; michael@0: x = obj.q; michael@0: } michael@0: } michael@0: finally michael@0: { michael@0: print("in finally block of testObj() function"); michael@0: return 999; michael@0: } michael@0: } michael@0: michael@0: status = inSection(12); michael@0: obj = {p:43}; michael@0: actual = testObj(obj); michael@0: expect = 999; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * Next two cases are from http://bugzilla.mozilla.org/show_bug.cgi?id=120571 michael@0: */ michael@0: function a120571() michael@0: { michael@0: while(0) michael@0: { michael@0: try michael@0: { michael@0: } michael@0: catch(e) michael@0: { michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // this caused a crash! Test to see that it doesn't now. michael@0: print(a120571); michael@0: michael@0: // Now test that we have a non-null value for a120571.toString() michael@0: status = inSection(13); michael@0: try michael@0: { michael@0: actual = a120571.toString().match(/continue/)[0]; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = 'FAILED! Did not find "continue" in function body'; michael@0: } michael@0: expect = 'continue'; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: function b() michael@0: { michael@0: for(;;) michael@0: { michael@0: try michael@0: { michael@0: } michael@0: catch(e) michael@0: { michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // this caused a crash!!! Test to see that it doesn't now. michael@0: print(b); michael@0: michael@0: // Now test that we have a non-null value for b.toString() michael@0: status = inSection(14); michael@0: try michael@0: { michael@0: actual = b.toString().match(/continue/)[0]; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = 'FAILED! Did not find "continue" in function body'; michael@0: } michael@0: expect = 'continue'; michael@0: captureThis(); michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function captureThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: for (var i=0; i