nashorn/test/script/basic/NASHORN-19.js
changeset 24778 2ff5d7041566
parent 16151 97c1e756ae1e
equal deleted inserted replaced
24777:ed1974940ff4 24778:2ff5d7041566
     1 /*
     1 /*
     2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  * 
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
     8  * 
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
     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
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    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
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    13  * accompanied this code).
    14  * 
    14  *
    15  * You should have received a copy of the GNU General Public License version
    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,
    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.
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  * 
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    36     myvalue: 11
    36     myvalue: 11
    37 };
    37 };
    38 
    38 
    39 do {
    39 do {
    40     with(myscope) {
    40     with(myscope) {
    41 	myvalue = 12;
    41     myvalue = 12;
    42 	break;
    42     break;
    43     }
    43     }
    44 } while (false);
    44 } while (false);
    45 
    45 
    46 if (myvalue != 'hello') {
    46 if (myvalue != 'hello') {
    47     throw "expecting to be hello";
    47     throw "expecting to be hello";
    60 var value = "hello";
    60 var value = "hello";
    61 var scope = {value:10};
    61 var scope = {value:10};
    62 var scope2 = {value:20};
    62 var scope2 = {value:20};
    63 while (true) {
    63 while (true) {
    64     with (scope) {
    64     with (scope) {
    65 	print(value);
    65     print(value);
    66 	value = 11;
    66     value = 11;
    67 	print(value);
    67     print(value);
    68 	with (scope2) {
    68     with (scope2) {
    69 	    print(value);
    69         print(value);
    70 	    value = 21;
    70         value = 21;
    71 	    print(value);
    71         print(value);
    72 	    break;
    72         break;
    73 	}
    73     }
    74     }
    74     }
    75 }
    75 }
    76 
    76 
    77 print(value);
    77 print(value);
    78 print("\n");
    78 print("\n");
    81 function test1() {
    81 function test1() {
    82     var value = "hello";
    82     var value = "hello";
    83     var scope = {value:10};
    83     var scope = {value:10};
    84     var scope2 = {value:20};
    84     var scope2 = {value:20};
    85     while (true) {
    85     while (true) {
    86 	with (scope) {
    86     with (scope) {
    87 	    print(value);
    87         print(value);
    88 	    value = 11;
    88         value = 11;
    89 	    print(value);
    89         print(value);
    90 	    with (scope2) {
    90         with (scope2) {
    91 		print(value);
    91         print(value);
    92 		value = 21;
    92         value = 21;
    93 		print(value);
    93         print(value);
    94 		break;
    94         break;
    95 	    }
    95         }
    96 	}
    96     }
    97     }
    97     }
    98     
    98 
    99     print(value);
    99     print(value);
   100 }
   100 }
   101 
   101 
   102 //one level scope
   102 //one level scope
   103 function test2() {
   103 function test2() {
   104     var value = "hello";
   104     var value = "hello";
   105     var scope = {value:10};
   105     var scope = {value:10};
   106     while (true) {
   106     while (true) {
   107 	with (scope) {
   107     with (scope) {
   108 	    print(value);
   108         print(value);
   109 	    value = 11;
   109         value = 11;
   110 	    print(value);
   110         print(value);
   111 	    if (value > ten()) {
   111         if (value > ten()) {
   112 		break;
   112         break;
   113 	    }
   113         }
   114 	}
   114     }
   115     }
   115     }
   116     print(value);
   116     print(value);
   117 }
   117 }
   118 
   118 
   119 //continue two levels
   119 //continue two levels
   121     var value = "hello";
   121     var value = "hello";
   122     var scope = {value:10};
   122     var scope = {value:10};
   123     var scope2 = {value:20};
   123     var scope2 = {value:20};
   124     var outer = 0;
   124     var outer = 0;
   125     while (outer < 5) {
   125     while (outer < 5) {
   126 	var i=0;
   126     var i=0;
   127 	while (i < 10) {
   127     while (i < 10) {
   128 	    with(scope) {
   128         with(scope) {
   129 		print("loop header "+i);
   129         print("loop header "+i);
   130 		with (scope2) {
   130         with (scope2) {
   131 		    value = 11;
   131             value = 11;
   132 		    i++;
   132             i++;
   133 		    if ((i & 1) != 0) {
   133             if ((i & 1) != 0) {
   134 			print("continue");
   134             print("continue");
   135 			continue;
   135             continue;
   136 		    }
   136             }
   137 		}
   137         }
   138 	    }
   138         }
   139 	    print(value);
   139         print(value);
   140 	}
   140     }
   141 	outer++;
   141     outer++;
   142     }
   142     }
   143 } 
   143 }
   144 
   144 
   145 //continue one level
   145 //continue one level
   146 function test4() {
   146 function test4() {
   147     var value = "hello";
   147     var value = "hello";
   148     var scope = {value:10};
   148     var scope = {value:10};
   149     var i=0;
   149     var i=0;
   150     while (i < 10) {
   150     while (i < 10) {
   151 	print("loop header "+i);
   151     print("loop header "+i);
   152 	with (scope) {
   152     with (scope) {
   153 	    value = 11;
   153         value = 11;
   154 	    i++;
   154         i++;
   155 	    if ((i & 1) != 0) {
   155         if ((i & 1) != 0) {
   156 		print("continue");
   156         print("continue");
   157 		continue;
   157         continue;
   158 	    }
   158         }
   159 	}
   159     }
   160     }
   160     }
   161     print(value);
   161     print(value);
   162 }
   162 }
   163 
   163 
   164 
   164 
   168     var scope = {value:10};
   168     var scope = {value:10};
   169     var scope2 = {value:20};
   169     var scope2 = {value:20};
   170     var outer = 0;
   170     var outer = 0;
   171     outer_label:
   171     outer_label:
   172     while (outer < 5) {
   172     while (outer < 5) {
   173 	var i=0;
   173     var i=0;
   174 	while (i < 10) {
   174     while (i < 10) {
   175 	    with(scope) {
   175         with(scope) {
   176 		print("loop header "+i);
   176         print("loop header "+i);
   177 		with (scope2) {
   177         with (scope2) {
   178 		    value = 11;
   178             value = 11;
   179 		    i++;
   179             i++;
   180 		    if ((i & 1) != 0) {
   180             if ((i & 1) != 0) {
   181 			print("continue");
   181             print("continue");
   182 			outer++;
   182             outer++;
   183 			continue outer_label;
   183             continue outer_label;
   184 		    }
   184             }
   185 		}
   185         }
   186 	    }
   186         }
   187 	    print(value);
   187         print(value);
   188 	}
   188     }
   189     }
   189     }
   190 } 
   190 }
   191 
   191 
   192 //labelled break
   192 //labelled break
   193 function test6() {
   193 function test6() {
   194     var value = "hello";
   194     var value = "hello";
   195     var scope = {value:10};
   195     var scope = {value:10};
   196     var scope2 = {value:20};
   196     var scope2 = {value:20};
   197     outer:
   197     outer:
   198     {
   198     {
   199 	var i=0;
   199     var i=0;
   200 	while (i < 10) {
   200     while (i < 10) {
   201 	    with(scope) {
   201         with(scope) {
   202 		print("loop header "+i);
   202         print("loop header "+i);
   203 		with (scope2) {
   203         with (scope2) {
   204 		    value = 11;
   204             value = 11;
   205 		    i++;
   205             i++;
   206 		    if ((i & 1) != 0) {
   206             if ((i & 1) != 0) {
   207 			print("break");
   207             print("break");
   208 			break outer;
   208             break outer;
   209 		    }
   209             }
   210 		}
   210         }
   211 	    }
   211         }
   212 	    print(value);
   212         print(value);
   213 	}
   213     }
   214     }
   214     }
   215 }
   215 }
   216 
   216 
   217 //exceptions in one scope and then the other
   217 //exceptions in one scope and then the other
   218 function test7() {
   218 function test7() {
   219     var value = "hello";
   219     var value = "hello";
   220     var scope = {value:10};
   220     var scope = {value:10};
   221     var scope2 = {value:20};    
   221     var scope2 = {value:20};
   222     var global = false;
   222     var global = false;
   223     try {
   223     try {
   224 	with(scope) {
   224     with(scope) {
   225 	    try {
   225         try {
   226 		print(value);
   226         print(value);
   227 		value = 4711;
   227         value = 4711;
   228 		print(value);
   228         print(value);
   229 		with(scope2) {
   229         with(scope2) {
   230 		    print(value);
   230             print(value);
   231 		    value = 17;
   231             value = 17;
   232 		    print(value);
   232             print(value);
   233 		    global = true;
   233             global = true;
   234 		    throw "inner";
   234             throw "inner";
   235 		}
   235         }
   236 	    } catch (ei) {
   236         } catch (ei) {
   237 		print(ei);
   237         print(ei);
   238 		print(value);
   238         print(value);
   239 		if (global) {
   239         if (global) {
   240 		    throw "outer";
   240             throw "outer";
   241 		}
   241         }
   242 	    }
   242         }
   243 	}
   243     }
   244     } catch (eo) {
   244     } catch (eo) {
   245 	print(eo);
   245     print(eo);
   246 	print(value);
   246     print(value);
   247     }
   247     }
   248     print(value);
   248     print(value);
   249 }
   249 }
   250 
   250 
   251 
   251