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