author | sundar |
Tue, 25 Jun 2013 17:31:19 +0530 | |
changeset 18615 | 3f6e6adcbc1a |
parent 17774 | 0407501fa563 |
child 18847 | 31ca64473a86 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
28 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.ACCESSOR_TYPES; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
29 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.DEBUG_FIELDS; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
30 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.LOG; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
31 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.OBJECT_FIELDS_ONLY; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
32 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.PRIMITIVE_TYPE; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
33 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createGetter; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
34 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createGuardBoxedPrimitiveSetter; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
35 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createSetter; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
36 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getAccessorType; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
37 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getAccessorTypeIndex; |
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
38 |
import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getNumberOfAccessorTypes; |
16277 | 39 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
40 |
import static jdk.nashorn.internal.lookup.MethodHandleFactory.stripName; |
|
16147 | 41 |
|
42 |
import java.lang.invoke.MethodHandle; |
|
43 |
import java.lang.invoke.MethodHandles; |
|
44 |
import java.lang.invoke.MethodType; |
|
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
45 |
import jdk.nashorn.internal.codegen.ObjectClassGenerator; |
16147 | 46 |
import jdk.nashorn.internal.codegen.types.Type; |
16277 | 47 |
import jdk.nashorn.internal.lookup.Lookup; |
48 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
|
16147 | 49 |
|
50 |
/** |
|
51 |
* An AccessorProperty is the most generic property type. An AccessorProperty is |
|
52 |
* represented as fields in a ScriptObject class. |
|
53 |
*/ |
|
54 |
public class AccessorProperty extends Property { |
|
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
55 |
private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); |
16147 | 56 |
private static final MethodHandle REPLACE_MAP = findOwnMH("replaceMap", Object.class, Object.class, PropertyMap.class, String.class, Class.class, Class.class); |
57 |
||
58 |
private static final int NOOF_TYPES = getNumberOfAccessorTypes(); |
|
59 |
||
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
60 |
/** |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
61 |
* Properties in different maps for the same structure class will share their field getters and setters. This could |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
62 |
* be further extended to other method handles that are looked up in the AccessorProperty constructor, but right now |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
63 |
* these are the most frequently retrieved ones, and lookup of method handle natives only registers in the profiler |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
64 |
* for them. |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
65 |
*/ |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
66 |
private static ClassValue<GettersSetters> GETTERS_SETTERS = new ClassValue<GettersSetters>() { |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
67 |
@Override |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
68 |
protected GettersSetters computeValue(Class<?> structure) { |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
69 |
return new GettersSetters(structure); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
70 |
} |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
71 |
}; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
72 |
|
16147 | 73 |
/** Property getter cache */ |
74 |
private MethodHandle[] getters = new MethodHandle[NOOF_TYPES]; |
|
75 |
||
76 |
private static final MethodType[] ACCESSOR_GETTER_TYPES = new MethodType[NOOF_TYPES]; |
|
77 |
private static final MethodType[] ACCESSOR_SETTER_TYPES = new MethodType[NOOF_TYPES]; |
|
17770 | 78 |
private static final MethodHandle SPILL_ELEMENT_GETTER; |
79 |
private static final MethodHandle SPILL_ELEMENT_SETTER; |
|
80 |
||
81 |
private static final int SPILL_CACHE_SIZE = 8; |
|
82 |
private static final MethodHandle[] SPILL_ACCESSORS = new MethodHandle[SPILL_CACHE_SIZE * 2]; |
|
83 |
||
84 |
static { |
|
85 |
for (int i = 0; i < NOOF_TYPES; i++) { |
|
86 |
final Type type = ACCESSOR_TYPES.get(i); |
|
87 |
ACCESSOR_GETTER_TYPES[i] = MH.type(type.getTypeClass(), Object.class); |
|
88 |
ACCESSOR_SETTER_TYPES[i] = MH.type(void.class, Object.class, type.getTypeClass()); |
|
89 |
} |
|
90 |
||
91 |
final MethodHandle spillGetter = MH.getter(MethodHandles.lookup(), ScriptObject.class, "spill", Object[].class); |
|
92 |
SPILL_ELEMENT_GETTER = MH.filterArguments(MH.arrayElementGetter(Object[].class), 0, spillGetter); |
|
93 |
SPILL_ELEMENT_SETTER = MH.filterArguments(MH.arrayElementSetter(Object[].class), 0, spillGetter); |
|
94 |
} |
|
16147 | 95 |
|
96 |
/** Seed getter for the primitive version of this field (in -Dnashorn.fields.dual=true mode) */ |
|
97 |
private MethodHandle primitiveGetter; |
|
98 |
||
99 |
/** Seed setter for the primitive version of this field (in -Dnashorn.fields.dual=true mode) */ |
|
100 |
private MethodHandle primitiveSetter; |
|
101 |
||
102 |
/** Seed getter for the Object version of this field */ |
|
103 |
private MethodHandle objectGetter; |
|
104 |
||
105 |
/** Seed setter for the Object version of this field */ |
|
106 |
private MethodHandle objectSetter; |
|
107 |
||
108 |
/** |
|
109 |
* Current type of this object, in object only mode, this is an Object.class. In dual-fields mode |
|
110 |
* null means undefined, and primitive types are allowed. The reason a special type is used for |
|
111 |
* undefined, is that are no bits left to represent it in primitive types |
|
112 |
*/ |
|
113 |
private Class<?> currentType; |
|
114 |
||
115 |
/** |
|
116 |
* Delegate constructor. This is used when adding properties to the Global scope, which |
|
117 |
* is necessary for outermost levels in a script (the ScriptObject is represented by |
|
16275 | 118 |
* a JO-prefixed ScriptObject class, but the properties need to be in the Global scope |
16147 | 119 |
* and are thus rebound with that as receiver |
120 |
* |
|
121 |
* @param property accessor property to rebind |
|
122 |
* @param delegate delegate script object to rebind receiver to |
|
123 |
*/ |
|
124 |
public AccessorProperty(final AccessorProperty property, final ScriptObject delegate) { |
|
17770 | 125 |
super(property); |
16147 | 126 |
|
17770 | 127 |
this.primitiveGetter = bindTo(property.primitiveGetter, delegate); |
128 |
this.primitiveSetter = bindTo(property.primitiveSetter, delegate); |
|
129 |
this.objectGetter = bindTo(property.objectGetter, delegate); |
|
130 |
this.objectSetter = bindTo(property.objectSetter, delegate); |
|
16147 | 131 |
|
132 |
setCurrentType(property.getCurrentType()); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
17770 | 136 |
* Constructor for spill properties. Array getters and setters will be created on demand. |
137 |
* |
|
138 |
* @param key the property key |
|
139 |
* @param flags the property flags |
|
140 |
* @param slot spill slot |
|
141 |
*/ |
|
142 |
public AccessorProperty(final String key, final int flags, final int slot) { |
|
143 |
super(key, flags, slot); |
|
144 |
assert (flags & IS_SPILL) == IS_SPILL; |
|
145 |
||
146 |
setCurrentType(Object.class); |
|
147 |
} |
|
148 |
||
149 |
/** |
|
16147 | 150 |
* Constructor. Similar to the constructor with both primitive getters and setters, the difference |
151 |
* here being that only one getter and setter (setter is optional for non writable fields) is given |
|
152 |
* to the constructor, and the rest are created from those. Used e.g. by Nasgen classes |
|
153 |
* |
|
154 |
* @param key the property key |
|
155 |
* @param flags the property flags |
|
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
156 |
* @param slot the property field number or spill slot |
16147 | 157 |
* @param getter the property getter |
158 |
* @param setter the property setter or null if non writable, non configurable |
|
159 |
*/ |
|
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
160 |
public AccessorProperty(final String key, final int flags, final int slot, final MethodHandle getter, final MethodHandle setter) { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
161 |
super(key, flags, slot); |
16147 | 162 |
|
163 |
// we don't need to prep the setters these will never be invalidated as this is a nasgen |
|
164 |
// or known type getter/setter. No invalidations will take place |
|
165 |
||
166 |
final Class<?> getterType = getter.type().returnType(); |
|
167 |
final Class<?> setterType = setter == null ? null : setter.type().parameterType(1); |
|
168 |
||
169 |
assert setterType == null || setterType == getterType; |
|
170 |
||
171 |
if (getterType.isPrimitive()) { |
|
172 |
for (int i = 0; i < NOOF_TYPES; i++) { |
|
173 |
getters[i] = MH.asType( |
|
174 |
Lookup.filterReturnType( |
|
175 |
getter, |
|
176 |
getAccessorType(i).getTypeClass()), |
|
177 |
ACCESSOR_GETTER_TYPES[i]); |
|
178 |
} |
|
179 |
} else { |
|
180 |
//this will work as the object setter and getter will be converted appropriately |
|
181 |
objectGetter = getter; |
|
182 |
objectSetter = setter; |
|
183 |
} |
|
184 |
||
185 |
setCurrentType(getterType); |
|
186 |
} |
|
187 |
||
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
188 |
private static class GettersSetters { |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
189 |
final MethodHandle[] getters; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
190 |
final MethodHandle[] setters; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
191 |
|
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
192 |
public GettersSetters(Class<?> structure) { |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
193 |
final int fieldCount = ObjectClassGenerator.getFieldCount(structure); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
194 |
getters = new MethodHandle[fieldCount]; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
195 |
setters = new MethodHandle[fieldCount]; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
196 |
for(int i = 0; i < fieldCount; ++i) { |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
197 |
final String fieldName = ObjectClassGenerator.getFieldName(i, Type.OBJECT); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
198 |
getters[i] = MH.getter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
199 |
setters[i] = MH.setter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
200 |
} |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
201 |
} |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
202 |
} |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
203 |
|
16147 | 204 |
/** |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
205 |
* Constructor for dual field AccessorPropertys. |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
206 |
* |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
207 |
* @param key property key |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
208 |
* @param flags property flags |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
209 |
* @param structure structure for objects associated with this property |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
210 |
* @param slot property field number or spill slot |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
211 |
*/ |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
212 |
public AccessorProperty(final String key, final int flags, final Class<?> structure, final int slot) { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
213 |
super(key, flags, slot); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
214 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
215 |
/* |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
216 |
* primitiveGetter and primitiveSetter are only used in dual fields mode. Setting them to null also |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
217 |
* works in dual field mode, it only means that the property never has a primitive |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
218 |
* representation. |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
219 |
*/ |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
220 |
primitiveGetter = null; |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
221 |
primitiveSetter = null; |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
222 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
223 |
if (isParameter() && hasArguments()) { |
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
224 |
final MethodHandle arguments = MH.getter(lookup, structure, "arguments", Object.class); |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
225 |
final MethodHandle argumentsSO = MH.asType(arguments, arguments.type().changeReturnType(ScriptObject.class)); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
226 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
227 |
objectGetter = MH.insertArguments(MH.filterArguments(ScriptObject.GET_ARGUMENT.methodHandle(), 0, argumentsSO), 1, slot); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
228 |
objectSetter = MH.insertArguments(MH.filterArguments(ScriptObject.SET_ARGUMENT.methodHandle(), 0, argumentsSO), 1, slot); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
229 |
} else { |
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
230 |
final GettersSetters gs = GETTERS_SETTERS.get(structure); |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
231 |
objectGetter = gs.getters[slot]; |
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
232 |
objectSetter = gs.setters[slot]; |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
233 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
234 |
if (!OBJECT_FIELDS_ONLY) { |
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
235 |
final String fieldNamePrimitive = ObjectClassGenerator.getFieldName(slot, ObjectClassGenerator.PRIMITIVE_TYPE); |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
236 |
primitiveGetter = MH.getter(lookup, structure, fieldNamePrimitive, PRIMITIVE_TYPE.getTypeClass()); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
237 |
primitiveSetter = MH.setter(lookup, structure, fieldNamePrimitive, PRIMITIVE_TYPE.getTypeClass()); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
238 |
} |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
239 |
} |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
240 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
241 |
Class<?> initialType = null; |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
242 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
243 |
if (OBJECT_FIELDS_ONLY || isAlwaysObject()) { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
244 |
initialType = Object.class; |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
245 |
} else if (!canBePrimitive()) { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
246 |
info(key + " cannot be primitive"); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
247 |
initialType = Object.class; |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
248 |
} else { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
249 |
info(key + " CAN be primitive"); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
250 |
if (!canBeUndefined()) { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
251 |
info(key + " is always defined"); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
252 |
initialType = int.class; //double works too for less type invalidation, but this requires experimentation, e.g. var x = 17; x += 2 will turn it into double now because of lack of range analysis |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
253 |
} |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
254 |
} |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
255 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
256 |
// is always object means "is never initialized to undefined, and always of object type |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
257 |
setCurrentType(initialType); |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
258 |
} |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
259 |
|
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16151
diff
changeset
|
260 |
/** |
16147 | 261 |
* Copy constructor |
262 |
* |
|
263 |
* @param property source property |
|
264 |
*/ |
|
265 |
protected AccessorProperty(final AccessorProperty property) { |
|
266 |
super(property); |
|
267 |
||
268 |
this.getters = property.getters; |
|
269 |
this.primitiveGetter = property.primitiveGetter; |
|
270 |
this.primitiveSetter = property.primitiveSetter; |
|
271 |
this.objectGetter = property.objectGetter; |
|
272 |
this.objectSetter = property.objectSetter; |
|
273 |
||
274 |
setCurrentType(property.getCurrentType()); |
|
275 |
} |
|
276 |
||
277 |
private static MethodHandle bindTo(final MethodHandle mh, final Object receiver) { |
|
278 |
if (mh == null) { |
|
279 |
return null; |
|
280 |
} |
|
281 |
||
282 |
return MH.dropArguments(MH.bindTo(mh, receiver), 0, Object.class); |
|
283 |
} |
|
284 |
||
285 |
@Override |
|
286 |
protected Property copy() { |
|
287 |
return new AccessorProperty(this); |
|
288 |
} |
|
289 |
||
290 |
@Override |
|
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
17774
diff
changeset
|
291 |
public void setObjectValue(final ScriptObject self, final ScriptObject owner, final Object value, final boolean strict) { |
17770 | 292 |
if (isSpill()) { |
293 |
self.spill[getSlot()] = value; |
|
294 |
} else { |
|
295 |
try { |
|
296 |
getSetter(Object.class, self.getMap()).invokeExact((Object)self, value); |
|
297 |
} catch (final Error|RuntimeException e) { |
|
298 |
throw e; |
|
299 |
} catch (final Throwable e) { |
|
300 |
throw new RuntimeException(e); |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
@Override |
|
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
17774
diff
changeset
|
306 |
public Object getObjectValue(final ScriptObject self, final ScriptObject owner) { |
17770 | 307 |
if (isSpill()) { |
308 |
return self.spill[getSlot()]; |
|
17774
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
309 |
} |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
310 |
|
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
311 |
try { |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
312 |
return getGetter(Object.class).invokeExact((Object)self); |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
313 |
} catch (final Error|RuntimeException e) { |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
314 |
throw e; |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
315 |
} catch (final Throwable e) { |
0407501fa563
8014219: Make the run-octane harness more deterministic by not measuring elapsed time every iteration. Also got rid of most of the run logic in base.js and call benchmarks directly for the same purpose
lagergren
parents:
17770
diff
changeset
|
316 |
throw new RuntimeException(e); |
17770 | 317 |
} |
318 |
} |
|
319 |
||
320 |
@Override |
|
16147 | 321 |
public MethodHandle getGetter(final Class<?> type) { |
17770 | 322 |
if (isSpill() && objectGetter == null) { |
323 |
objectGetter = getSpillGetter(); |
|
324 |
} |
|
16147 | 325 |
final int i = getAccessorTypeIndex(type); |
326 |
if (getters[i] == null) { |
|
327 |
getters[i] = debug( |
|
328 |
MH.asType( |
|
329 |
createGetter( |
|
330 |
currentType, |
|
331 |
type, |
|
332 |
primitiveGetter, |
|
333 |
objectGetter), |
|
334 |
ACCESSOR_GETTER_TYPES[i]), |
|
335 |
currentType, |
|
336 |
type, |
|
337 |
"get"); |
|
338 |
} |
|
339 |
||
17770 | 340 |
return getters[i]; |
16147 | 341 |
} |
342 |
||
343 |
private Property getWiderProperty(final Class<?> type) { |
|
344 |
final AccessorProperty newProperty = new AccessorProperty(this); |
|
345 |
newProperty.invalidate(type); |
|
346 |
return newProperty; |
|
347 |
} |
|
348 |
||
349 |
private PropertyMap getWiderMap(final PropertyMap oldMap, final Property newProperty) { |
|
350 |
final PropertyMap newMap = oldMap.replaceProperty(this, newProperty); |
|
351 |
assert oldMap.size() > 0; |
|
352 |
assert newMap.size() == oldMap.size(); |
|
353 |
return newMap; |
|
354 |
} |
|
355 |
||
356 |
// the final three arguments are for debug printout purposes only |
|
357 |
@SuppressWarnings("unused") |
|
358 |
private static Object replaceMap(final Object sobj, final PropertyMap newMap, final String key, final Class<?> oldType, final Class<?> newType) { |
|
359 |
if (DEBUG_FIELDS) { |
|
360 |
final PropertyMap oldMap = ((ScriptObject)sobj).getMap(); |
|
361 |
info("Type change for '" + key + "' " + oldType + "=>" + newType); |
|
362 |
finest("setting map " + sobj + " from " + Debug.id(oldMap) + " to " + Debug.id(newMap) + " " + oldMap + " => " + newMap); |
|
363 |
} |
|
364 |
((ScriptObject)sobj).setMap(newMap); |
|
365 |
return sobj; |
|
366 |
} |
|
367 |
||
368 |
private MethodHandle generateSetter(final Class<?> forType, final Class<?> type) { |
|
17770 | 369 |
if (isSpill() && objectSetter == null) { |
370 |
objectSetter = getSpillSetter(); |
|
371 |
} |
|
16147 | 372 |
MethodHandle mh = createSetter(forType, type, primitiveSetter, objectSetter); |
373 |
mh = MH.asType(mh, ACCESSOR_SETTER_TYPES[getAccessorTypeIndex(type)]); //has to be the case for invokeexact to work in ScriptObject |
|
374 |
mh = debug(mh, currentType, type, "set"); |
|
375 |
return mh; |
|
376 |
} |
|
377 |
||
378 |
@Override |
|
379 |
public MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap) { |
|
380 |
final int i = getAccessorTypeIndex(type); |
|
381 |
final int ci = currentType == null ? -1 : getAccessorTypeIndex(currentType); |
|
382 |
final Class<?> forType = currentType == null ? type : currentType; |
|
383 |
||
384 |
//if we are asking for an object setter, but are still a primitive type, we might try to box it |
|
17513 | 385 |
MethodHandle mh; |
16147 | 386 |
|
387 |
if (needsInvalidator(i, ci)) { |
|
388 |
final Property newProperty = getWiderProperty(type); |
|
389 |
final PropertyMap newMap = getWiderMap(currentMap, newProperty); |
|
390 |
final MethodHandle widerSetter = newProperty.getSetter(type, newMap); |
|
391 |
final MethodHandle explodeTypeSetter = MH.filterArguments(widerSetter, 0, MH.insertArguments(REPLACE_MAP, 1, newMap, getKey(), currentType, type)); |
|
392 |
if (currentType != null && currentType.isPrimitive() && type == Object.class) { |
|
393 |
//might try a box check on this to avoid widening field to object storage |
|
17513 | 394 |
mh = createGuardBoxedPrimitiveSetter(currentType, generateSetter(currentType, currentType), explodeTypeSetter); |
395 |
} else { |
|
396 |
mh = explodeTypeSetter; |
|
16147 | 397 |
} |
17513 | 398 |
} else { |
399 |
mh = generateSetter(forType, type); |
|
16147 | 400 |
} |
401 |
||
17770 | 402 |
return mh; |
16147 | 403 |
} |
404 |
||
405 |
@Override |
|
406 |
public boolean canChangeType() { |
|
407 |
if (OBJECT_FIELDS_ONLY) { |
|
408 |
return false; |
|
409 |
} |
|
410 |
return currentType != Object.class && (isConfigurable() || isWritable()); |
|
411 |
} |
|
412 |
||
413 |
private boolean needsInvalidator(final int ti, final int fti) { |
|
414 |
return canChangeType() && ti > fti; |
|
415 |
} |
|
416 |
||
417 |
private void invalidate(final Class<?> newType) { |
|
418 |
getters = new MethodHandle[NOOF_TYPES]; |
|
419 |
setCurrentType(newType); |
|
420 |
} |
|
421 |
||
17770 | 422 |
private MethodHandle getSpillGetter() { |
423 |
final int slot = getSlot(); |
|
424 |
MethodHandle getter = slot < SPILL_CACHE_SIZE ? SPILL_ACCESSORS[slot * 2] : null; |
|
425 |
if (getter == null) { |
|
426 |
getter = MH.asType(MH.insertArguments(SPILL_ELEMENT_GETTER, 1, slot), Lookup.GET_OBJECT_TYPE); |
|
427 |
if (slot < SPILL_CACHE_SIZE) { |
|
428 |
SPILL_ACCESSORS[slot * 2] = getter; |
|
429 |
} |
|
430 |
} |
|
431 |
return getter; |
|
432 |
} |
|
433 |
||
434 |
private MethodHandle getSpillSetter() { |
|
435 |
final int slot = getSlot(); |
|
436 |
MethodHandle setter = slot < SPILL_CACHE_SIZE ? SPILL_ACCESSORS[slot * 2 + 1] : null; |
|
437 |
if (setter == null) { |
|
438 |
setter = MH.asType(MH.insertArguments(SPILL_ELEMENT_SETTER, 1, slot), Lookup.SET_OBJECT_TYPE); |
|
439 |
if (slot < SPILL_CACHE_SIZE) { |
|
440 |
SPILL_ACCESSORS[slot * 2 + 1] = setter; |
|
441 |
} |
|
442 |
} |
|
443 |
return setter; |
|
444 |
} |
|
445 |
||
16147 | 446 |
private static void finest(final String str) { |
447 |
if (DEBUG_FIELDS) { |
|
448 |
LOG.finest(str); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
private static void info(final String str) { |
|
453 |
if (DEBUG_FIELDS) { |
|
454 |
LOG.info(str); |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
private MethodHandle debug(final MethodHandle mh, final Class<?> forType, final Class<?> type, final String tag) { |
|
459 |
if (DEBUG_FIELDS) { |
|
16523
af8b30edebce
8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents:
16277
diff
changeset
|
460 |
return MethodHandleFactory.addDebugPrintout( |
16147 | 461 |
LOG, |
462 |
mh, |
|
463 |
tag + " '" + getKey() + "' (property="+ Debug.id(this) + ", forType=" + stripName(forType) + ", type=" + stripName(type) + ')'); |
|
464 |
} |
|
465 |
return mh; |
|
466 |
} |
|
467 |
||
468 |
private void setCurrentType(final Class<?> currentType) { |
|
469 |
this.currentType = currentType; |
|
470 |
} |
|
471 |
||
472 |
@Override |
|
473 |
public Class<?> getCurrentType() { |
|
474 |
return currentType; |
|
475 |
} |
|
476 |
||
477 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
17246
a1bcf4d2bff1
8013203: A collection of smaller speedups to compilation pipeline
attila
parents:
16523
diff
changeset
|
478 |
return MH.findStatic(lookup, AccessorProperty.class, name, MH.type(rtype, types)); |
16147 | 479 |
} |
480 |
||
481 |
} |