test/script/basic/NASHORN-19.js

changeset 0
b1a7da25b547
child 952
6d5471a497fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/script/basic/NASHORN-19.js	Wed Apr 27 01:36:41 2016 +0800
     1.3 @@ -0,0 +1,279 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + * 
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + * 
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + * 
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + * 
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * NASHORN-19:  with blocks in various scopes and breaking from them if they are inloops
    1.29 + * (also continues)
    1.30 + *
    1.31 + * @test
    1.32 + * @run
    1.33 + */
    1.34 +
    1.35 +
    1.36 +var myvalue = "hello";
    1.37 +
    1.38 +var myscope = {
    1.39 +    myvalue: 11
    1.40 +};
    1.41 +
    1.42 +do {
    1.43 +    with(myscope) {
    1.44 +	myvalue = 12;
    1.45 +	break;
    1.46 +    }
    1.47 +} while (false);
    1.48 +
    1.49 +if (myvalue != 'hello') {
    1.50 +    throw "expecting to be hello";
    1.51 +} else {
    1.52 +    print("value is 'hello' as expected");
    1.53 +}
    1.54 +
    1.55 +print("\n");
    1.56 +
    1.57 +function ten() {
    1.58 +    return 0xa;
    1.59 +}
    1.60 +
    1.61 +//make sure the scope works outside functions too
    1.62 +print("starting 0");
    1.63 +var value = "hello";
    1.64 +var scope = {value:10};
    1.65 +var scope2 = {value:20};
    1.66 +while (true) {
    1.67 +    with (scope) {
    1.68 +	print(value);
    1.69 +	value = 11;
    1.70 +	print(value);
    1.71 +	with (scope2) {
    1.72 +	    print(value);
    1.73 +	    value = 21;
    1.74 +	    print(value);
    1.75 +	    break;
    1.76 +	}
    1.77 +    }
    1.78 +}
    1.79 +
    1.80 +print(value);
    1.81 +print("\n");
    1.82 +
    1.83 +//two level scope
    1.84 +function test1() {
    1.85 +    var value = "hello";
    1.86 +    var scope = {value:10};
    1.87 +    var scope2 = {value:20};
    1.88 +    while (true) {
    1.89 +	with (scope) {
    1.90 +	    print(value);
    1.91 +	    value = 11;
    1.92 +	    print(value);
    1.93 +	    with (scope2) {
    1.94 +		print(value);
    1.95 +		value = 21;
    1.96 +		print(value);
    1.97 +		break;
    1.98 +	    }
    1.99 +	}
   1.100 +    }
   1.101 +    
   1.102 +    print(value);
   1.103 +}
   1.104 +
   1.105 +//one level scope
   1.106 +function test2() {
   1.107 +    var value = "hello";
   1.108 +    var scope = {value:10};
   1.109 +    while (true) {
   1.110 +	with (scope) {
   1.111 +	    print(value);
   1.112 +	    value = 11;
   1.113 +	    print(value);
   1.114 +	    if (value > ten()) {
   1.115 +		break;
   1.116 +	    }
   1.117 +	}
   1.118 +    }
   1.119 +    print(value);
   1.120 +}
   1.121 +
   1.122 +//continue two levels
   1.123 +function test3() {
   1.124 +    var value = "hello";
   1.125 +    var scope = {value:10};
   1.126 +    var scope2 = {value:20};
   1.127 +    var outer = 0;
   1.128 +    while (outer < 5) {
   1.129 +	var i=0;
   1.130 +	while (i < 10) {
   1.131 +	    with(scope) {
   1.132 +		print("loop header "+i);
   1.133 +		with (scope2) {
   1.134 +		    value = 11;
   1.135 +		    i++;
   1.136 +		    if ((i & 1) != 0) {
   1.137 +			print("continue");
   1.138 +			continue;
   1.139 +		    }
   1.140 +		}
   1.141 +	    }
   1.142 +	    print(value);
   1.143 +	}
   1.144 +	outer++;
   1.145 +    }
   1.146 +} 
   1.147 +
   1.148 +//continue one level
   1.149 +function test4() {
   1.150 +    var value = "hello";
   1.151 +    var scope = {value:10};
   1.152 +    var i=0;
   1.153 +    while (i < 10) {
   1.154 +	print("loop header "+i);
   1.155 +	with (scope) {
   1.156 +	    value = 11;
   1.157 +	    i++;
   1.158 +	    if ((i & 1) != 0) {
   1.159 +		print("continue");
   1.160 +		continue;
   1.161 +	    }
   1.162 +	}
   1.163 +    }
   1.164 +    print(value);
   1.165 +}
   1.166 +
   1.167 +
   1.168 +//labelled continue;
   1.169 +function test5() {
   1.170 +    var value = "hello";
   1.171 +    var scope = {value:10};
   1.172 +    var scope2 = {value:20};
   1.173 +    var outer = 0;
   1.174 +    outer_label:
   1.175 +    while (outer < 5) {
   1.176 +	var i=0;
   1.177 +	while (i < 10) {
   1.178 +	    with(scope) {
   1.179 +		print("loop header "+i);
   1.180 +		with (scope2) {
   1.181 +		    value = 11;
   1.182 +		    i++;
   1.183 +		    if ((i & 1) != 0) {
   1.184 +			print("continue");
   1.185 +			outer++;
   1.186 +			continue outer_label;
   1.187 +		    }
   1.188 +		}
   1.189 +	    }
   1.190 +	    print(value);
   1.191 +	}
   1.192 +    }
   1.193 +} 
   1.194 +
   1.195 +//labelled break
   1.196 +function test6() {
   1.197 +    var value = "hello";
   1.198 +    var scope = {value:10};
   1.199 +    var scope2 = {value:20};
   1.200 +    outer:
   1.201 +    {
   1.202 +	var i=0;
   1.203 +	while (i < 10) {
   1.204 +	    with(scope) {
   1.205 +		print("loop header "+i);
   1.206 +		with (scope2) {
   1.207 +		    value = 11;
   1.208 +		    i++;
   1.209 +		    if ((i & 1) != 0) {
   1.210 +			print("break");
   1.211 +			break outer;
   1.212 +		    }
   1.213 +		}
   1.214 +	    }
   1.215 +	    print(value);
   1.216 +	}
   1.217 +    }
   1.218 +}
   1.219 +
   1.220 +//exceptions in one scope and then the other
   1.221 +function test7() {
   1.222 +    var value = "hello";
   1.223 +    var scope = {value:10};
   1.224 +    var scope2 = {value:20};    
   1.225 +    var global = false;
   1.226 +    try {
   1.227 +	with(scope) {
   1.228 +	    try {
   1.229 +		print(value);
   1.230 +		value = 4711;
   1.231 +		print(value);
   1.232 +		with(scope2) {
   1.233 +		    print(value);
   1.234 +		    value = 17;
   1.235 +		    print(value);
   1.236 +		    global = true;
   1.237 +		    throw "inner";
   1.238 +		}
   1.239 +	    } catch (ei) {
   1.240 +		print(ei);
   1.241 +		print(value);
   1.242 +		if (global) {
   1.243 +		    throw "outer";
   1.244 +		}
   1.245 +	    }
   1.246 +	}
   1.247 +    } catch (eo) {
   1.248 +	print(eo);
   1.249 +	print(value);
   1.250 +    }
   1.251 +    print(value);
   1.252 +}
   1.253 +
   1.254 +
   1.255 +print("starting 1");
   1.256 +test1();
   1.257 +print("\n");
   1.258 +
   1.259 +print("starting 2");
   1.260 +test2();
   1.261 +print("\n");
   1.262 +
   1.263 +print("starting 3");
   1.264 +test3();
   1.265 +print("\n");
   1.266 +
   1.267 +print("starting 4");
   1.268 +test4();
   1.269 +print("\n");
   1.270 +
   1.271 +print("starting 5");
   1.272 +test5();
   1.273 +print("\n");
   1.274 +
   1.275 +print("starting 6");
   1.276 +test6();
   1.277 +print("\n");
   1.278 +
   1.279 +print("starting 7");
   1.280 +test7();
   1.281 +print("\n");
   1.282 +

mercurial