SetMethodCreator.java revision 1002:2f0161551858
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 26package jdk.nashorn.internal.runtime; 27 28import static jdk.nashorn.internal.lookup.Lookup.MH; 29import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; 30import static jdk.nashorn.internal.runtime.JSType.getAccessorTypeIndex; 31 32import java.lang.invoke.MethodHandle; 33import java.lang.invoke.SwitchPoint; 34import jdk.internal.dynalink.CallSiteDescriptor; 35import jdk.internal.dynalink.linker.GuardedInvocation; 36import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 37import jdk.nashorn.internal.runtime.linker.NashornGuards; 38 39/** 40 * Instances of this class are quite ephemeral; they only exist for the duration of an invocation of 41 * {@link ScriptObject#findSetMethod(CallSiteDescriptor, jdk.internal.dynalink.linker.LinkRequest)} and 42 * serve as the actual encapsulation of the algorithm for creating an appropriate property setter method. 43 */ 44final class SetMethodCreator { 45 // See constructor parameters for description of fields 46 private final ScriptObject sobj; 47 private final PropertyMap map; 48 private final FindProperty find; 49 private final CallSiteDescriptor desc; 50 private final Class<?> type; 51 private final boolean explicitInstanceOfCheck; 52 53 /** 54 * Creates a new property setter method creator. 55 * @param sobj the object for which we're creating the property setter 56 * @param find a result of a {@link ScriptObject#findProperty(String, boolean)} on the object for the property we 57 * want to create a setter for. Can be null if the property does not yet exist on the object. 58 * @param desc the descriptor of the call site that triggered the property setter lookup 59 */ 60 SetMethodCreator(final ScriptObject sobj, final FindProperty find, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck) { 61 this.sobj = sobj; 62 this.map = sobj.getMap(); 63 this.find = find; 64 this.desc = desc; 65 this.type = desc.getMethodType().parameterType(1); 66 this.explicitInstanceOfCheck = explicitInstanceOfCheck; 67 68 } 69 70 private String getName() { 71 return desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 72 } 73 74 private PropertyMap getMap() { 75 return map; 76 } 77 78 /** 79 * Creates the actual guarded invocation that represents the dynamic setter method for the property. 80 * @return the actual guarded invocation that represents the dynamic setter method for the property. 81 */ 82 GuardedInvocation createGuardedInvocation() { 83 return createSetMethod().createGuardedInvocation(); 84 } 85 86 /** 87 * This class encapsulates the results of looking up a setter method; it's basically a triple of a method handle, 88 * a Property object, and flags for invocation. 89 * 90 */ 91 private class SetMethod { 92 private final MethodHandle methodHandle; 93 private final Property property; 94 95 /** 96 * Creates a new lookup result. 97 * @param methodHandle the actual method handle 98 * @param property the property object. Can be null in case we're creating a new property in the global object. 99 */ 100 SetMethod(final MethodHandle methodHandle, final Property property) { 101 assert methodHandle != null; 102 this.methodHandle = methodHandle; 103 this.property = property; 104 } 105 106 /** 107 * Composes from its components an actual guarded invocation that represents the dynamic setter method for the property. 108 * @return the composed guarded invocation that represents the dynamic setter method for the property. 109 */ 110 GuardedInvocation createGuardedInvocation() { 111 // getGuard() and getException() either both return null, or neither does. The reason for that is that now 112 // getGuard returns a map guard that casts its argument to ScriptObject, and if that fails, we need to 113 // relink on ClassCastException. 114 return new GuardedInvocation(methodHandle, NashornGuards.getGuard(sobj, property, desc, explicitInstanceOfCheck), 115 (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class); 116 } 117 } 118 119 private SetMethod createSetMethod() { 120 if (find != null) { 121 return createExistingPropertySetter(); 122 } 123 124 checkStrictCreateNewVariable(); 125 126 if (sobj.isScope()) { 127 return createGlobalPropertySetter(); 128 } 129 130 return createNewPropertySetter(); 131 } 132 133 private void checkStrictCreateNewVariable() { 134 // In strict mode, assignment can not create a new variable. 135 // See also ECMA Annex C item 4. ReferenceError is thrown. 136 if (NashornCallSiteDescriptor.isScope(desc) && NashornCallSiteDescriptor.isStrict(desc)) { 137 throw referenceError("not.defined", getName()); 138 } 139 } 140 141 private SetMethod createExistingPropertySetter() { 142 final Property property = find.getProperty(); 143 final MethodHandle methodHandle; 144 145 if (NashornCallSiteDescriptor.isDeclaration(desc)) { 146 assert property.needsDeclaration(); 147 // This is a LET or CONST being declared. The property is already there but flagged as needing declaration. 148 // We create a new PropertyMap with the flag removed. The map is installed with a fast compare-and-set 149 // method if the pre-callsite map is stable (which should be the case for function scopes except for 150 // non-strict functions containing eval() with var). Otherwise we have to use a slow setter that creates 151 // a new PropertyMap on the fly. 152 final PropertyMap oldMap = getMap(); 153 final Property newProperty = property.removeFlags(Property.NEEDS_DECLARATION); 154 final PropertyMap newMap = oldMap.replaceProperty(property, newProperty); 155 final MethodHandle fastSetter = find.replaceProperty(newProperty).getSetter(type, NashornCallSiteDescriptor.isStrict(desc)); 156 final MethodHandle slowSetter = MH.insertArguments(ScriptObject.DECLARE_AND_SET, 1, getName()).asType(fastSetter.type()); 157 158 // cas map used as guard, if true that means we can do the set fast 159 MethodHandle casMap = MH.insertArguments(ScriptObject.CAS_MAP, 1, oldMap, newMap); 160 casMap = MH.dropArguments(casMap, 1, type); 161 casMap = MH.asType(casMap, casMap.type().changeParameterType(0, Object.class)); 162 methodHandle = MH.guardWithTest(casMap, fastSetter, slowSetter); 163 } else { 164 methodHandle = find.getSetter(type, NashornCallSiteDescriptor.isStrict(desc)); 165 } 166 167 assert methodHandle != null; 168 assert property != null; 169 170 final MethodHandle boundHandle; 171 if (!property.hasSetterFunction(find.getOwner()) && find.isInherited()) { 172 boundHandle = ScriptObject.addProtoFilter(methodHandle, find.getProtoChainLength()); 173 } else { 174 boundHandle = methodHandle; 175 } 176 return new SetMethod(boundHandle, property); 177 } 178 179 private SetMethod createGlobalPropertySetter() { 180 final ScriptObject global = Context.getGlobal(); 181 return new SetMethod(MH.filterArguments(global.addSpill(type, getName()), 0, ScriptObject.GLOBALFILTER), null); 182 } 183 184 private SetMethod createNewPropertySetter() { 185 final SetMethod sm = map.getFreeFieldSlot() > -1 ? createNewFieldSetter() : createNewSpillPropertySetter(); 186 final PropertyListeners listeners = map.getListeners(); 187 if (listeners != null) { 188 listeners.propertyAdded(sm.property); 189 } 190 return sm; 191 } 192 193 private SetMethod createNewSetter(final Property property) { 194 final PropertyMap oldMap = getMap(); 195 final PropertyMap newMap = getNewMap(property); 196 final boolean isStrict = NashornCallSiteDescriptor.isStrict(desc); 197 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 198 199 //fast type specific setter 200 final MethodHandle fastSetter = property.getSetter(type, newMap); //0 sobj, 1 value, slot folded for spill property already 201 202 //slow setter, that calls ScriptObject.set with appropraite type and key name 203 MethodHandle slowSetter = ScriptObject.SET_SLOW[getAccessorTypeIndex(type)]; 204 slowSetter = MH.insertArguments(slowSetter, 3, NashornCallSiteDescriptor.isStrict(desc)); 205 slowSetter = MH.insertArguments(slowSetter, 1, name); 206 slowSetter = MH.asType(slowSetter, slowSetter.type().changeParameterType(0, Object.class)); 207 208 assert slowSetter.type().equals(fastSetter.type()) : "slow=" + slowSetter + " != fast=" + fastSetter; 209 210 //cas map used as guard, if true that means we can do the set fast 211 MethodHandle casMap = MH.insertArguments(ScriptObject.CAS_MAP, 1, oldMap, newMap); 212 casMap = MH.dropArguments(casMap, 1, type); 213 casMap = MH.asType(casMap, casMap.type().changeParameterType(0, Object.class)); 214 final MethodHandle casGuard = MH.guardWithTest(casMap, fastSetter, slowSetter); 215 216 //outermost level needs an extendable check. if object can be extended, guard is true and 217 //we can run the cas setter. The setter goes to "nop" VOID_RETURN if false or throws an 218 //exception if we are in strict mode and object is not extensible 219 MethodHandle extCheck = MH.insertArguments(ScriptObject.EXTENSION_CHECK, 1, isStrict, name); 220 extCheck = MH.asType(extCheck, extCheck.type().changeParameterType(0, Object.class)); 221 extCheck = MH.dropArguments(extCheck, 1, type); 222 223 MethodHandle nop = JSType.VOID_RETURN.methodHandle(); 224 nop = MH.dropArguments(nop, 0, Object.class, type); 225 226 return new SetMethod(MH.asType(MH.guardWithTest(extCheck, casGuard, nop), fastSetter.type()), property); 227 } 228 229 private SetMethod createNewFieldSetter() { 230 return createNewSetter(new AccessorProperty(getName(), 0, sobj.getClass(), getMap().getFreeFieldSlot(), type)); 231 } 232 233 private SetMethod createNewSpillPropertySetter() { 234 return createNewSetter(new SpillProperty(getName(), 0, getMap().getFreeSpillSlot(), type)); 235 } 236 237 private PropertyMap getNewMap(final Property property) { 238 return getMap().addProperty(property); 239 } 240} 241