NASHORN-19.js revision 877:cf4d2252d444
159024Sobrien/* 2218822Sdim * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 459024Sobrien * 559024Sobrien * This code is free software; you can redistribute it and/or modify it 6218822Sdim * under the terms of the GNU General Public License version 2 only, as 759024Sobrien * published by the Free Software Foundation. 8218822Sdim * 9218822Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10218822Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11218822Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1259024Sobrien * version 2 for more details (a copy is included in the LICENSE file that 13218822Sdim * accompanied this code). 14218822Sdim * 15218822Sdim * You should have received a copy of the GNU General Public License version 16218822Sdim * 2 along with this work; if not, write to the Free Software Foundation, 1759024Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18218822Sdim * 19218822Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20218822Sdim * or visit www.oracle.com if you need additional information or have any 2159024Sobrien * questions. 2259024Sobrien */ 2359024Sobrien 2489857Sobrien/** 2589857Sobrien * NASHORN-19: with blocks in various scopes and breaking from them if they are inloops 2689857Sobrien * (also continues) 2789857Sobrien * 2889857Sobrien * @test 2959024Sobrien * @run 3059024Sobrien */ 3159024Sobrien 3259024Sobrien 3359024Sobrienvar myvalue = "hello"; 3459024Sobrien 3559024Sobrienvar myscope = { 3659024Sobrien myvalue: 11 3759024Sobrien}; 3884865Sobrien 3984865Sobriendo { 4059024Sobrien with(myscope) { 4159024Sobrien myvalue = 12; 4259024Sobrien break; 43218822Sdim } 44218822Sdim} while (false); 45218822Sdim 46218822Sdimif (myvalue != 'hello') { 47218822Sdim throw "expecting to be hello"; 48218822Sdim} else { 49218822Sdim print("value is 'hello' as expected"); 50218822Sdim} 51218822Sdim 52218822Sdimprint("\n"); 53218822Sdim 54218822Sdimfunction ten() { 55218822Sdim return 0xa; 56218822Sdim} 57218822Sdim 58218822Sdim//make sure the scope works outside functions too 59218822Sdimprint("starting 0"); 60218822Sdimvar value = "hello"; 61218822Sdimvar scope = {value:10}; 62218822Sdimvar scope2 = {value:20}; 63218822Sdimwhile (true) { 64218822Sdim with (scope) { 65218822Sdim print(value); 66218822Sdim value = 11; 67218822Sdim print(value); 6859024Sobrien with (scope2) { 6959024Sobrien print(value); 7059024Sobrien value = 21; 7184865Sobrien print(value); 7284865Sobrien break; 7384865Sobrien } 7459024Sobrien } 7559024Sobrien} 7659024Sobrien 7759024Sobrienprint(value); 7859024Sobrienprint("\n"); 7959024Sobrien 80130561Sobrien//two level scope 8159024Sobrienfunction test1() { 8259024Sobrien var value = "hello"; 8359024Sobrien var scope = {value:10}; 8459024Sobrien var scope2 = {value:20}; 8559024Sobrien while (true) { 8659024Sobrien with (scope) { 8759024Sobrien print(value); 8859024Sobrien value = 11; 8959024Sobrien print(value); 9059024Sobrien with (scope2) { 9159024Sobrien print(value); 9259024Sobrien value = 21; 9359024Sobrien print(value); 9459024Sobrien break; 95218822Sdim } 9659024Sobrien } 9759024Sobrien } 9859024Sobrien 9959024Sobrien print(value); 10059024Sobrien} 10159024Sobrien 10259024Sobrien//one level scope 10359024Sobrienfunction test2() { 10459024Sobrien var value = "hello"; 10559024Sobrien var scope = {value:10}; 10659024Sobrien while (true) { 10759024Sobrien with (scope) { 10859024Sobrien print(value); 109130561Sobrien value = 11; 11059024Sobrien print(value); 111130561Sobrien if (value > ten()) { 112218822Sdim break; 11359024Sobrien } 11459024Sobrien } 11559024Sobrien } 11659024Sobrien print(value); 11759024Sobrien} 11859024Sobrien 11959024Sobrien//continue two levels 12089857Sobrienfunction test3() { 12159024Sobrien var value = "hello"; 122218822Sdim var scope = {value:10}; 123130561Sobrien var scope2 = {value:20}; 12459024Sobrien var outer = 0; 12559024Sobrien while (outer < 5) { 12659024Sobrien var i=0; 12759024Sobrien while (i < 10) { 128130561Sobrien with(scope) { 12959024Sobrien print("loop header "+i); 13059024Sobrien with (scope2) { 13189857Sobrien value = 11; 132218822Sdim i++; 13359024Sobrien if ((i & 1) != 0) { 134130561Sobrien print("continue"); 135130561Sobrien continue; 13659024Sobrien } 13759024Sobrien } 13859024Sobrien } 13959024Sobrien print(value); 14059024Sobrien } 141218822Sdim outer++; 14259024Sobrien } 14359024Sobrien} 14459024Sobrien 14559024Sobrien//continue one level 14659024Sobrienfunction test4() { 14759024Sobrien var value = "hello"; 14859024Sobrien var scope = {value:10}; 14959024Sobrien var i=0; 15059024Sobrien while (i < 10) { 151130561Sobrien print("loop header "+i); 152218822Sdim with (scope) { 15389857Sobrien value = 11; 15489857Sobrien i++; 155130561Sobrien if ((i & 1) != 0) { 15659024Sobrien print("continue"); 15759024Sobrien continue; 15859024Sobrien } 159130561Sobrien } 16059024Sobrien } 16159024Sobrien print(value); 16259024Sobrien} 16359024Sobrien 16459024Sobrien 16559024Sobrien//labelled continue; 16659024Sobrienfunction test5() { 16759024Sobrien var value = "hello"; 16859024Sobrien var scope = {value:10}; 16959024Sobrien var scope2 = {value:20}; 170218822Sdim var outer = 0; 171130561Sobrien outer_label: 17259024Sobrien while (outer < 5) { 17359024Sobrien var i=0; 17459024Sobrien while (i < 10) { 175218822Sdim with(scope) { 17689857Sobrien print("loop header "+i); 17759024Sobrien with (scope2) { 178130561Sobrien value = 11; 17959024Sobrien i++; 18059024Sobrien if ((i & 1) != 0) { 18159024Sobrien print("continue"); 18259024Sobrien outer++; 18359024Sobrien continue outer_label; 18459024Sobrien } 18559024Sobrien } 18659024Sobrien } 18759024Sobrien print(value); 18859024Sobrien } 18959024Sobrien } 19059024Sobrien} 19159024Sobrien 19259024Sobrien//labelled break 19359024Sobrienfunction test6() { 19459024Sobrien var value = "hello"; 19559024Sobrien var scope = {value:10}; 19659024Sobrien var scope2 = {value:20}; 19759024Sobrien outer: 19859024Sobrien { 19959024Sobrien var i=0; 20059024Sobrien while (i < 10) { 20159024Sobrien with(scope) { 20259024Sobrien print("loop header "+i); 20359024Sobrien with (scope2) { 20459024Sobrien value = 11; 20559024Sobrien i++; 20659024Sobrien if ((i & 1) != 0) { 20759024Sobrien print("break"); 20859024Sobrien break outer; 20959024Sobrien } 21059024Sobrien } 21159024Sobrien } 21259024Sobrien print(value); 21359024Sobrien } 21459024Sobrien } 21559024Sobrien} 21659024Sobrien 21759024Sobrien//exceptions in one scope and then the other 21859024Sobrienfunction test7() { 21959024Sobrien var value = "hello"; 22059024Sobrien var scope = {value:10}; 22159024Sobrien var scope2 = {value:20}; 22259024Sobrien var global = false; 22359024Sobrien try { 22459024Sobrien with(scope) { 225130561Sobrien try { 22659024Sobrien print(value); 227130561Sobrien value = 4711; 22859024Sobrien print(value); 22959024Sobrien with(scope2) { 23059024Sobrien print(value); 23159024Sobrien value = 17; 23259024Sobrien print(value); 233218822Sdim global = true; 23459024Sobrien throw "inner"; 23559024Sobrien } 23659024Sobrien } catch (ei) { 23759024Sobrien print(ei); 23859024Sobrien print(value); 23959024Sobrien if (global) { 24059024Sobrien throw "outer"; 24159024Sobrien } 24259024Sobrien } 24359024Sobrien } 24459024Sobrien } catch (eo) { 24559024Sobrien print(eo); 24659024Sobrien print(value); 24759024Sobrien } 24859024Sobrien print(value); 24959024Sobrien} 25059024Sobrien 25159024Sobrien 252130561Sobrienprint("starting 1"); 253218822Sdimtest1(); 25459024Sobrienprint("\n"); 25559024Sobrien 25689857Sobrienprint("starting 2"); 25759024Sobrientest2(); 25859024Sobrienprint("\n"); 25959024Sobrien 26059024Sobrienprint("starting 3"); 26159024Sobrientest3(); 262130561Sobrienprint("\n"); 26359024Sobrien 26459024Sobrienprint("starting 4"); 26559024Sobrientest4(); 26659024Sobrienprint("\n"); 26759024Sobrien 26859024Sobrienprint("starting 5"); 269130561Sobrientest5(); 27059024Sobrienprint("\n"); 27159024Sobrien 27259024Sobrienprint("starting 6"); 273218822Sdimtest6(); 27459024Sobrienprint("\n"); 27589857Sobrien 276218822Sdimprint("starting 7"); 27759024Sobrientest7(); 278130561Sobrienprint("\n"); 27989857Sobrien 280218822Sdim