src/jdk/nashorn/internal/runtime/linker/NashornGuards.java

Print this page




  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 
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.ref.WeakReference;
  33 import jdk.internal.dynalink.CallSiteDescriptor;

  34 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
  35 import jdk.nashorn.internal.objects.Global;
  36 import jdk.nashorn.internal.runtime.Property;
  37 import jdk.nashorn.internal.runtime.PropertyMap;
  38 import jdk.nashorn.internal.runtime.ScriptFunction;
  39 import jdk.nashorn.internal.runtime.ScriptObject;
  40 
  41 /**
  42  * Constructor of method handles used to guard call sites.
  43  */
  44 public final class NashornGuards {
  45     private static final MethodHandle IS_SCRIPTOBJECT   = findOwnMH("isScriptObject", boolean.class, Object.class);

  46     private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class);
  47     private static final MethodHandle IS_MAP            = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class);
  48     private static final MethodHandle SAME_OBJECT       = findOwnMH("sameObject", boolean.class, Object.class, WeakReference.class);
  49     private static final MethodHandle IS_INSTANCEOF_2   = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class);
  50 
  51     // don't create me!
  52     private NashornGuards() {
  53     }
  54 
  55     /**
  56      * Get the guard that checks if an item is a {@code ScriptObject}
  57      * @return method handle for guard
  58      */
  59     public static MethodHandle getScriptObjectGuard() {
  60         return IS_SCRIPTOBJECT;
  61     }
  62 
  63     /**








  64      * Get the guard that checks if an item is a {@code ScriptFunction}
  65      * @return method handle for guard
  66      */
  67     public static MethodHandle getScriptFunctionGuard() {
  68         return IS_SCRIPTFUNCTION;
  69     }
  70 
  71     /**
  72      * Get the guard that checks if a {@link PropertyMap} is equal to
  73      * a known map, using reference comparison
  74      *
  75      * @param map The map to check against. This will be bound to the guard method handle
  76      *
  77      * @return method handle for guard
  78      */
  79     public static MethodHandle getMapGuard(final PropertyMap map) {
  80         return MH.insertArguments(IS_MAP, 1, map);
  81     }
  82 
  83     /**


 137      * @return method handle for guard
 138      */
 139     public static MethodHandle getInstanceOf2Guard(final Class<?> class1, final Class<?> class2) {
 140         return MH.insertArguments(IS_INSTANCEOF_2, 1, class1, class2);
 141     }
 142 
 143     /**
 144      * Combine two method handles of type {@code (Object)boolean} using logical AND.
 145      *
 146      * @param guard1 the first guard
 147      * @param guard2 the second guard, only invoked if guard1 returns true
 148      * @return true if both guard1 and guard2 returned true
 149      */
 150     public static MethodHandle combineGuards(final MethodHandle guard1, final MethodHandle guard2) {
 151         return MH.guardWithTest(guard1, guard2, MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class));
 152     }
 153 
 154     @SuppressWarnings("unused")
 155     private static boolean isScriptObject(final Object self) {
 156         return self instanceof ScriptObject;





 157     }
 158 
 159     @SuppressWarnings("unused")
 160     private static boolean isScriptFunction(final Object self) {
 161         return self instanceof ScriptFunction;
 162     }
 163 
 164     @SuppressWarnings("unused")
 165     private static boolean isMap(final Object self, final PropertyMap map) {
 166         return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map;
 167     }
 168 
 169     @SuppressWarnings("unused")
 170     private static boolean sameObject(final Object self, final WeakReference<ScriptObject> ref) {
 171         return self == ref.get();
 172     }
 173 
 174     @SuppressWarnings("unused")
 175     private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) {
 176         return class1.isInstance(self) || class2.isInstance(self);


  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 
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.ref.WeakReference;
  33 import jdk.internal.dynalink.CallSiteDescriptor;
  34 import jdk.nashorn.api.scripting.JSObject;
  35 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
  36 import jdk.nashorn.internal.objects.Global;
  37 import jdk.nashorn.internal.runtime.Property;
  38 import jdk.nashorn.internal.runtime.PropertyMap;
  39 import jdk.nashorn.internal.runtime.ScriptFunction;
  40 import jdk.nashorn.internal.runtime.ScriptObject;
  41 
  42 /**
  43  * Constructor of method handles used to guard call sites.
  44  */
  45 public final class NashornGuards {
  46     private static final MethodHandle IS_SCRIPTOBJECT   = findOwnMH("isScriptObject", boolean.class, Object.class);
  47     private static final MethodHandle IS_NOT_JSOBJECT   = findOwnMH("isNotJSObject", boolean.class, Object.class);
  48     private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class);
  49     private static final MethodHandle IS_MAP            = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class);
  50     private static final MethodHandle SAME_OBJECT       = findOwnMH("sameObject", boolean.class, Object.class, WeakReference.class);
  51     private static final MethodHandle IS_INSTANCEOF_2   = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class);
  52 
  53     // don't create me!
  54     private NashornGuards() {
  55     }
  56 
  57     /**
  58      * Get the guard that checks if an item is a {@code ScriptObject}
  59      * @return method handle for guard
  60      */
  61     public static MethodHandle getScriptObjectGuard() {
  62         return IS_SCRIPTOBJECT;
  63     }
  64 
  65     /**
  66      * Get the guard that checks if an item is not a {@code JSObject}
  67      * @return method handle for guard
  68      */
  69     public static MethodHandle getNotJSObjectGuard() {
  70         return IS_NOT_JSOBJECT;
  71     }
  72 
  73     /**
  74      * Get the guard that checks if an item is a {@code ScriptFunction}
  75      * @return method handle for guard
  76      */
  77     public static MethodHandle getScriptFunctionGuard() {
  78         return IS_SCRIPTFUNCTION;
  79     }
  80 
  81     /**
  82      * Get the guard that checks if a {@link PropertyMap} is equal to
  83      * a known map, using reference comparison
  84      *
  85      * @param map The map to check against. This will be bound to the guard method handle
  86      *
  87      * @return method handle for guard
  88      */
  89     public static MethodHandle getMapGuard(final PropertyMap map) {
  90         return MH.insertArguments(IS_MAP, 1, map);
  91     }
  92 
  93     /**


 147      * @return method handle for guard
 148      */
 149     public static MethodHandle getInstanceOf2Guard(final Class<?> class1, final Class<?> class2) {
 150         return MH.insertArguments(IS_INSTANCEOF_2, 1, class1, class2);
 151     }
 152 
 153     /**
 154      * Combine two method handles of type {@code (Object)boolean} using logical AND.
 155      *
 156      * @param guard1 the first guard
 157      * @param guard2 the second guard, only invoked if guard1 returns true
 158      * @return true if both guard1 and guard2 returned true
 159      */
 160     public static MethodHandle combineGuards(final MethodHandle guard1, final MethodHandle guard2) {
 161         return MH.guardWithTest(guard1, guard2, MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class));
 162     }
 163 
 164     @SuppressWarnings("unused")
 165     private static boolean isScriptObject(final Object self) {
 166         return self instanceof ScriptObject;
 167     }
 168 
 169     @SuppressWarnings("unused")
 170     private static boolean isNotJSObject(final Object self) {
 171         return !(self instanceof JSObject);
 172     }
 173 
 174     @SuppressWarnings("unused")
 175     private static boolean isScriptFunction(final Object self) {
 176         return self instanceof ScriptFunction;
 177     }
 178 
 179     @SuppressWarnings("unused")
 180     private static boolean isMap(final Object self, final PropertyMap map) {
 181         return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map;
 182     }
 183 
 184     @SuppressWarnings("unused")
 185     private static boolean sameObject(final Object self, final WeakReference<ScriptObject> ref) {
 186         return self == ref.get();
 187     }
 188 
 189     @SuppressWarnings("unused")
 190     private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) {
 191         return class1.isInstance(self) || class2.isInstance(self);