1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * NASHORN-19:  with blocks in various scopes and breaking from them if they are inloops
  26  * (also continues)
  27  *
  28  * @test
  29  * @run
  30  */
  31 
  32 
  33 var myvalue = "hello";
  34 
  35 var myscope = {
  36     myvalue: 11
  37 };
  38 
  39 do {
  40     with(myscope) {
  41     myvalue = 12;
  42     break;
  43     }
  44 } while (false);
  45 
  46 if (myvalue != 'hello') {
  47     throw "expecting to be hello";
  48 } else {
  49     print("value is 'hello' as expected");
  50 }
  51 
  52 print("\n");
  53 
  54 function ten() {
  55     return 0xa;
  56 }
  57 
  58 //make sure the scope works outside functions too
  59 print("starting 0");
  60 var value = "hello";
  61 var scope = {value:10};
  62 var scope2 = {value:20};
  63 while (true) {
  64     with (scope) {
  65     print(value);
  66     value = 11;
  67     print(value);
  68     with (scope2) {
  69         print(value);
  70         value = 21;
  71         print(value);
  72         break;
  73     }
  74     }
  75 }
  76 
  77 print(value);
  78 print("\n");
  79 
  80 //two level scope
  81 function test1() {
  82     var value = "hello";
  83     var scope = {value:10};
  84     var scope2 = {value:20};
  85     while (true) {
  86     with (scope) {
  87         print(value);
  88         value = 11;
  89         print(value);
  90         with (scope2) {
  91         print(value);
  92         value = 21;
  93         print(value);
  94         break;
  95         }
  96     }
  97     }
  98 
  99     print(value);
 100 }
 101 
 102 //one level scope
 103 function test2() {
 104     var value = "hello";
 105     var scope = {value:10};
 106     while (true) {
 107     with (scope) {
 108         print(value);
 109         value = 11;
 110         print(value);
 111         if (value > ten()) {
 112         break;
 113         }
 114     }
 115     }
 116     print(value);
 117 }
 118 
 119 //continue two levels
 120 function test3() {
 121     var value = "hello";
 122     var scope = {value:10};
 123     var scope2 = {value:20};
 124     var outer = 0;
 125     while (outer < 5) {
 126     var i=0;
 127     while (i < 10) {
 128         with(scope) {
 129         print("loop header "+i);
 130         with (scope2) {
 131             value = 11;
 132             i++;
 133             if ((i & 1) != 0) {
 134             print("continue");
 135             continue;
 136             }
 137         }
 138         }
 139         print(value);
 140     }
 141     outer++;
 142     }
 143 }
 144 
 145 //continue one level
 146 function test4() {
 147     var value = "hello";
 148     var scope = {value:10};
 149     var i=0;
 150     while (i < 10) {
 151     print("loop header "+i);
 152     with (scope) {
 153         value = 11;
 154         i++;
 155         if ((i & 1) != 0) {
 156         print("continue");
 157         continue;
 158         }
 159     }
 160     }
 161     print(value);
 162 }
 163 
 164 
 165 //labelled continue;
 166 function test5() {
 167     var value = "hello";
 168     var scope = {value:10};
 169     var scope2 = {value:20};
 170     var outer = 0;
 171     outer_label:
 172     while (outer < 5) {
 173     var i=0;
 174     while (i < 10) {
 175         with(scope) {
 176         print("loop header "+i);
 177         with (scope2) {
 178             value = 11;
 179             i++;
 180             if ((i & 1) != 0) {
 181             print("continue");
 182             outer++;
 183             continue outer_label;
 184             }
 185         }
 186         }
 187         print(value);
 188     }
 189     }
 190 }
 191 
 192 //labelled break
 193 function test6() {
 194     var value = "hello";
 195     var scope = {value:10};
 196     var scope2 = {value:20};
 197     outer:
 198     {
 199     var i=0;
 200     while (i < 10) {
 201         with(scope) {
 202         print("loop header "+i);
 203         with (scope2) {
 204             value = 11;
 205             i++;
 206             if ((i & 1) != 0) {
 207             print("break");
 208             break outer;
 209             }
 210         }
 211         }
 212         print(value);
 213     }
 214     }
 215 }
 216 
 217 //exceptions in one scope and then the other
 218 function test7() {
 219     var value = "hello";
 220     var scope = {value:10};
 221     var scope2 = {value:20};
 222     var global = false;
 223     try {
 224     with(scope) {
 225         try {
 226         print(value);
 227         value = 4711;
 228         print(value);
 229         with(scope2) {
 230             print(value);
 231             value = 17;
 232             print(value);
 233             global = true;
 234             throw "inner";
 235         }
 236         } catch (ei) {
 237         print(ei);
 238         print(value);
 239         if (global) {
 240             throw "outer";
 241         }
 242         }
 243     }
 244     } catch (eo) {
 245     print(eo);
 246     print(value);
 247     }
 248     print(value);
 249 }
 250 
 251 
 252 print("starting 1");
 253 test1();
 254 print("\n");
 255 
 256 print("starting 2");
 257 test2();
 258 print("\n");
 259 
 260 print("starting 3");
 261 test3();
 262 print("\n");
 263 
 264 print("starting 4");
 265 test4();
 266 print("\n");
 267 
 268 print("starting 5");
 269 test5();
 270 print("\n");
 271 
 272 print("starting 6");
 273 test6();
 274 print("\n");
 275 
 276 print("starting 7");
 277 test7();
 278 print("\n");
 279