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 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 /** 84 * Determine whether the given callsite needs a guard. 85 * @param property the property, or null 86 * @param desc the callsite descriptor 87 * @return true if a guard should be used for this callsite 88 */ 89 static boolean needsGuard(final Property property, final CallSiteDescriptor desc) { 90 return property == null || property.isConfigurable() 91 || property.isBound() || !ObjectClassGenerator.OBJECT_FIELDS_ONLY 92 || !NashornCallSiteDescriptor.isFastScope(desc) || property.canChangeType(); 93 } 94 95 /** 96 * Get the guard for a property access. This returns an identity guard for non-configurable global properties 97 * and a map guard for everything else. 98 * 99 * @param sobj the first object in the prototype chain 100 * @param property the property 101 * @param desc the callsite descriptor 102 * @return method handle for guard 103 */ 104 public static MethodHandle getGuard(final ScriptObject sobj, final Property property, final CallSiteDescriptor desc) { 105 if (!needsGuard(property, desc)) { 106 return null; 107 } 108 if (NashornCallSiteDescriptor.isScope(desc)) { 109 if (property != null && property.isBound()) { 110 // This is a declared top level variables in main script or eval, use identity guard. 111 return getIdentityGuard(sobj); 112 } 113 if (!(sobj instanceof Global) && (property == null || property.isConfigurable())) { 114 // Undeclared variables in nested evals need stronger guards 115 return combineGuards(getIdentityGuard(sobj), getMapGuard(sobj.getMap())); 116 } 117 } 118 return getMapGuard(sobj.getMap()); 119 } 120 121 122 /** 123 * Get a guard that checks referential identity of the current object. 124 * 125 * @param sobj the self object 126 * @return true if same self object instance 127 */ 128 public static MethodHandle getIdentityGuard(final ScriptObject sobj) { 129 return MH.insertArguments(SAME_OBJECT, 1, new WeakReference<>(sobj)); 130 } 131 132 /** 133 * Get a guard that checks if in item is an instance of either of two classes. 134 * 135 * @param class1 the first class 136 * @param class2 the second class 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); 177 } 178 179 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 180 return MH.findStatic(MethodHandles.lookup(), NashornGuards.class, name, MH.type(rtype, types)); 181 } 182 183 }