BoundCallableLinker.java revision 1116:ad912b034639
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 java.lang.invoke.MethodHandle; 29import java.lang.invoke.MethodHandles; 30import java.lang.invoke.MethodType; 31import java.util.Arrays; 32import jdk.internal.dynalink.CallSiteDescriptor; 33import jdk.internal.dynalink.linker.GuardedInvocation; 34import jdk.internal.dynalink.linker.LinkRequest; 35import jdk.internal.dynalink.linker.LinkerServices; 36import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 37import jdk.internal.dynalink.support.Guards; 38 39/** 40 * Links {@link BoundCallable} objects. Passes through to linker services for linking a callable (for either 41 * "dyn:call" or "dyn:new"), and modifies the returned invocation to deal with the receiver and argument binding. 42 */ 43final class BoundCallableLinker implements TypeBasedGuardingDynamicLinker { 44 @Override 45 public boolean canLinkType(final Class<?> type) { 46 return type == BoundCallable.class; 47 } 48 49 @Override 50 public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { 51 final Object objBoundCallable = linkRequest.getReceiver(); 52 if(!(objBoundCallable instanceof BoundCallable)) { 53 return null; 54 } 55 56 final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor(); 57 if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) { 58 return null; 59 } 60 final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR); 61 // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form 62 // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter. 63 final boolean isCall; 64 if ("new".equals(operation)) { 65 isCall = false; 66 } else if ("call".equals(operation)) { 67 isCall = true; 68 } else { 69 // Only dyn:call and dyn:new are supported. 70 return null; 71 } 72 final BoundCallable boundCallable = (BoundCallable)objBoundCallable; 73 final Object callable = boundCallable.getCallable(); 74 final Object boundThis = boundCallable.getBoundThis(); 75 76 // We need to ask the linker services for a delegate invocation on the target callable. 77 78 // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating 79 final Object[] args = linkRequest.getArguments(); 80 final Object[] boundArgs = boundCallable.getBoundArgs(); 81 final int argsLen = args.length; 82 final int boundArgsLen = boundArgs.length; 83 final Object[] newArgs = new Object[argsLen + boundArgsLen]; 84 newArgs[0] = callable; 85 final int firstArgIndex; 86 if (isCall) { 87 newArgs[1] = boundThis; 88 firstArgIndex = 2; 89 } else { 90 firstArgIndex = 1; 91 } 92 System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen); 93 System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex); 94 95 // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...) 96 // call site type when delegating to underlying linker (for dyn:new, there's no this). 97 final MethodType type = descriptor.getMethodType(); 98 // Use R(T0, ...) => R(callable.class, ...) 99 MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass()); 100 if (isCall) { 101 // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...) 102 newMethodType = newMethodType.changeParameterType(1, boundThis.getClass()); 103 } 104 // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...) 105 for(int i = boundArgs.length; i-- > 0;) { 106 newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass()); 107 } 108 final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType); 109 110 // Delegate to target's linker 111 final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs)); 112 if(inv == null) { 113 return null; 114 } 115 116 // Bind (callable[, boundThis], boundArgs) to the delegate handle 117 final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0, 118 Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length)); 119 final Class<?> p0Type = type.parameterType(0); 120 final MethodHandle droppingHandle; 121 if (isCall) { 122 // Ignore incoming boundCallable and this 123 droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1)); 124 } else { 125 // Ignore incoming boundCallable 126 droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type); 127 } 128 // Identity guard on boundCallable object 129 final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable); 130 return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type))); 131 } 132} 133