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; 27 28 import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; 29 import static jdk.nashorn.internal.lookup.Lookup.MH; 30 31 import java.lang.invoke.MethodHandle; 32 import jdk.internal.dynalink.CallSiteDescriptor; 33 import jdk.internal.dynalink.linker.GuardedInvocation; 34 import jdk.nashorn.internal.codegen.ObjectClassGenerator; 35 import jdk.nashorn.internal.lookup.Lookup; 36 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 37 import jdk.nashorn.internal.runtime.linker.NashornGuards; 38 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 */ 45 final 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 52 /** 53 * Creates a new property setter method creator. 54 * @param sobj the object for which we're creating the property setter 55 * @param find a result of a {@link ScriptObject#findProperty(String, boolean)} on the object for the property we 56 * want to create a setter for. Can be null if the property does not yet exist on the object. 57 * @param desc the descriptor of the call site that triggered the property setter lookup 58 */ 59 SetMethodCreator(final ScriptObject sobj, final FindProperty find, final CallSiteDescriptor desc) { 60 this.sobj = sobj; 61 this.map = sobj.getMap(); 62 this.find = find; 63 this.desc = desc; 64 } 65 66 private String getName() { 67 return desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 68 } 69 70 private PropertyMap getMap() { 71 return map; 72 } 73 74 /** 75 * Creates the actual guarded invocation that represents the dynamic setter method for the property. 76 * @return the actual guarded invocation that represents the dynamic setter method for the property. 77 */ 78 GuardedInvocation createGuardedInvocation() { 79 return createSetMethod().createGuardedInvocation(); 80 } 81 82 /** 83 * This class encapsulates the results of looking up a setter method; it's basically a triple of a method hanle, 84 * a Property object, and flags for invocation. 85 * 86 */ 87 private class SetMethod { 88 private final MethodHandle methodHandle; 89 private final Property property; 90 91 /** 92 * Creates a new lookup result. 93 * @param methodHandle the actual method handle 94 * @param property the property object. Can be null in case we're creating a new property in the global object. 95 */ 96 SetMethod(final MethodHandle methodHandle, final Property property) { 97 assert methodHandle != null; 98 this.methodHandle = methodHandle; 99 this.property = property; 100 } 101 102 /** 103 * Composes from its components an actual guarded invocation that represents the dynamic setter method for the property. 104 * @return the composed guarded invocation that represents the dynamic setter method for the property. 105 */ 106 GuardedInvocation createGuardedInvocation() { 107 return new GuardedInvocation(methodHandle, getGuard()); 108 } 109 110 private MethodHandle getGuard() { 111 return needsNoGuard() ? null : NashornGuards.getMapGuard(getMap()); 112 } 113 114 private boolean needsNoGuard() { 115 return NashornCallSiteDescriptor.isFastScope(desc) && 116 (ObjectClassGenerator.OBJECT_FIELDS_ONLY || isPropertyTypeStable()); 117 } 118 119 private boolean isPropertyTypeStable() { 120 return property == null || !property.canChangeType(); 121 } 122 } 123 124 private SetMethod createSetMethod() { 125 if (find != null) { 126 return createExistingPropertySetter(); 127 } 128 129 checkStrictCreateNewVariable(); 130 131 if (sobj.isScope()) { 132 return createGlobalPropertySetter(); 133 } 134 135 return createNewPropertySetter(); 136 } 137 138 private void checkStrictCreateNewVariable() { 139 // In strict mode, assignment can not create a new variable. 140 // See also ECMA Annex C item 4. ReferenceError is thrown. 141 if (NashornCallSiteDescriptor.isScope(desc) && NashornCallSiteDescriptor.isStrict(desc)) { 142 throw referenceError("not.defined", getName()); 143 } 144 } 145 146 private SetMethod createExistingPropertySetter() { 147 final Property property = find.getProperty(); 148 final Class<?> type = desc.getMethodType().parameterType(1); 149 final MethodHandle methodHandle = find.getSetter(type, NashornCallSiteDescriptor.isStrict(desc)); 150 151 assert methodHandle != null; 152 assert property != null; 153 154 final MethodHandle boundHandle; 155 if (!property.hasSetterFunction(find.getOwner()) && find.isInherited()) { 156 // Bind or add prototype filter depending on whether this is a scope object. 157 boundHandle = sobj.isScope() ? 158 ScriptObject.addProtoFilter(methodHandle, find.getProtoChainLength()): 159 ScriptObject.bindTo(methodHandle, find.getOwner()); 160 } else { 161 boundHandle = methodHandle; 162 } 163 return new SetMethod(boundHandle, property); 164 } 165 166 private SetMethod createGlobalPropertySetter() { 167 final ScriptObject global = Context.getGlobalTrusted(); 168 return new SetMethod(ScriptObject.bindTo(global.addSpill(getName()), global), null); 169 } 170 171 private SetMethod createNewPropertySetter() { 172 final SetMethod sm = map.getFieldCount() < map.getFieldMaximum() ? createNewFieldSetter() : createNewSpillPropertySetter(); 173 sobj.notifyPropertyAdded(sobj, sm.property); 174 return sm; 175 } 176 177 private SetMethod createNewFieldSetter() { 178 final PropertyMap oldMap = getMap(); 179 final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount()); 180 final PropertyMap newMap = oldMap.addProperty(property); 181 MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap)); 182 183 return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property); 184 } 185 186 private SetMethod createNewSpillPropertySetter() { 187 final int nextSpill = getMap().getSpillLength(); 188 189 final Property property = new AccessorProperty(getName(), Property.IS_SPILL, nextSpill); 190 return new SetMethod(createSpillMethodHandle(nextSpill, property), property); 191 } 192 193 private MethodHandle createSpillMethodHandle(final int nextSpill, Property property) { 194 final PropertyMap oldMap = getMap(); 195 final PropertyMap newMap = getNewMap(property); 196 197 final Object[] spill = sobj.spill; 198 if (spill == null) { 199 return MH.insertArguments(ScriptObject.SETSPILLWITHNEW, 0, desc, oldMap, newMap, nextSpill); 200 } else if (nextSpill < spill.length) { 201 return MH.insertArguments(ScriptObject.SETSPILL, 0, desc, oldMap, newMap, nextSpill); 202 } else { 203 final int newLength = (nextSpill + ScriptObject.SPILL_RATE) / ScriptObject.SPILL_RATE * ScriptObject.SPILL_RATE; 204 return MH.insertArguments(ScriptObject.SETSPILLWITHGROW, 0, desc, oldMap, newMap, nextSpill, newLength); 205 } 206 } 207 208 private PropertyMap getNewMap(Property property) { 209 return getMap().addProperty(property); 210 } 211 }