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 |
|
|
28 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE;
|
|
29 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE;
|
|
30 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
|
|
31 |
|
|
32 |
import java.lang.invoke.MethodHandle;
|
|
33 |
import java.util.Objects;
|
|
34 |
import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator;
|
|
35 |
import jdk.nashorn.internal.codegen.types.Type;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This is the abstract superclass representing a JavaScript Property.
|
|
39 |
* The {@link PropertyMap} map links keys to properties, and consequently
|
|
40 |
* instances of this class make up the values in the PropertyMap
|
|
41 |
*
|
|
42 |
* @see PropertyMap
|
|
43 |
* @see AccessorProperty
|
|
44 |
* @see SpillProperty
|
|
45 |
* @see UserAccessorProperty
|
|
46 |
*/
|
|
47 |
public abstract class Property {
|
|
48 |
/*
|
|
49 |
* ECMA 8.6.1 Property Attributes
|
|
50 |
*
|
|
51 |
* We use negative flags because most properties are expected to
|
|
52 |
* be 'writable', 'configurable' and 'enumerable'. With negative flags,
|
|
53 |
* we can use leave flag byte initialized with (the default) zero value.
|
|
54 |
*/
|
|
55 |
|
|
56 |
/** ECMA 8.6.1 - Is this property not writable? */
|
|
57 |
public static final int NOT_WRITABLE = 0b0000_0000_0001;
|
|
58 |
|
|
59 |
/** ECMA 8.6.1 - Is this property not enumerable? */
|
|
60 |
public static final int NOT_ENUMERABLE = 0b0000_0000_0010;
|
|
61 |
|
|
62 |
/** ECMA 8.6.1 - Is this property not configurable? */
|
|
63 |
public static final int NOT_CONFIGURABLE = 0b0000_0000_0100;
|
|
64 |
|
|
65 |
private static final int MODIFY_MASK = 0b0000_0000_1111;
|
|
66 |
|
|
67 |
/** Is this a spill property? See {@link SpillProperty} */
|
|
68 |
public static final int IS_SPILL = 0b0000_0001_0000;
|
|
69 |
|
|
70 |
/** Is this a function parameter ? */
|
|
71 |
public static final int IS_PARAMETER = 0b0000_0010_0000;
|
|
72 |
|
|
73 |
/** Is this property always represented as an Object? See {@link ObjectClassGenerator} and dual fields flag. */
|
|
74 |
public static final int IS_ALWAYS_OBJECT = 0b0000_0100_0000;
|
|
75 |
|
|
76 |
/** Can this property be primitive? */
|
|
77 |
public static final int CAN_BE_PRIMITIVE = 0b0000_1000_0000;
|
|
78 |
|
|
79 |
/** Can this property be undefined? */
|
|
80 |
public static final int CAN_BE_UNDEFINED = 0b0001_0000_0000;
|
|
81 |
|
|
82 |
/** Property key. */
|
|
83 |
private final String key;
|
|
84 |
|
|
85 |
/** Property flags. */
|
|
86 |
protected int flags;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Constructor
|
|
90 |
*
|
|
91 |
* @param key property key
|
|
92 |
* @param flags property flags
|
|
93 |
*/
|
|
94 |
public Property(final String key, final int flags) {
|
|
95 |
this.key = key;
|
|
96 |
this.flags = flags;
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Copy constructor
|
|
101 |
*
|
|
102 |
* @param property source property
|
|
103 |
*/
|
|
104 |
protected Property(final Property property) {
|
|
105 |
this.key = property.key;
|
|
106 |
this.flags = property.flags;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Copy function
|
|
111 |
*
|
|
112 |
* @return cloned property
|
|
113 |
*/
|
|
114 |
protected abstract Property copy();
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Property flag utility method for {@link PropertyDescriptor}s. Given two property descriptors,
|
|
118 |
* return the result of merging their flags.
|
|
119 |
*
|
|
120 |
* @param oldDesc first property descriptor
|
|
121 |
* @param newDesc second property descriptor
|
|
122 |
* @return merged flags.
|
|
123 |
*/
|
|
124 |
static int mergeFlags(final PropertyDescriptor oldDesc, final PropertyDescriptor newDesc) {
|
|
125 |
int propFlags = 0;
|
|
126 |
boolean value;
|
|
127 |
|
|
128 |
value = newDesc.has(CONFIGURABLE) ? newDesc.isConfigurable() : oldDesc.isConfigurable();
|
|
129 |
if (!value) {
|
|
130 |
propFlags |= NOT_CONFIGURABLE;
|
|
131 |
}
|
|
132 |
|
|
133 |
value = newDesc.has(ENUMERABLE) ? newDesc.isEnumerable() : oldDesc.isEnumerable();
|
|
134 |
if (!value) {
|
|
135 |
propFlags |= NOT_ENUMERABLE;
|
|
136 |
}
|
|
137 |
|
|
138 |
value = newDesc.has(WRITABLE) ? newDesc.isWritable() : oldDesc.isWritable();
|
|
139 |
if (!value) {
|
|
140 |
propFlags |= NOT_WRITABLE;
|
|
141 |
}
|
|
142 |
|
|
143 |
return propFlags;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Property flag utility method for {@link PropertyDescriptor}. Get the property flags
|
|
148 |
* conforming to any Property using this PropertyDescriptor
|
|
149 |
*
|
|
150 |
* @param desc property descriptor
|
|
151 |
* @return flags for properties that conform to property descriptor
|
|
152 |
*/
|
|
153 |
static int toFlags(final PropertyDescriptor desc) {
|
|
154 |
int propFlags = 0;
|
|
155 |
|
|
156 |
if (!desc.isConfigurable()) {
|
|
157 |
propFlags |= NOT_CONFIGURABLE;
|
|
158 |
}
|
|
159 |
if (!desc.isEnumerable()) {
|
|
160 |
propFlags |= NOT_ENUMERABLE;
|
|
161 |
}
|
|
162 |
if (!desc.isWritable()) {
|
|
163 |
propFlags |= NOT_WRITABLE;
|
|
164 |
}
|
|
165 |
|
|
166 |
return propFlags;
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Check whether this property has a user defined getter function. See {@link UserAccessorProperty}
|
|
171 |
* @return true if getter function exists, false is default
|
|
172 |
*/
|
|
173 |
public boolean hasGetterFunction() {
|
|
174 |
return false;
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Check whether this property has a user defined setter function. See {@link UserAccessorProperty}
|
|
179 |
* @return true if getter function exists, false is default
|
|
180 |
*/
|
|
181 |
public boolean hasSetterFunction() {
|
|
182 |
return false;
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Check whether this property is writable (see ECMA 8.6.1)
|
|
187 |
* @return true if writable
|
|
188 |
*/
|
|
189 |
public boolean isWritable() {
|
|
190 |
return (flags & NOT_WRITABLE) == 0;
|
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Check whether this property is writable (see ECMA 8.6.1)
|
|
195 |
* @return true if configurable
|
|
196 |
*/
|
|
197 |
public boolean isConfigurable() {
|
|
198 |
return (flags & NOT_CONFIGURABLE) == 0;
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Check whether this property is enumerable (see ECMA 8.6.1)
|
|
203 |
* @return true if enumerable
|
|
204 |
*/
|
|
205 |
public boolean isEnumerable() {
|
|
206 |
return (flags & NOT_ENUMERABLE) == 0;
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Check whether this property is used as a function parameter
|
|
211 |
* @return true if parameter
|
|
212 |
*/
|
|
213 |
public boolean isParameter() {
|
|
214 |
return (flags & IS_PARAMETER) == IS_PARAMETER;
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* Check whether this is a spill property, i.e. one that will not
|
|
219 |
* be stored in a specially generated field in the property class.
|
|
220 |
* The spill pool is maintained separately, as a growing Object array
|
|
221 |
* in the {@link ScriptObject}.
|
|
222 |
*
|
|
223 |
* @return true if spill property
|
|
224 |
*/
|
|
225 |
public boolean isSpill() {
|
|
226 |
return (flags & IS_SPILL) == IS_SPILL;
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Does this property use any slots in the spill array described in
|
|
231 |
* {@link Property#isSpill}? In that case how many. Currently a property
|
|
232 |
* only uses max one spill slot, but this may change in future representations
|
|
233 |
* Only {@link SpillProperty} instances use spill slots
|
|
234 |
*
|
|
235 |
* @return number of spill slots a property is using
|
|
236 |
*/
|
|
237 |
public int getSpillCount() {
|
|
238 |
return isSpill() ? 1 : 0;
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* Add more property flags to the property. Properties are immutable here,
|
|
243 |
* so any property change that results in a larger flag set results in the
|
|
244 |
* property being cloned. Use only the return value
|
|
245 |
*
|
|
246 |
* @param propertyFlags flags to be OR:ed to the existing property flags
|
|
247 |
* @return new property if property set was changed, {@code this} otherwise
|
|
248 |
*/
|
|
249 |
public Property addFlags(final int propertyFlags) {
|
|
250 |
if ((this.flags & propertyFlags) != propertyFlags) {
|
|
251 |
final Property cloned = this.copy();
|
|
252 |
cloned.flags |= propertyFlags;
|
|
253 |
return cloned;
|
|
254 |
}
|
|
255 |
return this;
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Get the flags for this property
|
|
260 |
* @return property flags
|
|
261 |
*/
|
|
262 |
public int getFlags() {
|
|
263 |
return flags;
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Get the modify flags for this property. The modify flags are the ECMA 8.6.1
|
|
268 |
* flags that decide if the Property is writable, configurable and/or enumerable.
|
|
269 |
*
|
|
270 |
* @return modify flags for property
|
|
271 |
*/
|
|
272 |
public int getModifyFlags() {
|
|
273 |
return flags & MODIFY_MASK;
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Remove property flags from the property. Properties are immutable here,
|
|
278 |
* so any property change that results in a smaller flag set results in the
|
|
279 |
* property being cloned. Use only the return value
|
|
280 |
*
|
|
281 |
* @param propertyFlags flags to be subtracted from the existing property flags
|
|
282 |
* @return new property if property set was changed, {@code this} otherwise
|
|
283 |
*/
|
|
284 |
public Property removeFlags(final int propertyFlags) {
|
|
285 |
if ((this.flags & propertyFlags) != 0) {
|
|
286 |
final Property cloned = this.copy();
|
|
287 |
cloned.flags &= ~propertyFlags;
|
|
288 |
return cloned;
|
|
289 |
}
|
|
290 |
return this;
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Reset the property for this property. Properties are immutable here,
|
|
295 |
* so any property change that results in a different flag sets results in the
|
|
296 |
* property being cloned. Use only the return value
|
|
297 |
*
|
|
298 |
* @param propertyFlags flags that are replacing from the existing property flags
|
|
299 |
* @return new property if property set was changed, {@code this} otherwise
|
|
300 |
*/
|
|
301 |
public Property setFlags(final int propertyFlags) {
|
|
302 |
if (this.flags != propertyFlags) {
|
|
303 |
final Property cloned = this.copy();
|
|
304 |
cloned.flags &= ~MODIFY_MASK;
|
|
305 |
cloned.flags |= propertyFlags & MODIFY_MASK;
|
|
306 |
return cloned;
|
|
307 |
}
|
|
308 |
return this;
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Abstract method for retrieving the getter for the property. We do not know
|
|
313 |
* anything about the internal representation when we request the getter, we only
|
|
314 |
* know that the getter will return the property as the given type.
|
|
315 |
*
|
|
316 |
* @param type getter return value type
|
|
317 |
* @return a getter for this property as {@code type}
|
|
318 |
*/
|
|
319 |
public abstract MethodHandle getGetter(final Class<?> type);
|
|
320 |
|
|
321 |
/**
|
|
322 |
* Get the key for this property. This key is an ordinary string. The "name".
|
|
323 |
* @return key for property
|
|
324 |
*/
|
|
325 |
public String getKey() {
|
|
326 |
return key;
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* Abstract method for retrieving the setter for the property. We do not know
|
|
331 |
* anything about the internal representation when we request the setter, we only
|
|
332 |
* know that the setter will take the property as a parameter of the given type.
|
|
333 |
* <p>
|
|
334 |
* Note that we have to pass the current property map from which we retrieved
|
|
335 |
* the property here. This is necessary for map guards if, e.g. the internal
|
|
336 |
* representation of the field, and consequently also the setter, changes. Then
|
|
337 |
* we automatically get a map guard that relinks the call site so that the
|
|
338 |
* older setter will never be used again.
|
|
339 |
* <p>
|
|
340 |
* see {@link ObjectClassGenerator#createSetter(Class, Class, MethodHandle, MethodHandle)}
|
|
341 |
* if you are interested in the internal details of this. Note that if you
|
|
342 |
* are running in default mode, with {@code -Dnashorn.fields.dual=true}, disabled, the setters
|
|
343 |
* will currently never change, as all properties are represented as Object field,
|
|
344 |
* the Object fields are Initialized to {@code ScriptRuntime.UNDEFINED} and primitives are
|
|
345 |
* boxed/unboxed upon every access, which is not necessarily optimal
|
|
346 |
*
|
|
347 |
* @param type setter parameter type
|
|
348 |
* @param currentMap current property map for property
|
|
349 |
* @return a getter for this property as {@code type}
|
|
350 |
*/
|
|
351 |
public abstract MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap);
|
|
352 |
|
|
353 |
/**
|
|
354 |
* Get the user defined getter function if one exists. Only {@link UserAccessorProperty} instances
|
|
355 |
* can have user defined getters
|
|
356 |
* @param obj the script object
|
|
357 |
* @return user defined getter function, or {@code null} if none exists
|
|
358 |
*/
|
|
359 |
public ScriptFunction getGetterFunction(final ScriptObject obj) {
|
|
360 |
return null;
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Get the user defined setter function if one exists. Only {@link UserAccessorProperty} instances
|
|
365 |
* can have user defined getters
|
|
366 |
* @param obj the script object
|
|
367 |
* @return user defined getter function, or {@code null} if none exists
|
|
368 |
*/
|
|
369 |
public ScriptFunction getSetterFunction(final ScriptObject obj) {
|
|
370 |
return null;
|
|
371 |
}
|
|
372 |
|
|
373 |
/**
|
|
374 |
* Get the spill slot as described in {@link Property#getSpillCount()}.
|
|
375 |
* @return spill slot, -1 if none exists
|
|
376 |
*/
|
|
377 |
public int getSlot() {
|
|
378 |
return -1;
|
|
379 |
}
|
|
380 |
|
|
381 |
@Override
|
|
382 |
public int hashCode() {
|
|
383 |
final Class<?> type = getCurrentType();
|
|
384 |
return Objects.hashCode(this.key) ^ flags ^ (type == null ? 0 : type.hashCode());
|
|
385 |
}
|
|
386 |
|
|
387 |
@Override
|
|
388 |
public boolean equals(final Object other) {
|
|
389 |
if (this == other) {
|
|
390 |
return true;
|
|
391 |
}
|
|
392 |
|
|
393 |
if (!(other instanceof Property)) {
|
|
394 |
return false;
|
|
395 |
}
|
|
396 |
|
|
397 |
final Property otherProperty = (Property)other;
|
|
398 |
final Object otherKey = otherProperty.key;
|
|
399 |
final Class<?> otherType = otherProperty.getCurrentType();
|
|
400 |
|
|
401 |
return flags == otherProperty.flags &&
|
|
402 |
(key == null ? otherKey == null : key.equals(otherKey)) &&
|
|
403 |
(getCurrentType() == otherType);
|
|
404 |
}
|
|
405 |
|
|
406 |
@Override
|
|
407 |
public String toString() {
|
|
408 |
final StringBuilder sb = new StringBuilder();
|
|
409 |
final Class<?> type = getCurrentType();
|
|
410 |
|
|
411 |
sb.append(getKey()).
|
|
412 |
append("(0x").
|
|
413 |
append(Integer.toHexString(flags)).
|
|
414 |
append(") ").
|
|
415 |
append(getClass().getSimpleName()).
|
|
416 |
append(" {").
|
|
417 |
append(type == null ? "UNDEFINED" : Type.typeFor(type).getDescriptor()).
|
|
418 |
append('}');
|
|
419 |
|
|
420 |
return sb.toString();
|
|
421 |
}
|
|
422 |
|
|
423 |
/**
|
|
424 |
* Get the current type of this field. If you are not running with dual fields enabled,
|
|
425 |
* this will always be Object.class. See the value representation explanation in
|
|
426 |
* {@link Property#getSetter(Class, PropertyMap)} and {@link ObjectClassGenerator}
|
|
427 |
* for more information.
|
|
428 |
*
|
|
429 |
* @return current type of property, null means undefined
|
|
430 |
*/
|
|
431 |
public Class<?> getCurrentType() {
|
|
432 |
return Object.class;
|
|
433 |
}
|
|
434 |
|
|
435 |
/**
|
|
436 |
* Check whether this Property can ever change its type. The default is false, and if
|
|
437 |
* you are not running with dual fields, the type is always object and can never change
|
|
438 |
* @return true if this property can change types
|
|
439 |
*/
|
|
440 |
public boolean canChangeType() {
|
|
441 |
return false;
|
|
442 |
}
|
|
443 |
|
|
444 |
/**
|
|
445 |
* Check whether this Property is ever used as anything but an Object. If this is used only
|
|
446 |
* as an object, dual fields mode need not even try to represent it as a primitive at any
|
|
447 |
* callsite, saving map rewrites for performance.
|
|
448 |
*
|
|
449 |
* @return true if representation should always be an object field
|
|
450 |
*/
|
|
451 |
public boolean isAlwaysObject() {
|
|
452 |
return (flags & IS_ALWAYS_OBJECT) == IS_ALWAYS_OBJECT;
|
|
453 |
}
|
|
454 |
|
|
455 |
/**
|
|
456 |
* Check whether this property can be primitive. This is a conservative
|
|
457 |
* analysis result, so {@code false} might mean that it can still be
|
|
458 |
* primitive
|
|
459 |
*
|
|
460 |
* @return can be primitive status
|
|
461 |
*/
|
|
462 |
public boolean canBePrimitive() {
|
|
463 |
return (flags & CAN_BE_PRIMITIVE) == CAN_BE_PRIMITIVE;
|
|
464 |
}
|
|
465 |
|
|
466 |
/**
|
|
467 |
* Check whether this property can be primitive. This is a conservative
|
|
468 |
* analysis result, so {@code true} might mean that it can still be
|
|
469 |
* defined, but it will never say that a property can not be undefined
|
|
470 |
* if it can
|
|
471 |
*
|
|
472 |
* @return can be undefined status
|
|
473 |
*/
|
|
474 |
public boolean canBeUndefined() {
|
|
475 |
return (flags & CAN_BE_UNDEFINED) == CAN_BE_UNDEFINED;
|
|
476 |
}
|
|
477 |
}
|