NashornGuards.java revision 953:221a84ef44c0
155682Smarkm/* 255682Smarkm * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 355682Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 455682Smarkm * 555682Smarkm * This code is free software; you can redistribute it and/or modify it 655682Smarkm * under the terms of the GNU General Public License version 2 only, as 755682Smarkm * published by the Free Software Foundation. Oracle designates this 855682Smarkm * particular file as subject to the "Classpath" exception as provided 955682Smarkm * by Oracle in the LICENSE file that accompanied this code. 1055682Smarkm * 1155682Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT 1255682Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1355682Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1455682Smarkm * version 2 for more details (a copy is included in the LICENSE file that 1555682Smarkm * accompanied this code). 1655682Smarkm * 1755682Smarkm * You should have received a copy of the GNU General Public License version 1855682Smarkm * 2 along with this work; if not, write to the Free Software Foundation, 1955682Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2055682Smarkm * 2155682Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2255682Smarkm * or visit www.oracle.com if you need additional information or have any 2355682Smarkm * questions. 2455682Smarkm */ 2555682Smarkm 2655682Smarkmpackage jdk.nashorn.internal.runtime.linker; 2755682Smarkm 2855682Smarkmimport static jdk.nashorn.internal.lookup.Lookup.MH; 2955682Smarkm 3055682Smarkmimport java.lang.invoke.MethodHandle; 3155682Smarkmimport java.lang.invoke.MethodHandles; 3255682Smarkmimport java.lang.ref.WeakReference; 3355682Smarkmimport jdk.internal.dynalink.CallSiteDescriptor; 3455682Smarkmimport jdk.internal.dynalink.linker.LinkRequest; 3555682Smarkmimport jdk.nashorn.api.scripting.JSObject; 36178825Sdfrimport jdk.nashorn.internal.codegen.ObjectClassGenerator; 37178825Sdfrimport jdk.nashorn.internal.objects.Global; 38178825Sdfrimport jdk.nashorn.internal.runtime.Property; 3955682Smarkmimport jdk.nashorn.internal.runtime.PropertyMap; 4055682Smarkmimport jdk.nashorn.internal.runtime.ScriptFunction; 41178825Sdfrimport jdk.nashorn.internal.runtime.ScriptObject; 4255682Smarkmimport jdk.nashorn.internal.runtime.options.Options; 4355682Smarkm 4455682Smarkm/** 4555682Smarkm * Constructor of method handles used to guard call sites. 4655682Smarkm */ 4755682Smarkmpublic final class NashornGuards { 4855682Smarkm private static final MethodHandle IS_MAP = findOwnMH("isMap", boolean.class, ScriptObject.class, PropertyMap.class); 4955682Smarkm private static final MethodHandle IS_MAP_SCRIPTOBJECT = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class); 5055682Smarkm private static final MethodHandle IS_INSTANCEOF_2 = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class); 5155682Smarkm private static final MethodHandle IS_SCRIPTOBJECT = findOwnMH("isScriptObject", boolean.class, Object.class); 5255682Smarkm private static final MethodHandle IS_NOT_JSOBJECT = findOwnMH("isNotJSObject", boolean.class, Object.class); 5355682Smarkm private static final MethodHandle SAME_OBJECT = findOwnMH("sameObject", boolean.class, Object.class, WeakReference.class); 5455682Smarkm //TODO - maybe put this back in ScriptFunction instead of the ClassCastException.class relinkage 5555682Smarkm //private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class); 5655682Smarkm 5755682Smarkm private static final boolean CCE_ONLY = Options.getBooleanProperty("nashorn.cce"); 5855682Smarkm 5955682Smarkm // don't create me! 6055682Smarkm private NashornGuards() { 6155682Smarkm } 6255682Smarkm 6355682Smarkm /** 6455682Smarkm * Given a callsite descriptor and a link request, determine whether we should use an instanceof 6555682Smarkm * check explicitly for the guard if needed, or if we should link it with a try/catch ClassCastException 6655682Smarkm * combinator as its relink criteria - i.e. relink when CCE is thrown. 6755682Smarkm * 6855682Smarkm * @param desc callsite descriptor 6955682Smarkm * @param request link request 7055682Smarkm * @return true of explicit instanceof check is needed 7155682Smarkm */ 7255682Smarkm public static boolean explicitInstanceOfCheck(final CallSiteDescriptor desc, final LinkRequest request) { 7355682Smarkm //THIS is currently true, as the inliner encounters several problems with sun.misc.ValueConversions.castReference 7455682Smarkm //otherwise. We should only use the exception based relink where we have no choice, and the result is faster code, 7555682Smarkm //for example in the NativeArray, TypedArray, ContinuousArray getters. For the standard callsite, it appears that 7655682Smarkm //we lose performance rather than gain it, due to JVM issues. :-( 7755682Smarkm return !CCE_ONLY; 7855682Smarkm } 7955682Smarkm 8055682Smarkm /** 8155682Smarkm * Returns a guard that does an instanceof ScriptObject check on the receiver 8255682Smarkm * @return guard 8355682Smarkm */ 8455682Smarkm public static MethodHandle getScriptObjectGuard() { 8555682Smarkm return IS_SCRIPTOBJECT; 8655682Smarkm } 8755682Smarkm 8855682Smarkm /** 8955682Smarkm * Get the guard that checks if an item is not a {@code JSObject} 9055682Smarkm * @return method handle for guard 9155682Smarkm */ 9255682Smarkm public static MethodHandle getNotJSObjectGuard() { 9355682Smarkm return IS_NOT_JSOBJECT; 9455682Smarkm } 9555682Smarkm 9655682Smarkm /** 9755682Smarkm * Returns a guard that does an instanceof ScriptObject check on the receiver 9855682Smarkm * @param explicitInstanceOfCheck - if false, then this is a nop, because it's all the guard does 9955682Smarkm * @return guard 10055682Smarkm */ 10155682Smarkm public static MethodHandle getScriptObjectGuard(final boolean explicitInstanceOfCheck) { 10255682Smarkm return explicitInstanceOfCheck ? IS_SCRIPTOBJECT : null; 10355682Smarkm } 10455682Smarkm 10555682Smarkm /** 10655682Smarkm * Get the guard that checks if a {@link PropertyMap} is equal to 10755682Smarkm * a known map, using reference comparison 10855682Smarkm * 10955682Smarkm * @param explicitInstanceOfCheck true if we should do an explicit script object instanceof check instead of just casting 11055682Smarkm * @param map The map to check against. This will be bound to the guard method handle 11155682Smarkm * 11255682Smarkm * @return method handle for guard 11355682Smarkm */ 11455682Smarkm public static MethodHandle getMapGuard(final PropertyMap map, final boolean explicitInstanceOfCheck) { 11555682Smarkm return MH.insertArguments(explicitInstanceOfCheck ? IS_MAP_SCRIPTOBJECT : IS_MAP, 1, map); 11655682Smarkm } 11755682Smarkm 11855682Smarkm /** 11955682Smarkm * Determine whether the given callsite needs a guard. 12055682Smarkm * @param property the property, or null 12155682Smarkm * @param desc the callsite descriptor 12255682Smarkm * @return true if a guard should be used for this callsite 12355682Smarkm */ 12455682Smarkm static boolean needsGuard(final Property property, final CallSiteDescriptor desc) { 12555682Smarkm return property == null || property.isConfigurable() 12655682Smarkm || property.isBound() || !ObjectClassGenerator.OBJECT_FIELDS_ONLY 12755682Smarkm || !NashornCallSiteDescriptor.isFastScope(desc) || property.canChangeType(); 12855682Smarkm } 12955682Smarkm 13055682Smarkm /** 13155682Smarkm * Get the guard for a property access. This returns an identity guard for non-configurable global properties 13255682Smarkm * and a map guard for everything else. 13355682Smarkm * 13455682Smarkm * @param sobj the first object in the prototype chain 13555682Smarkm * @param property the property 13655682Smarkm * @param desc the callsite descriptor 13755682Smarkm * @param explicitInstanceOfCheck true if we should do an explicit script object instanceof check instead of just casting 13855682Smarkm * @return method handle for guard 13955682Smarkm */ 14055682Smarkm public static MethodHandle getGuard(final ScriptObject sobj, final Property property, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck) { 14155682Smarkm if (!needsGuard(property, desc)) { 14255682Smarkm return null; 14355682Smarkm } 14455682Smarkm if (NashornCallSiteDescriptor.isScope(desc)) { 14555682Smarkm if (property != null && property.isBound() && !property.canChangeType()) { 14655682Smarkm // This is a declared top level variables in main script or eval, use identity guard. 14755682Smarkm return getIdentityGuard(sobj); 14855682Smarkm } 14955682Smarkm if (!(sobj instanceof Global) && (property == null || property.isConfigurable())) { 15055682Smarkm // Undeclared variables in nested evals need stronger guards 15155682Smarkm return combineGuards(getIdentityGuard(sobj), getMapGuard(sobj.getMap(), explicitInstanceOfCheck)); 15255682Smarkm } 15355682Smarkm } 15455682Smarkm return getMapGuard(sobj.getMap(), explicitInstanceOfCheck); 15555682Smarkm } 15655682Smarkm 15755682Smarkm 15855682Smarkm /** 15955682Smarkm * Get a guard that checks referential identity of the current object. 16055682Smarkm * 16155682Smarkm * @param sobj the self object 16255682Smarkm * @return true if same self object instance 16355682Smarkm */ 16455682Smarkm public static MethodHandle getIdentityGuard(final ScriptObject sobj) { 16555682Smarkm return MH.insertArguments(SAME_OBJECT, 1, new WeakReference<>(sobj)); 16655682Smarkm } 16755682Smarkm 16855682Smarkm /** 16955682Smarkm * Get a guard that checks if in item is an instance of either of two classes. 17055682Smarkm * 17155682Smarkm * @param class1 the first class 17255682Smarkm * @param class2 the second class 17355682Smarkm * @return method handle for guard 17455682Smarkm */ 17555682Smarkm public static MethodHandle getInstanceOf2Guard(final Class<?> class1, final Class<?> class2) { 17655682Smarkm return MH.insertArguments(IS_INSTANCEOF_2, 1, class1, class2); 17755682Smarkm } 17855682Smarkm 17955682Smarkm /** 18055682Smarkm * Combine two method handles of type {@code (Object)boolean} using logical AND. 18155682Smarkm * 18255682Smarkm * @param guard1 the first guard 18355682Smarkm * @param guard2 the second guard, only invoked if guard1 returns true 18455682Smarkm * @return true if both guard1 and guard2 returned true 18555682Smarkm */ 18655682Smarkm public static MethodHandle combineGuards(final MethodHandle guard1, final MethodHandle guard2) { 18755682Smarkm if (guard1 == null) { 18855682Smarkm return guard2; 18955682Smarkm } else if (guard2 == null) { 19055682Smarkm return guard1; 19155682Smarkm } else { 19255682Smarkm return MH.guardWithTest(guard1, guard2, MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class)); 19355682Smarkm } 19455682Smarkm } 19555682Smarkm 19655682Smarkm @SuppressWarnings("unused") 19755682Smarkm private static boolean isScriptObject(final Object self) { 19855682Smarkm return self instanceof ScriptObject; 19955682Smarkm } 20055682Smarkm 20155682Smarkm @SuppressWarnings("unused") 20255682Smarkm private static boolean isScriptObject(final Class<? extends ScriptObject> clazz, final Object self) { 20355682Smarkm return clazz.isInstance(self); 20455682Smarkm } 20555682Smarkm 20655682Smarkm @SuppressWarnings("unused") 20755682Smarkm private static boolean isMap(final ScriptObject self, final PropertyMap map) { 20855682Smarkm return self.getMap() == map; 20955682Smarkm } 21055682Smarkm 21155682Smarkm @SuppressWarnings("unused") 21255682Smarkm private static boolean isNotJSObject(final Object self) { 21355682Smarkm return !(self instanceof JSObject); 21455682Smarkm } 21555682Smarkm 21655682Smarkm @SuppressWarnings("unused") 21755682Smarkm private static boolean isMap(final Object self, final PropertyMap map) { 21855682Smarkm return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map; 21955682Smarkm } 22055682Smarkm 22155682Smarkm 22255682Smarkm @SuppressWarnings("unused") 22355682Smarkm private static boolean sameObject(final Object self, final WeakReference<ScriptObject> ref) { 22455682Smarkm return self == ref.get(); 22555682Smarkm } 22655682Smarkm 22755682Smarkm @SuppressWarnings("unused") 22855682Smarkm private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) { 22955682Smarkm return class1.isInstance(self) || class2.isInstance(self); 23055682Smarkm } 23155682Smarkm 23255682Smarkm @SuppressWarnings("unused") 23355682Smarkm private static boolean isScriptFunction(final Object self) { 23455682Smarkm return self instanceof ScriptFunction; 23555682Smarkm } 23655682Smarkm 23755682Smarkm private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 23855682Smarkm return MH.findStatic(MethodHandles.lookup(), NashornGuards.class, name, MH.type(rtype, types)); 23955682Smarkm } 24055682Smarkm} 24155682Smarkm