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
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 final PropertyListeners listeners = map.getListeners();
174 if (listeners != null) {
175 listeners.propertyAdded(sm.property);
176 }
177 return sm;
178 }
179
180 private SetMethod createNewFieldSetter() {
181 final PropertyMap oldMap = getMap();
182 final Property property = new AccessorProperty(getName(), 0, sobj.getClass(), oldMap.getFieldCount());
183 final PropertyMap newMap = oldMap.addProperty(property);
184 MethodHandle setter = MH.insertArguments(ScriptObject.SETFIELD, 0, desc, oldMap, newMap, property.getSetter(Object.class, newMap));
185
186 return new SetMethod(MH.asType(setter, Lookup.SET_OBJECT_TYPE), property);
187 }
188
|
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
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
|