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 26 package jdk.nashorn.internal.runtime.linker; 27 28 import static jdk.nashorn.internal.lookup.Lookup.MH; 29 import java.lang.invoke.MethodHandle; 30 import java.lang.invoke.MethodType; 31 import java.lang.invoke.SwitchPoint; 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.Guards; 36 import jdk.nashorn.internal.runtime.Context; 37 import jdk.nashorn.internal.runtime.FindProperty; 38 import jdk.nashorn.internal.runtime.GlobalConstants; 39 import jdk.nashorn.internal.runtime.ScriptObject; 40 import 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 */ 47 public 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) { 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 } | 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 26 package jdk.nashorn.internal.runtime.linker; 27 28 import static jdk.nashorn.internal.lookup.Lookup.MH; 29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodHandles; 33 import java.lang.invoke.MethodType; 34 import java.lang.invoke.SwitchPoint; 35 import jdk.internal.dynalink.CallSiteDescriptor; 36 import jdk.internal.dynalink.linker.GuardedInvocation; 37 import jdk.internal.dynalink.linker.LinkRequest; 38 import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 39 import jdk.internal.dynalink.support.Guards; 40 import jdk.nashorn.internal.runtime.Context; 41 import jdk.nashorn.internal.runtime.FindProperty; 42 import jdk.nashorn.internal.runtime.GlobalConstants; 43 import jdk.nashorn.internal.runtime.JSType; 44 import jdk.nashorn.internal.runtime.ScriptObject; 45 import jdk.nashorn.internal.runtime.ScriptRuntime; 46 import jdk.nashorn.internal.runtime.UserAccessorProperty; 47 48 /** 49 * Implements lookup of methods to link for dynamic operations on JavaScript primitive values (booleans, strings, and 50 * numbers). This class is only public so it can be accessed by classes in the {@code jdk.nashorn.internal.objects} 51 * package. 52 */ 53 public final class PrimitiveLookup { 54 55 /** Method handle to link setters on primitive base. See ES5 8.7.2. */ 56 private static final MethodHandle PRIMITIVE_SETTER = findOwnMH("primitiveSetter", 57 MH.type(void.class, ScriptObject.class, Object.class, Object.class, boolean.class, Object.class)); 58 59 60 private PrimitiveLookup() { 61 } 62 63 /** 64 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 65 * @param request the link request for the dynamic call site. 66 * @param receiverClass the class of the receiver value (e.g., {@link java.lang.Boolean}, {@link java.lang.String} etc.) 67 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 68 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 69 * primitive base value. This instance will be used to delegate actual lookup. 70 * @param wrapFilter A method handle that takes a primitive value of type specified in the {@code receiverClass} and 71 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 72 * method - it will be combined into the returned invocation as an argument filter on the receiver. 73 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 74 * @param protoFilter A method handle that walks up the proto chain of this receiver object 75 * type {@code receiverClass}. 76 */ 77 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass, 78 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 79 final MethodHandle protoFilter) { 81 } 82 83 /** 84 * Returns a guarded invocation representing the linkage for a dynamic operation on a primitive Java value. 85 * @param request the link request for the dynamic call site. 86 * @param guard an explicit guard that will be used for the returned guarded invocation. 87 * @param wrappedReceiver a transient JavaScript native wrapper object created as the object proxy for the primitive 88 * value; see ECMAScript 5.1, section 8.7.1 for discussion of using {@code [[Get]]} on a property reference with a 89 * primitive base value. This instance will be used to delegate actual lookup. 90 * @param wrapFilter A method handle that takes a primitive value of type guarded by the {@code guard} and 91 * creates a transient native wrapper of the same type as {@code wrappedReceiver} for subsequent invocations of the 92 * method - it will be combined into the returned invocation as an argument filter on the receiver. 93 * @param protoFilter A method handle that walks up the proto chain of this receiver object 94 * @return a guarded invocation representing the operation at the call site when performed on a JavaScript primitive 95 * type (that is implied by both {@code guard} and {@code wrappedReceiver}). 96 */ 97 public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard, 98 final ScriptObject wrappedReceiver, final MethodHandle wrapFilter, 99 final MethodHandle protoFilter) { 100 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 101 final String name; 102 final FindProperty find; 103 104 if (desc.getNameTokenCount() > 2) { 105 name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 106 find = wrappedReceiver.findProperty(name, true); 107 } else { 108 name = null; 109 find = null; 110 } 111 112 final String firstOp = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); 113 114 switch (firstOp) { 115 case "getProp": 116 case "getElem": 117 case "getMethod": 118 //checks whether the property name is hard-coded in the call-site (i.e. a getProp vs a getElem, or setProp vs setElem) 119 //if it is we can make assumptions on the property: that if it is not defined on primitive wrapper itself it never will be. 120 //so in that case we can skip creation of primitive wrapper and start our search with the prototype. 121 if (name != null) { 122 if (find == null) { 123 // Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it. 124 return null; 125 } 126 127 final SwitchPoint sp = find.getProperty().getBuiltinSwitchPoint(); //can use this instead of proto filter 128 if (sp instanceof Context.BuiltinSwitchPoint && !sp.hasBeenInvalidated()) { 129 return new GuardedInvocation(GlobalConstants.staticConstantGetter(find.getObjectValue()), guard, sp, null); 130 } 131 132 if (find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) { 133 // If property is found in the prototype object bind the method handle directly to 134 // the proto filter instead of going through wrapper instantiation below. 135 final ScriptObject proto = wrappedReceiver.getProto(); 136 final GuardedInvocation link = proto.lookup(desc, request); 137 138 if (link != null) { 139 final MethodHandle invocation = link.getInvocation(); //this contains the builtin switchpoint 140 final MethodHandle adaptedInvocation = MH.asType(invocation, invocation.type().changeParameterType(0, Object.class)); 141 final MethodHandle method = MH.filterArguments(adaptedInvocation, 0, protoFilter); 142 final MethodHandle protoGuard = MH.filterArguments(link.getGuard(), 0, protoFilter); 143 return new GuardedInvocation(method, NashornGuards.combineGuards(guard, protoGuard)); 144 } 145 } 146 } 147 break; 148 case "setProp": 149 case "setElem": 150 return getPrimitiveSetter(name, guard, wrapFilter, NashornCallSiteDescriptor.isStrict(desc)); 151 default: 152 break; 153 } 154 155 final GuardedInvocation link = wrappedReceiver.lookup(desc, request); 156 if (link != null) { 157 MethodHandle method = link.getInvocation(); 158 final Class<?> receiverType = method.type().parameterType(0); 159 if (receiverType != Object.class) { 160 final MethodType wrapType = wrapFilter.type(); 161 assert receiverType.isAssignableFrom(wrapType.returnType()); 162 method = MH.filterArguments(method, 0, MH.asType(wrapFilter, wrapType.changeReturnType(receiverType))); 163 } 164 165 return new GuardedInvocation(method, guard, link.getSwitchPoints(), null); 166 } 167 168 return null; 169 } 170 171 private static GuardedInvocation getPrimitiveSetter(final String name, final MethodHandle guard, 172 final MethodHandle wrapFilter, final boolean isStrict) { 173 MethodHandle filter = MH.asType(wrapFilter, wrapFilter.type().changeReturnType(ScriptObject.class)); 174 final MethodHandle target; 175 176 if (name == null) { 177 filter = MH.dropArguments(filter, 1, Object.class, Object.class); 178 target = MH.insertArguments(PRIMITIVE_SETTER, 3, isStrict); 179 } else { 180 filter = MH.dropArguments(filter, 1, Object.class); 181 target = MH.insertArguments(PRIMITIVE_SETTER, 2, name, isStrict); 182 } 183 184 return new GuardedInvocation(MH.foldArguments(target, filter), guard); 185 } 186 187 188 @SuppressWarnings("unused") 189 private static void primitiveSetter(final ScriptObject wrappedSelf, final Object self, final Object key, 190 final boolean strict, final Object value) { 191 // See ES5.1 8.7.2 PutValue (V, W) 192 final String name = JSType.toString(key); 193 final FindProperty find = wrappedSelf.findProperty(name, true); 194 if (find == null || !(find.getProperty() instanceof UserAccessorProperty) || !find.getProperty().isWritable()) { 195 if (strict) { 196 throw typeError("property.not.writable", name, ScriptRuntime.safeToString(self)); 197 } 198 return; 199 } 200 // property found and is a UserAccessorProperty 201 find.setValue(value, strict); 202 } 203 204 private static MethodHandle findOwnMH(final String name, final MethodType type) { 205 return MH.findStatic(MethodHandles.lookup(), PrimitiveLookup.class, name, type); 206 } 207 } |