changeset 41143:7a3195db5d86

8166298: 3 nashorn ant tests fail with latest jdk9-dev tip Reviewed-by: hannesw, mhaupt
author sundar
date Tue, 20 Sep 2016 21:53:00 +0530
parents a48be9ca03ba
children 44c67c27002d
files nashorn/test/script/trusted/JDK-8006529.js nashorn/test/script/trusted/event_queue.js nashorn/test/script/trusted/optimistic_recompilation.js nashorn/test/src/jdk/nashorn/test/models/Reflector.java
diffstat 4 files changed, 129 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/nashorn/test/script/trusted/JDK-8006529.js	Tue Sep 20 11:33:03 2016 +0200
+++ b/nashorn/test/script/trusted/JDK-8006529.js	Tue Sep 20 21:53:00 2016 +0530
@@ -38,7 +38,7 @@
  * We cannot use direct Java class (via dynalink bean linker) to Compiler
  * and FunctionNode because of package-access check and so reflective calls.
  */
-
+var Reflector           = Java.type("jdk.nashorn.test.models.Reflector");
 var forName             = java.lang.Class["forName(String)"];
 var Parser              = forName("jdk.nashorn.internal.parser.Parser").static
 var Compiler            = forName("jdk.nashorn.internal.codegen.Compiler").static
@@ -69,7 +69,11 @@
 var lhsMethod = BinaryNode.class.getMethod("lhs")
 var binaryRhsMethod = BinaryNode.class.getMethod("rhs")
 var debugIdMethod = Debug.class.getMethod("id", java.lang.Object.class)
-var compilePhases = CompilationPhases.class.getField("COMPILE_UPTO_BYTECODE").get(null);
+var compilePhases = Reflector.get(CompilationPhases.class.getField("COMPILE_UPTO_BYTECODE"), null);
+
+function invoke(m, obj) {
+    return Reflector.invoke(m, obj);
+}
 
 // These are method names of methods in FunctionNode class
 var allAssertionList = ['isVarArg', 'needsParentScope', 'needsCallee', 'hasScopeBlock', 'usesSelfSymbol', 'isSplit', 'hasEval', 'allVarsInScope', 'isStrict']
@@ -86,7 +90,7 @@
 
 // returns functionNode.getBody().getStatements().get(0)
 function getFirstFunction(functionNode) {
-    var f = findFunction(getBodyMethod.invoke(functionNode))
+    var f = findFunction(invoke(getBodyMethod, functionNode))
     if (f == null) {
         throw new Error();
     }
@@ -95,7 +99,7 @@
 
 function findFunction(node) {
     if(node instanceof Block) {
-        var stmts = getStatementsMethod.invoke(node)
+        var stmts = invoke(getStatementsMethod, node)
         for(var i = 0; i < stmts.size(); ++i) {
             var retval = findFunction(stmts.get(i))
             if(retval != null) {
@@ -103,13 +107,13 @@
             }
         }
     } else if(node instanceof VarNode) {
-        return findFunction(getInitMethod.invoke(node))
+        return findFunction(invoke(getInitMethod, node))
     } else if(node instanceof UnaryNode) {
-        return findFunction(rhsMethod.invoke(node))
+        return findFunction(invoke(rhsMethod, node))
     } else if(node instanceof BinaryNode) {
-        return findFunction(lhsMethod.invoke(node)) || findFunction(binaryRhsMethod.invoke(node))
+        return findFunction(invoke(lhsMethod, node)) || findFunction(invoke(binaryRhsMethod, node))
     } else if(node instanceof ExpressionStatement) {
-        return findFunction(getExpressionMethod.invoke(node))
+        return findFunction(invoke(getExpressionMethod, node))
     } else if(node instanceof FunctionNode) {
         return node
     }
@@ -131,12 +135,12 @@
     var ctxt = getContextMethod.invoke(null);
     var env = getEnvMethod.invoke(ctxt);
 
-    var parser   = ParserConstructor.newInstance(env, source, ThrowErrorManager.class.newInstance());
-    var func     = parseMethod.invoke(parser);
+    var parser   = Reflector.newInstance(ParserConstructor, env, source, ThrowErrorManager.class.newInstance());
+    var func     = invoke(parseMethod, parser);
 
-    var compiler = CompilerConstructor.invoke(null, ctxt, source, false);
+    var compiler = Reflector.invoke(CompilerConstructor, null, ctxt, source, false);
 
-    return compileMethod.invoke(compiler, func, phases);
+    return Reflector.invoke(compileMethod, compiler, func, phases);
 };
 
 var allAssertions = (function() {
@@ -161,9 +165,10 @@
     }
     for(var assertion in allAssertions) {
         var expectedValue = !!assertions[assertion]
-        var actualValue = functionNodeMethods[assertion].invoke(f)
+        var actualValue = invoke(functionNodeMethods[assertion], f)
         if(actualValue !== expectedValue) {
-            throw "Expected " + assertion + " === " + expectedValue + ", got " + actualValue + " for " + f + ":" + debugIdMethod.invoke(null, f);
+            throw "Expected " + assertion + " === " + expectedValue + ", got " + actualValue + " for " + f + ":" 
+                + invoke(debugIdMethod, null, f);
         }
     }
 }
--- a/nashorn/test/script/trusted/event_queue.js	Tue Sep 20 11:33:03 2016 +0200
+++ b/nashorn/test/script/trusted/event_queue.js	Tue Sep 20 21:53:00 2016 +0530
@@ -36,6 +36,7 @@
 print(Debug);
 print();
 
+var Reflector     = Java.type("jdk.nashorn.test.models.Reflector");
 var forName       = java.lang.Class["forName(String)"];
 var RuntimeEvent  = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;
 var getValue      = RuntimeEvent.class.getMethod("getValue");
@@ -84,19 +85,19 @@
     var e = events[i];
     print("event #" + i);
     print("\tevent class=" + e.getClass());
-    print("\tvalueClass in event=" + getValueClass.invoke(e));
-    var v = getValue.invoke(e);
+    print("\tvalueClass in event=" + Reflector.invoke(getValueClass, e));
+    var v = Reflector.invoke(getValue, e);
     print("\tclass of value=" + v.getClass());
-    print("\treturn type=" + getReturnType.invoke(v));
+    print("\treturn type=" + Reflector.invoke(getReturnType, v));
     lastInLoop = events[i];
 }
 
 print();
 print("in loop last class = " + lastInLoop.getClass());
