author | sundar |
Wed, 24 Jul 2013 20:28:03 +0530 | |
changeset 19097 | f544a2ea40ef |
parent 18860 | e387fde9322a |
child 19619 | 4085b74056ee |
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 |
||
17513 | 28 |
import static jdk.nashorn.internal.runtime.PropertyHashMap.EMPTY_HASHMAP; |
16147 | 29 |
|
30 |
import java.lang.invoke.SwitchPoint; |
|
31 |
import java.lang.ref.WeakReference; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.Collection; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.Iterator; |
|
36 |
import java.util.LinkedHashMap; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.NoSuchElementException; |
|
39 |
import java.util.WeakHashMap; |
|
40 |
||
41 |
/** |
|
42 |
* Map of object properties. The PropertyMap is the "template" for JavaScript object |
|
43 |
* layouts. It contains a map with prototype names as keys and {@link Property} instances |
|
44 |
* as values. A PropertyMap is typically passed to the {@link ScriptObject} constructor |
|
45 |
* to form the seed map for the ScriptObject. |
|
46 |
* <p> |
|
47 |
* All property maps are immutable. If a property is added, modified or removed, the mutator |
|
48 |
* will return a new map. |
|
49 |
*/ |
|
50 |
public final class PropertyMap implements Iterable<Object>, PropertyListener { |
|
51 |
/** Used for non extensible PropertyMaps, negative logic as the normal case is extensible. See {@link ScriptObject#preventExtensions()} */ |
|
17513 | 52 |
public static final int NOT_EXTENSIBLE = 0b0000_0001; |
16147 | 53 |
/** This mask is used to preserve certain flags when cloning the PropertyMap. Others should not be copied */ |
54 |
private static final int CLONEABLE_FLAGS_MASK = 0b0000_1111; |
|
55 |
/** Has a listener been added to this property map. This flag is not copied when cloning a map. See {@link PropertyListener} */ |
|
56 |
public static final int IS_LISTENER_ADDED = 0b0001_0000; |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
57 |
/** Is this process wide "shared" map?. This flag is not copied when cloning a map */ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
58 |
public static final int IS_SHARED = 0b0010_0000; |
17513 | 59 |
|
16147 | 60 |
/** Map status flags. */ |
61 |
private int flags; |
|
62 |
||
63 |
/** Map of properties. */ |
|
64 |
private final PropertyHashMap properties; |
|
65 |
||
17513 | 66 |
/** Number of fields in use. */ |
67 |
private int fieldCount; |
|
68 |
||
69 |
/** Number of fields available. */ |
|
70 |
private int fieldMaximum; |
|
16147 | 71 |
|
72 |
/** Length of spill in use. */ |
|
73 |
private int spillLength; |
|
74 |
||
75 |
/** {@link SwitchPoint}s for gets on inherited properties. */ |
|
76 |
private Map<String, SwitchPoint> protoGetSwitches; |
|
77 |
||
78 |
/** History of maps, used to limit map duplication. */ |
|
79 |
private HashMap<Property, PropertyMap> history; |
|
80 |
||
81 |
/** History of prototypes, used to limit map duplication. */ |
|
82 |
private WeakHashMap<ScriptObject, WeakReference<PropertyMap>> protoHistory; |
|
83 |
||
84 |
/** Cache for hashCode */ |
|
85 |
private int hashCode; |
|
86 |
||
87 |
/** |
|
88 |
* Constructor. |
|
89 |
* |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
90 |
* @param properties A {@link PropertyHashMap} with initial contents. |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
91 |
* @param fieldCount Number of fields in use. |
17513 | 92 |
* @param fieldMaximum Number of fields available. |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
93 |
* @param spillLength Number of spill slots used. |
16147 | 94 |
*/ |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
95 |
private PropertyMap(final PropertyHashMap properties, final int fieldCount, final int fieldMaximum, final int spillLength) { |
17513 | 96 |
this.properties = properties; |
97 |
this.fieldCount = fieldCount; |
|
98 |
this.fieldMaximum = fieldMaximum; |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
99 |
this.spillLength = spillLength; |
16147 | 100 |
|
101 |
if (Context.DEBUG) { |
|
102 |
count++; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/** |
|
17513 | 107 |
* Constructor. |
108 |
* |
|
109 |
* @param properties A {@link PropertyHashMap} with initial contents. |
|
110 |
*/ |
|
111 |
private PropertyMap(final PropertyHashMap properties) { |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
112 |
this(properties, 0, 0, 0); |
17513 | 113 |
} |
114 |
||
115 |
/** |
|
16147 | 116 |
* Cloning constructor. |
117 |
* |
|
118 |
* @param propertyMap Existing property map. |
|
119 |
* @param properties A {@link PropertyHashMap} with a new set of properties. |
|
120 |
*/ |
|
121 |
private PropertyMap(final PropertyMap propertyMap, final PropertyHashMap properties) { |
|
17513 | 122 |
this.properties = properties; |
123 |
this.flags = propertyMap.getClonedFlags(); |
|
124 |
this.spillLength = propertyMap.spillLength; |
|
125 |
this.fieldCount = propertyMap.fieldCount; |
|
126 |
this.fieldMaximum = propertyMap.fieldMaximum; |
|
16147 | 127 |
|
128 |
if (Context.DEBUG) { |
|
129 |
count++; |
|
130 |
clonedCount++; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
/** |
|
17513 | 135 |
* Cloning constructor. |
136 |
* |
|
137 |
* @param propertyMap Existing property map. |
|
138 |
*/ |
|
139 |
private PropertyMap(final PropertyMap propertyMap) { |
|
140 |
this(propertyMap, propertyMap.properties); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
144 |
* Duplicates this PropertyMap instance. This is used to duplicate 'shared' |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
145 |
* maps {@link PropertyMap} used as process wide singletons. Shared maps are |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
146 |
* duplicated for every global scope object. That way listeners, proto and property |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
147 |
* histories are scoped within a global scope. |
16147 | 148 |
* |
149 |
* @return Duplicated {@link PropertyMap}. |
|
150 |
*/ |
|
151 |
public PropertyMap duplicate() { |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
152 |
if (Context.DEBUG) { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
153 |
duplicatedCount++; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
154 |
} |
17513 | 155 |
return new PropertyMap(this.properties); |
16147 | 156 |
} |
157 |
||
158 |
/** |
|
159 |
* Public property map allocator. |
|
160 |
* |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
161 |
* @param properties Collection of initial properties. |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
162 |
* @param fieldCount Number of fields in use. |
17513 | 163 |
* @param fieldMaximum Number of fields available. |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
164 |
* @param spillLength Number of used spill slots. |
16147 | 165 |
* @return New {@link PropertyMap}. |
166 |
*/ |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
167 |
public static PropertyMap newMap(final Collection<Property> properties, final int fieldCount, final int fieldMaximum, final int spillLength) { |
17513 | 168 |
PropertyHashMap newProperties = EMPTY_HASHMAP.immutableAdd(properties); |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
169 |
return new PropertyMap(newProperties, fieldCount, fieldMaximum, spillLength); |
16147 | 170 |
} |
171 |
||
172 |
/** |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
173 |
* Public property map allocator. Used by nasgen generated code. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
174 |
* @param properties Collection of initial properties. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
175 |
* @return New {@link PropertyMap}. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
176 |
*/ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
177 |
public static PropertyMap newMap(final Collection<Property> properties) { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
178 |
return (properties == null || properties.isEmpty())? newMap() : newMap(properties, 0, 0, 0); |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
179 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
180 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
181 |
/** |
16147 | 182 |
* Return a sharable empty map. |
183 |
* |
|
184 |
* @return New empty {@link PropertyMap}. |
|
185 |
*/ |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
18618
diff
changeset
|
186 |
public static PropertyMap newMap() { |
17513 | 187 |
return new PropertyMap(EMPTY_HASHMAP); |
16147 | 188 |
} |
189 |
||
190 |
/** |
|
191 |
* Return number of properties in the map. |
|
192 |
* |
|
193 |
* @return Number of properties. |
|
194 |
*/ |
|
195 |
public int size() { |
|
196 |
return properties.size(); |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Return a SwitchPoint used to track changes of a property in a prototype. |
|
201 |
* |
|
17513 | 202 |
* @param proto Object prototype. |
203 |
* @param key {@link Property} key. |
|
16147 | 204 |
* |
205 |
* @return A shared {@link SwitchPoint} for the property. |
|
206 |
*/ |
|
17513 | 207 |
public SwitchPoint getProtoGetSwitchPoint(final ScriptObject proto, final String key) { |
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
208 |
assert !isShared() : "proto SwitchPoint from a shared PropertyMap"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
209 |
|
16147 | 210 |
if (proto == null) { |
211 |
return null; |
|
212 |
} |
|
213 |
||
214 |
if (protoGetSwitches == null) { |
|
215 |
protoGetSwitches = new HashMap<>(); |
|
216 |
if (! isListenerAdded()) { |
|
217 |
proto.addPropertyListener(this); |
|
218 |
setIsListenerAdded(); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
if (protoGetSwitches.containsKey(key)) { |
|
223 |
return protoGetSwitches.get(key); |
|
224 |
} |
|
225 |
||
226 |
final SwitchPoint switchPoint = new SwitchPoint(); |
|
227 |
protoGetSwitches.put(key, switchPoint); |
|
228 |
||
229 |
return switchPoint; |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* Indicate that a prototype property hash changed. |
|
234 |
* |
|
235 |
* @param property {@link Property} to invalidate. |
|
236 |
*/ |
|
237 |
private void invalidateProtoGetSwitchPoint(final Property property) { |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
238 |
assert !isShared() : "proto invalidation on a shared PropertyMap"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
239 |
|
16147 | 240 |
if (protoGetSwitches != null) { |
241 |
final String key = property.getKey(); |
|
242 |
final SwitchPoint sp = protoGetSwitches.get(key); |
|
243 |
if (sp != null) { |
|
244 |
protoGetSwitches.put(key, new SwitchPoint()); |
|
245 |
if (Context.DEBUG) { |
|
246 |
protoInvalidations++; |
|
247 |
} |
|
248 |
SwitchPoint.invalidateAll(new SwitchPoint[] { sp }); |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Add a property to the map, re-binding its getters and setters, |
|
255 |
* if available, to a given receiver. This is typically the global scope. See |
|
256 |
* {@link ScriptObject#addBoundProperties(ScriptObject)} |
|
257 |
* |
|
258 |
* @param property {@link Property} being added. |
|
259 |
* @param bindTo Object to bind to. |
|
260 |
* |
|
261 |
* @return New {@link PropertyMap} with {@link Property} added. |
|
262 |
*/ |
|
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
18855
diff
changeset
|
263 |
PropertyMap addPropertyBind(final AccessorProperty property, final Object bindTo) { |
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
264 |
return addProperty(new AccessorProperty(property, bindTo)); |
16147 | 265 |
} |
266 |
||
267 |
/** |
|
268 |
* Add a property to the map. Cloning or using an existing map if available. |
|
269 |
* |
|
270 |
* @param property {@link Property} being added. |
|
271 |
* |
|
272 |
* @return New {@link PropertyMap} with {@link Property} added. |
|
273 |
*/ |
|
18617
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
274 |
public PropertyMap addProperty(final Property property) { |
16147 | 275 |
PropertyMap newMap = checkHistory(property); |
276 |
||
277 |
if (newMap == null) { |
|
278 |
final PropertyHashMap newProperties = properties.immutableAdd(property); |
|
279 |
newMap = new PropertyMap(this, newProperties); |
|
280 |
addToHistory(property, newMap); |
|
17513 | 281 |
|
282 |
if(!property.isSpill()) { |
|
283 |
newMap.fieldCount = Math.max(newMap.fieldCount, property.getSlot() + 1); |
|
284 |
} |
|
285 |
||
16147 | 286 |
newMap.spillLength += property.getSpillCount(); |
287 |
} |
|
288 |
||
289 |
return newMap; |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Remove a property from a map. Cloning or using an existing map if available. |
|
294 |
* |
|
295 |
* @param property {@link Property} being removed. |
|
296 |
* |
|
297 |
* @return New {@link PropertyMap} with {@link Property} removed or {@code null} if not found. |
|
298 |
*/ |
|
16228 | 299 |
public PropertyMap deleteProperty(final Property property) { |
16147 | 300 |
PropertyMap newMap = checkHistory(property); |
301 |
final String key = property.getKey(); |
|
302 |
||
303 |
if (newMap == null && properties.containsKey(key)) { |
|
304 |
final PropertyHashMap newProperties = properties.immutableRemove(key); |
|
305 |
newMap = new PropertyMap(this, newProperties); |
|
306 |
addToHistory(property, newMap); |
|
307 |
} |
|
308 |
||
309 |
return newMap; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Replace an existing property with a new one. |
|
314 |
* |
|
315 |
* @param oldProperty Property to replace. |
|
316 |
* @param newProperty New {@link Property}. |
|
317 |
* |
|
318 |
* @return New {@link PropertyMap} with {@link Property} replaced. |
|
319 |
*/ |
|
320 |
PropertyMap replaceProperty(final Property oldProperty, final Property newProperty) { |
|
321 |
// Add replaces existing property. |
|
322 |
final PropertyHashMap newProperties = properties.immutableAdd(newProperty); |
|
323 |
final PropertyMap newMap = new PropertyMap(this, newProperties); |
|
324 |
||
325 |
/* |
|
326 |
* See ScriptObject.modifyProperty and ScriptObject.setUserAccessors methods. |
|
327 |
* |
|
328 |
* This replaceProperty method is called only for the following three cases: |
|
329 |
* |
|
330 |
* 1. To change flags OR TYPE of an old (cloned) property. We use the same spill slots. |
|
331 |
* 2. To change one UserAccessor property with another - user getter or setter changed via |
|
332 |
* Object.defineProperty function. Again, same spill slots are re-used. |
|
333 |
* 3. Via ScriptObject.setUserAccessors method to set user getter and setter functions |
|
334 |
* replacing the dummy AccessorProperty with null method handles (added during map init). |
|
335 |
* |
|
336 |
* In case (1) and case(2), the property type of old and new property is same. For case (3), |
|
337 |
* the old property is an AccessorProperty and the new one is a UserAccessorProperty property. |
|
338 |
*/ |
|
339 |
||
340 |
final boolean sameType = (oldProperty.getClass() == newProperty.getClass()); |
|
341 |
assert sameType || |
|
342 |
(oldProperty instanceof AccessorProperty && |
|
343 |
newProperty instanceof UserAccessorProperty) : "arbitrary replaceProperty attempted"; |
|
344 |
||
345 |
newMap.flags = getClonedFlags(); |
|
346 |
||
347 |
/* |
|
348 |
* spillLength remains same in case (1) and (2) because of slot reuse. Only for case (3), we need |
|
349 |
* to add spill count of the newly added UserAccessorProperty property. |
|
350 |
*/ |
|
351 |
newMap.spillLength = spillLength + (sameType? 0 : newProperty.getSpillCount()); |
|
352 |
return newMap; |
|
353 |
} |
|
354 |
||
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18860
diff
changeset
|
355 |
/** |
18617
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
356 |
* Make a new UserAccessorProperty property. getter and setter functions are stored in |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
357 |
* this ScriptObject and slot values are used in property object. Note that slots |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
358 |
* are assigned speculatively and should be added to map before adding other |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
359 |
* properties. |
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18860
diff
changeset
|
360 |
* |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18860
diff
changeset
|
361 |
* @param key the property name |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18860
diff
changeset
|
362 |
* @param propertyFlags attribute flags of the property |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18860
diff
changeset
|
363 |
* @return the newly created UserAccessorProperty |
18617
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
364 |
*/ |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
365 |
public UserAccessorProperty newUserAccessors(final String key, final int propertyFlags) { |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
366 |
int oldSpillLength = spillLength; |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
367 |
|
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
368 |
final int getterSlot = oldSpillLength++; |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
369 |
final int setterSlot = oldSpillLength++; |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
370 |
|
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
371 |
return new UserAccessorProperty(key, propertyFlags, getterSlot, setterSlot); |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
372 |
} |
f6fe338f62c3
8008458: Strict functions dont share property map
jlaskey
parents:
18325
diff
changeset
|
373 |
|
16147 | 374 |
/** |
375 |
* Find a property in the map. |
|
376 |
* |
|
377 |
* @param key Key to search for. |
|
378 |
* |
|
379 |
* @return {@link Property} matching key. |
|
380 |
*/ |
|
381 |
public Property findProperty(final String key) { |
|
382 |
return properties.find(key); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Adds all map properties from another map. |
|
387 |
* |
|
388 |
* @param other The source of properties. |
|
389 |
* |
|
390 |
* @return New {@link PropertyMap} with added properties. |
|
391 |
*/ |
|
392 |
public PropertyMap addAll(final PropertyMap other) { |
|
16216
46ed48fd84d3
8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
16201
diff
changeset
|
393 |
assert this != other : "adding property map to itself"; |
16147 | 394 |
final Property[] otherProperties = other.properties.getProperties(); |
395 |
final PropertyHashMap newProperties = properties.immutableAdd(otherProperties); |
|
396 |
||
397 |
final PropertyMap newMap = new PropertyMap(this, newProperties); |
|
398 |
for (final Property property : otherProperties) { |
|
399 |
newMap.spillLength += property.getSpillCount(); |
|
400 |
} |
|
401 |
||
402 |
return newMap; |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Return an array of all properties. |
|
407 |
* |
|
408 |
* @return Properties as an array. |
|
409 |
*/ |
|
410 |
public Property[] getProperties() { |
|
411 |
return properties.getProperties(); |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Prevents the map from having additional properties. |
|
416 |
* |
|
417 |
* @return New map with {@link #NOT_EXTENSIBLE} flag set. |
|
418 |
*/ |
|
419 |
PropertyMap preventExtensions() { |
|
17513 | 420 |
final PropertyMap newMap = new PropertyMap(this); |
16147 | 421 |
newMap.flags |= NOT_EXTENSIBLE; |
422 |
return newMap; |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* Prevents properties in map from being modified. |
|
427 |
* |
|
16154
de44634fa4ec
8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents:
16152
diff
changeset
|
428 |
* @return New map with {@link #NOT_EXTENSIBLE} flag set and properties with |
de44634fa4ec
8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents:
16152
diff
changeset
|
429 |
* {@link Property#NOT_CONFIGURABLE} set. |
16147 | 430 |
*/ |
431 |
PropertyMap seal() { |
|
17513 | 432 |
PropertyHashMap newProperties = EMPTY_HASHMAP; |
16147 | 433 |
|
434 |
for (final Property oldProperty : properties.getProperties()) { |
|
435 |
newProperties = newProperties.immutableAdd(oldProperty.addFlags(Property.NOT_CONFIGURABLE)); |
|
436 |
} |
|
437 |
||
438 |
final PropertyMap newMap = new PropertyMap(this, newProperties); |
|
439 |
newMap.flags |= NOT_EXTENSIBLE; |
|
440 |
||
441 |
return newMap; |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Prevents properties in map from being modified or written to. |
|
446 |
* |
|
447 |
* @return New map with {@link #NOT_EXTENSIBLE} flag set and properties with |
|
16154
de44634fa4ec
8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents:
16152
diff
changeset
|
448 |
* {@link Property#NOT_CONFIGURABLE} and {@link Property#NOT_WRITABLE} set. |
16147 | 449 |
*/ |
450 |
PropertyMap freeze() { |
|
17513 | 451 |
PropertyHashMap newProperties = EMPTY_HASHMAP; |
16147 | 452 |
|
453 |
for (Property oldProperty : properties.getProperties()) { |
|
454 |
int propertyFlags = Property.NOT_CONFIGURABLE; |
|
455 |
||
456 |
if (!(oldProperty instanceof UserAccessorProperty)) { |
|
457 |
propertyFlags |= Property.NOT_WRITABLE; |
|
458 |
} |
|
459 |
||
460 |
newProperties = newProperties.immutableAdd(oldProperty.addFlags(propertyFlags)); |
|
461 |
} |
|
462 |
||
463 |
final PropertyMap newMap = new PropertyMap(this, newProperties); |
|
464 |
newMap.flags |= NOT_EXTENSIBLE; |
|
465 |
||
466 |
return newMap; |
|
467 |
} |
|
468 |
||
469 |
/** |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
470 |
* Make this property map 'shared' one. Shared property map instances are |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
471 |
* process wide singleton objects. A shaped map should never be added as a listener |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
472 |
* to a proto object. Nor it should have history or proto history. A shared map |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
473 |
* is just a template that is meant to be duplicated before use. All nasgen initialized |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
474 |
* property maps are shared. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
475 |
* |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
476 |
* @return this map after making it as shared |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
477 |
*/ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
478 |
public PropertyMap setIsShared() { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
479 |
assert !isListenerAdded() : "making PropertyMap shared after listener added"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
480 |
assert protoHistory == null : "making PropertyMap shared after associating a proto with it"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
481 |
if (Context.DEBUG) { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
482 |
sharedCount++; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
483 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
484 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
485 |
flags |= IS_SHARED; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
486 |
// clear any history on this PropertyMap, won't be used. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
487 |
history = null; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
488 |
return this; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
489 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
490 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
491 |
/** |
16147 | 492 |
* Check for any configurable properties. |
493 |
* |
|
494 |
* @return {@code true} if any configurable. |
|
495 |
*/ |
|
496 |
private boolean anyConfigurable() { |
|
497 |
for (final Property property : properties.getProperties()) { |
|
498 |
if (property.isConfigurable()) { |
|
499 |
return true; |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
return false; |
|
504 |
} |
|
505 |
||
506 |
/** |
|
507 |
* Check if all properties are frozen. |
|
508 |
* |
|
509 |
* @return {@code true} if all are frozen. |
|
510 |
*/ |
|
511 |
private boolean allFrozen() { |
|
512 |
for (final Property property : properties.getProperties()) { |
|
513 |
// check if it is a data descriptor |
|
514 |
if (!(property instanceof UserAccessorProperty)) { |
|
515 |
if (property.isWritable()) { |
|
516 |
return false; |
|
517 |
} |
|
518 |
} |
|
519 |
if (property.isConfigurable()) { |
|
520 |
return false; |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
return true; |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Check prototype history for an existing property map with specified prototype. |
|
529 |
* |
|
530 |
* @param newProto New prototype object. |
|
531 |
* |
|
532 |
* @return Existing {@link PropertyMap} or {@code null} if not found. |
|
533 |
*/ |
|
534 |
private PropertyMap checkProtoHistory(final ScriptObject newProto) { |
|
535 |
final PropertyMap cachedMap; |
|
536 |
if (protoHistory != null) { |
|
537 |
final WeakReference<PropertyMap> weakMap = protoHistory.get(newProto); |
|
538 |
cachedMap = (weakMap != null ? weakMap.get() : null); |
|
539 |
} else { |
|
540 |
cachedMap = null; |
|
541 |
} |
|
542 |
||
543 |
if (Context.DEBUG && cachedMap != null) { |
|
544 |
protoHistoryHit++; |
|
545 |
} |
|
546 |
||
547 |
return cachedMap; |
|
548 |
} |
|
549 |
||
550 |
/** |
|
551 |
* Add a map to the prototype history. |
|
552 |
* |
|
553 |
* @param newProto Prototype to add (key.) |
|
554 |
* @param newMap {@link PropertyMap} associated with prototype. |
|
555 |
*/ |
|
556 |
private void addToProtoHistory(final ScriptObject newProto, final PropertyMap newMap) { |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
557 |
assert !isShared() : "proto history modified on a shared PropertyMap"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
558 |
|
16759
ecf99910fc31
8011219: Regression with recent PropertyMap history changes
hannesw
parents:
16758
diff
changeset
|
559 |
if (protoHistory == null) { |
ecf99910fc31
8011219: Regression with recent PropertyMap history changes
hannesw
parents:
16758
diff
changeset
|
560 |
protoHistory = new WeakHashMap<>(); |
ecf99910fc31
8011219: Regression with recent PropertyMap history changes
hannesw
parents:
16758
diff
changeset
|
561 |
} |
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16275
diff
changeset
|
562 |
|
16759
ecf99910fc31
8011219: Regression with recent PropertyMap history changes
hannesw
parents:
16758
diff
changeset
|
563 |
protoHistory.put(newProto, new WeakReference<>(newMap)); |
16147 | 564 |
} |
565 |
||
566 |
/** |
|
567 |
* Track the modification of the map. |
|
568 |
* |
|
569 |
* @param property Mapping property. |
|
570 |
* @param newMap Modified {@link PropertyMap}. |
|
571 |
*/ |
|
572 |
private void addToHistory(final Property property, final PropertyMap newMap) { |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
573 |
assert !isShared() : "history modified on a shared PropertyMap"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
574 |
|
16774
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
575 |
if (!properties.isEmpty()) { |
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
576 |
if (history == null) { |
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
577 |
history = new LinkedHashMap<>(); |
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
578 |
} |
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
579 |
|
745fe7d2536d
8011540: PropertyMap histories should not begin with empty map
jlaskey
parents:
16759
diff
changeset
|
580 |
history.put(property, newMap); |
16759
ecf99910fc31
8011219: Regression with recent PropertyMap history changes
hannesw
parents:
16758
diff
changeset
|
581 |
} |
16147 | 582 |
} |
583 |
||
584 |
/** |
|
585 |
* Check the history for a map that already has the given property added. |
|
586 |
* |
|
587 |
* @param property {@link Property} to add. |
|
588 |
* |
|
589 |
* @return Existing map or {@code null} if not found. |
|
590 |
*/ |
|
591 |
private PropertyMap checkHistory(final Property property) { |
|
592 |
if (history != null) { |
|
593 |
PropertyMap historicMap = history.get(property); |
|
594 |
||
595 |
if (historicMap != null) { |
|
596 |
if (Context.DEBUG) { |
|
597 |
historyHit++; |
|
598 |
} |
|
599 |
||
600 |
return historicMap; |
|
601 |
} |
|
602 |
} |
|
603 |
||
604 |
return null; |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* Calculate the hash code for the map. |
|
609 |
* |
|
610 |
* @return Computed hash code. |
|
611 |
*/ |
|
612 |
private int computeHashCode() { |
|
17513 | 613 |
int hash = 0; |
16147 | 614 |
|
615 |
for (final Property property : getProperties()) { |
|
616 |
hash = hash << 7 ^ hash >> 7; |
|
617 |
hash ^= property.hashCode(); |
|
618 |
} |
|
619 |
||
620 |
return hash; |
|
621 |
} |
|
622 |
||
623 |
@Override |
|
624 |
public int hashCode() { |
|
18325 | 625 |
if (hashCode == 0 && !properties.isEmpty()) { |
626 |
hashCode = computeHashCode(); |
|
627 |
} |
|
16147 | 628 |
return hashCode; |
629 |
} |
|
630 |
||
631 |
@Override |
|
632 |
public boolean equals(final Object other) { |
|
633 |
if (!(other instanceof PropertyMap)) { |
|
634 |
return false; |
|
635 |
} |
|
636 |
||
637 |
final PropertyMap otherMap = (PropertyMap)other; |
|
638 |
||
17513 | 639 |
if (properties.size() != otherMap.properties.size()) { |
16147 | 640 |
return false; |
641 |
} |
|
642 |
||
643 |
final Iterator<Property> iter = properties.values().iterator(); |
|
644 |
final Iterator<Property> otherIter = otherMap.properties.values().iterator(); |
|
645 |
||
646 |
while (iter.hasNext() && otherIter.hasNext()) { |
|
647 |
if (!iter.next().equals(otherIter.next())) { |
|
648 |
return false; |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
return true; |
|
653 |
} |
|
654 |
||
655 |
@Override |
|
656 |
public String toString() { |
|
657 |
final StringBuilder sb = new StringBuilder(); |
|
658 |
||
659 |
sb.append(" ["); |
|
660 |
boolean isFirst = true; |
|
661 |
||
662 |
for (final Property property : properties.values()) { |
|
663 |
if (!isFirst) { |
|
664 |
sb.append(", "); |
|
665 |
} |
|
666 |
||
667 |
isFirst = false; |
|
668 |
||
669 |
sb.append(ScriptRuntime.safeToString(property.getKey())); |
|
670 |
final Class<?> ctype = property.getCurrentType(); |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
671 |
sb.append(" <"). |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
672 |
append(property.getClass().getSimpleName()). |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
673 |
append(':'). |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
674 |
append(ctype == null ? |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
675 |
"undefined" : |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
676 |
ctype.getSimpleName()). |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16154
diff
changeset
|
677 |
append('>'); |
16147 | 678 |
} |
679 |
||
680 |
sb.append(']'); |
|
681 |
||
682 |
return sb.toString(); |
|
683 |
} |
|
684 |
||
685 |
@Override |
|
686 |
public Iterator<Object> iterator() { |
|
687 |
return new PropertyMapIterator(this); |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Check whether a {@link PropertyListener} has been added to this map. |
|
692 |
* |
|
693 |
* @return {@code true} if {@link PropertyListener} exists |
|
694 |
*/ |
|
695 |
public boolean isListenerAdded() { |
|
696 |
return (flags & IS_LISTENER_ADDED) != 0; |
|
697 |
} |
|
698 |
||
699 |
/** |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
700 |
* Check if this map shared or not. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
701 |
* |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
702 |
* @return true if this map is shared. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
703 |
*/ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
704 |
public boolean isShared() { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
705 |
return (flags & IS_SHARED) != 0; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
706 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
707 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
708 |
/** |
16147 | 709 |
* Test to see if {@link PropertyMap} is extensible. |
710 |
* |
|
711 |
* @return {@code true} if {@link PropertyMap} can be added to. |
|
712 |
*/ |
|
713 |
boolean isExtensible() { |
|
714 |
return (flags & NOT_EXTENSIBLE) == 0; |
|
715 |
} |
|
716 |
||
717 |
/** |
|
718 |
* Test to see if {@link PropertyMap} is not extensible or any properties |
|
719 |
* can not be modified. |
|
720 |
* |
|
721 |
* @return {@code true} if {@link PropertyMap} is sealed. |
|
722 |
*/ |
|
723 |
boolean isSealed() { |
|
724 |
return !isExtensible() && !anyConfigurable(); |
|
725 |
} |
|
726 |
||
727 |
/** |
|
728 |
* Test to see if {@link PropertyMap} is not extensible or all properties |
|
729 |
* can not be modified. |
|
730 |
* |
|
731 |
* @return {@code true} if {@link PropertyMap} is frozen. |
|
732 |
*/ |
|
733 |
boolean isFrozen() { |
|
734 |
return !isExtensible() && allFrozen(); |
|
735 |
} |
|
17513 | 736 |
/** |
737 |
* Get the number of fields allocated for this {@link PropertyMap}. |
|
738 |
* |
|
739 |
* @return Number of fields allocated. |
|
740 |
*/ |
|
741 |
int getFieldCount() { |
|
742 |
return fieldCount; |
|
743 |
} |
|
744 |
/** |
|
745 |
* Get maximum number of fields available for this {@link PropertyMap}. |
|
746 |
* |
|
747 |
* @return Number of fields available. |
|
748 |
*/ |
|
749 |
int getFieldMaximum() { |
|
750 |
return fieldMaximum; |
|
751 |
} |
|
16147 | 752 |
|
753 |
/** |
|
754 |
* Get length of spill area associated with this {@link PropertyMap}. |
|
755 |
* |
|
756 |
* @return Length of spill area. |
|
757 |
*/ |
|
758 |
int getSpillLength() { |
|
759 |
return spillLength; |
|
760 |
} |
|
761 |
||
762 |
/** |
|
17513 | 763 |
* Change the prototype of objects associated with this {@link PropertyMap}. |
16147 | 764 |
* |
17513 | 765 |
* @param oldProto Current prototype object. |
766 |
* @param newProto New prototype object to replace oldProto. |
|
16147 | 767 |
* |
768 |
* @return New {@link PropertyMap} with prototype changed. |
|
769 |
*/ |
|
17513 | 770 |
PropertyMap changeProto(final ScriptObject oldProto, final ScriptObject newProto) { |
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
771 |
assert !isShared() : "proto associated with a shared PropertyMap"; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
772 |
|
17522
0f5e4480fbb6
8013874: Function argument's prototype seem cached and wrongly reused
hannesw
parents:
17513
diff
changeset
|
773 |
if (oldProto == newProto) { |
16147 | 774 |
return this; |
775 |
} |
|
776 |
||
777 |
final PropertyMap nextMap = checkProtoHistory(newProto); |
|
778 |
if (nextMap != null) { |
|
779 |
return nextMap; |
|
780 |
} |
|
781 |
||
782 |
if (Context.DEBUG) { |
|
783 |
incrementSetProtoNewMapCount(); |
|
784 |
} |
|
17513 | 785 |
|
786 |
final PropertyMap newMap = new PropertyMap(this); |
|
16147 | 787 |
addToProtoHistory(newProto, newMap); |
788 |
||
789 |
return newMap; |
|
790 |
} |
|
791 |
||
792 |
/** |
|
793 |
* Indicate that the map has listeners. |
|
794 |
*/ |
|
795 |
private void setIsListenerAdded() { |
|
796 |
flags |= IS_LISTENER_ADDED; |
|
797 |
} |
|
798 |
||
799 |
/** |
|
800 |
* Return only the flags that should be copied during cloning. |
|
801 |
* |
|
802 |
* @return Subset of flags that should be copied. |
|
803 |
*/ |
|
804 |
private int getClonedFlags() { |
|
805 |
return flags & CLONEABLE_FLAGS_MASK; |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* {@link PropertyMap} iterator. |
|
810 |
*/ |
|
811 |
private static class PropertyMapIterator implements Iterator<Object> { |
|
812 |
/** Property iterator. */ |
|
813 |
final Iterator<Property> iter; |
|
814 |
||
815 |
/** Current Property. */ |
|
816 |
Property property; |
|
817 |
||
818 |
/** |
|
819 |
* Constructor. |
|
820 |
* |
|
821 |
* @param propertyMap {@link PropertyMap} to iterate over. |
|
822 |
*/ |
|
823 |
PropertyMapIterator(final PropertyMap propertyMap) { |
|
824 |
iter = Arrays.asList(propertyMap.properties.getProperties()).iterator(); |
|
825 |
property = iter.hasNext() ? iter.next() : null; |
|
826 |
skipNotEnumerable(); |
|
827 |
} |
|
828 |
||
829 |
/** |
|
830 |
* Ignore properties that are not enumerable. |
|
831 |
*/ |
|
832 |
private void skipNotEnumerable() { |
|
833 |
while (property != null && !property.isEnumerable()) { |
|
834 |
property = iter.hasNext() ? iter.next() : null; |
|
835 |
} |
|
836 |
} |
|
837 |
||
838 |
@Override |
|
839 |
public boolean hasNext() { |
|
840 |
return property != null; |
|
841 |
} |
|
842 |
||
843 |
@Override |
|
844 |
public Object next() { |
|
845 |
if (property == null) { |
|
846 |
throw new NoSuchElementException(); |
|
847 |
} |
|
848 |
||
849 |
final Object key = property.getKey(); |
|
850 |
property = iter.next(); |
|
851 |
skipNotEnumerable(); |
|
852 |
||
853 |
return key; |
|
854 |
} |
|
855 |
||
856 |
@Override |
|
857 |
public void remove() { |
|
858 |
throw new UnsupportedOperationException(); |
|
859 |
} |
|
860 |
} |
|
861 |
||
862 |
/* |
|
863 |
* PropertyListener implementation. |
|
864 |
*/ |
|
865 |
||
866 |
@Override |
|
867 |
public void propertyAdded(final ScriptObject object, final Property prop) { |
|
868 |
invalidateProtoGetSwitchPoint(prop); |
|
869 |
} |
|
870 |
||
871 |
@Override |
|
872 |
public void propertyDeleted(final ScriptObject object, final Property prop) { |
|
873 |
invalidateProtoGetSwitchPoint(prop); |
|
874 |
} |
|
875 |
||
876 |
@Override |
|
877 |
public void propertyModified(final ScriptObject object, final Property oldProp, final Property newProp) { |
|
878 |
invalidateProtoGetSwitchPoint(oldProp); |
|
879 |
} |
|
880 |
||
881 |
/* |
|
882 |
* Debugging and statistics. |
|
883 |
*/ |
|
884 |
||
885 |
// counters updated only in debug mode |
|
886 |
private static int count; |
|
887 |
private static int clonedCount; |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
888 |
private static int sharedCount; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
889 |
private static int duplicatedCount; |
16147 | 890 |
private static int historyHit; |
891 |
private static int protoInvalidations; |
|
892 |
private static int protoHistoryHit; |
|
893 |
private static int setProtoNewMapCount; |
|
894 |
||
895 |
/** |
|
896 |
* @return Total number of maps. |
|
897 |
*/ |
|
898 |
public static int getCount() { |
|
899 |
return count; |
|
900 |
} |
|
901 |
||
902 |
/** |
|
903 |
* @return The number of maps that were cloned. |
|
904 |
*/ |
|
905 |
public static int getClonedCount() { |
|
906 |
return clonedCount; |
|
907 |
} |
|
908 |
||
909 |
/** |
|
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
910 |
* @return The number of maps that are shared. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
911 |
*/ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
912 |
public static int getSharedCount() { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
913 |
return sharedCount; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
914 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
915 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
916 |
/** |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
917 |
* @return The number of maps that are duplicated. |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
918 |
*/ |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
919 |
public static int getDuplicatedCount() { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
920 |
return duplicatedCount; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
921 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
922 |
|
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18852
diff
changeset
|
923 |
/** |
16147 | 924 |
* @return The number of times history was successfully used. |
925 |
*/ |
|
926 |
public static int getHistoryHit() { |
|
927 |
return historyHit; |
|
928 |
} |
|
929 |
||
930 |
/** |
|
931 |
* @return The number of times prototype changes caused invalidation. |
|
932 |
*/ |
|
933 |
public static int getProtoInvalidations() { |
|
934 |
return protoInvalidations; |
|
935 |
} |
|
936 |
||
937 |
/** |
|
938 |
* @return The number of times proto history was successfully used. |
|
939 |
*/ |
|
940 |
public static int getProtoHistoryHit() { |
|
941 |
return protoHistoryHit; |
|
942 |
} |
|
943 |
||
944 |
/** |
|
945 |
* @return The number of times prototypes were modified. |
|
946 |
*/ |
|
947 |
public static int getSetProtoNewMapCount() { |
|
948 |
return setProtoNewMapCount; |
|
949 |
} |
|
950 |
||
951 |
/** |
|
952 |
* Increment the prototype set count. |
|
953 |
*/ |
|
954 |
private static void incrementSetProtoNewMapCount() { |
|
955 |
setProtoNewMapCount++; |
|
956 |
} |
|
957 |
} |