1/*
2 * Copyright (c) 2014, 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 * JDK-8047369: Add regression tests for passing test cases of JDK-8024971
26 *
27 * @test
28 * @run
29 * @option -scripting
30 */
31
32function makeFuncAndCall(code) {
33    Function(code)();
34}
35
36function makeFuncExpectError(code, ErrorType) {
37    try {
38        makeFuncAndCall(code);
39    } catch (e) {
40        if (! (e instanceof ErrorType)) {
41            fail(ErrorType.name + " expected, got " + e);
42        }
43    }
44}
45
46function evalExpectError(code, ErrorType) {
47    try {
48        eval(code)();
49    } catch (e) {
50        if (! (e instanceof ErrorType)) {
51            fail(ErrorType.name + " expected, got " + e);
52        }
53    }
54}
55
56function evalExpectValue(code, value) {
57    if (eval(code) != value) {
58        fail("Expected " + value + " with eval of " + code);
59    }
60}
61
62makeFuncAndCall("for(x.x in 0) {}");
63// bug JDK-8047357
64// makeFuncAndCall("switch((null >> x3)) { default: {var x; break ; }\nthrow x; }");
65makeFuncExpectError("switch(x) { case 8: break; case false: }", ReferenceError);
66makeFuncAndCall("try { return true; } finally { return false; } ");
67makeFuncAndCall("({ get 1e81(){} })");
68makeFuncAndCall("{var x, x3;try { return 0; } finally { return 3/0; } }");
69makeFuncExpectError("with(x ? 1e81 : (x2.constructor = 0.1)) {}", ReferenceError);
70makeFuncAndCall("while(x-=1) {var x=0; }");
71makeFuncAndCall("while((x-=false) && 0) { var x = this; }");
72makeFuncAndCall("/*infloop*/while(x4-=x) var x, x4 = x1;");
73makeFuncAndCall("/*infloop*/L:while(x+=null) { this;var x = /x/g ; }");
74makeFuncAndCall("while((x1|=0.1) && 0) { var x1 = -0, functional; }");
75makeFuncAndCall("with({}) return (eval(\"arguments\"));");
76
77evalExpectValue(<<CODE
78    var s = "(function() { return y })()";
79    (function() {
80        with({ y:1 })
81            eval(s)
82    })();
83    (function() {
84        with({
85            get y() { return "get"; }
86        })
87        return eval(s)
88    })();
89CODE, "get");
90
91// bug JDK-8047359
92// evalExpectValue("s = ' '; for (var i=0;i<31;++i) s+=s; s.length", RangeError);
93
94evalExpectValue(<<CODE
95    function f(o) {
96        var eval=0;
97        with({
98            get eval() { return o.eval }
99        })
100        return eval("1+2");
101    }
102    f(this);
103CODE, 3)
104
105evalExpectValue(<<CODE
106    function f() {
107        var a=1,e=2;
108        try {
109            throw 3
110        } catch(e) {
111            return + function g(){return eval('a+e')}()
112        }
113    }
114    f();
115CODE, 4);
116
117//evalExpectValue(
118// "function f(){var a=1; with({get a(){return false}}) return a}; f()", false);
119
120evalExpectError("function public() {\"use strict\"}", SyntaxError);
121evalExpectError("function f(public) {\"use strict\"}", SyntaxError);
122evalExpectError("function f() { switch(x) {} } f()", ReferenceError);
123
124// bug JDK-8047364
125// makeFuncAndCall("L1:try { return } finally { break L1 }");
126
127evalExpectValue(<<CODE
128    function f() {
129        function g() { return 0 }
130        function g() { return 1 }
131        function g$1() { return 2 }
132        return g$1()
133    }
134
135    f();
136CODE, 2);
137
138evalExpectValue(<<CODE
139    function f() {
140        function g() {return 0 }
141        var h = function g() { return 1 };
142        function g$1() { return 2 };
143        return h()
144    }
145
146    f()
147CODE, 1);
148
149evalExpectValue(<<CODE
150    function f() {
151        var obj = { get ":"() {} }
152        var desc = Object.getOwnPropertyDescriptor(obj, ":")
153        return desc.get.name
154    }
155
156    f()
157CODE, ":");
158
159evalExpectValue(<<CODE
160    function f() {
161        var obj = { set ":"(a) {} };
162        var desc = Object.getOwnPropertyDescriptor(obj, ":");
163        return desc.set;
164    }
165
166    f()
167CODE, "set \":\"(a) {}");
168
169// bug JDK-8047366
170evalExpectValue("(1000000000000000128).toString()", "1000000000000000100");
171evalExpectValue("(1000000000000000128).toFixed().toString()", "1000000000000000128");
172
173try {
174    Function("-", {
175        toString: function() {
176            throw "err"
177        }
178    })();
179} catch (e) {
180    if (e != "err") {
181        fail("Expected 'err' here, got " + e);
182    }
183}
184evalExpectError("function f() { switch(x) {} } f()", ReferenceError);
185Array.prototype.splice.call(Java.type("java.util.HashMap"))
186Array.prototype.slice.call(Java.type("java.util.HashMap"))
187