BrowserJSObjectLinker.java revision 1483:7cb19fa78763
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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.runtime.linker; 27 28import static jdk.nashorn.internal.runtime.JSType.isString; 29import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_CALL; 30import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_GETMEMBER; 31import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_GETSLOT; 32import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_SETMEMBER; 33import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_SETSLOT; 34 35import java.lang.invoke.MethodHandle; 36import java.lang.invoke.MethodHandles; 37import jdk.internal.dynalink.CallSiteDescriptor; 38import jdk.internal.dynalink.StandardOperation; 39import jdk.internal.dynalink.linker.GuardedInvocation; 40import jdk.internal.dynalink.linker.LinkRequest; 41import jdk.internal.dynalink.linker.LinkerServices; 42import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 43import jdk.nashorn.internal.lookup.MethodHandleFactory; 44import jdk.nashorn.internal.lookup.MethodHandleFunctionality; 45import jdk.nashorn.internal.runtime.JSType; 46 47/** 48 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects. 49 */ 50final class BrowserJSObjectLinker implements TypeBasedGuardingDynamicLinker { 51 private static ClassLoader extLoader; 52 static { 53 extLoader = BrowserJSObjectLinker.class.getClassLoader(); 54 // in case nashorn is loaded as bootstrap! 55 if (extLoader == null) { 56 extLoader = ClassLoader.getSystemClassLoader().getParent(); 57 } 58 } 59 60 private static final String JSOBJECT_CLASS = "netscape.javascript.JSObject"; 61 // not final because this is lazily initialized 62 // when we hit a subclass for the first time. 63 private static volatile Class<?> jsObjectClass; 64 private final NashornBeansLinker nashornBeansLinker; 65 66 BrowserJSObjectLinker(final NashornBeansLinker nashornBeansLinker) { 67 this.nashornBeansLinker = nashornBeansLinker; 68 } 69 70 @Override 71 public boolean canLinkType(final Class<?> type) { 72 return canLinkTypeStatic(type); 73 } 74 75 static boolean canLinkTypeStatic(final Class<?> type) { 76 if (jsObjectClass != null && jsObjectClass.isAssignableFrom(type)) { 77 return true; 78 } 79 80 // check if this class is a subclass of JSObject 81 Class<?> clazz = type; 82 while (clazz != null) { 83 if (clazz.getClassLoader() == extLoader && 84 clazz.getName().equals(JSOBJECT_CLASS)) { 85 jsObjectClass = clazz; 86 return true; 87 } 88 clazz = clazz.getSuperclass(); 89 } 90 91 return false; 92 } 93 94 private static void checkJSObjectClass() { 95 assert jsObjectClass != null : JSOBJECT_CLASS + " not found!"; 96 } 97 98 @Override 99 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { 100 final Object self = request.getReceiver(); 101 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 102 checkJSObjectClass(); 103 104 GuardedInvocation inv; 105 if (jsObjectClass.isInstance(self)) { 106 inv = lookup(desc, request, linkerServices); 107 inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard()); 108 } else { 109 throw new AssertionError(); // Should never reach here. 110 } 111 112 return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc); 113 } 114 115 private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception { 116 GuardedInvocation inv; 117 try { 118 inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices); 119 } catch (final Throwable th) { 120 inv = null; 121 } 122 123 final StandardOperation op = NashornCallSiteDescriptor.getFirstStandardOperation(desc); 124 if (op == null) { 125 return null; 126 } 127 final String name = NashornCallSiteDescriptor.getOperand(desc); 128 switch (op) { 129 case GET_PROPERTY: 130 case GET_ELEMENT: 131 case GET_METHOD: 132 return name != null ? findGetMethod(name, inv) : findGetIndexMethod(inv); 133 case SET_PROPERTY: 134 case SET_ELEMENT: 135 return name != null ? findSetMethod(name, inv) : findSetIndexMethod(); 136 case CALL: 137 return findCallMethod(desc); 138 default: 139 } 140 return null; 141 } 142 143 private static GuardedInvocation findGetMethod(final String name, final GuardedInvocation inv) { 144 if (inv != null) { 145 return inv; 146 } 147 final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name); 148 return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); 149 } 150 151 private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) { 152 final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation()); 153 return inv.replaceMethods(getter, inv.getGuard()); 154 } 155 156 private static GuardedInvocation findSetMethod(final String name, final GuardedInvocation inv) { 157 if (inv != null) { 158 return inv; 159 } 160 final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, name); 161 return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); 162 } 163 164 private static GuardedInvocation findSetIndexMethod() { 165 return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD); 166 } 167 168 private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) { 169 final MethodHandle call = MH.insertArguments(JSOBJECT_CALL, 1, "call"); 170 return new GuardedInvocation(MH.asCollector(call, Object[].class, desc.getMethodType().parameterCount() - 1), IS_JSOBJECT_GUARD); 171 } 172 173 @SuppressWarnings("unused") 174 private static boolean isJSObject(final Object self) { 175 return jsObjectClass.isInstance(self); 176 } 177 178 @SuppressWarnings("unused") 179 private static Object get(final MethodHandle fallback, final Object jsobj, final Object key) throws Throwable { 180 if (key instanceof Integer) { 181 return JSOBJECT_GETSLOT.invokeExact(jsobj, (int)key); 182 } else if (key instanceof Number) { 183 final int index = getIndex((Number)key); 184 if (index > -1) { 185 return JSOBJECT_GETSLOT.invokeExact(jsobj, index); 186 } 187 } else if (isString(key)) { 188 final String name = key.toString(); 189 if (name.indexOf('(') != -1) { 190 return fallback.invokeExact(jsobj, (Object) name); 191 } 192 return JSOBJECT_GETMEMBER.invokeExact(jsobj, name); 193 } 194 return null; 195 } 196 197 @SuppressWarnings("unused") 198 private static void put(final Object jsobj, final Object key, final Object value) throws Throwable { 199 if (key instanceof Integer) { 200 JSOBJECT_SETSLOT.invokeExact(jsobj, (int)key, value); 201 } else if (key instanceof Number) { 202 JSOBJECT_SETSLOT.invokeExact(jsobj, getIndex((Number)key), value); 203 } else if (isString(key)) { 204 JSOBJECT_SETMEMBER.invokeExact(jsobj, key.toString(), value); 205 } 206 } 207 208 private static int getIndex(final Number n) { 209 final double value = n.doubleValue(); 210 return JSType.isRepresentableAsInt(value) ? (int)value : -1; 211 } 212 213 private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); 214 // method handles of the current class 215 private static final MethodHandle IS_JSOBJECT_GUARD = findOwnMH_S("isJSObject", boolean.class, Object.class); 216 private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class); 217 private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class); 218 219 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { 220 return MH.findStatic(MethodHandles.lookup(), BrowserJSObjectLinker.class, name, MH.type(rtype, types)); 221 } 222 223 // method handles of netscape.javascript.JSObject class 224 // These are in separate class as we lazily initialize these 225 // method handles when we hit a subclass of JSObject first time. 226 static class JSObjectHandles { 227 // method handles of JSObject class 228 static final MethodHandle JSOBJECT_GETMEMBER = findJSObjectMH_V("getMember", Object.class, String.class).asType(MH.type(Object.class, Object.class, String.class)); 229 static final MethodHandle JSOBJECT_GETSLOT = findJSObjectMH_V("getSlot", Object.class, int.class).asType(MH.type(Object.class, Object.class, int.class)); 230 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)); 231 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)); 232 static final MethodHandle JSOBJECT_CALL = findJSObjectMH_V("call", Object.class, String.class, Object[].class).asType(MH.type(Object.class, Object.class, String.class, Object[].class)); 233 234 private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) { 235 checkJSObjectClass(); 236 return MH.findVirtual(MethodHandles.publicLookup(), jsObjectClass, name, MH.type(rtype, types)); 237 } 238 } 239} 240