changeset 101:4738de1bd57f

8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker Reviewed-by: lagergren, jlaskey, hannesw
author sundar
date Mon, 18 Feb 2013 20:41:12 +0530
parents f8221ce53c2e
children b228e3cdbfc3
files src/jdk/internal/dynalink/beans/BeansLinker.java src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java test/script/basic/javamethodcallerrors.js test/script/basic/jsobject.js
diffstat 4 files changed, 154 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/internal/dynalink/beans/BeansLinker.java	Mon Feb 18 16:00:15 2013 +0100
+++ b/src/jdk/internal/dynalink/beans/BeansLinker.java	Mon Feb 18 20:41:12 2013 +0530
@@ -159,6 +159,16 @@
         return linkers.get(clazz);
     }
 
+    /*
+     * Returns true if the object is a Dynalink Java dynamic method.
+     *
+     * @param obj the object we want to test for being a dynamic method
+     * @return true if it is a dynamic method, false otherwise.
+     */
+    public static boolean isDynamicMethod(final Object obj) {
+        return obj instanceof DynamicMethod;
+    }
+
     @Override
     public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
             throws Exception {
--- a/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java	Mon Feb 18 16:00:15 2013 +0100
+++ b/src/jdk/nashorn/internal/runtime/linker/NashornBottomLinker.java	Mon Feb 18 20:41:12 2013 +0530
@@ -31,6 +31,7 @@
 
 import java.lang.invoke.MethodHandle;
 import jdk.internal.dynalink.CallSiteDescriptor;
+import jdk.internal.dynalink.beans.BeansLinker;
 import jdk.internal.dynalink.linker.GuardedInvocation;
 import jdk.internal.dynalink.linker.GuardingDynamicLinker;
 import jdk.internal.dynalink.linker.LinkRequest;
@@ -78,14 +79,14 @@
         final String operator = desc.getFirstOperator();
         switch (operator) {
         case "new":
-            if(isJavaDynamicMethod(self)) {
+            if(BeansLinker.isDynamicMethod(self)) {
                 typeError("method.not.constructor", ScriptRuntime.safeToString(self));
             } else {
                 typeError("not.a.function", ScriptRuntime.safeToString(self));
             }
             break;
         case "call":
-            if(isJavaDynamicMethod(self)) {
+            if(BeansLinker.isDynamicMethod(self)) {
                 typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
             } else {
                 typeError("not.a.function", ScriptRuntime.safeToString(self));
@@ -113,16 +114,6 @@
         throw new AssertionError("unknown call type " + desc);
     }
 
-    /**
-     * Returns true if the object is a Dynalink dynamic method. Unfortunately, the dynamic method classes are package
-     * private in Dynalink, so this is the closest we can get to determining it.
-     * @param obj the object we want to test for being a dynamic method
-     * @return true if it is a dynamic method, false otherwise.
-     */
-    private static boolean isJavaDynamicMethod(Object obj) {
-        return obj.getClass().getName().endsWith("DynamicMethod");
-    }
-
     private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
         return Bootstrap.asType(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/javamethodcallerrors.js	Mon Feb 18 20:41:12 2013 +0530
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010, 2013, 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.
+ *
+ * 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.
+ */
+
+/**
+ * Expect TypeError on wrong Java method invocations.
+ *
+ * @test
+ * @run
+ */
+
+var Exit = java.lang.System.exit;
+
+// Try to invoke it as constructor
+try {
+    new Exit();
+    fail("Should have thrown TypeError");
+} catch (e) {
+    if (! (e instanceof TypeError)) {
+        fail("TypeError expected, got " + e);
+    }
+}
+
+// Try to invoke with wrong number of args
+
+try {
+    Exit(33, 44);
+    fail("Should have thrown TypeError");
+} catch (e) {
+    if (! (e instanceof TypeError)) {
+        fail("TypeError expected, got " + e);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/jsobject.js	Mon Feb 18 20:41:12 2013 +0530
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2010, 2013, 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.
+ *
+ * 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.
+ */
+
+/**
+ * JSObject tests
+ *
+ * @test
+ * @run
+ */
+
+var m = new javax.script.ScriptEngineManager();
+var e = m.getEngineByName("nashorn");
+
+e.eval("obj = { foo:'hello', 0: 'world', func: function(x) { return x.toUpperCase() } } ");
+var obj = e.get("obj");
+
+
+// try various getters
+if (obj.foo != 'hello') {
+    fail("obj.foo does have expected value");
+}
+
+function checkPropGetter(obj, prop, expected) {
+    if (obj[prop] != expected) {
+        fail(prop + " does not have value: " + expected);
+    }
+}
+
+checkPropGetter(obj, "foo", "hello");
+checkPropGetter(obj, 0, "world");
+checkPropGetter(obj, "0", "world");
+
+// try various setters
+
+obj.foo = "HELLO";
+if (obj.foo != "HELLO") {
+    fail("obj.foo set does not work as expected");
+}
+
+function checkPropSetter(obj, prop, newValue) {
+    obj[prop] = newValue;
+    checkPropGetter(obj, prop, newValue);
+}
+
+checkPropSetter(obj, "foo", "NASHORN");
+checkPropSetter(obj, 0, "ECMASCRIPT");
+checkPropSetter(obj, "0", "CHANGED");
+
+function callFunc(input, expected) {
+   if (obj.func(input) != expected) {
+       fail("obj.func(..) does not work as expected");
+   }
+}
+
+callFunc("nashorn", "NASHORN");
+callFunc("javascript", "JAVASCRIPT");
+callFunc("hello", "HELLO");
+
+var Func = obj.func;
+
+function callWithoutObject(input, expected) {
+   if (Func(input) != expected) {
+       fail("obj.func(..) does not work as expected");
+   }
+}
+
+callWithoutObject("nashorn", "NASHORN");
+callWithoutObject("javascript", "JAVASCRIPT");
+callWithoutObject("hello", "HELLO");