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 26 package jdk.nashorn.internal.runtime.linker; 27 28 import static jdk.nashorn.internal.lookup.Lookup.MH; 29 30 import java.lang.invoke.MethodHandle; 31 import java.lang.invoke.MethodType; 32 import jdk.internal.dynalink.CallSiteDescriptor; 33 import jdk.internal.dynalink.linker.GuardedInvocation; 34 import jdk.internal.dynalink.linker.LinkRequest; 35 import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 36 import jdk.internal.dynalink.support.Guards; 37 import jdk.nashorn.internal.lookup.Lookup; 38 import jdk.nashorn.internal.runtime.ScriptObject; 39 40 /** 41 * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and 42 * numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects} 43 * package. 44 */ 45 public final class PrimitiveLookup { 46 47 private PrimitiveLookup() { 48 } 49 50 /** 51 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 52 * @param request the link request for the dynamic call site. 53 * @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.) 54 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 55 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 56 * primitive base value. This instance will be used to delegate actual lookup. 57 * @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and 58 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 59 * method - it will be combined into the returned invocation as an argument filter on the receiver. 60 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 61 * type {@code receiverClass}. 62 */ 63 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass, 64 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) { 65 return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter); 66 } 67 68 /** 69 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 70 * @param request the link request for the dynamic call site. 71 * @param guard an explicit guard that will be used for the returned guarded invocation. 72 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 73 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 74 * primitive base value. This instance will be used to delegate actual lookup. 75 * @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and 76 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 77 * method - it will be combined into the returned invocation as an argument filter on the receiver. 78 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 79 * type (that is implied by both {@code guard} and {@code wrappedReceiver}). 80 */ 81 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard, 82 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) { 83 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 84 final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); 85 if ("setProp".equals(operator) || "setElem".equals(operator)) { 86 MethodType type = desc.getMethodType(); 87 MethodHandle method = MH.asType(Lookup.EMPTY_SETTER, MH.type(void.class, Object.class, type.parameterType(1))); 88 if (type.parameterCount() == 3) { 89 method = MH.dropArguments(method, 2, type.parameterType(2)); 90 } 91 return new GuardedInvocation(method, guard); 92 } 93 94 if(desc.getNameTokenCount() > 2) { 95 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 96 if(wrappedReceiver.findProperty(name, true) == null) { 97 // Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it. 98 return null; 99 } 100 } 101 final GuardedInvocation link = wrappedReceiver.lookup(desc, request); 102 if (link != null) { 103 MethodHandle method = link.getInvocation(); 104 final Class<?> receiverType = method.type().parameterType(0); 105 if (receiverType != Object.class) { 106 final MethodType wrapType = wrapFilter.type(); 107 assert receiverType.isAssignableFrom(wrapType.returnType()); 108 method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType))); 109 } 110 return new GuardedInvocation(method, guard, link.getSwitchPoint()); 111 } 112 return null; 113 } 114 } | 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 26 package jdk.nashorn.internal.runtime.linker; 27 28 import static jdk.nashorn.internal.lookup.Lookup.MH; 29 30 import java.lang.invoke.MethodHandle; 31 import java.lang.invoke.MethodType; 32 import jdk.internal.dynalink.CallSiteDescriptor; 33 import jdk.internal.dynalink.linker.GuardedInvocation; 34 import jdk.internal.dynalink.linker.LinkRequest; 35 import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 36 import jdk.internal.dynalink.support.Guards; 37 import jdk.nashorn.internal.lookup.Lookup; 38 import jdk.nashorn.internal.runtime.FindProperty; 39 import jdk.nashorn.internal.runtime.ScriptObject; 40 41 /** 42 * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and 43 * numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects} 44 * package. 45 */ 46 public final class PrimitiveLookup { 47 48 private PrimitiveLookup() { 49 } 50 51 /** 52 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 53 * @param request the link request for the dynamic call site. 54 * @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.) 55 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 56 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 57 * primitive base value. This instance will be used to delegate actual lookup. 58 * @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and 59 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 60 * method - it will be combined into the returned invocation as an argument filter on the receiver. 61 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 62 * type {@code receiverClass}. 63 */ 64 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass, 65 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 66 final MethodHandle protoFilter) { 67 return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter, protoFilter); 68 } 69 70 /** 71 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 72 * @param request the link request for the dynamic call site. 73 * @param guard an explicit guard that will be used for the returned guarded invocation. 74 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 75 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 76 * primitive base value. This instance will be used to delegate actual lookup. 77 * @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and 78 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 79 * method - it will be combined into the returned invocation as an argument filter on the receiver. 80 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 81 * type (that is implied by both {@code guard} and {@code wrappedReceiver}). 82 */ 83 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard, 84 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 85 final MethodHandle protoFilter) { 86 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 87 final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); 88 if ("setProp".equals(operator) || "setElem".equals(operator)) { 89 MethodType type = desc.getMethodType(); 90 MethodHandle method = MH.asType(Lookup.EMPTY_SETTER, MH.type(void.class, Object.class, type.parameterType(1))); 91 if (type.parameterCount() == 3) { 92 method = MH.dropArguments(method, 2, type.parameterType(2)); 93 } 94 return new GuardedInvocation(method, guard); 95 } 96 97 if(desc.getNameTokenCount() > 2) { 98 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 99 final FindProperty find = wrappedReceiver.findProperty(name, true); 100 if(find == null) { 101 // Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it. 102 return null; 103 } else if (find.isInherited() && !find.getProperty().hasGetterFunction(find.getOwner())) { 104 // If property is found in the prototype object bind the method handle directly to 105 // the proto filter instead of going through wrapper instantiation below. 106 final ScriptObject proto = wrappedReceiver.getProto(); 107 final GuardedInvocation link = proto.lookup(desc, request); 108 109 if (link != null) { 110 final MethodHandle invocation = link.getInvocation(); 111 final MethodHandle adaptedInvocation = MH.asType(invocation, invocation.type().changeParameterType(0, Object.class)); 112 final MethodHandle method = MH.filterArguments(adaptedInvocation, 0, protoFilter); 113 final MethodHandle protoGuard = MH.filterArguments(link.getGuard(), 0, protoFilter); 114 return new GuardedInvocation(method, NashornGuards.combineGuards(guard, protoGuard)); 115 } 116 } 117 } 118 final GuardedInvocation link = wrappedReceiver.lookup(desc, request); 119 if (link != null) { 120 MethodHandle method = link.getInvocation(); 121 final Class<?> receiverType = method.type().parameterType(0); 122 if (receiverType != Object.class) { 123 final MethodType wrapType = wrapFilter.type(); 124 assert receiverType.isAssignableFrom(wrapType.returnType()); 125 method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType))); 126 } 127 return new GuardedInvocation(method, guard, link.getSwitchPoint()); 128 } 129 return null; 130 } 131 } |