JSObjectLinker.java revision 1483:7cb19fa78763
1/* 2 * Copyright (c) 2010, 2013, 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; 29 30import java.lang.invoke.MethodHandle; 31import java.lang.invoke.MethodHandles; 32import java.util.Map; 33import javax.script.Bindings; 34import jdk.internal.dynalink.CallSiteDescriptor; 35import jdk.internal.dynalink.StandardOperation; 36import jdk.internal.dynalink.linker.GuardedInvocation; 37import jdk.internal.dynalink.linker.LinkRequest; 38import jdk.internal.dynalink.linker.LinkerServices; 39import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 40import jdk.nashorn.api.scripting.JSObject; 41import jdk.nashorn.internal.lookup.MethodHandleFactory; 42import jdk.nashorn.internal.lookup.MethodHandleFunctionality; 43import jdk.nashorn.internal.runtime.JSType; 44 45/** 46 * A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well 47 * as ScriptObjects from other Nashorn contexts. 48 */ 49final class JSObjectLinker implements TypeBasedGuardingDynamicLinker { 50 private final NashornBeansLinker nashornBeansLinker; 51 52 JSObjectLinker(final NashornBeansLinker nashornBeansLinker) { 53 this.nashornBeansLinker = nashornBeansLinker; 54 } 55 56 @Override 57 public boolean canLinkType(final Class<?> type) { 58 return canLinkTypeStatic(type); 59 } 60 61 static boolean canLinkTypeStatic(final Class<?> type) { 62 // can link JSObject also handles Map, Bindings to make 63 // sure those are not JSObjects. 64 return Map.class.isAssignableFrom(type) || 65 Bindings.class.isAssignableFrom(type) || 66 JSObject.class.isAssignableFrom(type); 67 } 68 69 @Override 70 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { 71 final Object self = request.getReceiver(); 72 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 73 74 GuardedInvocation inv; 75 if (self instanceof JSObject) { 76 inv = lookup(desc, request, linkerServices); 77 inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard()); 78 } else if (self instanceof Map || self instanceof Bindings) { 79 // guard to make sure the Map or Bindings does not turn into JSObject later! 80 final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices); 81 inv = new GuardedInvocation(beanInv.getInvocation(), 82 NashornGuards.combineGuards(beanInv.getGuard(), NashornGuards.getNotJSObjectGuard())); 83 } else { 84 throw new AssertionError(); // Should never reach here. 85 } 86 87 return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc); 88 } 89 90 private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception { 91 final StandardOperation op = NashornCallSiteDescriptor.getFirstStandardOperation(desc); 92 if (op == null) { 93 return null; 94 } 95 final String name = NashornCallSiteDescriptor.getOperand(desc); 96 switch (op) { 97 case GET_PROPERTY: 98 case GET_ELEMENT: 99 case GET_METHOD: 100 if (name != null) { 101 return findGetMethod(name); 102 } 103 // For indexed get, we want get GuardedInvocation beans linker and pass it. 104 // JSObjectLinker.get uses this fallback getter for explicit signature method access. 105 return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices)); 106 case SET_PROPERTY: 107 case SET_ELEMENT: 108 return name != null ? findSetMethod(name) : findSetIndexMethod(); 109 case CALL: 110 return findCallMethod(desc); 111 case NEW: 112 return findNewMethod(desc); 113 default: 114 } 115 return null; 116 } 117 118 private static GuardedInvocation findGetMethod(final String name) { 119 final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name); 120 return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); 121 } 122 123 private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) { 124 final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation()); 125 return inv.replaceMethods(getter, inv.getGuard()); 126 } 127 128 private static GuardedInvocation findSetMethod(final String name) { 129 final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, name); 130 return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); 131 } 132 133 private static GuardedInvocation findSetIndexMethod() { 134 return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD); 135 } 136 137 private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) { 138 // TODO: if call site is already a vararg, don't do asCollector 139 MethodHandle mh = JSOBJECT_CALL; 140 if (NashornCallSiteDescriptor.isApplyToCall(desc)) { 141 mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, JSOBJECT_CALL); 142 } 143 return new GuardedInvocation(MH.asCollector(mh, Object[].class, desc.getMethodType().parameterCount() - 2), IS_JSOBJECT_GUARD); 144 } 145 146 private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) { 147 final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1); 148 return new GuardedInvocation(func, IS_JSOBJECT_GUARD); 149 } 150 151 @SuppressWarnings("unused") 152 private static boolean isJSObject(final Object self) { 153 return self instanceof JSObject; 154 } 155 156 @SuppressWarnings("unused") 157 private static Object get(final MethodHandle fallback, final Object jsobj, final Object key) 158 throws Throwable { 159 if (key instanceof Integer) { 160 return ((JSObject)jsobj).getSlot((Integer)key); 161 } else if (key instanceof Number) { 162 final int index = getIndex((Number)key); 163 if (index > -1) { 164 return ((JSObject)jsobj).getSlot(index); 165 } 166 } else if (isString(key)) { 167 final String name = key.toString(); 168 // get with method name and signature. delegate it to beans linker! 169 if (name.indexOf('(') != -1) { 170 return fallback.invokeExact(jsobj, (Object) name); 171 } 172 return ((JSObject)jsobj).getMember(name); 173 } 174 return null; 175 } 176 177 @SuppressWarnings("unused") 178 private static void put(final Object jsobj, final Object key, final Object value) { 179 if (key instanceof Integer) { 180 ((JSObject)jsobj).setSlot((Integer)key, value); 181 } else if (key instanceof Number) { 182 ((JSObject)jsobj).setSlot(getIndex((Number)key), value); 183 } else if (isString(key)) { 184 ((JSObject)jsobj).setMember(key.toString(), value); 185 } 186 } 187 188 private static int getIndex(final Number n) { 189 final double value = n.doubleValue(); 190 return JSType.isRepresentableAsInt(value) ? (int)value : -1; 191 } 192 193 @SuppressWarnings("unused") 194 private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) { 195 assert args.length >= 2; 196 final Object receiver = args[0]; 197 final Object[] arguments = new Object[args.length - 1]; 198 System.arraycopy(args, 1, arguments, 0, arguments.length); 199 try { 200 return mh.invokeExact(obj, thiz, new Object[] { receiver, arguments }); 201 } catch (final RuntimeException | Error e) { 202 throw e; 203 } catch (final Throwable e) { 204 throw new RuntimeException(e); 205 } 206 } 207 208 private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); 209 210 // method handles of the current class 211 private static final MethodHandle IS_JSOBJECT_GUARD = findOwnMH_S("isJSObject", boolean.class, Object.class); 212 private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class); 213 private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class); 214 215 // method handles of JSObject class 216 private static final MethodHandle JSOBJECT_GETMEMBER = findJSObjectMH_V("getMember", Object.class, String.class); 217 private static final MethodHandle JSOBJECT_SETMEMBER = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class); 218 private static final MethodHandle JSOBJECT_CALL = findJSObjectMH_V("call", Object.class, Object.class, Object[].class); 219 private static final MethodHandle JSOBJECT_CALL_TO_APPLY = findOwnMH_S("callToApply", Object.class, MethodHandle.class, JSObject.class, Object.class, Object[].class); 220 private static final MethodHandle JSOBJECT_NEW = findJSObjectMH_V("newObject", Object.class, Object[].class); 221 222 private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) { 223 return MH.findVirtual(MethodHandles.lookup(), JSObject.class, name, MH.type(rtype, types)); 224 } 225 226 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { 227 return MH.findStatic(MethodHandles.lookup(), JSObjectLinker.class, name, MH.type(rtype, types)); 228 } 229} 230