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 
  32 function makeFuncAndCall(code) {
  33     Function(code)();
  34 }
  35 
  36 function 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 
  46 function 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 
  56 function evalExpectValue(code, value) {
  57     if (eval(code) != value) {
  58         fail("Expected " + value + " with eval of " + code);
  59     }
  60 }
  61 
  62 makeFuncAndCall("for(x.x in 0) {}");
  63 // bug JDK-8047357
  64 // makeFuncAndCall("switch((null >> x3)) { default: {var x; break ; }\nthrow x; }");
  65 makeFuncExpectError("switch(x) { case 8: break; case false: }", ReferenceError);
  66 makeFuncAndCall("try { return true; } finally { return false; } ");
  67 makeFuncAndCall("({ get 1e81(){} })");
  68 makeFuncAndCall("{var x, x3;try { return 0; } finally { return 3/0; } }");
  69 makeFuncExpectError("with(x ? 1e81 : (x2.constructor = 0.1)) {}", ReferenceError);
  70 makeFuncAndCall("while(x-=1) {var x=0; }");
  71 makeFuncAndCall("while((x-=false) && 0) { var x = this; }");
  72 makeFuncAndCall("/*infloop*/while(x4-=x) var x, x4 = x1;");
  73 makeFuncAndCall("/*infloop*/L:while(x+=null) { this;var x = /x/g ; }");
  74 makeFuncAndCall("while((x1|=0.1) && 0) { var x1 = -0, functional; }");
  75 makeFuncAndCall("with({}) return (eval(\"arguments\"));");
  76 
  77 evalExpectValue(<<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     })();
  89 CODE, "get");
  90 
  91 // bug JDK-8047359
  92 // evalExpectValue("s = ' '; for (var i=0;i<31;++i) s+=s; s.length", RangeError);
  93 
  94 evalExpectValue(<<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);
 103 CODE, 3)
 104 
 105 evalExpectValue(<<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();
 115 CODE, 4);
 116 
 117 //evalExpectValue(
 118 // "function f(){var a=1; with({get a(){return false}}) return a}; f()", false);
 119 
 120 evalExpectError("function public() {\"use strict\"}", SyntaxError);
 121 evalExpectError("function f(public) {\"use strict\"}", SyntaxError);
 122 evalExpectError("function f() { switch(x) {} } f()", ReferenceError);
 123 
 124 // bug JDK-8047364
 125 // makeFuncAndCall("L1:try { return } finally { break L1 }");
 126 
 127 evalExpectValue(<<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();
 136 CODE, 2);
 137 
 138 evalExpectValue(<<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()
 147 CODE, 1);
 148 
 149 evalExpectValue(<<CODE
 150     function f() {
 151         var obj = { get ":"() {} }
 152         var desc = Object.getOwnPropertyDescriptor(obj, ":")
 153         return desc.get.name
 154     }
 155 
 156     f()
 157 CODE, ":");
 158 
 159 evalExpectValue(<<CODE
 160     function f() {
 161         var obj = { set ":"(a) {} };
 162         var desc = Object.getOwnPropertyDescriptor(obj, ":");
 163         return desc.set;
 164     }
 165 
 166     f()
 167 CODE, "set \":\"(a) {}");
 168 
 169 // bug JDK-8047366
 170 evalExpectValue("(1000000000000000128).toString()", "1000000000000000100");
 171 evalExpectValue("(1000000000000000128).toFixed().toString()", "1000000000000000128");
 172 
 173 try {
 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 }
 184 evalExpectError("function f() { switch(x) {} } f()", ReferenceError);
 185 Array.prototype.splice.call(Java.type("java.util.HashMap"))
 186 Array.prototype.slice.call(Java.type("java.util.HashMap"))