8055762: Nashorn misses linker for netscape.javascript.JSObject instances
Reviewed-by: lagergren, jlaskey
--- a/nashorn/make/build.xml Wed Aug 20 21:32:09 2014 +0530
+++ b/nashorn/make/build.xml Thu Aug 21 20:06:48 2014 +0530
@@ -34,6 +34,7 @@
<loadproperties srcFile="make/project.properties"/>
<path id="nashorn.ext.path">
<pathelement location="${dist.dir}"/>
+ <pathelement location="${java.ext.dirs}"/>
</path>
<property name="ext.class.path" value="-Djava.ext.dirs="${toString:nashorn.ext.path}""/>
<condition property="svn.executable" value="/usr/local/bin/svn" else="svn">
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Wed Aug 20 21:32:09 2014 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Thu Aug 21 20:06:48 2014 +0530
@@ -96,6 +96,7 @@
new BoundDynamicMethodLinker(),
new JavaSuperAdapterLinker(),
jsObjectLinker,
+ new BrowserJSObjectLinker(),
new ReflectionCheckLinker());
factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker());
factory.setSyncOnRelink(true);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/BrowserJSObjectLinker.java Thu Aug 21 20:06:48 2014 +0530
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2014, 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.internal.runtime.linker;
+
+import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.*;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import jdk.internal.dynalink.CallSiteDescriptor;
+import jdk.internal.dynalink.linker.GuardedInvocation;
+import jdk.internal.dynalink.linker.LinkRequest;
+import jdk.internal.dynalink.linker.LinkerServices;
+import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
+import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
+import jdk.nashorn.internal.lookup.MethodHandleFactory;
+import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
+import jdk.nashorn.internal.runtime.JSType;
+
+/**
+ * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects.
+ */
+final class BrowserJSObjectLinker implements TypeBasedGuardingDynamicLinker {
+ private static final ClassLoader myLoader = BrowserJSObjectLinker.class.getClassLoader();
+ private static final String JSOBJECT_CLASS = "netscape.javascript.JSObject";
+ // not final because this is lazily initialized
+ // when we hit a subclass for the first time.
+ private static volatile Class<?> jsObjectClass;
+
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return canLinkTypeStatic(type);
+ }
+
+ static boolean canLinkTypeStatic(final Class<?> type) {
+ if (jsObjectClass != null && jsObjectClass.isAssignableFrom(type)) {
+ return true;
+ }
+
+ // check if this class is a subclass of JSObject
+ Class<?> clazz = type;
+ while (clazz != null) {
+ if (clazz.getClassLoader() == myLoader &&
+ clazz.getName().equals(JSOBJECT_CLASS)) {
+ jsObjectClass = clazz;
+ return true;
+ }
+ clazz = clazz.getSuperclass();
+ }
+
+ return false;
+ }
+
+ private static void checkJSObjectClass() {
+ assert jsObjectClass != null : JSOBJECT_CLASS + " not found!";
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
+ final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
+ final Object self = requestWithoutContext.getReceiver();
+ final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
+ checkJSObjectClass();
+
+ if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
+ // We only support standard "dyn:*[:*]" operations
+ return null;
+ }
+
+ final GuardedInvocation inv;
+ if (jsObjectClass.isInstance(self)) {
+ inv = lookup(desc);
+ } else {
+ throw new AssertionError(); // Should never reach here.
+ }
+
+ return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
+ }
+
+ private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
+ final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
+ final int c = desc.getNameTokenCount();
+
+ switch (operator) {
+ case "getProp":
+ case "getElem":
+ case "getMethod":
+ return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
+ case "setProp":
+ case "setElem":
+ return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
+ default:
+ return null;
+ }
+ }
+
+ private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) {
+ final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
+ final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name);
+ return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
+ }
+
+ private static GuardedInvocation findGetIndexMethod() {
+ return new GuardedInvocation(JSOBJECTLINKER_GET, IS_JSOBJECT_GUARD);
+ }
+
+ private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) {
+ final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, desc.getNameToken(2));
+ return new GuardedInvocation(getter, IS_JSOBJECT_GUARD);
+ }
+
+ private static GuardedInvocation findSetIndexMethod() {
+ return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD);
+ }
+
+ @SuppressWarnings("unused")
+ private static boolean isJSObject(final Object self) {
+ return jsObjectClass.isInstance(self);
+ }
+
+ @SuppressWarnings("unused")
+ private static Object get(final Object jsobj, final Object key) throws Throwable {
+ if (key instanceof Integer) {
+ return JSOBJECT_GETSLOT.invokeExact(jsobj, (int)key);
+ } else if (key instanceof Number) {
+ final int index = getIndex((Number)key);
+ if (index > -1) {
+ return JSOBJECT_GETSLOT.invokeExact(jsobj, index);
+ }
+ } else if (key instanceof String) {
+ return JSOBJECT_GETMEMBER.invokeExact(jsobj, (String)key);
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unused")
+ private static void put(final Object jsobj, final Object key, final Object value) throws Throwable {
+ if (key instanceof Integer) {
+ JSOBJECT_SETSLOT.invokeExact(jsobj, (int)key, value);
+ } else if (key instanceof Number) {
+ JSOBJECT_SETSLOT.invokeExact(jsobj, getIndex((Number)key), value);
+ } else if (key instanceof String) {
+ JSOBJECT_SETMEMBER.invokeExact(jsobj, (String)key, value);
+ }
+ }
+
+ private static int getIndex(final Number n) {
+ final double value = n.doubleValue();
+ return JSType.isRepresentableAsInt(value) ? (int)value : -1;
+ }
+
+ private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
+ // method handles of the current class
+ private static final MethodHandle IS_JSOBJECT_GUARD = findOwnMH_S("isJSObject", boolean.class, Object.class);
+ private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, Object.class, Object.class);
+ private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class);
+
+ private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
+ return MH.findStatic(MethodHandles.lookup(), BrowserJSObjectLinker.class, name, MH.type(rtype, types));
+ }
+
+ // method handles of netscape.javascript.JSObject class
+ // These are in separate class as we lazily initialize these
+ // method handles when we hit a subclass of JSObject first time.
+ static class JSObjectHandles {
+ // method handles of JSObject class
+ static final MethodHandle JSOBJECT_GETMEMBER = findJSObjectMH_V("getMember", Object.class, String.class).asType(MH.type(Object.class, Object.class, String.class));
+ static final MethodHandle JSOBJECT_GETSLOT = findJSObjectMH_V("getSlot", Object.class, int.class).asType(MH.type(Object.class, Object.class, int.class));
+ static final MethodHandle JSOBJECT_SETMEMBER = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class).asType(MH.type(Void.TYPE, Object.class, String.class, Object.class));
+ static final MethodHandle JSOBJECT_SETSLOT = findJSObjectMH_V("setSlot", Void.TYPE, int.class, Object.class).asType(MH.type(Void.TYPE, Object.class, int.class, Object.class));
+
+ private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
+ checkJSObjectClass();
+ return MH.findVirtual(MethodHandles.publicLookup(), jsObjectClass, name, MH.type(rtype, types));
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8055762.js Thu Aug 21 20:06:48 2014 +0530
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2014, 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-8055762: Nashorn misses linker for netscape.javascript.JSObject instances
+ * @test
+ * @run
+ */
+
+// basic checks for special linkage for netscape.javascript.JSObject
+// instances. For this test, we just subclass that class rather than
+// involve actual browser script engine or javafx webkit objects.
+
+var JSObject = Java.type("netscape.javascript.JSObject");
+var obj = new (Java.extend(JSObject))() {
+ getMember: function(name) {
+ if (name == "func") {
+ return function(arg) {
+ print("func called with " + arg);
+ }
+ }
+ return name.toUpperCase();
+ },
+
+ getSlot: function(index) {
+ return index^2;
+ },
+
+ setMember: function(name, value) {
+ print(name + " set to " + value);
+ },
+
+ setSlot: function(index, value) {
+ print("[" + index + "] set to " + value);
+ }
+};
+
+print(obj["foo"]);
+print(obj[2]);
+obj.bar = 23;
+obj[3] = 23;
+obj.func("hello");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8055762.js.EXPECTED Thu Aug 21 20:06:48 2014 +0530
@@ -0,0 +1,5 @@
+FOO
+0
+bar set to 23
+[3] set to 23
+func called with hello