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 // Bind or add prototype filter depending on whether this is a scope object. 144 boundHandle = ScriptObject.addProtoFilter(methodHandle, find.getProtoChainLength()); 145 } else { 146 boundHandle = methodHandle; 147 } 148 return new SetMethod(boundHandle, property); 149 } 150 151 private SetMethod createGlobalPropertySetter() { 152 final ScriptObject global = Context.getGlobalTrusted(); 153 return new SetMethod(MH.filterArguments(global.addSpill(getName()), 0, ScriptObject.GLOBALFILTER), null); 154 } 155 156 private SetMethod createNewPropertySetter() { 157 final SetMethod sm = map.getFieldCount() < map.getFieldMaximum() ? createNewFieldSetter() : createNewSpillPropertySetter(); 158 final PropertyListeners listeners = map.getListeners(); 159 if (listeners != null) { 160 listeners.propertyAdded(sm.property); 161 } 162 return sm; 163 } 164 165 private SetMethod createNewFieldSetter() { 166 final PropertyMap oldMap = getMap(); 167 final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount()); 168 final PropertyMap newMap = oldMap.addProperty(property); 169 MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap)); 170 171 return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property); 172 } 173 174 private SetMethod createNewSpillPropertySetter() { 175 final int nextSpill = getMap().getSpillLength(); 176 177 final Property property = new AccessorProperty(getName(), Property.IS_SPILL, nextSpill); 178 return new SetMethod(createSpillMethodHandle(nextSpill, property), property); 179 } 180 181 private MethodHandle createSpillMethodHandle(final int nextSpill, Property property) { 182 final PropertyMap oldMap = getMap(); 183 final PropertyMap newMap = getNewMap(property); 184 185 final Object[] spill = sobj.spill; 186 if (spill == null) { 187 return MH.insertArguments(ScriptObject.SETSPILLWITHNEW, 0, desc, oldMap, newMap, nextSpill); 188 } else if (nextSpill < spill.length) { 189 return MH.insertArguments(ScriptObject.SETSPILL, 0, desc, oldMap, newMap, nextSpill); 190 } else { 191 final int newLength = (nextSpill + ScriptObject.SPILL_RATE) / ScriptObject.SPILL_RATE * ScriptObject.SPILL_RATE; 192 return MH.insertArguments(ScriptObject.SETSPILLWITHGROW, 0, desc, oldMap, newMap, nextSpill, newLength); 193 } 194 } 195 196 private PropertyMap getNewMap(Property property) { 197 return getMap().addProperty(property); 198 } 199 } --- EOF ---