PrimitiveLookup.java revision 1551:f3b883bec2d0
155714Skris/* 2280297Sjkim * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3280297Sjkim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4280297Sjkim * 555714Skris * This code is free software; you can redistribute it and/or modify it 655714Skris * under the terms of the GNU General Public License version 2 only, as 755714Skris * published by the Free Software Foundation. Oracle designates this 855714Skris * particular file as subject to the "Classpath" exception as provided 955714Skris * by Oracle in the LICENSE file that accompanied this code. 1055714Skris * 1155714Skris * This code is distributed in the hope that it will be useful, but WITHOUT 1255714Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1355714Skris * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14280297Sjkim * version 2 for more details (a copy is included in the LICENSE file that 1555714Skris * accompanied this code). 1655714Skris * 1755714Skris * You should have received a copy of the GNU General Public License version 1855714Skris * 2 along with this work; if not, write to the Free Software Foundation, 1955714Skris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2055714Skris * 2155714Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2255714Skris * or visit www.oracle.com if you need additional information or have any 2355714Skris * questions. 2455714Skris */ 2555714Skris 2655714Skrispackage jdk.nashorn.internal.runtime.linker; 2755714Skris 2855714Skrisimport static jdk.nashorn.internal.lookup.Lookup.MH; 2955714Skrisimport static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 3055714Skris 3155714Skrisimport java.lang.invoke.MethodHandle; 3255714Skrisimport java.lang.invoke.MethodHandles; 3355714Skrisimport java.lang.invoke.MethodType; 3455714Skrisimport java.lang.invoke.SwitchPoint; 3555714Skrisimport jdk.dynalink.linker.GuardedInvocation; 3655714Skrisimport jdk.dynalink.linker.LinkRequest; 3755714Skrisimport jdk.dynalink.linker.support.Guards; 3855714Skrisimport jdk.nashorn.internal.runtime.Context; 3955714Skrisimport jdk.nashorn.internal.runtime.FindProperty; 4055714Skrisimport jdk.nashorn.internal.runtime.GlobalConstants; 4155714Skrisimport jdk.nashorn.internal.runtime.JSType; 4255714Skrisimport jdk.nashorn.internal.runtime.ScriptObject; 4355714Skrisimport jdk.nashorn.internal.runtime.ScriptRuntime; 4455714Skrisimport jdk.nashorn.internal.runtime.UserAccessorProperty; 4555714Skris 4655714Skris/** 4755714Skris * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and 4855714Skris * numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects} 4955714Skris * package. 5055714Skris */ 5155714Skrispublic final class PrimitiveLookup { 5255714Skris 5355714Skris /** Method handle to link setters on primitive base. See ES5 8.7.2. */ 5455714Skris private static final MethodHandle PRIMITIVE_SETTER = findOwnMH("primitiveSetter", 5555714Skris MH.type(void.class, ScriptObject.class, Object.class, Object.class, boolean.class, Object.class)); 5655714Skris 5755714Skris 5855714Skris private PrimitiveLookup() { 5955714Skris } 6055714Skris 6155714Skris /** 62109998Smarkm * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 6355714Skris * @param request the link request for the dynamic call site. 6455714Skris * @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.) 6555714Skris * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 66280297Sjkim * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 67280297Sjkim * primitive base value. This instance will be used to delegate actual lookup. 6855714Skris * @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and 6955714Skris * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 7055714Skris * method - it will be combined into the returned invocation as an argument filter on the receiver. 7155714Skris * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 7255714Skris * @param protoFilter A method handle that walks up the proto chain of this receiver object 7355714Skris * type {@code receiverClass}. 7455714Skris */ 75280297Sjkim public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass, 76280297Sjkim final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 7755714Skris final MethodHandle protoFilter) { 78280297Sjkim return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter, protoFilter); 79280297Sjkim } 80280297Sjkim 81280297Sjkim /** 82280297Sjkim * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 83280297Sjkim * @param request the link request for the dynamic call site. 84280297Sjkim * @param guard an explicit guard that will be used for the returned guarded invocation. 85280297Sjkim * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 86280297Sjkim * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 87280297Sjkim * primitive base value. This instance will be used to delegate actual lookup. 88280297Sjkim * @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and 8955714Skris * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 90280297Sjkim * method - it will be combined into the returned invocation as an argument filter on the receiver. 91280297Sjkim * @param protoFilter A method handle that walks up the proto chain of this receiver object 92280297Sjkim * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 93280297Sjkim * type (that is implied by both {@code guard} and {@code wrappedReceiver}). 94280297Sjkim */ 95280297Sjkim public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard, 96160814Ssimon final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 97280297Sjkim final MethodHandle protoFilter) { 98280297Sjkim // lookupPrimitive is only ever invoked from NashornPrimitiveLinker, 99280297Sjkim // which is a linker private to Nashorn, therefore the call site 100280297Sjkim // descriptor class will always be NashornCallSiteDescriptor. 101280297Sjkim final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)request.getCallSiteDescriptor(); 10255714Skris final String name = desc.getOperand(); 103280297Sjkim final FindProperty find = name != null ? wrappedReceiver.findProperty(name, true) : null; 104280297Sjkim 105280297Sjkim switch (desc.getFirstOperation()) { 106280297Sjkim case GET_PROPERTY: 107280297Sjkim case GET_ELEMENT: 108280297Sjkim case GET_METHOD: 10955714Skris //checks whether the property name is hard-coded in the call-site (i.e. a getProp vs a getElem, or setProp vs setElem) 110280297Sjkim //if it is we can make assumptions on the property: that if it is not defined on primitive wrapper itself it never will be. 111280297Sjkim //so in that case we can skip creation of primitive wrapper and start our search with the prototype. 112280297Sjkim if (name != null) { 113280297Sjkim if (find == null) { 11468651Skris // Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it. 115280297Sjkim return null; 116280297Sjkim } 117280297Sjkim 118280297Sjkim final SwitchPoint sp = find.getProperty().getBuiltinSwitchPoint(); //can use this instead of proto filter 119280297Sjkim if (sp instanceof Context.BuiltinSwitchPoint && !sp.hasBeenInvalidated()) { 120280297Sjkim return new GuardedInvocation(GlobalConstants.staticConstantGetter(find.getObjectValue()), guard, sp, null); 121280297Sjkim } 122280297Sjkim 123280297Sjkim if (find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) { 124280297Sjkim // If property is found in the prototype object bind the method handle directly to 125280297Sjkim // the proto filter instead of going through wrapper instantiation below. 126280297Sjkim final ScriptObject proto = wrappedReceiver.getProto(); 127280297Sjkim final GuardedInvocation link = proto.lookup(desc, request); 128280297Sjkim 129280297Sjkim if (link != null) { 130280297Sjkim final MethodHandle invocation = link.getInvocation(); //this contains the builtin switchpoint 131280297Sjkim final MethodHandle adaptedInvocation = MH.asType(invocation, invocation.type().changeParameterType(0, Object.class)); 132280297Sjkim final MethodHandle method = MH.filterArguments(adaptedInvocation, 0, protoFilter); 133280297Sjkim final MethodHandle protoGuard = MH.filterArguments(link.getGuard(), 0, protoFilter); 134280297Sjkim return new GuardedInvocation(method, NashornGuards.combineGuards(guard, protoGuard)); 135280297Sjkim } 136280297Sjkim } 137280297Sjkim } 138280297Sjkim break; 139280297Sjkim case SET_PROPERTY: 140280297Sjkim case SET_ELEMENT: 141280297Sjkim return getPrimitiveSetter(name, guard, wrapFilter, NashornCallSiteDescriptor.isStrict(desc)); 142280297Sjkim default: 143280297Sjkim break; 144280297Sjkim } 145280297Sjkim 146280297Sjkim final GuardedInvocation link = wrappedReceiver.lookup(desc, request); 147280297Sjkim if (link != null) { 148280297Sjkim MethodHandle method = link.getInvocation(); 14955714Skris final Class<?> receiverType = method.type().parameterType(0); 150 if (receiverType != Object.class) { 151 final MethodType wrapType = wrapFilter.type(); 152 assert receiverType.isAssignableFrom(wrapType.returnType()); 153 method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType))); 154 } 155 156 return new GuardedInvocation(method, guard, link.getSwitchPoints(), null); 157 } 158 159 return null; 160 } 161 162 private static GuardedInvocation getPrimitiveSetter(final String name, final MethodHandle guard, 163 final MethodHandle wrapFilter, final boolean isStrict) { 164 MethodHandle filter = MH.asType(wrapFilter, wrapFilter.type().changeReturnType(ScriptObject.class)); 165 final MethodHandle target; 166 167 if (name == null) { 168 filter = MH.dropArguments(filter, 1, Object.class, Object.class); 169 target = MH.insertArguments(PRIMITIVE_SETTER, 3, isStrict); 170 } else { 171 filter = MH.dropArguments(filter, 1, Object.class); 172 target = MH.insertArguments(PRIMITIVE_SETTER, 2, name, isStrict); 173 } 174 175 return new GuardedInvocation(MH.foldArguments(target, filter), guard); 176 } 177 178 179 @SuppressWarnings("unused") 180 private static void primitiveSetter(final ScriptObject wrappedSelf, final Object self, final Object key, 181 final boolean strict, final Object value) { 182 // See ES5.1 8.7.2 PutValue (V, W) 183 final String name = JSType.toString(key); 184 final FindProperty find = wrappedSelf.findProperty(name, true); 185 if (find == null || !(find.getProperty() instanceof UserAccessorProperty) || !find.getProperty().isWritable()) { 186 if (strict) { 187 throw typeError("property.not.writable", name, ScriptRuntime.safeToString(self)); 188 } 189 return; 190 } 191 // property found and is a UserAccessorProperty 192 find.setValue(value, strict); 193 } 194 195 private static MethodHandle findOwnMH(final String name, final MethodType type) { 196 return MH.findStatic(MethodHandles.lookup(), PrimitiveLookup.class, name, type); 197 } 198} 199