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