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