changeset 177:050fd5696bcb

8011555: Invalid class name in with block with JavaImporter causes MH type mismatch Reviewed-by: jlaskey, lagergren
author attila
date Thu, 04 Apr 2013 18:32:00 +0200
parents 349360cc1df5
children 1c29dc809de2
files src/jdk/nashorn/internal/runtime/WithObject.java test/script/basic/JDK-8011555.js test/script/basic/JDK-8011555.js.EXPECTED
diffstat 3 files changed, 59 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/runtime/WithObject.java	Thu Apr 04 20:46:31 2013 +0530
+++ b/src/jdk/nashorn/internal/runtime/WithObject.java	Thu Apr 04 18:32:00 2013 +0200
@@ -29,6 +29,7 @@
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import jdk.internal.dynalink.CallSiteDescriptor;
 import jdk.internal.dynalink.linker.GuardedInvocation;
 import jdk.internal.dynalink.linker.LinkRequest;
@@ -42,9 +43,10 @@
  */
 public final class WithObject extends ScriptObject implements Scope {
 
-    private static final MethodHandle WITHEXPRESSIONFILTER = findOwnMH("withFilterExpression", Object.class, Object.class);
-    private static final MethodHandle WITHSCOPEFILTER      = findOwnMH("withFilterScope",      Object.class, Object.class);
-    private static final MethodHandle BIND_TO_EXPRESSION   = findOwnMH("bindToExpression",     Object.class, Object.class, Object.class);
+    private static final MethodHandle WITHEXPRESSIONFILTER   = findOwnMH("withFilterExpression", Object.class, Object.class);
+    private static final MethodHandle WITHSCOPEFILTER        = findOwnMH("withFilterScope",      Object.class, Object.class);
+    private static final MethodHandle BIND_TO_EXPRESSION_OBJ = findOwnMH("bindToExpression",     Object.class, Object.class, Object.class);
+    private static final MethodHandle BIND_TO_EXPRESSION_FN  = findOwnMH("bindToExpression",     Object.class, ScriptFunction.class, Object.class);
 
     /** With expression object. */
     private final Object expression;
@@ -237,9 +239,14 @@
             return link.filterArguments(0, WITHEXPRESSIONFILTER);
         }
 
+        final MethodHandle linkInvocation = link.getInvocation();
+        final MethodType linkType = linkInvocation.type();
+        final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
         return link.replaceMethods(
                 // Make sure getMethod will bind the script functions it receives to WithObject.expression
-                MH.foldArguments(BIND_TO_EXPRESSION, filter(link.getInvocation(), WITHEXPRESSIONFILTER)),
+                MH.foldArguments(linkReturnsFunction ? BIND_TO_EXPRESSION_FN : BIND_TO_EXPRESSION_OBJ,
+                        filter(linkInvocation.asType(linkType.changeReturnType(
+                                linkReturnsFunction ? ScriptFunction.class : Object.class)), WITHEXPRESSIONFILTER)),
                 // No clever things for the guard -- it is still identically filtered.
                 filterGuard(link, WITHEXPRESSIONFILTER));
     }
@@ -269,7 +276,11 @@
 
     @SuppressWarnings("unused")
     private static Object bindToExpression(final Object fn, final Object receiver) {
-        return fn instanceof ScriptFunction ? ((ScriptFunction) fn).makeBoundFunction(withFilterExpression(receiver), new Object[0]) : fn;
+        return fn instanceof ScriptFunction ? bindToExpression((ScriptFunction) fn, receiver) : fn;
+    }
+
+    private static Object bindToExpression(final ScriptFunction fn, final Object receiver) {
+        return fn.makeBoundFunction(withFilterExpression(receiver), new Object[0]);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8011555.js	Thu Apr 04 18:32:00 2013 +0200
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+/**
+ * JDK-8011555: Invalid class name in with block with JavaImporter causes MH type mismatch
+ *
+ * @test
+ * @run
+ */
+
+with(new JavaImporter()) {
+    try {
+        new X()
+        print("Expected to fail!")
+    } catch(e) {
+        // We expect to get a TypeError for trying to use __noSuchMethod__ as
+        // a constructor. Before we fixed this bug, we were getting a runtime
+        // exception with MH type mismatch on a MH.foldArguments within the 
+        // WithObject code instead.
+        print(e)
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8011555.js.EXPECTED	Thu Apr 04 18:32:00 2013 +0200
@@ -0,0 +1,1 @@
+TypeError: function __noSuchMethod__() { [native code] } is not a constructor function