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