Mon, 11 Feb 2013 21:26:06 +0530
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
Reviewed-by: lagergren, jlaskey, attila
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 */
26 package jdk.nashorn.internal.runtime;
28 import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
29 import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
31 import java.lang.invoke.MethodHandle;
33 import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator;
34 import jdk.nashorn.internal.runtime.linker.Lookup;
35 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
36 import jdk.nashorn.internal.runtime.linker.NashornGuards;
38 import org.dynalang.dynalink.CallSiteDescriptor;
39 import org.dynalang.dynalink.linker.GuardedInvocation;
41 /**
42 * Instances of this class are quite ephemeral; they only exist for the duration of an invocation of
43 * {@link ScriptObject#findSetMethod(CallSiteDescriptor, org.dynalang.dynalink.linker.LinkRequest)} and
44 * serve as the actual encapsulation of the algorithm for creating an appropriate property setter method.
45 */
46 final class SetMethodCreator {
47 // See constructor parameters for description of fields
48 private final ScriptObject sobj;
49 private final PropertyMap map;
50 private final FindProperty find;
51 private final CallSiteDescriptor desc;
53 /**
54 * Creates a new property setter method creator.
55 * @param sobj the object for which we're creating the property setter
56 * @param find a result of a {@link ScriptObject#findProperty(String, boolean)} on the object for the property we
57 * want to create a setter for. Can be null if the property does not yet exist on the object.
58 * @param desc the descriptor of the call site that triggered the property setter lookup
59 */
60 SetMethodCreator(final ScriptObject sobj, final FindProperty find, final CallSiteDescriptor desc) {
61 this.sobj = sobj;
62 this.map = sobj.getMap();
63 this.find = find;
64 this.desc = desc;
65 }
67 private String getName() {
68 return desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
69 }
71 private PropertyMap getMap() {
72 return map;
73 }
75 /**
76 * Creates the actual guarded invocation that represents the dynamic setter method for the property.
77 * @return the actual guarded invocation that represents the dynamic setter method for the property.
78 */
79 GuardedInvocation createGuardedInvocation() {
80 return createSetMethod().createGuardedInvocation();
81 }
83 /**
84 * This class encapsulates the results of looking up a setter method; it's basically a triple of a method hanle,
85 * a Property object, and flags for invocation.
86 *
87 */
88 private class SetMethod {
89 private final MethodHandle methodHandle;
90 private final Property property;
92 /**
93 * Creates a new lookup result.
94 * @param methodHandle the actual method handle
95 * @param property the property object. Can be null in case we're creating a new property in the global object.
96 */
97 SetMethod(final MethodHandle methodHandle, final Property property) {
98 assert methodHandle != null;
99 this.methodHandle = methodHandle;
100 this.property = property;
101 }
103 /**
104 * Composes from its components an actual guarded invocation that represents the dynamic setter method for the property.
105 * @return the composed guarded invocation that represents the dynamic setter method for the property.
106 */
107 GuardedInvocation createGuardedInvocation() {
108 return new GuardedInvocation(methodHandle, getGuard());
109 }
111 private MethodHandle getGuard() {
112 return needsNoGuard() ? null : NashornGuards.getMapGuard(getMap());
113 }
115 private boolean needsNoGuard() {
116 return NashornCallSiteDescriptor.isFastScope(desc) &&
117 (ObjectClassGenerator.OBJECT_FIELDS_ONLY || isPropertyTypeStable());
118 }
120 private boolean isPropertyTypeStable() {
121 return property == null || !property.canChangeType();
122 }
123 }
125 private SetMethod createSetMethod() {
126 if (find != null) {
127 return createExistingPropertySetter();
128 }
130 checkStrictCreateNewVariable();
132 if (sobj.isScope()) {
133 return createGlobalPropertySetter();
134 }
136 return createNewPropertySetter();
137 }
139 private void checkStrictCreateNewVariable() {
140 // In strict mode, assignment can not create a new variable.
141 // See also ECMA Annex C item 4. ReferenceError is thrown.
142 if (NashornCallSiteDescriptor.isScope(desc) && NashornCallSiteDescriptor.isStrict(desc)) {
143 referenceError("not.defined", getName());
144 }
145 }
147 private SetMethod createExistingPropertySetter() {
148 final Property property = find.getProperty();
149 final Class<?> type = desc.getMethodType().parameterType(1);
150 final MethodHandle methodHandle = find.getSetter(type, NashornCallSiteDescriptor.isStrict(desc));
152 assert methodHandle != null;
153 assert property != null;
155 final MethodHandle boundHandle;
156 if (!property.hasSetterFunction() && find.isInherited()) {
157 boundHandle = ScriptObject.bindTo(methodHandle, find.getOwner());
158 } else {
159 boundHandle = methodHandle;
160 }
161 return new SetMethod(boundHandle, property);
162 }
164 private SetMethod createGlobalPropertySetter() {
165 final ScriptObject global = Context.getGlobalTrusted();
166 return new SetMethod(ScriptObject.bindTo(global.addSpill(getName()), global), null);
167 }
169 private SetMethod createNewPropertySetter() {
170 final int nextEmbed = sobj.findEmbed();
171 final SetMethod sm;
172 if (nextEmbed >= ScriptObject.EMBED_SIZE) {
173 sm = createNewSpillPropertySetter();
174 } else {
175 sm = createNewEmbedPropertySetter(nextEmbed);
176 }
178 sobj.notifyPropertyAdded(sobj, sm.property);
179 return sm;
180 }
182 private SetMethod createNewSpillPropertySetter() {
183 final int nextSpill = getMap().getSpillLength();
185 final Property property = createSpillProperty(nextSpill);
186 return new SetMethod(createSpillMethodHandle(nextSpill, property), property);
187 }
189 private Property createSpillProperty(final int nextSpill) {
190 final MethodHandle getter = MH.asType(MH.insertArguments(MH.arrayElementGetter(Object[].class), 1, nextSpill), Lookup.GET_OBJECT_TYPE);
191 final MethodHandle setter = MH.asType(MH.insertArguments(MH.arrayElementSetter(Object[].class), 1, nextSpill), Lookup.SET_OBJECT_TYPE);
193 return new SpillProperty(getName(), Property.IS_SPILL, nextSpill, getter, setter);
194 }
196 private MethodHandle createSpillMethodHandle(final int nextSpill, Property property) {
197 final PropertyMap oldMap = getMap();
198 final PropertyMap newMap = getNewMap(property);
200 final Object[] spill = sobj.spill;
201 if (spill == null) {
202 return MH.insertArguments(ScriptObject.SETSPILLWITHNEW, 0, desc, oldMap, newMap, nextSpill);
203 } else if (nextSpill < spill.length) {
204 return MH.insertArguments(ScriptObject.SETSPILL, 0, desc, oldMap, newMap, nextSpill);
205 } else {
206 final int newLength = (nextSpill + ScriptObject.SPILL_RATE) / ScriptObject.SPILL_RATE * ScriptObject.SPILL_RATE;
207 return MH.insertArguments(ScriptObject.SETSPILLWITHGROW, 0, desc, oldMap, newMap, nextSpill, newLength);
208 }
209 }
211 private SetMethod createNewEmbedPropertySetter(final int nextEmbed) {
212 sobj.useEmbed(nextEmbed);
213 final Property property = new SpillProperty(getName(), 0, nextEmbed, ScriptObject.GET_EMBED[nextEmbed], ScriptObject.SET_EMBED[nextEmbed]);
214 //TODO specfields
215 final MethodHandle methodHandle = MH.insertArguments(ScriptObject.SETEMBED, 0, desc, getMap(), getNewMap(property), property.getSetter(Object.class, getMap()), nextEmbed);
216 return new SetMethod(methodHandle, property);
217 }
219 private PropertyMap getNewMap(Property property) {
220 return getMap().addProperty(property);
221 }
222 }