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 
  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 jdk.nashorn.internal.runtime.PropertyMap;
  33 import jdk.nashorn.internal.runtime.ScriptFunction;
  34 import jdk.nashorn.internal.runtime.ScriptObject;
  35 
  36 /**
  37  * Constructor of method handles used to guard call sites.
  38  */
  39 public final class NashornGuards {
  40     private static final MethodHandle IS_SCRIPTOBJECT   = findOwnMH("isScriptObject", boolean.class, Object.class);
  41     private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class);
  42     private static final MethodHandle IS_MAP            = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class);

  43     private static final MethodHandle IS_INSTANCEOF_2   = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class);
  44 
  45     // don't create me!
  46     private NashornGuards() {
  47     }
  48 
  49     /**
  50      * Get the guard that checks if an item is a {@code ScriptObject}
  51      * @return method handle for guard
  52      */
  53     public static MethodHandle getScriptObjectGuard() {
  54         return IS_SCRIPTOBJECT;
  55     }
  56 
  57     /**
  58      * Get the guard that checks if an item is a {@code ScriptFunction}
  59      * @return method handle for guard
  60      */
  61     public static MethodHandle getScriptFunctionGuard() {
  62         return IS_SCRIPTFUNCTION;
  63     }
  64 
  65     /**
  66      * Get the guard that checks if a {@link PropertyMap} is equal to
  67      * a known map, using reference comparison
  68      *
  69      * @param map The map to check against. This will be bound to the guard method handle
  70      *
  71      * @return method handle for guard
  72      */
  73     public static MethodHandle getMapGuard(final PropertyMap map) {
  74         return MH.insertArguments(IS_MAP, 1, map);
  75     }
  76 
  77     /**

















































  78      * Get a guard that checks if in item is an instance of either of two classes.
  79      *
  80      * @param class1 the first class
  81      * @param class2 the second class
  82      * @return method handle for guard
  83      */
  84     public static MethodHandle getInstanceOf2Guard(final Class<?> class1, final Class<?> class2) {
  85         return MH.insertArguments(IS_INSTANCEOF_2, 1, class1, class2);
  86     }
  87 
  88     /**
  89      * Combine two method handles of type {@code (Object)boolean} using logical AND.
  90      *
  91      * @param guard1 the first guard
  92      * @param guard2 the second guard, only invoked if guard1 returns true
  93      * @return true if both guard1 and guard2 returned true
  94      */
  95     public static MethodHandle combineGuards(final MethodHandle guard1, final MethodHandle guard2) {
  96         return MH.guardWithTest(guard1, guard2, MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class));
  97     }
  98 
  99     @SuppressWarnings("unused")
 100     private static boolean isScriptObject(final Object self) {
 101         return self instanceof ScriptObject;
 102     }
 103 
 104     @SuppressWarnings("unused")
 105     private static boolean isScriptFunction(final Object self) {
 106         return self instanceof ScriptFunction;
 107     }
 108 
 109     @SuppressWarnings("unused")
 110     private static boolean isMap(final Object self, final PropertyMap map) {
 111         return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map;





 112     }
 113 
 114     @SuppressWarnings("unused")
 115     private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) {
 116         return class1.isInstance(self) || class2.isInstance(self);
 117     }
 118 
 119     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 120         return MH.findStatic(MethodHandles.lookup(), NashornGuards.class, name, MH.type(rtype, types));
 121     }
 122 
 123 }
--- EOF ---