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 @SuppressWarnings("unused") 89 private static boolean isScriptObject(final Object self) { 90 return self instanceof ScriptObject; 91 } 92 93 @SuppressWarnings("unused") 94 private static boolean isScriptFunction(final Object self) { 95 return self instanceof ScriptFunction; 96 } 97 98 @SuppressWarnings("unused") 99 private static boolean isMap(final Object self, final PropertyMap map) { 100 return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map; 101 } 102 103 @SuppressWarnings("unused") 104 private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) { 105 return class1.isInstance(self) || class2.isInstance(self); 106 } 107 108 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 109 return MH.findStatic(MethodHandles.lookup(), NashornGuards.class, name, MH.type(rtype, types)); 110 } 111 112 }