-print("in loop last value class = " + getValueClass.invoke(lastInLoop));
-var rexInLoop = getValue.invoke(lastInLoop);
+print("in loop last value class = " + Reflector.invoke(getValueClass, lastInLoop));
+var rexInLoop = Reflector.invoke(getValue, lastInLoop);
 print("in loop rex class = " + rexInLoop.getClass());
-print("in loop rex return type = " + getReturnType.invoke(rexInLoop));
+print("in loop rex return type = " + Reflector.invoke(getReturnType, rexInLoop));
 
 //try last runtime events
 var last = Debug.getLastRuntimeEvent();
@@ -106,10 +107,10 @@
 print();
 
 print("last class = " + last.getClass());
-print("last value class = " + getValueClass.invoke(last));
-var rex = getValue.invoke(last);
+print("last value class = " + Reflector.invoke(getValueClass, last));
+var rex = Reflector.invoke(getValue, last);
 print("rex class = " + rex.getClass());
-print("rex return type = " + getReturnType.invoke(rex));
+print("rex return type = " + Reflector.invoke(getReturnType, rex));
 
 //try the capacity setter
 print();
--- a/nashorn/test/script/trusted/optimistic_recompilation.js	Tue Sep 20 11:33:03 2016 +0200
+++ b/nashorn/test/script/trusted/optimistic_recompilation.js	Tue Sep 20 21:53:00 2016 +0530
@@ -32,6 +32,7 @@
  * @run
  */
 
+var Reflector     = Java.type("jdk.nashorn.test.models.Reflector");
 var forName       = java.lang.Class["forName(String)"];
 var RuntimeEvent  = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;
 var getValue      = RuntimeEvent.class.getMethod("getValue");
@@ -42,6 +43,10 @@
 var setReturnTypeAndValue = [];
 var expectedValues = [];
 
+function invoke(m, obj) {
+    return Reflector.invoke(m, obj);
+}
+
 function checkExpectedRecompilation(f, expectedValues, testCase) {
     Debug.clearRuntimeEvents();
     print(f());
@@ -51,12 +56,12 @@
     if (events.length ==  expectedValues.length) {
         for (var i in events) {
             var e = events[i];
-            var returnValue = getReturnValue.invoke(e);
+            var returnValue = invoke(getReturnValue, e);
             if (typeof returnValue != 'undefined') {
-            setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];
+                setReturnTypeAndValue[i] = [invoke(getReturnType, invoke(getValue, e)), returnValue];
             } else {
                 returnValue = "undefined";
-                setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];
+                setReturnTypeAndValue[i] = [invoke(getReturnType, invoke(getValue, e)), returnValue];
             }
             if (!setReturnTypeAndValue[i].toString().equals(expectedValues[i].toString())) {
                 fail("The return values are not as expected. Expected value: " + expectedValues[i] + " and got: " + setReturnTypeAndValue[i] + " in test case: " + f);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/src/jdk/nashorn/test/models/Reflector.java	Tue Sep 20 21:53:00 2016 +0530
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.nashorn.test.models;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Module;
+import jdk.nashorn.internal.runtime.Context;
+
+/**
+ * Few tests reflectively invoke or read fields of Nashorn classes
+ * and objects - but of packages that are not exported to any module!
+ * But, those packages are qualified exported to test [java] code
+ * such as this class. So, test scripts can invoke the methods of this
+ * class instead.
+ */
+public final class Reflector {
+    private Reflector() {}
+    private static final Module NASHORN_MOD = Context.class.getModule();
+
+    public static Object invoke(Method m, Object self, Object...args) {
+        if (m.getDeclaringClass().getModule() != NASHORN_MOD) {
+            throw new RuntimeException(m + " is not from Nashorn module");
+        }
+
+        try {
+            return m.invoke(self, args);
+        } catch (final Exception e) {
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException)e;
+            } else {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    public static Object newInstance(Constructor c, Object...args) {
+        if (c.getDeclaringClass().getModule() != NASHORN_MOD) {
+            throw new RuntimeException(c + " is not from Nashorn module");
+        }
+
+        try {
+            return c.newInstance(args);
+        } catch (final Exception e) {
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException)e;
+            } else {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    public static Object get(Field f, Object self) {
+        if (f.getDeclaringClass().getModule() != NASHORN_MOD) {
+            throw new RuntimeException(f + " is not from Nashorn module");
+        }
+
+        try {
+            return f.get(self);
+        } catch (final Exception e) {
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException)e;
+            } else {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+}