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: var BUGNUMBER = 349326; michael@0: var summary = 'closing generators'; michael@0: var actual = 'PASS'; michael@0: var expect = 'PASS'; michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); 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: let closed; michael@0: michael@0: function gen() michael@0: { michael@0: try { michael@0: yield 1; michael@0: yield 2; michael@0: } finally { michael@0: closed = true; michael@0: } michael@0: } michael@0: michael@0: // Test that return closes the generator michael@0: function test_return() michael@0: { michael@0: for (let i in gen()) { michael@0: if (i != 1) michael@0: throw "unexpected generator value"; michael@0: return 10; michael@0: } michael@0: } michael@0: michael@0: closed = false; michael@0: test_return(); michael@0: if (closed !== true) michael@0: throw "return does not close generator"; michael@0: michael@0: // test that break closes generator michael@0: michael@0: closed = false; michael@0: for (let i in gen()) { michael@0: if (i != 1) michael@0: throw "unexpected generator value"; michael@0: break; michael@0: } michael@0: if (closed !== true) michael@0: throw "break does not close generator"; michael@0: michael@0: label: { michael@0: for (;;) { michael@0: closed = false; michael@0: for (let i in gen()) { michael@0: if (i != 1) michael@0: throw "unexpected generator value"; michael@0: break label; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (closed !== true) michael@0: throw "break does not close generator"; michael@0: michael@0: // test that an exception closes generator michael@0: michael@0: function function_that_throws() michael@0: { michael@0: throw function_that_throws; michael@0: } michael@0: michael@0: try { michael@0: closed = false; michael@0: for (let i in gen()) { michael@0: if (i != 1) michael@0: throw "unexpected generator value"; michael@0: function_that_throws(); michael@0: } michael@0: } catch (e) { michael@0: if (e !== function_that_throws) michael@0: throw e; michael@0: } michael@0: michael@0: if (closed !== true) michael@0: throw "exception does not close generator"; michael@0: michael@0: // Check consistency of finally execution in presence of generators michael@0: michael@0: let gen2_was_closed = false; michael@0: let gen3_was_closed = false; michael@0: let finally_was_executed = false; michael@0: michael@0: function gen2() { michael@0: try { michael@0: yield 2; michael@0: } finally { michael@0: if (gen2_was_closed || !finally_was_executed || !gen3_was_closed) michael@0: throw "bad oder of gen2 finally execution" michael@0: gen2_was_closed = true; michael@0: throw gen2; michael@0: } michael@0: } michael@0: michael@0: function gen3() { michael@0: try { michael@0: yield 3; michael@0: } finally { michael@0: if (gen2_was_closed || finally_was_executed || gen3_was_closed) michael@0: throw "bad oder of gen3 finally execution" michael@0: gen3_was_closed = true; michael@0: } michael@0: } michael@0: michael@0: label2: { michael@0: try { michael@0: for (let i in gen2()) { michael@0: try { michael@0: for (let j in gen3()) { michael@0: break label2; michael@0: } michael@0: } finally { michael@0: if (gen2_was_closed || finally_was_executed || !gen3_was_closed) michael@0: throw "bad oder of try-finally execution"; michael@0: finally_was_executed = true; michael@0: } michael@0: } michael@0: throw "gen2 finally should throw"; michael@0: } catch (e) { michael@0: if (e != gen2) throw e; michael@0: } michael@0: } michael@0: michael@0: print("OK"); michael@0: michael@0: reportCompare(expect, actual, summary); michael@0: michael@0: exitFunc ('test'); michael@0: }