author | hannesw |
Thu, 07 Feb 2013 15:33:17 +0100 | |
changeset 16223 | dff592a332a4 |
parent 16217 | c5ac9be5c444 |
child 16224 | 0c49ad4e3b55 |
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 |
||
28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall; |
|
29 |
import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCall; |
|
30 |
import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup; |
|
31 |
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; |
|
32 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
33 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE; |
|
34 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE; |
|
35 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.GET; |
|
36 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.SET; |
|
37 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE; |
|
38 |
import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE; |
|
39 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
40 |
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow; |
|
41 |
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex; |
|
42 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH; |
|
43 |
||
44 |
import java.lang.invoke.MethodHandle; |
|
45 |
import java.lang.invoke.MethodHandles; |
|
46 |
import java.lang.invoke.MethodType; |
|
47 |
import java.util.AbstractMap; |
|
48 |
import java.util.ArrayList; |
|
49 |
import java.util.Arrays; |
|
50 |
import java.util.Collection; |
|
51 |
import java.util.Collections; |
|
52 |
import java.util.HashSet; |
|
53 |
import java.util.Iterator; |
|
54 |
import java.util.LinkedHashSet; |
|
55 |
import java.util.List; |
|
56 |
import java.util.Map; |
|
57 |
import java.util.Set; |
|
58 |
import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
|
59 |
import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator; |
|
60 |
import jdk.nashorn.internal.objects.AccessorPropertyDescriptor; |
|
61 |
import jdk.nashorn.internal.objects.DataPropertyDescriptor; |
|
62 |
import jdk.nashorn.internal.runtime.arrays.ArrayData; |
|
63 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
|
64 |
import jdk.nashorn.internal.runtime.linker.Lookup; |
|
65 |
import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; |
|
66 |
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; |
|
67 |
import jdk.nashorn.internal.runtime.linker.NashornGuards; |
|
68 |
import org.dynalang.dynalink.CallSiteDescriptor; |
|
69 |
import org.dynalang.dynalink.linker.GuardedInvocation; |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
70 |
import org.dynalang.dynalink.linker.LinkRequest; |
16147 | 71 |
import org.dynalang.dynalink.support.CallSiteDescriptorFactory; |
72 |
||
73 |
/** |
|
74 |
* Base class for generic JavaScript objects. |
|
75 |
* <p> |
|
76 |
* Notes: |
|
77 |
* <ul> |
|
78 |
* <li>The map is used to identify properties in the object.</li> |
|
79 |
* <li>If the map is modified then it must be cloned and replaced. This notifies |
|
80 |
* any code that made assumptions about the object that things have changed. |
|
81 |
* Ex. CallSites that have been validated must check to see if the map has |
|
82 |
* changed (or a map from a different object type) and hence relink the method |
|
83 |
* to call.</li> |
|
84 |
* <li>Modifications of the map include adding/deleting attributes or changing a |
|
85 |
* function field value.</li> |
|
86 |
* </ul> |
|
87 |
*/ |
|
88 |
||
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16173
diff
changeset
|
89 |
public abstract class ScriptObject extends PropertyListenerManager implements PropertyAccess { |
16147 | 90 |
|
91 |
/** Search fall back routine name for "no such method" */ |
|
16223
dff592a332a4
8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents:
16217
diff
changeset
|
92 |
static final String NO_SUCH_METHOD_NAME = "__noSuchMethod__"; |
16147 | 93 |
|
94 |
/** Search fall back routine name for "no such property" */ |
|
16223
dff592a332a4
8007718: Make static RegExp properties fully compatible to other engines
hannesw
parents:
16217
diff
changeset
|
95 |
static final String NO_SUCH_PROPERTY_NAME = "__noSuchProperty__"; |
16147 | 96 |
|
97 |
/** Per ScriptObject flag - is this a scope object? */ |
|
98 |
public static final int IS_SCOPE = 0b0000_0001; |
|
99 |
||
100 |
/** Per ScriptObject flag - is this an array object? */ |
|
101 |
public static final int IS_ARRAY = 0b0000_0010; |
|
102 |
||
103 |
/** Per ScriptObject flag - is this an arguments object? */ |
|
104 |
public static final int IS_ARGUMENTS = 0b0000_0100; |
|
105 |
||
106 |
/** Spill growth rate - by how many elements does {@link ScriptObject#spill} when full */ |
|
107 |
public static final int SPILL_RATE = 8; |
|
108 |
||
109 |
/** Map to property information and accessor functions. Ordered by insertion. */ |
|
110 |
private PropertyMap map; |
|
111 |
||
112 |
/** Object flags. */ |
|
113 |
private int flags; |
|
114 |
||
115 |
/** Area for properties added to object after instantiation, see {@link SpillProperty} */ |
|
116 |
public Object[] spill; |
|
117 |
||
118 |
/** Local embed area position 0 - used for {@link SpillProperty} before {@link ScriptObject#spill} */ |
|
119 |
public Object embed0; |
|
120 |
||
121 |
/** Local embed area position 1 - used for {@link SpillProperty} before {@link ScriptObject#spill} */ |
|
122 |
public Object embed1; |
|
123 |
||
124 |
/** Local embed area position 2 - used for {@link SpillProperty} before {@link ScriptObject#spill} */ |
|
125 |
public Object embed2; |
|
126 |
||
127 |
/** Local embed area position 3 - used for {@link SpillProperty} before {@link ScriptObject#spill} */ |
|
128 |
public Object embed3; |
|
129 |
||
130 |
/** Indexed array data. */ |
|
131 |
private ArrayData arrayData; |
|
132 |
||
16159 | 133 |
static final MethodHandle SETEMBED = findOwnMH("setEmbed", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, MethodHandle.class, int.class, Object.class, Object.class); |
134 |
static final MethodHandle SETSPILL = findOwnMH("setSpill", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, Object.class, Object.class); |
|
135 |
static final MethodHandle SETSPILLWITHNEW = findOwnMH("setSpillWithNew", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, Object.class, Object.class); |
|
136 |
static final MethodHandle SETSPILLWITHGROW = findOwnMH("setSpillWithGrow", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, int.class, Object.class, Object.class); |
|
137 |
||
16147 | 138 |
private static final MethodHandle TRUNCATINGFILTER = findOwnMH("truncatingFilter", Object[].class, int.class, Object[].class); |
139 |
private static final MethodHandle KNOWNFUNCPROPGUARD = findOwnMH("knownFunctionPropertyGuard", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, Object.class, ScriptFunction.class); |
|
140 |
||
141 |
/** Method handle for getting a function argument at a given index. Used from MapCreator */ |
|
142 |
public static final Call GET_ARGUMENT = virtualCall(ScriptObject.class, "getArgument", Object.class, int.class); |
|
143 |
||
144 |
/** Method handle for setting a function argument at a given index. Used from MapCreator */ |
|
145 |
public static final Call SET_ARGUMENT = virtualCall(ScriptObject.class, "setArgument", void.class, int.class, Object.class); |
|
146 |
||
147 |
/** Method handle for getting the proto of a ScriptObject - used by {@link jdk.nashorn.internal.codegen.CodeGenerator} */ |
|
148 |
public static final Call GET_PROTO = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class); |
|
149 |
||
150 |
/** Method handle for setting the proto of a ScriptObject - used by {@link jdk.nashorn.internal.codegen.CodeGenerator} */ |
|
151 |
public static final Call SET_PROTO = virtualCallNoLookup(ScriptObject.class, "setProto", void.class, ScriptObject.class); |
|
152 |
||
153 |
/** Method handle for setting the user accessors of a ScriptObject - used by {@link jdk.nashorn.internal.codegen.CodeGenerator} */ |
|
154 |
public static final Call SET_USER_ACCESSORS = virtualCall(ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class); |
|
155 |
||
156 |
/** Method handle for getter for {@link UserAccessorProperty}, given a slot */ |
|
157 |
static final Call USER_ACCESSOR_GETTER = staticCall(MethodHandles.lookup(), ScriptObject.class, "userAccessorGetter", Object.class, ScriptObject.class, int.class, Object.class); |
|
158 |
||
159 |
/** Method handle for setter for {@link UserAccessorProperty}, given a slot */ |
|
160 |
static final Call USER_ACCESSOR_SETTER = staticCall(MethodHandles.lookup(), ScriptObject.class, "userAccessorSetter", void.class, ScriptObject.class, int.class, String.class, Object.class, Object.class); |
|
161 |
||
162 |
private static final MethodHandle INVOKE_UA_GETTER = Bootstrap.createDynamicInvoker("dyn:call", Object.class, |
|
163 |
Object.class, Object.class); |
|
164 |
private static final MethodHandle INVOKE_UA_SETTER = Bootstrap.createDynamicInvoker("dyn:call", void.class, |
|
165 |
Object.class, Object.class, Object.class); |
|
166 |
||
167 |
/** |
|
168 |
* Constructor |
|
169 |
*/ |
|
170 |
public ScriptObject() { |
|
171 |
this(null); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Constructor |
|
176 |
* |
|
177 |
* @param map {@link PropertyMap} used to create the initial object |
|
178 |
*/ |
|
179 |
public ScriptObject(final PropertyMap map) { |
|
180 |
if (Context.DEBUG) { |
|
181 |
ScriptObject.count++; |
|
182 |
} |
|
183 |
||
184 |
this.arrayData = ArrayData.EMPTY_ARRAY; |
|
185 |
||
186 |
if (map == null) { |
|
187 |
this.setMap(PropertyMap.newMap(getClass())); |
|
188 |
return; |
|
189 |
} |
|
190 |
||
191 |
this.setMap(map); |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Copy all properties from the source object with their receiver bound to the source. |
|
196 |
* This function was known as mergeMap |
|
197 |
* |
|
198 |
* @param source The source object to copy from. |
|
199 |
*/ |
|
200 |
public void addBoundProperties(final ScriptObject source) { |
|
201 |
PropertyMap newMap = this.getMap(); |
|
202 |
||
203 |
for (final Property property : source.getMap().getProperties()) { |
|
204 |
final String key = property.getKey(); |
|
205 |
||
206 |
if (newMap.findProperty(key) == null) { |
|
207 |
if (property instanceof UserAccessorProperty) { |
|
208 |
final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source)); |
|
209 |
newMap = newMap.addProperty(prop); |
|
210 |
} else { |
|
211 |
newMap = newMap.newPropertyBind((AccessorProperty)property, source); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
this.setMap(newMap); |
|
217 |
} |
|
218 |
||
219 |
/** |
|
16159 | 220 |
* Bind the method handle to the specified receiver, while preserving its original type (it will just ignore the |
221 |
* first argument in lieu of the bound argument). |
|
16147 | 222 |
* @param methodHandle Method handle to bind to. |
223 |
* @param receiver Object to bind. |
|
224 |
* @return Bound method handle. |
|
225 |
*/ |
|
16159 | 226 |
static MethodHandle bindTo(final MethodHandle methodHandle, final Object receiver) { |
227 |
return MH.dropArguments(MH.bindTo(methodHandle, receiver), 0, methodHandle.type().parameterType(0)); |
|
16147 | 228 |
} |
229 |
||
230 |
/** |
|
231 |
* Return a property iterator. |
|
232 |
* @return Property iterator. |
|
233 |
*/ |
|
234 |
public Iterator<String> propertyIterator() { |
|
235 |
return new KeyIterator(this); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Return a property value iterator. |
|
240 |
* @return Property value iterator. |
|
241 |
*/ |
|
242 |
public Iterator<Object> valueIterator() { |
|
243 |
return new ValueIterator(this); |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* ECMA 8.10.1 IsAccessorDescriptor ( Desc ) |
|
248 |
* @return true if this has a {@link AccessorPropertyDescriptor} with a getter or a setter |
|
249 |
*/ |
|
250 |
public final boolean isAccessorDescriptor() { |
|
251 |
return has(GET) || has(SET); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* ECMA 8.10.2 IsDataDescriptor ( Desc ) |
|
256 |
* @return true if this has a {@link DataPropertyDescriptor}, i.e. the object has a property value and is writable |
|
257 |
*/ |
|
258 |
public final boolean isDataDescriptor() { |
|
259 |
return has(VALUE) || has(WRITABLE); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* ECMA 8.10.3 IsGenericDescriptor ( Desc ) |
|
264 |
* @return true if this has a descriptor describing an {@link AccessorPropertyDescriptor} or {@link DataPropertyDescriptor} |
|
265 |
*/ |
|
266 |
public final boolean isGenericDescriptor() { |
|
267 |
return isAccessorDescriptor() || isDataDescriptor(); |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* ECMA 8.10.5 ToPropertyDescriptor ( Obj ) |
|
272 |
* |
|
273 |
* @return property descriptor |
|
274 |
*/ |
|
275 |
public final PropertyDescriptor toPropertyDescriptor() { |
|
16188 | 276 |
final GlobalObject global = (GlobalObject) Context.getGlobalTrusted(); |
16147 | 277 |
|
278 |
final PropertyDescriptor desc; |
|
279 |
if (isDataDescriptor()) { |
|
280 |
if (has(SET) || has(GET)) { |
|
281 |
typeError((ScriptObject)global, "inconsistent.property.descriptor"); |
|
282 |
} |
|
283 |
||
284 |
desc = global.newDataDescriptor(UNDEFINED, false, false, false); |
|
285 |
} else if (isAccessorDescriptor()) { |
|
286 |
if (has(VALUE) || has(WRITABLE)) { |
|
287 |
typeError((ScriptObject)global, "inconsistent.property.descriptor"); |
|
288 |
} |
|
289 |
||
290 |
desc = global.newAccessorDescriptor(UNDEFINED, UNDEFINED, false, false); |
|
291 |
} else { |
|
292 |
desc = global.newGenericDescriptor(false, false); |
|
293 |
} |
|
294 |
||
295 |
return desc.fillFrom(this); |
|
296 |
} |
|
297 |
||
298 |
/** |
|
299 |
* ECMA 8.10.5 ToPropertyDescriptor ( Obj ) |
|
300 |
* |
|
301 |
* @param global global scope object |
|
302 |
* @param obj object to create property descriptor from |
|
303 |
* |
|
304 |
* @return property descriptor |
|
305 |
*/ |
|
306 |
public static PropertyDescriptor toPropertyDescriptor(final ScriptObject global, final Object obj) { |
|
307 |
if (obj instanceof ScriptObject) { |
|
308 |
return ((ScriptObject)obj).toPropertyDescriptor(); |
|
309 |
} |
|
310 |
||
311 |
typeError(global, "not.an.object", ScriptRuntime.safeToString(obj)); |
|
312 |
return null; |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* ECMA 8.12.1 [[GetOwnProperty]] (P) |
|
317 |
* |
|
318 |
* @param key property key |
|
319 |
* |
|
320 |
* @return Returns the Property Descriptor of the named own property of this |
|
321 |
* object, or undefined if absent. |
|
322 |
*/ |
|
323 |
public Object getOwnPropertyDescriptor(final String key) { |
|
324 |
final Property property = getMap().findProperty(key); |
|
325 |
||
16188 | 326 |
final GlobalObject global = (GlobalObject)Context.getGlobalTrusted(); |
16147 | 327 |
|
328 |
if (property != null) { |
|
329 |
final ScriptFunction get = property.getGetterFunction(this); |
|
330 |
final ScriptFunction set = property.getSetterFunction(this); |
|
331 |
||
332 |
final boolean configurable = property.isConfigurable(); |
|
333 |
final boolean enumerable = property.isEnumerable(); |
|
334 |
final boolean writable = property.isWritable(); |
|
335 |
||
336 |
if (property instanceof UserAccessorProperty) { |
|
337 |
return global.newAccessorDescriptor( |
|
338 |
(get != null) ? |
|
339 |
get : |
|
340 |
UNDEFINED, |
|
341 |
(set != null) ? |
|
342 |
set : |
|
343 |
UNDEFINED, |
|
344 |
configurable, |
|
345 |
enumerable); |
|
346 |
} |
|
347 |
||
348 |
return global.newDataDescriptor(getWithProperty(property), configurable, enumerable, writable); |
|
349 |
} |
|
350 |
||
351 |
final int index = getArrayIndexNoThrow(key); |
|
352 |
final ArrayData array = getArray(); |
|
353 |
||
354 |
if (array.has(index)) { |
|
355 |
return array.getDescriptor(global, index); |
|
356 |
} |
|
357 |
||
358 |
return UNDEFINED; |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* ECMA 8.12.2 [[GetProperty]] (P) |
|
363 |
* |
|
364 |
* @param key property key |
|
365 |
* |
|
366 |
* @return Returns the fully populated Property Descriptor of the named property |
|
367 |
* of this object, or undefined if absent. |
|
368 |
*/ |
|
369 |
public Object getPropertyDescriptor(final String key) { |
|
370 |
final Object res = getOwnPropertyDescriptor(key); |
|
371 |
||
372 |
if (res != UNDEFINED) { |
|
373 |
return res; |
|
374 |
} else if (getProto() != null) { |
|
375 |
return getProto().getOwnPropertyDescriptor(key); |
|
376 |
} else { |
|
377 |
return UNDEFINED; |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* ECMA 8.12.9 [[DefineOwnProperty]] (P, Desc, Throw) |
|
383 |
* |
|
384 |
* @param key the property key |
|
385 |
* @param propertyDesc the property descriptor |
|
386 |
* @param reject is the property extensible - true means new definitions are rejected |
|
387 |
* |
|
388 |
* @return true if property was successfully defined |
|
389 |
*/ |
|
390 |
public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) { |
|
16188 | 391 |
final ScriptObject global = Context.getGlobalTrusted(); |
16147 | 392 |
final PropertyDescriptor desc = toPropertyDescriptor(global, propertyDesc); |
393 |
final Object current = getOwnPropertyDescriptor(key); |
|
394 |
final String name = JSType.toString(key); |
|
395 |
||
396 |
if (current == UNDEFINED) { |
|
397 |
if (isExtensible()) { |
|
398 |
// add a new own property |
|
399 |
addOwnProperty(key, desc); |
|
400 |
return true; |
|
401 |
} |
|
402 |
// new property added to non-extensible object |
|
403 |
if (reject) { |
|
404 |
typeError(global, "object.non.extensible", name, ScriptRuntime.safeToString(this)); |
|
405 |
} |
|
406 |
return false; |
|
407 |
} |
|
408 |
// modifying an existing property |
|
409 |
final PropertyDescriptor currentDesc = (PropertyDescriptor) current; |
|
410 |
final PropertyDescriptor newDesc = desc; |
|
411 |
||
412 |
if (newDesc.type() == PropertyDescriptor.GENERIC && |
|
413 |
! newDesc.has(CONFIGURABLE) && ! newDesc.has(ENUMERABLE)) { |
|
414 |
// every descriptor field is absent |
|
415 |
return true; |
|
416 |
} |
|
417 |
||
418 |
if (currentDesc.equals(newDesc)) { |
|
419 |
// every descriptor field of the new is same as the current |
|
420 |
return true; |
|
421 |
} |
|
422 |
||
423 |
if (! currentDesc.isConfigurable()) { |
|
424 |
if (newDesc.has(CONFIGURABLE) && newDesc.isConfigurable()) { |
|
425 |
// not configurable can not be made configurable |
|
426 |
if (reject) { |
|
427 |
typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this)); |
|
428 |
} |
|
429 |
return false; |
|
430 |
} |
|
431 |
||
432 |
if (newDesc.has(ENUMERABLE) && |
|
433 |
currentDesc.isEnumerable() != newDesc.isEnumerable()) { |
|
434 |
// cannot make non-enumerable as enumerable or vice-versa |
|
435 |
if (reject) { |
|
436 |
typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this)); |
|
437 |
} |
|
438 |
return false; |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
int propFlags = Property.mergeFlags(currentDesc, newDesc); |
|
443 |
Property property = getMap().findProperty(key); |
|
444 |
||
445 |
if (currentDesc.type() == PropertyDescriptor.DATA && |
|
446 |
(newDesc.type() == PropertyDescriptor.DATA || newDesc.type() == PropertyDescriptor.GENERIC)) { |
|
447 |
if (! currentDesc.isConfigurable() && ! currentDesc.isWritable()) { |
|
448 |
if (newDesc.has(WRITABLE) && newDesc.isWritable() || |
|
449 |
newDesc.has(VALUE) && ! ScriptRuntime.sameValue(currentDesc.getValue(), newDesc.getValue())) { |
|
450 |
if (reject) { |
|
451 |
typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this)); |
|
452 |
} |
|
453 |
return false; |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
final boolean newValue = newDesc.has(VALUE); |
|
458 |
final Object value = newValue? newDesc.getValue() : currentDesc.getValue(); |
|
459 |
if (newValue && property != null) { |
|
460 |
// Temporarily clear flags. |
|
461 |
property = modifyOwnProperty(property, 0); |
|
462 |
set(key, value, getContext()._strict); |
|
463 |
} |
|
464 |
||
465 |
if (property == null) { |
|
466 |
// promoting an arrayData value to actual property |
|
467 |
addOwnProperty(key, propFlags, value); |
|
468 |
removeArraySlot(key); |
|
469 |
} else { |
|
470 |
// Now set the new flags |
|
471 |
modifyOwnProperty(property, propFlags); |
|
472 |
} |
|
473 |
} else if (currentDesc.type() == PropertyDescriptor.ACCESSOR && |
|
474 |
(newDesc.type() == PropertyDescriptor.ACCESSOR || |
|
475 |
newDesc.type() == PropertyDescriptor.GENERIC)) { |
|
476 |
if (! currentDesc.isConfigurable()) { |
|
477 |
if (newDesc.has(PropertyDescriptor.GET) && ! ScriptRuntime.sameValue(currentDesc.getGetter(), newDesc.getGetter()) || |
|
478 |
newDesc.has(PropertyDescriptor.SET) && ! ScriptRuntime.sameValue(currentDesc.getSetter(), newDesc.getSetter())) { |
|
479 |
if (reject) { |
|
480 |
typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this)); |
|
481 |
} |
|
482 |
return false; |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
// New set the new features. |
|
487 |
modifyOwnProperty(property, propFlags, |
|
488 |
newDesc.has(GET) ? newDesc.getGetter() : currentDesc.getGetter(), |
|
489 |
newDesc.has(SET) ? newDesc.getSetter() : currentDesc.getSetter()); |
|
490 |
} else { |
|
491 |
// changing descriptor type |
|
492 |
if (! currentDesc.isConfigurable()) { |
|
493 |
// not configurable can not be made configurable |
|
494 |
if (reject) { |
|
495 |
typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this)); |
|
496 |
} |
|
497 |
return false; |
|
498 |
} |
|
499 |
||
500 |
propFlags = 0; |
|
501 |
||
502 |
// Preserve only configurable and enumerable from current desc |
|
503 |
// if those are not overridden in the new property descriptor. |
|
504 |
boolean value = newDesc.has(CONFIGURABLE)? newDesc.isConfigurable() : currentDesc.isConfigurable(); |
|
505 |
if (!value) { |
|
506 |
propFlags |= Property.NOT_CONFIGURABLE; |
|
507 |
} |
|
508 |
value = newDesc.has(ENUMERABLE)? newDesc.isEnumerable() : currentDesc.isEnumerable(); |
|
509 |
if (!value) { |
|
510 |
propFlags |= Property.NOT_ENUMERABLE; |
|
511 |
} |
|
512 |
||
513 |
final int type = newDesc.type(); |
|
514 |
if (type == PropertyDescriptor.DATA) { |
|
515 |
// get writable from the new descriptor |
|
516 |
value = newDesc.has(WRITABLE) && newDesc.isWritable(); |
|
517 |
if (! value) { |
|
518 |
propFlags |= Property.NOT_WRITABLE; |
|
519 |
} |
|
520 |
||
521 |
// delete the old property |
|
522 |
deleteOwnProperty(property); |
|
523 |
// add new data property |
|
524 |
addOwnProperty(key, propFlags, newDesc.getValue()); |
|
525 |
} else if (type == PropertyDescriptor.ACCESSOR) { |
|
526 |
if (property == null) { |
|
527 |
addOwnProperty(key, propFlags, |
|
528 |
newDesc.has(GET) ? newDesc.getGetter() : null, |
|
529 |
newDesc.has(SET) ? newDesc.getSetter() : null); |
|
530 |
} else { |
|
531 |
// Modify old property with the new features. |
|
532 |
modifyOwnProperty(property, propFlags, |
|
533 |
newDesc.has(GET) ? newDesc.getGetter() : null, |
|
534 |
newDesc.has(SET) ? newDesc.getSetter() : null); |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
checkIntegerKey(key); |
|
540 |
||
541 |
return true; |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* Spec. mentions use of [[DefineOwnProperty]] for indexed properties in |
|
546 |
* certain places (eg. Array.prototype.map, filter). We can not use ScriptObject.set |
|
547 |
* method in such cases. This is because set method uses inherited setters (if any) |
|
548 |
* from any object in proto chain such as Array.prototype, Object.prototype. |
|
549 |
* This method directly sets a particular element value in the current object. |
|
550 |
* |
|
551 |
* @param index index key for property |
|
552 |
* @param value value to define |
|
553 |
*/ |
|
554 |
protected final void defineOwnProperty(final int index, final Object value) { |
|
555 |
if (index >= getArray().length()) { |
|
556 |
// make array big enough to hold.. |
|
557 |
setArray(getArray().ensure(index)); |
|
558 |
} |
|
559 |
setArray(getArray().set(index, value, false)); |
|
560 |
} |
|
561 |
||
562 |
private void checkIntegerKey(final String key) { |
|
563 |
final int index = getArrayIndexNoThrow(key); |
|
564 |
||
565 |
if (isValidArrayIndex(index)) { |
|
566 |
final ArrayData data = getArray(); |
|
567 |
||
568 |
if (data.has(index)) { |
|
569 |
setArray(data.delete(index)); |
|
570 |
} |
|
571 |
} |
|
572 |
} |
|
573 |
||
574 |
private void removeArraySlot(final String key) { |
|
575 |
final int index = getArrayIndexNoThrow(key); |
|
576 |
final ArrayData array = getArray(); |
|
577 |
||
578 |
if (array.has(index)) { |
|
579 |
setArray(array.delete(index)); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
/** |
|
584 |
* Add a new property to the object. |
|
585 |
* |
|
586 |
* @param key property key |
|
587 |
* @param propertyDesc property descriptor for property |
|
588 |
*/ |
|
589 |
public final void addOwnProperty(final String key, final PropertyDescriptor propertyDesc) { |
|
590 |
// Already checked that there is no own property with that key. |
|
591 |
PropertyDescriptor pdesc = propertyDesc; |
|
592 |
||
593 |
final int propFlags = Property.toFlags(pdesc); |
|
594 |
||
595 |
if (pdesc.type() == PropertyDescriptor.GENERIC) { |
|
16188 | 596 |
final GlobalObject global = (GlobalObject) Context.getGlobalTrusted(); |
16147 | 597 |
final PropertyDescriptor dDesc = global.newDataDescriptor(UNDEFINED, false, false, false); |
598 |
||
599 |
dDesc.fillFrom((ScriptObject)pdesc); |
|
600 |
pdesc = dDesc; |
|
601 |
} |
|
602 |
||
603 |
final int type = pdesc.type(); |
|
604 |
if (type == PropertyDescriptor.DATA) { |
|
605 |
addOwnProperty(key, propFlags, pdesc.getValue()); |
|
606 |
} else if (type == PropertyDescriptor.ACCESSOR) { |
|
607 |
addOwnProperty(key, propFlags, |
|
608 |
pdesc.has(GET) ? pdesc.getGetter() : null, |
|
609 |
pdesc.has(SET) ? pdesc.getSetter() : null); |
|
610 |
} |
|
611 |
||
612 |
checkIntegerKey(key); |
|
613 |
} |
|
614 |
||
615 |
/** |
|
616 |
* Low level property API (not using property descriptors) |
|
617 |
* <p> |
|
618 |
* Find a property in the prototype hierarchy. Note: this is final and not |
|
619 |
* a good idea to override. If you have to, use |
|
620 |
* {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or |
|
621 |
* {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the |
|
622 |
* overriding way to find array properties |
|
623 |
* |
|
624 |
* @see jdk.nashorn.internal.objects.NativeArray |
|
625 |
* |
|
626 |
* @param key Property key. |
|
627 |
* @param deep Whether the search should look up proto chain. |
|
628 |
* |
|
629 |
* @return FindPropertyData or null if not found. |
|
630 |
*/ |
|
631 |
public final FindProperty findProperty(final String key, final boolean deep) { |
|
16161
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
632 |
return findProperty(key, deep, false); |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
633 |
} |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
634 |
|
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
635 |
/** |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
636 |
* Low level property API (not using property descriptors) |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
637 |
* <p> |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
638 |
* Find a property in the prototype hierarchy. Note: this is final and not |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
639 |
* a good idea to override. If you have to, use |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
640 |
* {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
641 |
* {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
642 |
* overriding way to find array properties |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
643 |
* |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
644 |
* @see jdk.nashorn.internal.objects.NativeArray |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
645 |
* |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
646 |
* @param key Property key. |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
647 |
* @param deep Whether the search should look up proto chain. |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
648 |
* @param stopOnNonScope should a deep search stop on the first non-scope object? |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
649 |
* |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
650 |
* @return FindPropertyData or null if not found. |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
651 |
*/ |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
652 |
public final FindProperty findProperty(final String key, final boolean deep, final boolean stopOnNonScope) { |
16147 | 653 |
int depth = 0; |
654 |
||
655 |
for (ScriptObject self = this; self != null; self = self.getProto()) { |
|
16161
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
656 |
// if doing deep search, stop search on the first non-scope object if asked to do so |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
657 |
if (stopOnNonScope && depth != 0 && !self.isScope()) { |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
658 |
break; |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
659 |
} |
16147 | 660 |
final PropertyMap selfMap = self.getMap(); |
661 |
final Property property = selfMap.findProperty(key); |
|
662 |
||
663 |
if (property != null) { |
|
664 |
return new FindProperty(this, self, selfMap, property, depth); |
|
665 |
} else if (!deep) { |
|
666 |
return null; |
|
667 |
} |
|
668 |
||
669 |
depth++; |
|
670 |
} |
|
671 |
||
672 |
return null; |
|
673 |
} |
|
674 |
||
675 |
/** |
|
676 |
* Add a new property to the object. |
|
677 |
* <p> |
|
678 |
* This a more "low level" way that doesn't involve {@link PropertyDescriptor}s |
|
679 |
* |
|
680 |
* @param key Property key. |
|
681 |
* @param propertyFlags Property flags. |
|
682 |
* @param getter Property getter, or null if not defined |
|
683 |
* @param setter Property setter, or null if not defined |
|
684 |
* |
|
685 |
* @return New property. |
|
686 |
*/ |
|
687 |
public final Property addOwnProperty(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { |
|
688 |
return addOwnProperty(newUserAccessors(key, propertyFlags, getter, setter)); |
|
689 |
} |
|
690 |
||
691 |
/** |
|
692 |
* Add a new property to the object. |
|
693 |
* <p> |
|
694 |
* This a more "low level" way that doesn't involve {@link PropertyDescriptor}s |
|
695 |
* |
|
696 |
* @param key Property key. |
|
697 |
* @param propertyFlags Property flags. |
|
698 |
* @param value Value of property |
|
699 |
* |
|
700 |
* @return New property. |
|
701 |
*/ |
|
702 |
public final Property addOwnProperty(final String key, final int propertyFlags, final Object value) { |
|
703 |
final MethodHandle setter = addSpill(key, propertyFlags); |
|
704 |
||
705 |
try { |
|
706 |
setter.invokeExact((Object)this, value); |
|
707 |
} catch (final Error|RuntimeException e) { |
|
708 |
throw e; |
|
709 |
} catch (final Throwable e) { |
|
710 |
throw new RuntimeException(e); |
|
711 |
} |
|
712 |
||
713 |
return getMap().findProperty(key); |
|
714 |
} |
|
715 |
||
716 |
/** |
|
717 |
* Add a new property to the object. |
|
718 |
* <p> |
|
719 |
* This a more "low level" way that doesn't involve {@link PropertyDescriptor}s |
|
720 |
* |
|
721 |
* @param newProperty property to add |
|
722 |
* |
|
723 |
* @return New property. |
|
724 |
*/ |
|
725 |
public final Property addOwnProperty(final Property newProperty) { |
|
726 |
PropertyMap oldMap = getMap(); |
|
727 |
||
728 |
while (true) { |
|
729 |
final PropertyMap newMap = oldMap.addProperty(newProperty); |
|
730 |
||
731 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
732 |
oldMap = getMap(); |
|
733 |
final Property oldProperty = oldMap.findProperty(newProperty.getKey()); |
|
734 |
||
735 |
if (oldProperty != null) { |
|
736 |
return oldProperty; |
|
737 |
} |
|
738 |
} else { |
|
739 |
return newProperty; |
|
740 |
} |
|
741 |
} |
|
742 |
} |
|
743 |
||
744 |
private void erasePropertyValue(final Property property) { |
|
745 |
// Erase the property field value with undefined. If the property is defined |
|
746 |
// by user-defined accessors, we don't want to call the setter!! |
|
747 |
if (!(property instanceof UserAccessorProperty)) { |
|
748 |
try { |
|
749 |
// make the property value to be undefined |
|
750 |
//TODO specproperties |
|
751 |
property.getSetter(Object.class, getMap()).invokeExact((Object)this, (Object)UNDEFINED); |
|
752 |
} catch (final RuntimeException | Error e) { |
|
753 |
throw e; |
|
754 |
} catch (final Throwable t) { |
|
755 |
throw new RuntimeException(t); |
|
756 |
} |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
/** |
|
761 |
* Delete a property from the object. |
|
762 |
* |
|
763 |
* @param property Property to delete. |
|
764 |
* |
|
765 |
* @return true if deleted. |
|
766 |
*/ |
|
767 |
public final boolean deleteOwnProperty(final Property property) { |
|
768 |
erasePropertyValue(property); |
|
769 |
PropertyMap oldMap = getMap(); |
|
770 |
||
771 |
while (true) { |
|
772 |
final PropertyMap newMap = oldMap.deleteProperty(property); |
|
773 |
||
774 |
if (newMap == null) { |
|
775 |
return false; |
|
776 |
} |
|
777 |
||
778 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
779 |
oldMap = getMap(); |
|
780 |
} else { |
|
781 |
// delete getter and setter function references so that we don't leak |
|
782 |
if (property instanceof UserAccessorProperty) { |
|
783 |
final UserAccessorProperty uc = (UserAccessorProperty) property; |
|
784 |
setEmbedOrSpill(uc.getGetterSlot(), null); |
|
785 |
setEmbedOrSpill(uc.getSetterSlot(), null); |
|
786 |
} |
|
787 |
return true; |
|
788 |
} |
|
789 |
} |
|
790 |
} |
|
791 |
||
792 |
/** |
|
793 |
* Modify a property in the object |
|
794 |
* |
|
795 |
* @param oldProperty property to modify |
|
796 |
* @param propertyFlags new property flags |
|
797 |
* @param getter getter for {@link UserAccessorProperty}, null if not present or N/A |
|
798 |
* @param setter setter for {@link UserAccessorProperty}, null if not present or N/A |
|
799 |
* |
|
800 |
* @return new property |
|
801 |
*/ |
|
802 |
public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { |
|
803 |
Property newProperty; |
|
804 |
if (oldProperty instanceof UserAccessorProperty) { |
|
805 |
// re-use the slots of the old user accessor property. |
|
806 |
final UserAccessorProperty uc = (UserAccessorProperty) oldProperty; |
|
807 |
||
808 |
int getterSlot = uc.getGetterSlot(); |
|
809 |
// clear the old getter and set the new getter |
|
810 |
setEmbedOrSpill(getterSlot, getter); |
|
811 |
// if getter function is null, flag the slot to be negative (less by 1) |
|
812 |
if (getter == null) { |
|
813 |
getterSlot = -getterSlot - 1; |
|
814 |
} |
|
815 |
||
816 |
int setterSlot = uc.getSetterSlot(); |
|
817 |
// clear the old setter and set the new setter |
|
818 |
setEmbedOrSpill(setterSlot, setter); |
|
819 |
// if setter function is null, flag the slot to be negative (less by 1) |
|
820 |
if (setter == null) { |
|
821 |
setterSlot = -setterSlot - 1; |
|
822 |
} |
|
823 |
||
824 |
newProperty = new UserAccessorProperty(oldProperty.getKey(), propertyFlags, getterSlot, setterSlot); |
|
825 |
// if just flipping getter and setter with new functions, no need to change property or map |
|
826 |
if (oldProperty.equals(newProperty)) { |
|
827 |
return oldProperty; |
|
828 |
} |
|
829 |
} else { |
|
830 |
// erase old property value and create new user accessor property |
|
831 |
erasePropertyValue(oldProperty); |
|
832 |
newProperty = newUserAccessors(oldProperty.getKey(), propertyFlags, getter, setter); |
|
833 |
} |
|
834 |
||
835 |
notifyPropertyModified(this, oldProperty, newProperty); |
|
836 |
||
837 |
return modifyOwnProperty(oldProperty, newProperty); |
|
838 |
} |
|
839 |
||
840 |
/** |
|
841 |
* Modify a property in the object |
|
842 |
* |
|
843 |
* @param oldProperty property to modify |
|
844 |
* @param propertyFlags new property flags |
|
845 |
* |
|
846 |
* @return new property |
|
847 |
*/ |
|
848 |
public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags) { |
|
849 |
return modifyOwnProperty(oldProperty, oldProperty.setFlags(propertyFlags)); |
|
850 |
} |
|
851 |
||
852 |
/** |
|
853 |
* Modify a property in the object, replacing a property with a new one |
|
854 |
* |
|
855 |
* @param oldProperty property to replace |
|
856 |
* @param newProperty property to replace it with |
|
857 |
* |
|
858 |
* @return new property |
|
859 |
*/ |
|
860 |
private Property modifyOwnProperty(final Property oldProperty, final Property newProperty) { |
|
861 |
assert newProperty.getKey().equals(oldProperty.getKey()) : "replacing property with different key"; |
|
862 |
||
863 |
PropertyMap oldMap = getMap(); |
|
864 |
||
865 |
while (true) { |
|
866 |
final PropertyMap newMap = oldMap.replaceProperty(oldProperty, newProperty); |
|
867 |
||
868 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
869 |
oldMap = getMap(); |
|
870 |
final Property oldPropertyLookup = oldMap.findProperty(oldProperty.getKey()); |
|
871 |
||
872 |
if (oldPropertyLookup != null && oldPropertyLookup.equals(newProperty)) { |
|
873 |
return oldPropertyLookup; |
|
874 |
} |
|
875 |
} else { |
|
876 |
return newProperty; |
|
877 |
} |
|
878 |
} |
|
879 |
} |
|
880 |
||
881 |
/** |
|
882 |
* Update getter and setter in an object literal. |
|
883 |
* |
|
884 |
* @param key Property key. |
|
885 |
* @param getter {@link UserAccessorProperty} defined getter, or null if none |
|
886 |
* @param setter {@link UserAccessorProperty} defined setter, or null if none |
|
887 |
*/ |
|
888 |
public final void setUserAccessors(final String key, final ScriptFunction getter, final ScriptFunction setter) { |
|
889 |
final Property oldProperty = getMap().findProperty(key); |
|
890 |
if (oldProperty != null) { |
|
891 |
final UserAccessorProperty newProperty = newUserAccessors(oldProperty.getKey(), oldProperty.getFlags(), getter, setter); |
|
892 |
modifyOwnProperty(oldProperty, newProperty); |
|
893 |
} else { |
|
894 |
final UserAccessorProperty newProperty = newUserAccessors(key, 0, getter, setter); |
|
895 |
addOwnProperty(newProperty); |
|
896 |
} |
|
897 |
} |
|
898 |
||
899 |
private static int getIntValue(final FindProperty find) { |
|
900 |
final MethodHandle getter = find.getGetter(int.class); |
|
901 |
if (getter != null) { |
|
902 |
try { |
|
903 |
return (int)getter.invokeExact((Object)find.getOwner()); |
|
904 |
} catch (final Error|RuntimeException e) { |
|
905 |
throw e; |
|
906 |
} catch (final Throwable e) { |
|
907 |
throw new RuntimeException(e); |
|
908 |
} |
|
909 |
} |
|
910 |
||
911 |
return ObjectClassGenerator.UNDEFINED_INT; |
|
912 |
} |
|
913 |
||
914 |
private static long getLongValue(final FindProperty find) { |
|
915 |
final MethodHandle getter = find.getGetter(long.class); |
|
916 |
if (getter != null) { |
|
917 |
try { |
|
918 |
return (long)getter.invokeExact((Object)find.getOwner()); |
|
919 |
} catch (final Error|RuntimeException e) { |
|
920 |
throw e; |
|
921 |
} catch (final Throwable e) { |
|
922 |
throw new RuntimeException(e); |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
return ObjectClassGenerator.UNDEFINED_LONG; |
|
927 |
} |
|
928 |
||
929 |
private static double getDoubleValue(final FindProperty find) { |
|
930 |
final MethodHandle getter = find.getGetter(double.class); |
|
931 |
if (getter != null) { |
|
932 |
try { |
|
933 |
return (double)getter.invokeExact((Object)find.getOwner()); |
|
934 |
} catch (final Error|RuntimeException e) { |
|
935 |
throw e; |
|
936 |
} catch (final Throwable e) { |
|
937 |
throw new RuntimeException(e); |
|
938 |
} |
|
939 |
} |
|
940 |
||
941 |
return ObjectClassGenerator.UNDEFINED_DOUBLE; |
|
942 |
} |
|
943 |
||
944 |
/** |
|
945 |
* Get the object value of a property |
|
946 |
* |
|
947 |
* @param find {@link FindProperty} lookup result |
|
948 |
* |
|
949 |
* @return the value of the property |
|
950 |
*/ |
|
951 |
protected static Object getObjectValue(final FindProperty find) { |
|
952 |
final MethodHandle getter = find.getGetter(Object.class); |
|
953 |
if (getter != null) { |
|
954 |
try { |
|
955 |
return getter.invokeExact((Object)find.getOwner()); |
|
956 |
} catch (final Error|RuntimeException e) { |
|
957 |
throw e; |
|
958 |
} catch (final Throwable e) { |
|
959 |
throw new RuntimeException(e); |
|
960 |
} |
|
961 |
} |
|
962 |
||
963 |
return UNDEFINED; |
|
964 |
} |
|
965 |
||
966 |
/** |
|
967 |
* Return methodHandle of value function for call. |
|
968 |
* |
|
969 |
* @param find data from find property. |
|
970 |
* @param type method type of function. |
|
971 |
* @param bindName null or name to bind to second argument (property not found method.) |
|
972 |
* |
|
973 |
* @return value of property as a MethodHandle or null. |
|
974 |
* |
|
975 |
*/ |
|
976 |
@SuppressWarnings("static-method") |
|
977 |
protected MethodHandle getCallMethodHandle(final FindProperty find, final MethodType type, final String bindName) { |
|
978 |
return getCallMethodHandle(getObjectValue(find), type, bindName); |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Return methodHandle of value function for call. |
|
983 |
* |
|
984 |
* @param value value of receiver, it not a {@link ScriptFunction} this will return null. |
|
985 |
* @param type method type of function. |
|
986 |
* @param bindName null or name to bind to second argument (property not found method.) |
|
987 |
* |
|
988 |
* @return value of property as a MethodHandle or null. |
|
989 |
*/ |
|
990 |
protected static MethodHandle getCallMethodHandle(final Object value, final MethodType type, final String bindName) { |
|
991 |
return value instanceof ScriptFunction ? ((ScriptFunction)value).getCallMethodHandle(type, bindName) : null; |
|
992 |
} |
|
993 |
||
994 |
/** |
|
995 |
* Get value using found property. |
|
996 |
* |
|
997 |
* @param property Found property. |
|
998 |
* |
|
999 |
* @return Value of property. |
|
1000 |
*/ |
|
1001 |
public final Object getWithProperty(final Property property) { |
|
1002 |
return getObjectValue(new FindProperty(this, this, getMap(), property, 0)); |
|
1003 |
} |
|
1004 |
||
1005 |
/** |
|
1006 |
* Get a property given a key |
|
1007 |
* |
|
1008 |
* @param key property key |
|
1009 |
* |
|
1010 |
* @return property for key |
|
1011 |
*/ |
|
1012 |
public final Property getProperty(final String key) { |
|
1013 |
return getMap().findProperty(key); |
|
1014 |
} |
|
1015 |
||
1016 |
static String convertKey(final Object key) { |
|
1017 |
return (key instanceof String) ? (String)key : JSType.toString(key); |
|
1018 |
} |
|
1019 |
||
1020 |
/** |
|
1021 |
* Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) |
|
1022 |
* Used for argument access in a vararg function using parameter name. |
|
1023 |
* Returns the argument at a given key (index) |
|
1024 |
* |
|
1025 |
* @param key argument index |
|
1026 |
* |
|
1027 |
* @return the argument at the given position, or undefined if not present |
|
1028 |
*/ |
|
1029 |
public Object getArgument(final int key) { |
|
1030 |
return get(key); |
|
1031 |
} |
|
1032 |
||
1033 |
/** |
|
1034 |
* Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) |
|
1035 |
* Used for argument access in a vararg function using parameter name. |
|
1036 |
* Returns the argument at a given key (index) |
|
1037 |
* |
|
1038 |
* @param key argument index |
|
1039 |
* @param value the value to write at the given index |
|
1040 |
*/ |
|
1041 |
public void setArgument(final int key, final Object value) { |
|
1042 |
set(key, value, getContext()._strict); |
|
1043 |
} |
|
1044 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1045 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1046 |
* Return true if the script object context is strict |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1047 |
* @return true if strict context |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1048 |
*/ |
16185 | 1049 |
public final boolean isStrictContext() { |
1050 |
return getContext()._strict; |
|
1051 |
} |
|
1052 |
||
16147 | 1053 |
/** |
1054 |
* Return the current context from the object's map. |
|
1055 |
* @return Current context. |
|
1056 |
*/ |
|
16185 | 1057 |
protected final Context getContext() { |
16147 | 1058 |
return getMap().getContext(); |
1059 |
} |
|
1060 |
||
1061 |
/** |
|
1062 |
* Return the map of an object. |
|
1063 |
* @return PropertyMap object. |
|
1064 |
*/ |
|
1065 |
public final PropertyMap getMap() { |
|
1066 |
return map; |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
1070 |
* Set the initial map. |
|
1071 |
* @param map Initial map. |
|
1072 |
*/ |
|
1073 |
public final void setMap(final PropertyMap map) { |
|
1074 |
this.map = map; |
|
1075 |
} |
|
1076 |
||
1077 |
/** |
|
1078 |
* Conditionally set the new map if the old map is the same. |
|
1079 |
* @param oldMap Map prior to manipulation. |
|
1080 |
* @param newMap Replacement map. |
|
1081 |
* @return true if the operation succeeded. |
|
1082 |
*/ |
|
1083 |
protected synchronized final boolean compareAndSetMap(final PropertyMap oldMap, final PropertyMap newMap) { |
|
1084 |
final boolean update = oldMap == this.map; |
|
1085 |
||
1086 |
if (update) { |
|
1087 |
this.map = newMap; |
|
1088 |
} |
|
1089 |
||
1090 |
return update; |
|
1091 |
} |
|
1092 |
||
1093 |
/** |
|
1094 |
* Return the __proto__ of an object. |
|
1095 |
* @return __proto__ object. |
|
1096 |
*/ |
|
1097 |
public final ScriptObject getProto() { |
|
1098 |
return getMap().getProto(); |
|
1099 |
} |
|
1100 |
||
1101 |
/** |
|
1102 |
* Check if this is a prototype |
|
1103 |
* @return true if {@link PropertyMap#isPrototype()} is true for this ScriptObject |
|
1104 |
*/ |
|
1105 |
public final boolean isPrototype() { |
|
1106 |
return getMap().isPrototype(); |
|
1107 |
} |
|
1108 |
||
1109 |
/** |
|
1110 |
* Set the __proto__ of an object. |
|
1111 |
* @param newProto new __proto__ to set. |
|
1112 |
*/ |
|
1113 |
public final void setProto(final ScriptObject newProto) { |
|
1114 |
PropertyMap oldMap = getMap(); |
|
1115 |
ScriptObject oldProto = getProto(); |
|
1116 |
||
1117 |
while (oldProto != newProto) { |
|
1118 |
final PropertyMap newMap = oldMap.setProto(newProto); |
|
1119 |
||
1120 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
1121 |
oldMap = getMap(); |
|
1122 |
oldProto = getProto(); |
|
1123 |
} else { |
|
1124 |
if (isPrototype()) { |
|
1125 |
||
1126 |
if (oldProto != null) { |
|
1127 |
oldProto.removePropertyListener(this); |
|
1128 |
} |
|
1129 |
||
1130 |
if (newProto != null) { |
|
1131 |
newProto.addPropertyListener(this); |
|
1132 |
} |
|
1133 |
} |
|
1134 |
||
1135 |
return; |
|
1136 |
} |
|
1137 |
} |
|
1138 |
} |
|
1139 |
||
1140 |
/** |
|
1141 |
* Set the __proto__ of an object with checks. |
|
1142 |
* @param newProto Prototype to set. |
|
1143 |
*/ |
|
1144 |
public final void setProtoCheck(final Object newProto) { |
|
1145 |
if (newProto == null || newProto instanceof ScriptObject) { |
|
1146 |
setProto((ScriptObject)newProto); |
|
1147 |
} else { |
|
16188 | 1148 |
final ScriptObject global = Context.getGlobalTrusted(); |
1149 |
final Object newProtoObject = JSType.toScriptObject(global, newProto); |
|
16147 | 1150 |
|
1151 |
if (newProtoObject instanceof ScriptObject) { |
|
1152 |
setProto((ScriptObject)newProtoObject); |
|
1153 |
} else { |
|
1154 |
typeError(global, "cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto)); |
|
1155 |
} |
|
1156 |
} |
|
1157 |
} |
|
1158 |
||
1159 |
/** |
|
1160 |
* return a List of own keys associated with the object. |
|
1161 |
* @param all True if to include non-enumerable keys. |
|
1162 |
* @return Array of keys. |
|
1163 |
*/ |
|
1164 |
public String[] getOwnKeys(final boolean all) { |
|
1165 |
final List<Object> keys = new ArrayList<>(); |
|
1166 |
final PropertyMap selfMap = this.getMap(); |
|
1167 |
||
1168 |
final ArrayData array = getArray(); |
|
1169 |
final long length = array.length(); |
|
1170 |
||
1171 |
for (long i = 0; i < length; i = array.nextIndex(i)) { |
|
1172 |
if (array.has((int)i)) { |
|
1173 |
keys.add(JSType.toString(i)); |
|
1174 |
} |
|
1175 |
} |
|
1176 |
||
1177 |
for (final Property property : selfMap.getProperties()) { |
|
1178 |
if (all || property.isEnumerable()) { |
|
1179 |
keys.add(property.getKey()); |
|
1180 |
} |
|
1181 |
} |
|
1182 |
||
1183 |
return keys.toArray(new String[keys.size()]); |
|
1184 |
} |
|
1185 |
||
1186 |
/** |
|
1187 |
* Check if this ScriptObject has array entries. This means that someone has |
|
1188 |
* set values with numeric keys in the object. |
|
1189 |
* |
|
1190 |
* Note: this can be O(n) up to the array length |
|
1191 |
* |
|
1192 |
* @return true if array entries exists. |
|
1193 |
*/ |
|
1194 |
public boolean hasArrayEntries() { |
|
1195 |
final ArrayData array = getArray(); |
|
1196 |
final long length = array.length(); |
|
1197 |
||
1198 |
for (long i = 0; i < length; i++) { |
|
1199 |
if (array.has((int)i)) { |
|
1200 |
return true; |
|
1201 |
} |
|
1202 |
} |
|
1203 |
||
1204 |
return false; |
|
1205 |
} |
|
1206 |
||
1207 |
/** |
|
1208 |
* Return the valid JavaScript type name descriptor |
|
1209 |
* |
|
1210 |
* @return "Object" |
|
1211 |
*/ |
|
1212 |
public String getClassName() { |
|
1213 |
return "Object"; |
|
1214 |
} |
|
1215 |
||
1216 |
/** |
|
1217 |
* {@code length} is a well known property. This is its getter. |
|
1218 |
* Note that this *may* be optimized by other classes |
|
1219 |
* |
|
1220 |
* @return length property value for this ScriptObject |
|
1221 |
*/ |
|
1222 |
public Object getLength() { |
|
1223 |
return get("length"); |
|
1224 |
} |
|
1225 |
||
1226 |
/** |
|
1227 |
* Stateless toString for ScriptObjects. |
|
1228 |
* |
|
1229 |
* @return string description of this object, e.g. {@code [object Object]} |
|
1230 |
*/ |
|
1231 |
public String safeToString() { |
|
1232 |
return "[object " + getClassName() + "]"; |
|
1233 |
} |
|
1234 |
||
1235 |
/** |
|
1236 |
* Return the default value of the object with a given preferred type hint. |
|
1237 |
* The preferred type hints are String.class for type String, Number.class |
|
1238 |
* for type Number. <p> |
|
1239 |
* |
|
1240 |
* A <code>hint</code> of null means "no hint". |
|
1241 |
* |
|
1242 |
* ECMA 8.12.8 [[DefaultValue]](hint) |
|
1243 |
* |
|
1244 |
* @param typeHint the preferred type hint |
|
1245 |
* @return the default value |
|
1246 |
*/ |
|
1247 |
public Object getDefaultValue(final Class<?> typeHint) { |
|
1248 |
// We delegate to GlobalObject, as the implementation uses dynamic call sites to invoke object's "toString" and |
|
1249 |
// "valueOf" methods, and in order to avoid those call sites from becoming megamorphic when multiple contexts |
|
1250 |
// are being executed in a long-running program, we move the code and their associated dynamic call sites |
|
1251 |
// (Global.TO_STRING and Global.VALUE_OF) into per-context code. |
|
16188 | 1252 |
return ((GlobalObject)Context.getGlobalTrusted()).getDefaultValue(this, typeHint); |
16147 | 1253 |
} |
1254 |
||
1255 |
/** |
|
1256 |
* Checking whether a script object is an instance of another. Used |
|
1257 |
* in {@link ScriptFunction} for hasInstance implementation, walks |
|
1258 |
* the proto chain |
|
1259 |
* |
|
1260 |
* @param instance instace to check |
|
1261 |
* @return true if instance of instance |
|
1262 |
*/ |
|
1263 |
public boolean isInstance(final ScriptObject instance) { |
|
1264 |
return false; |
|
1265 |
} |
|
1266 |
||
1267 |
/** |
|
1268 |
* Flag this ScriptObject as non extensible |
|
1269 |
* |
|
1270 |
* @return the object after being made non extensible |
|
1271 |
*/ |
|
1272 |
public ScriptObject preventExtensions() { |
|
1273 |
PropertyMap oldMap = getMap(); |
|
1274 |
||
1275 |
while (true) { |
|
1276 |
final PropertyMap newMap = getMap().preventExtensions(); |
|
1277 |
||
1278 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
1279 |
oldMap = getMap(); |
|
1280 |
} else { |
|
1281 |
return this; |
|
1282 |
} |
|
1283 |
} |
|
1284 |
} |
|
1285 |
||
1286 |
/** |
|
1287 |
* Check whether if an Object (not just a ScriptObject) represents JavaScript array |
|
1288 |
* |
|
1289 |
* @param obj object to check |
|
1290 |
* |
|
1291 |
* @return true if array |
|
1292 |
*/ |
|
1293 |
public static boolean isArray(final Object obj) { |
|
1294 |
return (obj instanceof ScriptObject) && ((ScriptObject)obj).isArray(); |
|
1295 |
} |
|
1296 |
||
1297 |
/** |
|
1298 |
* Check if this ScriptObject is an array |
|
1299 |
* @return true if array |
|
1300 |
*/ |
|
1301 |
public final boolean isArray() { |
|
1302 |
return (flags & IS_ARRAY) != 0; |
|
1303 |
} |
|
1304 |
||
1305 |
/** |
|
1306 |
* Flag this ScriptObject as being an array |
|
1307 |
*/ |
|
1308 |
public final void setIsArray() { |
|
1309 |
flags |= IS_ARRAY; |
|
1310 |
} |
|
1311 |
||
1312 |
/** |
|
1313 |
* Check if this ScriptObject is an {@code arguments} vector |
|
1314 |
* @return true if arguments vector |
|
1315 |
*/ |
|
1316 |
public final boolean isArguments() { |
|
1317 |
return (flags & IS_ARGUMENTS) != 0; |
|
1318 |
} |
|
1319 |
||
1320 |
/** |
|
1321 |
* Flag this ScriptObject as being an {@code arguments} vector |
|
1322 |
*/ |
|
1323 |
public final void setIsArguments() { |
|
1324 |
flags |= IS_ARGUMENTS; |
|
1325 |
} |
|
1326 |
||
1327 |
/** |
|
1328 |
* Get the {@link ArrayData} for this ScriptObject if it is an array |
|
1329 |
* @return array data |
|
1330 |
*/ |
|
1331 |
public final ArrayData getArray() { |
|
1332 |
return arrayData; |
|
1333 |
} |
|
1334 |
||
1335 |
/** |
|
1336 |
* Set the {@link ArrayData} for this ScriptObject if it is to be an array |
|
1337 |
* @param arrayData the array data |
|
1338 |
*/ |
|
1339 |
public final void setArray(final ArrayData arrayData) { |
|
1340 |
this.arrayData = arrayData; |
|
1341 |
} |
|
1342 |
||
1343 |
/** |
|
1344 |
* Check if this ScriptObject is extensible |
|
1345 |
* @return true if extensible |
|
1346 |
*/ |
|
1347 |
public boolean isExtensible() { |
|
1348 |
return getMap().isExtensible(); |
|
1349 |
} |
|
1350 |
||
1351 |
/** |
|
1352 |
* ECMAScript 15.2.3.8 - seal implementation |
|
1353 |
* @return the sealed ScriptObject |
|
1354 |
*/ |
|
1355 |
public ScriptObject seal() { |
|
1356 |
PropertyMap oldMap = getMap(); |
|
1357 |
||
1358 |
while (true) { |
|
1359 |
final PropertyMap newMap = getMap().seal(); |
|
1360 |
||
1361 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
1362 |
oldMap = getMap(); |
|
1363 |
} else { |
|
1364 |
setArray(ArrayData.seal(getArray())); |
|
1365 |
return this; |
|
1366 |
} |
|
1367 |
} |
|
1368 |
} |
|
1369 |
||
1370 |
/** |
|
1371 |
* Check whether this ScriptObject is sealed |
|
1372 |
* @return true if sealed |
|
1373 |
*/ |
|
1374 |
public boolean isSealed() { |
|
1375 |
return getMap().isSealed(); |
|
1376 |
} |
|
1377 |
||
1378 |
/** |
|
1379 |
* ECMA 15.2.39 - freeze implementation. Freeze this ScriptObject |
|
1380 |
* @return the frozen ScriptObject |
|
1381 |
*/ |
|
1382 |
public ScriptObject freeze() { |
|
1383 |
PropertyMap oldMap = getMap(); |
|
1384 |
||
1385 |
while (true) { |
|
1386 |
final PropertyMap newMap = getMap().freeze(); |
|
1387 |
||
1388 |
if (!compareAndSetMap(oldMap, newMap)) { |
|
1389 |
oldMap = getMap(); |
|
1390 |
} else { |
|
1391 |
setArray(ArrayData.freeze(getArray())); |
|
1392 |
return this; |
|
1393 |
} |
|
1394 |
} |
|
1395 |
} |
|
1396 |
||
1397 |
/** |
|
1398 |
* Check whether this ScriptObject is frozen |
|
1399 |
* @return true if frozed |
|
1400 |
*/ |
|
1401 |
public boolean isFrozen() { |
|
1402 |
return getMap().isFrozen(); |
|
1403 |
} |
|
1404 |
||
1405 |
||
1406 |
/** |
|
1407 |
* Flag this ScriptObject as scope |
|
1408 |
*/ |
|
1409 |
public final void setIsScope() { |
|
1410 |
if (Context.DEBUG) { |
|
1411 |
scopeCount++; |
|
1412 |
} |
|
1413 |
flags |= IS_SCOPE; |
|
1414 |
} |
|
1415 |
||
1416 |
/** |
|
1417 |
* Check whether this ScriptObject is scope |
|
1418 |
* @return true if scope |
|
1419 |
*/ |
|
1420 |
public final boolean isScope() { |
|
1421 |
return (flags & IS_SCOPE) != 0; |
|
1422 |
} |
|
1423 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1424 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1425 |
* Clears the properties from a ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1426 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1427 |
*/ |
16147 | 1428 |
public void clear() { |
1429 |
final boolean strict = getContext()._strict; |
|
1430 |
final Iterator<String> iter = propertyIterator(); |
|
1431 |
while (iter.hasNext()) { |
|
1432 |
delete(iter.next(), strict); |
|
1433 |
} |
|
1434 |
} |
|
1435 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1436 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1437 |
* Checks if a property with a given key is present in a ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1438 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1439 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1440 |
* @param key the key to check for |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1441 |
* @return true if a property with the given key exists, false otherwise |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1442 |
*/ |
16147 | 1443 |
public boolean containsKey(final Object key) { |
1444 |
return has(key); |
|
1445 |
} |
|
1446 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1447 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1448 |
* Checks if a property with a given value is present in a ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1449 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1450 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1451 |
* @param value value to check for |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1452 |
* @return true if a property with the given value exists, false otherwise |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1453 |
*/ |
16147 | 1454 |
public boolean containsValue(final Object value) { |
1455 |
final Iterator<Object> iter = valueIterator(); |
|
1456 |
while (iter.hasNext()) { |
|
1457 |
if (iter.next().equals(value)) { |
|
1458 |
return true; |
|
1459 |
} |
|
1460 |
} |
|
1461 |
return false; |
|
1462 |
} |
|
1463 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1464 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1465 |
* Returns the set of <property, value> entries that make up this |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1466 |
* ScriptObject's properties |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1467 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1468 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1469 |
* @return an entry set of all the properties in this object |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1470 |
*/ |
16147 | 1471 |
public Set<Map.Entry<Object, Object>> entrySet() { |
1472 |
final Iterator<String> iter = propertyIterator(); |
|
1473 |
final Set<Map.Entry<Object, Object>> entries = new HashSet<>(); |
|
1474 |
while (iter.hasNext()) { |
|
1475 |
final Object key = iter.next(); |
|
1476 |
entries.add(new AbstractMap.SimpleImmutableEntry<>(key, get(key))); |
|
1477 |
} |
|
1478 |
return Collections.unmodifiableSet(entries); |
|
1479 |
} |
|
1480 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1481 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1482 |
* Check whether a ScriptObject contains no properties |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1483 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1484 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1485 |
* @return true if object has no properties |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1486 |
*/ |
16147 | 1487 |
public boolean isEmpty() { |
1488 |
return !propertyIterator().hasNext(); |
|
1489 |
} |
|
1490 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1491 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1492 |
* Return the set of keys (property names) for all properties |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1493 |
* in this ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1494 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1495 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1496 |
* @return keySet of this ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1497 |
*/ |
16147 | 1498 |
public Set<Object> keySet() { |
1499 |
final Iterator<String> iter = propertyIterator(); |
|
1500 |
final Set<Object> keySet = new HashSet<>(); |
|
1501 |
while (iter.hasNext()) { |
|
1502 |
keySet.add(iter.next()); |
|
1503 |
} |
|
1504 |
return Collections.unmodifiableSet(keySet); |
|
1505 |
} |
|
1506 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1507 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1508 |
* Put a property in the ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1509 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1510 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1511 |
* @param key property key |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1512 |
* @param value property value |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1513 |
* @return oldValue if property with same key existed already |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1514 |
*/ |
16147 | 1515 |
public Object put(final Object key, final Object value) { |
1516 |
final Object oldValue = get(key); |
|
1517 |
set(key, value, getContext()._strict); |
|
1518 |
return oldValue; |
|
1519 |
} |
|
1520 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1521 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1522 |
* Put several properties in the ScriptObject given a mapping |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1523 |
* of their keys to their values |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1524 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1525 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1526 |
* @param otherMap a <key,value> map of properties to add |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1527 |
*/ |
16147 | 1528 |
public void putAll(final Map<?, ?> otherMap) { |
1529 |
final boolean strict = getContext()._strict; |
|
1530 |
for (final Map.Entry<?, ?> entry : otherMap.entrySet()) { |
|
1531 |
set(entry.getKey(), entry.getValue(), strict); |
|
1532 |
} |
|
1533 |
} |
|
1534 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1535 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1536 |
* Remove a property from the ScriptObject. |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1537 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1538 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1539 |
* @param key the key of the property |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1540 |
* @return the oldValue of the removed property |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1541 |
*/ |
16147 | 1542 |
public Object remove(final Object key) { |
1543 |
final Object oldValue = get(key); |
|
1544 |
delete(key, getContext()._strict); |
|
1545 |
return oldValue; |
|
1546 |
} |
|
1547 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1548 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1549 |
* Return the size of the ScriptObject - i.e. the number of properties |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1550 |
* it contains |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1551 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1552 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1553 |
* @return number of properties in ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1554 |
*/ |
16147 | 1555 |
public int size() { |
1556 |
int n = 0; |
|
1557 |
for (final Iterator<String> iter = propertyIterator(); iter.hasNext(); iter.next()) { |
|
1558 |
n++; |
|
1559 |
} |
|
1560 |
return n; |
|
1561 |
} |
|
1562 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1563 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1564 |
* Return the values of the properties in the ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1565 |
* (java.util.Map-like method to help ScriptObjectMirror implementation) |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1566 |
* |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1567 |
* @return collection of values for the properties in this ScriptObject |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16195
diff
changeset
|
1568 |
*/ |
16147 | 1569 |
public Collection<Object> values() { |
1570 |
final List<Object> values = new ArrayList<>(size()); |
|
1571 |
final Iterator<Object> iter = valueIterator(); |
|
1572 |
while (iter.hasNext()) { |
|
1573 |
values.add(iter.next()); |
|
1574 |
} |
|
1575 |
return Collections.unmodifiableList(values); |
|
1576 |
} |
|
1577 |
||
1578 |
/** |
|
1579 |
* Lookup method that, given a CallSiteDescriptor, looks up the target |
|
1580 |
* MethodHandle and creates a GuardedInvocation |
|
1581 |
* with the appropriate guard(s). |
|
1582 |
* |
|
1583 |
* @param desc call site descriptor |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1584 |
* @param request the link request |
16147 | 1585 |
* |
1586 |
* @return GuardedInvocation for the callsite |
|
1587 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1588 |
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 1589 |
final int c = desc.getNameTokenCount(); |
1590 |
// JavaScript is "immune" to all currently defined Dynalink composite operation - getProp is the same as getElem |
|
1591 |
// is the same as getMethod as JavaScript objects have a single namespace for all three. Therefore, we don't |
|
1592 |
// care about them, and just link to whatever is the first operation. |
|
1593 |
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); |
|
1594 |
// NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself |
|
1595 |
// emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are |
|
1596 |
// more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the |
|
1597 |
// operation has an associated name or not. |
|
1598 |
switch (operator) { |
|
1599 |
case "getProp": |
|
1600 |
case "getElem": |
|
1601 |
case "getMethod": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1602 |
return c > 2 ? findGetMethod(desc, request, operator) : findGetIndexMethod(desc, request); |
16147 | 1603 |
case "setProp": |
1604 |
case "setElem": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1605 |
return c > 2 ? findSetMethod(desc, request) : findSetIndexMethod(desc); |
16147 | 1606 |
case "call": |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1607 |
return findCallMethod(desc, request); |
16147 | 1608 |
case "new": |
1609 |
return findNewMethod(desc); |
|
1610 |
case "callMethod": |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1611 |
return findCallMethodMethod(desc, request); |
16147 | 1612 |
default: |
1613 |
return null; |
|
1614 |
} |
|
1615 |
} |
|
1616 |
||
1617 |
/** |
|
1618 |
* Find the appropriate New method for an invoke dynamic call. |
|
1619 |
* |
|
1620 |
* @param desc The invoke dynamic call site descriptor. |
|
1621 |
* |
|
1622 |
* @return GuardedInvocation to be invoked at call site. |
|
1623 |
*/ |
|
1624 |
protected GuardedInvocation findNewMethod(final CallSiteDescriptor desc) { |
|
1625 |
return notAFunction(); |
|
1626 |
} |
|
1627 |
||
1628 |
/** |
|
1629 |
* Find the appropriate CALL method for an invoke dynamic call. |
|
1630 |
* This generates "not a function" always |
|
1631 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1632 |
* @param desc the call site descriptor. |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1633 |
* @param request the link request |
16147 | 1634 |
* |
1635 |
* @return GuardedInvocation to be invoed at call site. |
|
1636 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1637 |
protected GuardedInvocation findCallMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 1638 |
return notAFunction(); |
1639 |
} |
|
1640 |
||
1641 |
private GuardedInvocation notAFunction() { |
|
16188 | 1642 |
typeError("not.a.function", ScriptRuntime.safeToString(this)); |
16147 | 1643 |
return null; |
1644 |
} |
|
1645 |
||
1646 |
/** |
|
16173 | 1647 |
* Find an implementation for a "dyn:callMethod" operation. Note that Nashorn internally never uses |
1648 |
* "dyn:callMethod", but instead always emits two call sites in bytecode, one for "dyn:getMethod", and then another |
|
1649 |
* one for "dyn:call". Explicit support for "dyn:callMethod" is provided for the benefit of potential external |
|
1650 |
* callers. The implementation itself actually folds a "dyn:getMethod" method handle into a "dyn:call" method handle. |
|
16147 | 1651 |
* |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1652 |
* @param desc the call site descriptor. |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1653 |
* @param request the link request |
16147 | 1654 |
* |
1655 |
* @return GuardedInvocation to be invoked at call site. |
|
1656 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1657 |
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16173 | 1658 |
// R(P0, P1, ...) |
1659 |
final MethodType callType = desc.getMethodType(); |
|
1660 |
// use type Object(P0) for the getter |
|
1661 |
final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0))); |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1662 |
final GuardedInvocation getter = findGetMethod(getterType, request, "getMethod"); |
16173 | 1663 |
|
1664 |
// Object(P0) => Object(P0, P1, ...) |
|
1665 |
final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount())); |
|
1666 |
// R(Object, P0, P1, ...) |
|
1667 |
final MethodHandle invoker = Bootstrap.createDynamicInvoker("dyn:call", callType.insertParameterTypes(0, argDroppingGetter.type().returnType())); |
|
1668 |
// Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...) |
|
1669 |
return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard()); |
|
16147 | 1670 |
} |
1671 |
||
1672 |
/** |
|
1673 |
* Find the appropriate GET method for an invoke dynamic call. |
|
1674 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1675 |
* @param desc the call site descriptor |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1676 |
* @param request the link request |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1677 |
* @param operator operator for get: getProp, getMethod, getElem etc |
16147 | 1678 |
* |
1679 |
* @return GuardedInvocation to be invoked at call site. |
|
1680 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1681 |
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) { |
16147 | 1682 |
final String name = desc.getNameToken(2); |
1683 |
||
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1684 |
if (request.isCallSiteUnstable()) { |
16147 | 1685 |
return findMegaMorphicGetMethod(desc, name); |
1686 |
} |
|
1687 |
||
1688 |
final FindProperty find = findProperty(name, true); |
|
1689 |
||
1690 |
MethodHandle methodHandle; |
|
1691 |
||
1692 |
if (find == null) { |
|
1693 |
if ("getProp".equals(operator)) { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1694 |
return noSuchProperty(desc, request); |
16147 | 1695 |
} else if ("getMethod".equals(operator)) { |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1696 |
return noSuchMethod(desc, request); |
16147 | 1697 |
} else if ("getElem".equals(operator)) { |
1698 |
return createEmptyGetter(desc, name); |
|
1699 |
} |
|
1700 |
throw new AssertionError(); // never invoked with any other operation |
|
1701 |
} |
|
1702 |
||
1703 |
final Class<?> returnType = desc.getMethodType().returnType(); |
|
1704 |
final Property property = find.getProperty(); |
|
1705 |
methodHandle = find.getGetter(returnType); |
|
1706 |
||
1707 |
// getMap() is fine as we have the prototype switchpoint depending on where the property was found |
|
1708 |
final MethodHandle guard = NashornGuards.getMapGuard(getMap()); |
|
1709 |
||
1710 |
if (methodHandle != null) { |
|
1711 |
assert methodHandle.type().returnType().equals(returnType); |
|
1712 |
if (find.isSelf()) { |
|
16207
ed4aec2d599c
8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents:
16205
diff
changeset
|
1713 |
return new GuardedInvocation(methodHandle, ObjectClassGenerator.OBJECT_FIELDS_ONLY && |
ed4aec2d599c
8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents:
16205
diff
changeset
|
1714 |
NashornCallSiteDescriptor.isFastScope(desc) && !property.canChangeType() ? null : guard); |
16147 | 1715 |
} |
1716 |
||
1717 |
final ScriptObject prototype = find.getOwner(); |
|
1718 |
||
1719 |
if (!property.hasGetterFunction()) { |
|
1720 |
methodHandle = bindTo(methodHandle, prototype); |
|
1721 |
} |
|
16207
ed4aec2d599c
8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents:
16205
diff
changeset
|
1722 |
return new GuardedInvocation(methodHandle, getMap().getProtoGetSwitchPoint(name), guard); |
16147 | 1723 |
} |
1724 |
||
1725 |
assert !NashornCallSiteDescriptor.isFastScope(desc); |
|
16186
91bd9d2aa2f5
8006570: This-value for non-strict functions should be converted to object
hannesw
parents:
16185
diff
changeset
|
1726 |
return new GuardedInvocation(Lookup.emptyGetter(returnType), getMap().getProtoGetSwitchPoint(name), guard); |
16147 | 1727 |
} |
1728 |
||
1729 |
private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name) { |
|
1730 |
final GuardedInvocation inv = findGetIndexMethod(desc.getMethodType().insertParameterTypes(1, Object.class)); |
|
1731 |
return inv.replaceMethods(MH.insertArguments(inv.getInvocation(), 1, name), inv.getGuard()); |
|
1732 |
} |
|
1733 |
||
1734 |
/** |
|
1735 |
* Find the appropriate GETINDEX method for an invoke dynamic call. |
|
1736 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1737 |
* @param desc the call site descriptor |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1738 |
* @param request the link request |
16147 | 1739 |
* |
1740 |
* @return GuardedInvocation to be invoked at call site. |
|
1741 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1742 |
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 1743 |
return findGetIndexMethod(desc.getMethodType()); |
1744 |
} |
|
1745 |
||
1746 |
/** |
|
1747 |
* Find the appropriate GETINDEX method for an invoke dynamic call. |
|
1748 |
* |
|
1749 |
* @param callType the call site method type |
|
1750 |
* @return GuardedInvocation to be invoked at call site. |
|
1751 |
*/ |
|
1752 |
private static GuardedInvocation findGetIndexMethod(final MethodType callType) { |
|
1753 |
final Class<?> returnClass = callType.returnType(); |
|
1754 |
final Class<?> keyClass = callType.parameterType(1); |
|
1755 |
||
1756 |
String name = "get"; |
|
1757 |
if (returnClass.isPrimitive()) { |
|
1758 |
//turn e.g. get with a double into getDouble |
|
1759 |
final String returnTypeName = returnClass.getName(); |
|
1760 |
name += Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length()); |
|
1761 |
} |
|
1762 |
||
1763 |
return new GuardedInvocation(findOwnMH(name, returnClass, keyClass), getScriptObjectGuard(callType)); |
|
1764 |
} |
|
1765 |
||
1766 |
private static MethodHandle getScriptObjectGuard(final MethodType type) { |
|
1767 |
return ScriptObject.class.isAssignableFrom(type.parameterType(0)) ? null : NashornGuards.getScriptObjectGuard(); |
|
1768 |
} |
|
1769 |
||
1770 |
/** |
|
1771 |
* Find the appropriate SET method for an invoke dynamic call. |
|
1772 |
* |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1773 |
* @param desc the call site descriptor |
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1774 |
* @param request the link request |
16147 | 1775 |
* |
1776 |
* @return GuardedInvocation to be invoked at call site. |
|
1777 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1778 |
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16159 | 1779 |
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1780 |
if(request.isCallSiteUnstable()) { |
16147 | 1781 |
return findMegaMorphicSetMethod(desc, name); |
1782 |
} |
|
1783 |
||
16161
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1784 |
final boolean scope = isScope(); |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1785 |
/* |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1786 |
* If doing property set on a scope object, we should stop proto search on the first |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1787 |
* non-scope object. Without this, for exmaple, when assigning "toString" on global scope, |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1788 |
* we'll end up assigning it on it's proto - which is Object.prototype.toString !! |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1789 |
* |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1790 |
* toString = function() { print("global toString"); } // don't affect Object.prototype.toString |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1791 |
*/ |
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1792 |
FindProperty find = findProperty(name, true, scope); |
16159 | 1793 |
// If it's not a scope search, then we don't want any inherited properties except those with user defined accessors. |
16161
0ebfaec3a45b
8005848: assigning to global toString variable affects Object.prototype.toString
sundar
parents:
16159
diff
changeset
|
1794 |
if (!scope && find != null && find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) { |
16159 | 1795 |
// We should still check if inherited data property is not writable |
1796 |
if (isExtensible() && !find.isWritable()) { |
|
1797 |
return createEmptySetMethod(desc, "property.not.writable", false); |
|
16147 | 1798 |
} |
16159 | 1799 |
// Otherwise, forget the found property |
1800 |
find = null; |
|
16147 | 1801 |
} |
1802 |
||
1803 |
if (find != null) { |
|
16159 | 1804 |
if(!find.isWritable()) { |
1805 |
// Existing, non-writable property |
|
1806 |
return createEmptySetMethod(desc, "property.not.writable", true); |
|
16147 | 1807 |
} |
1808 |
} else if (!isExtensible()) { |
|
16159 | 1809 |
// Non-existing property on a non-extensible object |
1810 |
return createEmptySetMethod(desc, "object.non.extensible", false); |
|
16147 | 1811 |
} |
1812 |
||
16159 | 1813 |
return new SetMethodCreator(this, find, desc).createGuardedInvocation(); |
1814 |
} |
|
1815 |
||
1816 |
private GuardedInvocation createEmptySetMethod(final CallSiteDescriptor desc, String strictErrorMessage, boolean canBeFastScope) { |
|
1817 |
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
|
1818 |
if (NashornCallSiteDescriptor.isStrict(desc)) { |
|
16188 | 1819 |
typeError(strictErrorMessage, name, ScriptRuntime.safeToString((this))); |
16159 | 1820 |
} |
1821 |
assert canBeFastScope || !NashornCallSiteDescriptor.isFastScope(desc); |
|
16168
f0c208287983
8005976: Break out AccessSpecializer into one pass before CodeGenerator instead of iterative applications from CodeGenerator
lagergren
parents:
16161
diff
changeset
|
1822 |
final PropertyMap myMap = getMap(); |
f0c208287983
8005976: Break out AccessSpecializer into one pass before CodeGenerator instead of iterative applications from CodeGenerator
lagergren
parents:
16161
diff
changeset
|
1823 |
return new GuardedInvocation(Lookup.EMPTY_SETTER, myMap.getProtoGetSwitchPoint(name), NashornGuards.getMapGuard(myMap)); |
16147 | 1824 |
} |
1825 |
||
1826 |
@SuppressWarnings("unused") |
|
1827 |
private static void setEmbed(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final MethodHandle setter, final int i, final Object self, final Object value) throws Throwable { |
|
1828 |
final ScriptObject obj = (ScriptObject)self; |
|
16168
f0c208287983
8005976: Break out AccessSpecializer into one pass before CodeGenerator instead of iterative applications from CodeGenerator
lagergren
parents:
16161
diff
changeset
|
1829 |
if (obj.trySetEmbedOrSpill(desc, oldMap, newMap, value)) { |
16147 | 1830 |
obj.useEmbed(i); |
1831 |
setter.invokeExact(self, value); |
|
1832 |
} |
|
1833 |
} |
|
1834 |
||
1835 |
@SuppressWarnings("unused") |
|
1836 |
private static void setSpill(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final int index, final Object self, final Object value) { |
|
1837 |
final ScriptObject obj = (ScriptObject)self; |
|
16168
f0c208287983
8005976: Break out AccessSpecializer into one pass before CodeGenerator instead of iterative applications from CodeGenerator
lagergren
parents:
16161
diff
changeset
|
1838 |
if (obj.trySetEmbedOrSpill(desc, oldMap, newMap, value)) { |
16147 | 1839 |
obj.spill[index] = value; |
1840 |
} |
|
1841 |
} |
|
1842 |
||
1843 |
private boolean trySetEmbedOrSpill(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final Object value) { |
|
1844 |
final boolean isStrict = NashornCallSiteDescriptor.isStrict(desc); |
|
1845 |
if (!isExtensible() && isStrict) { |
|
16188 | 1846 |
typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(this)); |
16147 | 1847 |
throw new AssertionError(); // never reached |
1848 |
} else if (compareAndSetMap(oldMap, newMap)) { |
|
1849 |
return true; |
|
1850 |
} else { |
|
1851 |
set(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND), value, isStrict); |
|
1852 |
return false; |
|
1853 |
} |
|
1854 |
} |
|
1855 |
||
1856 |
@SuppressWarnings("unused") |
|
1857 |
private static void setSpillWithNew(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final int index, final Object self, final Object value) { |
|
1858 |
final ScriptObject obj = (ScriptObject)self; |
|
1859 |
final boolean isStrict = NashornCallSiteDescriptor.isStrict(desc); |
|
1860 |
||
1861 |
if (!obj.isExtensible()) { |
|
1862 |
if (isStrict) { |
|
16188 | 1863 |
typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj)); |
16147 | 1864 |
} |
1865 |
} else if (obj.compareAndSetMap(oldMap, newMap)) { |
|
1866 |
obj.spill = new Object[SPILL_RATE]; |
|
1867 |
obj.spill[index] = value; |
|
1868 |
} else { |
|
1869 |
obj.set(desc.getNameToken(2), value, isStrict); |
|
1870 |
} |
|
1871 |
} |
|
1872 |
||
1873 |
@SuppressWarnings("unused") |
|
1874 |
private static void setSpillWithGrow(final CallSiteDescriptor desc, final PropertyMap oldMap, final PropertyMap newMap, final int index, final int newLength, final Object self, final Object value) { |
|
1875 |
final ScriptObject obj = (ScriptObject)self; |
|
1876 |
final boolean isStrict = NashornCallSiteDescriptor.isStrict(desc); |
|
1877 |
||
1878 |
if (!obj.isExtensible()) { |
|
1879 |
if (isStrict) { |
|
16188 | 1880 |
typeError("object.non.extensible", desc.getNameToken(2), ScriptRuntime.safeToString(obj)); |
16147 | 1881 |
} |
1882 |
} else if (obj.compareAndSetMap(oldMap, newMap)) { |
|
1883 |
final int oldLength = obj.spill.length; |
|
1884 |
final Object[] newSpill = new Object[newLength]; |
|
1885 |
System.arraycopy(obj.spill, 0, newSpill, 0, oldLength); |
|
1886 |
obj.spill = newSpill; |
|
1887 |
obj.spill[index] = value; |
|
1888 |
} else { |
|
1889 |
obj.set(desc.getNameToken(2), value, isStrict); |
|
1890 |
} |
|
1891 |
} |
|
1892 |
||
1893 |
private static GuardedInvocation findMegaMorphicSetMethod(final CallSiteDescriptor desc, final String name) { |
|
1894 |
final GuardedInvocation inv = findSetIndexMethod(desc.getMethodType().insertParameterTypes(1, Object.class), |
|
1895 |
NashornCallSiteDescriptor.isStrict(desc)); |
|
1896 |
return inv.replaceMethods(MH.insertArguments(inv.getInvocation(), 1, name), inv.getGuard()); |
|
1897 |
} |
|
1898 |
||
1899 |
private static GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc) { // array, index, value |
|
1900 |
return findSetIndexMethod(desc.getMethodType(), NashornCallSiteDescriptor.isStrict(desc)); |
|
1901 |
} |
|
1902 |
||
1903 |
/** |
|
1904 |
* Find the appropriate SETINDEX method for an invoke dynamic call. |
|
1905 |
* |
|
1906 |
* @param callType the method type at the call site |
|
1907 |
* @param isStrict are we in strict mode? |
|
1908 |
* |
|
1909 |
* @return GuardedInvocation to be invoked at call site. |
|
1910 |
*/ |
|
1911 |
private static GuardedInvocation findSetIndexMethod(final MethodType callType, final boolean isStrict) { |
|
1912 |
assert callType.parameterCount() == 3; |
|
1913 |
||
1914 |
final Class<?> keyClass = callType.parameterType(1); |
|
1915 |
final Class<?> valueClass = callType.parameterType(2); |
|
1916 |
||
1917 |
MethodHandle methodHandle = findOwnMH("set", void.class, keyClass, valueClass, boolean.class); |
|
1918 |
methodHandle = MH.insertArguments(methodHandle, 3, isStrict); |
|
1919 |
||
1920 |
return new GuardedInvocation(methodHandle, getScriptObjectGuard(callType)); |
|
1921 |
} |
|
1922 |
||
1923 |
/** |
|
1924 |
* Fall back if a function property is not found. |
|
1925 |
* @param desc The call site descriptor |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1926 |
* @param request the link request |
16147 | 1927 |
* @return GuardedInvocation to be invoked at call site. |
1928 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1929 |
public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 1930 |
final String name = desc.getNameToken(2); |
1931 |
final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true); |
|
1932 |
final boolean scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc); |
|
1933 |
||
1934 |
if (find == null) { |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
1935 |
return noSuchProperty(desc, request); |
16147 | 1936 |
} |
1937 |
||
1938 |
final ScriptFunction func = (ScriptFunction)getObjectValue(find); |
|
1939 |
final Object thiz = scopeCall && func.isStrict() ? ScriptRuntime.UNDEFINED : this; |
|
1940 |
// TODO: It'd be awesome if we could bind "name" without binding "this". |
|
1941 |
return new GuardedInvocation(MH.dropArguments(MH.constant(ScriptFunction.class, |
|
1942 |
func.makeBoundFunction(thiz, new Object[] { name })), 0, Object.class), |
|
1943 |
null, NashornGuards.getMapGuard(getMap())); |
|
1944 |
} |
|
1945 |
||
1946 |
/** |
|
1947 |
* Fall back if a property is not found. |
|
1948 |
* @param desc the call site descriptor. |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1949 |
* @param request the link request |
16147 | 1950 |
* @return GuardedInvocation to be invoked at call site. |
1951 |
*/ |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16194
diff
changeset
|
1952 |
public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 1953 |
final String name = desc.getNameToken(2); |
1954 |
final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); |
|
1955 |
final boolean scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc); |
|
1956 |
||
1957 |
if (find != null) { |
|
1958 |
final ScriptFunction func = (ScriptFunction)getObjectValue(find); |
|
1959 |
MethodHandle methodHandle = getCallMethodHandle(func, desc.getMethodType(), name); |
|
1960 |
||
1961 |
if (methodHandle != null) { |
|
1962 |
if (scopeAccess && func.isStrict()) { |
|
1963 |
methodHandle = bindTo(methodHandle, UNDEFINED); |
|
1964 |
} |
|
1965 |
return new GuardedInvocation(methodHandle, |
|
1966 |
find.isInherited()? getMap().getProtoGetSwitchPoint(NO_SUCH_PROPERTY_NAME) : null, |
|
1967 |
getKnownFunctionPropertyGuard(getMap(), find.getGetter(Object.class), find.getOwner(), func)); |
|
1968 |
} |
|
1969 |
} |
|
1970 |
||
1971 |
if (scopeAccess) { |
|
16188 | 1972 |
referenceError("not.defined", name); |
16147 | 1973 |
} |
1974 |
||
1975 |
return createEmptyGetter(desc, name); |
|
1976 |
} |
|
1977 |
||
1978 |
private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final String name) { |
|
1979 |
return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()), getMap().getProtoGetSwitchPoint(name), NashornGuards.getMapGuard(getMap())); |
|
1980 |
} |
|
1981 |
||
1982 |
private abstract static class ScriptObjectIterator <T extends Object> implements Iterator<T> { |
|
1983 |
protected T[] values; |
|
1984 |
protected final ScriptObject object; |
|
1985 |
private int index; |
|
1986 |
||
1987 |
ScriptObjectIterator(final ScriptObject object) { |
|
1988 |
this.object = object; |
|
1989 |
} |
|
1990 |
||
1991 |
protected abstract void init(); |
|
1992 |
||
1993 |
@Override |
|
1994 |
public boolean hasNext() { |
|
1995 |
if (values == null) { |
|
1996 |
init(); |
|
1997 |
} |
|
1998 |
return index < values.length; |
|
1999 |
} |
|
2000 |
||
2001 |
@Override |
|
2002 |
public T next() { |
|
2003 |
if (values == null) { |
|
2004 |
init(); |
|
2005 |
} |
|
2006 |
return values[index++]; |
|
2007 |
} |
|
2008 |
||
2009 |
@Override |
|
2010 |
public void remove() { |
|
2011 |
throw new UnsupportedOperationException(); |
|
2012 |
} |
|
2013 |
} |
|
2014 |
||
2015 |
private static class KeyIterator extends ScriptObjectIterator<String> { |
|
2016 |
KeyIterator(final ScriptObject object) { |
|
2017 |
super(object); |
|
2018 |
} |
|
2019 |
||
2020 |
@Override |
|
2021 |
protected void init() { |
|
2022 |
final Set<String> keys = new LinkedHashSet<>(); |
|
2023 |
for (ScriptObject self = object; self != null; self = self.getProto()) { |
|
2024 |
keys.addAll(Arrays.asList(self.getOwnKeys(false))); |
|
2025 |
} |
|
2026 |
this.values = keys.toArray(new String[keys.size()]); |
|
2027 |
} |
|
2028 |
} |
|
2029 |
||
2030 |
private static class ValueIterator extends ScriptObjectIterator<Object> { |
|
2031 |
ValueIterator(final ScriptObject object) { |
|
2032 |
super(object); |
|
2033 |
} |
|
2034 |
||
2035 |
@Override |
|
2036 |
protected void init() { |
|
2037 |
final ArrayList<Object> valueList = new ArrayList<>(); |
|
2038 |
for (ScriptObject self = object; self != null; self = self.getProto()) { |
|
2039 |
for (final String key : self.getOwnKeys(false)) { |
|
2040 |
valueList.add(self.get(key)); |
|
2041 |
} |
|
2042 |
} |
|
2043 |
this.values = valueList.toArray(new Object[valueList.size()]); |
|
2044 |
} |
|
2045 |
} |
|
2046 |
||
2047 |
/** |
|
2048 |
* Add a spill property for the given key. |
|
2049 |
* @param key Property key. |
|
2050 |
* @param propertyFlags Property flags. |
|
2051 |
* @return Added property. |
|
2052 |
*/ |
|
2053 |
private Property addSpillProperty(final String key, final int propertyFlags) { |
|
2054 |
int i = findEmbed(); |
|
2055 |
Property spillProperty; |
|
2056 |
||
2057 |
if (i >= EMBED_SIZE) { |
|
2058 |
i = getMap().getSpillLength(); |
|
2059 |
MethodHandle getter = MH.arrayElementGetter(Object[].class); |
|
2060 |
MethodHandle setter = MH.arrayElementSetter(Object[].class); |
|
2061 |
getter = MH.asType(MH.insertArguments(getter, 1, i), Lookup.GET_OBJECT_TYPE); |
|
2062 |
setter = MH.asType(MH.insertArguments(setter, 1, i), Lookup.SET_OBJECT_TYPE); |
|
2063 |
spillProperty = new SpillProperty(key, propertyFlags | Property.IS_SPILL, i, getter, setter); |
|
2064 |
notifyPropertyAdded(this, spillProperty); |
|
2065 |
spillProperty = addOwnProperty(spillProperty); |
|
2066 |
i = spillProperty.getSlot(); |
|
2067 |
||
2068 |
final int newLength = (i + SPILL_RATE) / SPILL_RATE * SPILL_RATE; |
|
2069 |
final Object[] newSpill = new Object[newLength]; |
|
2070 |
||
2071 |
if (spill != null) { |
|
2072 |
System.arraycopy(spill, 0, newSpill, 0, spill.length); |
|
2073 |
} |
|
2074 |
||
2075 |
spill = newSpill; |
|
2076 |
} else { |
|
2077 |
useEmbed(i); |
|
2078 |
spillProperty = new SpillProperty(key, propertyFlags, i, GET_EMBED[i], SET_EMBED[i]); |
|
2079 |
notifyPropertyAdded(this, spillProperty); |
|
2080 |
spillProperty = addOwnProperty(spillProperty); |
|
2081 |
} |
|
2082 |
||
2083 |
return spillProperty; |
|
2084 |
} |
|
2085 |
||
2086 |
||
2087 |
/** |
|
2088 |
* Add a spill entry for the given key. |
|
2089 |
* @param key Property key. |
|
2090 |
* @param propertyFlags Property flags. |
|
2091 |
* @return Setter method handle. |
|
2092 |
*/ |
|
2093 |
private MethodHandle addSpill(final String key, final int propertyFlags) { |
|
2094 |
final Property spillProperty = addSpillProperty(key, propertyFlags); |
|
2095 |
final Class<?> type = Object.class; |
|
2096 |
return spillProperty.getSetter(type, getMap()); //TODO specfields |
|
2097 |
} |
|
2098 |
||
16159 | 2099 |
MethodHandle addSpill(final String key) { |
16147 | 2100 |
return addSpill(key, 0); |
2101 |
} |
|
2102 |
||
2103 |
/** |
|
2104 |
* Make sure arguments are paired correctly, with respect to more parameters than declared, |
|
2105 |
* fewer parameters than declared and other things that JavaScript allows. This might involve |
|
2106 |
* creating collectors. |
|
2107 |
* |
|
2108 |
* @param methodHandle method handle for invoke |
|
2109 |
* @param callType type of the call |
|
2110 |
* |
|
2111 |
* @return method handle with adjusted arguments |
|
2112 |
*/ |
|
2113 |
protected static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType) { |
|
2114 |
return pairArguments(methodHandle, callType, null); |
|
2115 |
} |
|
2116 |
||
2117 |
/** |
|
2118 |
* Make sure arguments are paired correctly, with respect to more parameters than declared, |
|
2119 |
* fewer parameters than declared and other things that JavaScript allows. This might involve |
|
2120 |
* creating collectors. |
|
2121 |
* |
|
2122 |
* Make sure arguments are paired correctly. |
|
2123 |
* @param methodHandle MethodHandle to adjust. |
|
2124 |
* @param callType MethodType of caller. |
|
2125 |
* @param callerVarArg true if the caller is vararg, false otherwise, null if it should be inferred. |
|
2126 |
* |
|
2127 |
* @return method handle with adjusted arguments |
|
2128 |
*/ |
|
2129 |
public static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType, final Boolean callerVarArg) { |
|
2130 |
||
2131 |
final MethodType methodType = methodHandle.type(); |
|
2132 |
if (methodType.equals(callType)) { |
|
2133 |
return methodHandle; |
|
2134 |
} |
|
2135 |
||
2136 |
final int parameterCount = methodType.parameterCount(); |
|
2137 |
final int callCount = callType.parameterCount(); |
|
2138 |
||
16194
3d0b930f4b47
8006857: ClassCastException when interface implementing function uses arguments object
sundar
parents:
16188
diff
changeset
|
2139 |
final boolean isCalleeVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray(); |
16147 | 2140 |
final boolean isCallerVarArg = callerVarArg != null ? callerVarArg.booleanValue() : (callCount > 1 && |
2141 |
callType.parameterType(callCount - 1).isArray()); |
|
2142 |
||
2143 |
if (callCount < parameterCount) { |
|
2144 |
final int missingArgs = parameterCount - callCount; |
|
2145 |
final Object[] fillers = new Object[missingArgs]; |
|
2146 |
||
2147 |
Arrays.fill(fillers, UNDEFINED); |
|
2148 |
||
2149 |
if (isCalleeVarArg) { |
|
2150 |
fillers[missingArgs - 1] = new Object[0]; |
|
2151 |
} |
|
2152 |
||
2153 |
return MH.insertArguments( |
|
2154 |
methodHandle, |
|
2155 |
parameterCount - missingArgs, |
|
2156 |
fillers); |
|
2157 |
} |
|
2158 |
||
2159 |
if (isCalleeVarArg) { |
|
2160 |
return isCallerVarArg ? |
|
2161 |
methodHandle : |
|
2162 |
MH.asCollector(methodHandle, Object[].class, callCount - parameterCount + 1); |
|
2163 |
} |
|
2164 |
||
2165 |
if (isCallerVarArg) { |
|
2166 |
final int spreadArgs = parameterCount - callCount + 1; |
|
2167 |
return MH.filterArguments( |
|
2168 |
MH.asSpreader( |
|
2169 |
methodHandle, |
|
2170 |
Object[].class, |
|
2171 |
spreadArgs), |
|
2172 |
callCount - 1, |
|
2173 |
MH.insertArguments( |
|
2174 |
TRUNCATINGFILTER, |
|
2175 |
0, |
|
2176 |
spreadArgs) |
|
2177 |
); |
|
2178 |
} |
|
2179 |
||
2180 |
if (callCount > parameterCount) { |
|
2181 |
final int discardedArgs = callCount - parameterCount; |
|
2182 |
||
2183 |
final Class<?>[] discards = new Class<?>[discardedArgs]; |
|
2184 |
Arrays.fill(discards, Object.class); |
|
2185 |
||
2186 |
return MH.dropArguments(methodHandle, callCount - discardedArgs, discards); |
|
2187 |
} |
|
2188 |
||
2189 |
return methodHandle; |
|
2190 |
} |
|
2191 |
||
2192 |
@SuppressWarnings("unused") |
|
2193 |
private static Object[] truncatingFilter(final int n, final Object[] array) { |
|
2194 |
final int length = array == null ? 0 : array.length; |
|
2195 |
if (n == length) { |
|
2196 |
return array == null ? new Object[0] : array; |
|
2197 |
} |
|
2198 |
||
2199 |
final Object[] newArray = new Object[n]; |
|
2200 |
||
2201 |
if (array != null) { |
|
2202 |
for (int i = 0; i < n && i < length; i++) { |
|
2203 |
newArray[i] = array[i]; |
|
2204 |
} |
|
2205 |
} |
|
2206 |
||
2207 |
if (length < n) { |
|
2208 |
final Object fill = UNDEFINED; |
|
2209 |
||
2210 |
for (int i = length; i < n; i++) { |
|
2211 |
newArray[i] = fill; |
|
2212 |
} |
|
2213 |
} |
|
2214 |
||
2215 |
return newArray; |
|
2216 |
} |
|
2217 |
||
2218 |
/** |
|
2219 |
* Numeric length setter for length property |
|
2220 |
* |
|
2221 |
* @param newLength new length to set |
|
2222 |
*/ |
|
2223 |
public final void setLength(final long newLength) { |
|
2224 |
final long arrayLength = getArray().length(); |
|
2225 |
if (newLength == arrayLength) { |
|
2226 |
return; |
|
2227 |
} |
|
2228 |
||
2229 |
final boolean isStrict = getContext()._strict; |
|
2230 |
||
2231 |
if (newLength > arrayLength) { |
|
2232 |
setArray(getArray().ensure(newLength - 1)); |
|
2233 |
if (getArray().canDelete(arrayLength, (newLength - 1), isStrict)) { |
|
2234 |
setArray(getArray().delete(arrayLength, (newLength - 1))); |
|
2235 |
} |
|
2236 |
return; |
|
2237 |
} |
|
2238 |
||
2239 |
if (newLength < arrayLength) { |
|
2240 |
setArray(getArray().shrink(newLength)); |
|
2241 |
getArray().setLength(newLength); |
|
2242 |
} |
|
2243 |
} |
|
2244 |
||
2245 |
@Override |
|
2246 |
public int getInt(final Object key) { |
|
2247 |
final int index = getArrayIndexNoThrow(key); |
|
2248 |
||
2249 |
if (getArray().has(index)) { |
|
2250 |
return getArray().getInt(index); |
|
2251 |
} |
|
2252 |
||
2253 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2254 |
||
2255 |
if (find != null) { |
|
2256 |
return getIntValue(find); |
|
2257 |
} |
|
2258 |
||
2259 |
final ScriptObject proto = this.getProto(); |
|
2260 |
||
2261 |
return proto != null ? proto.getInt(key) : 0; |
|
2262 |
} |
|
2263 |
||
2264 |
@Override |
|
2265 |
public int getInt(final double key) { |
|
2266 |
final int index = getArrayIndexNoThrow(key); |
|
2267 |
||
2268 |
if (getArray().has(index)) { |
|
2269 |
return getArray().getInt(index); |
|
2270 |
} |
|
2271 |
||
2272 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2273 |
||
2274 |
if (find != null) { |
|
2275 |
return getIntValue(find); |
|
2276 |
} |
|
2277 |
||
2278 |
final ScriptObject proto = this.getProto(); |
|
2279 |
||
2280 |
return proto != null ? proto.getInt(key) : 0; |
|
2281 |
} |
|
2282 |
||
2283 |
@Override |
|
2284 |
public int getInt(final long key) { |
|
2285 |
final int index = getArrayIndexNoThrow(key); |
|
2286 |
||
2287 |
if (getArray().has(index)) { |
|
2288 |
return getArray().getInt(index); |
|
2289 |
} |
|
2290 |
||
2291 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2292 |
||
2293 |
if (find != null) { |
|
2294 |
return getIntValue(find); |
|
2295 |
} |
|
2296 |
||
2297 |
final ScriptObject proto = this.getProto(); |
|
2298 |
||
2299 |
return proto != null ? proto.getInt(key) : 0; |
|
2300 |
} |
|
2301 |
||
2302 |
@Override |
|
2303 |
public int getInt(final int key) { |
|
2304 |
final int index = getArrayIndexNoThrow(key); |
|
2305 |
||
2306 |
if (getArray().has(index)) { |
|
2307 |
return getArray().getInt(index); |
|
2308 |
} |
|
2309 |
||
2310 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2311 |
||
2312 |
if (find != null) { |
|
2313 |
return getIntValue(find); |
|
2314 |
} |
|
2315 |
||
2316 |
final ScriptObject proto = this.getProto(); |
|
2317 |
||
2318 |
return proto != null ? proto.getInt(key) : 0; |
|
2319 |
} |
|
2320 |
||
2321 |
@Override |
|
2322 |
public long getLong(final Object key) { |
|
2323 |
final int index = getArrayIndexNoThrow(key); |
|
2324 |
||
2325 |
if (getArray().has(index)) { |
|
2326 |
return getArray().getLong(index); |
|
2327 |
} |
|
2328 |
||
2329 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2330 |
||
2331 |
if (find != null) { |
|
2332 |
return getLongValue(find); |
|
2333 |
} |
|
2334 |
||
2335 |
final ScriptObject proto = this.getProto(); |
|
2336 |
||
2337 |
return proto != null ? proto.getLong(key) : 0L; |
|
2338 |
} |
|
2339 |
||
2340 |
@Override |
|
2341 |
public long getLong(final double key) { |
|
2342 |
final int index = getArrayIndexNoThrow(key); |
|
2343 |
||
2344 |
if (getArray().has(index)) { |
|
2345 |
return getArray().getLong(index); |
|
2346 |
} |
|
2347 |
||
2348 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2349 |
||
2350 |
if (find != null) { |
|
2351 |
return getLongValue(find); |
|
2352 |
} |
|
2353 |
||
2354 |
final ScriptObject proto = this.getProto(); |
|
2355 |
||
2356 |
return proto != null ? proto.getLong(key) : 0L; |
|
2357 |
} |
|
2358 |
||
2359 |
@Override |
|
2360 |
public long getLong(final long key) { |
|
2361 |
final int index = getArrayIndexNoThrow(key); |
|
2362 |
||
2363 |
if (getArray().has(index)) { |
|
2364 |
return getArray().getLong(index); |
|
2365 |
} |
|
2366 |
||
2367 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2368 |
||
2369 |
if (find != null) { |
|
2370 |
return getLongValue(find); |
|
2371 |
} |
|
2372 |
||
2373 |
final ScriptObject proto = this.getProto(); |
|
2374 |
||
2375 |
return proto != null ? proto.getLong(key) : 0L; |
|
2376 |
} |
|
2377 |
||
2378 |
@Override |
|
2379 |
public long getLong(final int key) { |
|
2380 |
final int index = getArrayIndexNoThrow(key); |
|
2381 |
||
2382 |
if (getArray().has(index)) { |
|
2383 |
return getArray().getLong(index); |
|
2384 |
} |
|
2385 |
||
2386 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2387 |
||
2388 |
if (find != null) { |
|
2389 |
return getLongValue(find); |
|
2390 |
} |
|
2391 |
||
2392 |
final ScriptObject proto = this.getProto(); |
|
2393 |
||
2394 |
return proto != null ? proto.getLong(key) : 0L; |
|
2395 |
} |
|
2396 |
||
2397 |
@Override |
|
2398 |
public double getDouble(final Object key) { |
|
2399 |
final int index = getArrayIndexNoThrow(key); |
|
2400 |
||
2401 |
if (getArray().has(index)) { |
|
2402 |
return getArray().getDouble(index); |
|
2403 |
} |
|
2404 |
||
2405 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2406 |
||
2407 |
if (find != null) { |
|
2408 |
return getDoubleValue(find); |
|
2409 |
} |
|
2410 |
||
2411 |
final ScriptObject proto = this.getProto(); |
|
2412 |
||
2413 |
return proto != null ? proto.getDouble(key) : Double.NaN; |
|
2414 |
} |
|
2415 |
||
2416 |
@Override |
|
2417 |
public double getDouble(final double key) { |
|
2418 |
final int index = getArrayIndexNoThrow(key); |
|
2419 |
||
2420 |
if (getArray().has(index)) { |
|
2421 |
return getArray().getDouble(index); |
|
2422 |
} |
|
2423 |
||
2424 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2425 |
||
2426 |
if (find != null) { |
|
2427 |
return getDoubleValue(find); |
|
2428 |
} |
|
2429 |
||
2430 |
final ScriptObject proto = this.getProto(); |
|
2431 |
||
2432 |
return proto != null ? proto.getDouble(key) : Double.NaN; |
|
2433 |
} |
|
2434 |
||
2435 |
@Override |
|
2436 |
public double getDouble(final long key) { |
|
2437 |
final int index = getArrayIndexNoThrow(key); |
|
2438 |
||
2439 |
if (getArray().has(index)) { |
|
2440 |
return getArray().getDouble(index); |
|
2441 |
} |
|
2442 |
||
2443 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2444 |
||
2445 |
if (find != null) { |
|
2446 |
return getDoubleValue(find); |
|
2447 |
} |
|
2448 |
||
2449 |
final ScriptObject proto = this.getProto(); |
|
2450 |
||
2451 |
return proto != null ? proto.getDouble(key) : Double.NaN; |
|
2452 |
} |
|
2453 |
||
2454 |
@Override |
|
2455 |
public double getDouble(final int key) { |
|
2456 |
final int index = getArrayIndexNoThrow(key); |
|
2457 |
||
2458 |
if (getArray().has(index)) { |
|
2459 |
return getArray().getDouble(index); |
|
2460 |
} |
|
2461 |
||
2462 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2463 |
||
2464 |
if (find != null) { |
|
2465 |
return getDoubleValue(find); |
|
2466 |
} |
|
2467 |
||
2468 |
final ScriptObject proto = this.getProto(); |
|
2469 |
||
2470 |
return proto != null ? proto.getDouble(key) : Double.NaN; |
|
2471 |
} |
|
2472 |
||
2473 |
@Override |
|
2474 |
public Object get(final Object key) { |
|
2475 |
final int index = getArrayIndexNoThrow(key); |
|
2476 |
||
2477 |
if (getArray().has(index)) { |
|
2478 |
return getArray().getObject(index); |
|
2479 |
} |
|
2480 |
||
2481 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2482 |
||
2483 |
if (find != null) { |
|
2484 |
return getObjectValue(find); |
|
2485 |
} |
|
2486 |
||
2487 |
final ScriptObject proto = this.getProto(); |
|
2488 |
||
2489 |
return proto != null ? proto.get(key) : UNDEFINED; |
|
2490 |
} |
|
2491 |
||
2492 |
@Override |
|
2493 |
public Object get(final double key) { |
|
2494 |
final int index = getArrayIndexNoThrow(key); |
|
2495 |
||
2496 |
if (getArray().has(index)) { |
|
2497 |
return getArray().getObject(index); |
|
2498 |
} |
|
2499 |
||
2500 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2501 |
||
2502 |
if (find != null) { |
|
2503 |
return getObjectValue(find); |
|
2504 |
} |
|
2505 |
||
2506 |
final ScriptObject proto = this.getProto(); |
|
2507 |
||
2508 |
return proto != null ? proto.get(key) : UNDEFINED; |
|
2509 |
} |
|
2510 |
||
2511 |
@Override |
|
2512 |
public Object get(final long key) { |
|
2513 |
final int index = getArrayIndexNoThrow(key); |
|
2514 |
||
2515 |
if (getArray().has(index)) { |
|
2516 |
return getArray().getObject(index); |
|
2517 |
} |
|
2518 |
||
2519 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2520 |
||
2521 |
if (find != null) { |
|
2522 |
return getObjectValue(find); |
|
2523 |
} |
|
2524 |
||
2525 |
final ScriptObject proto = this.getProto(); |
|
2526 |
||
2527 |
return proto != null ? proto.get(key) : UNDEFINED; |
|
2528 |
} |
|
2529 |
||
2530 |
@Override |
|
2531 |
public Object get(final int key) { |
|
2532 |
final int index = getArrayIndexNoThrow(key); |
|
2533 |
||
2534 |
if (getArray().has(index)) { |
|
2535 |
return getArray().getObject(index); |
|
2536 |
} |
|
2537 |
||
2538 |
final FindProperty find = findProperty(convertKey(key), false); |
|
2539 |
||
2540 |
if (find != null) { |
|
2541 |
return getObjectValue(find); |
|
2542 |
} |
|
2543 |
||
2544 |
final ScriptObject proto = this.getProto(); |
|
2545 |
||
2546 |
return proto != null ? proto.get(key) : UNDEFINED; |
|
2547 |
} |
|
2548 |
||
2549 |
/** |
|
2550 |
* Handle when an array doesn't have a slot - possibly grow and/or convert array. |
|
2551 |
* |
|
2552 |
* @param index key as index |
|
2553 |
* @param value element value |
|
2554 |
* @param strict are we in strict mode |
|
2555 |
*/ |
|
2556 |
private void doesNotHave(final int index, final Object value, final boolean strict) { |
|
2557 |
final long oldLength = getArray().length(); |
|
2558 |
final long longIndex = index & 0xffff_ffffL; |
|
2559 |
||
2560 |
if (!getArray().has(index)) { |
|
2561 |
final String key = convertKey(longIndex); |
|
2562 |
final FindProperty find = findProperty(key, true); |
|
2563 |
||
2564 |
if (find != null) { |
|
2565 |
setObject(find, strict, key, value); |
|
2566 |
return; |
|
2567 |
} |
|
2568 |
} |
|
2569 |
||
2570 |
if (longIndex >= oldLength) { |
|
2571 |
if (!isExtensible()) { |
|
2572 |
if (strict) { |
|
16188 | 2573 |
typeError("object.non.extensible", JSType.toString(index), ScriptRuntime.safeToString(this)); |
16147 | 2574 |
} |
2575 |
return; |
|
2576 |
} |
|
2577 |
setArray(getArray().ensure(longIndex)); |
|
2578 |
} |
|
2579 |
||
2580 |
if (value instanceof Integer) { |
|
2581 |
setArray(getArray().set(index, (int)value, strict)); |
|
2582 |
} else if (value instanceof Long) { |
|
2583 |
setArray(getArray().set(index, (long)value, strict)); |
|
2584 |
} else if (value instanceof Double) { |
|
2585 |
setArray(getArray().set(index, (double)value, strict)); |
|
2586 |
} else { |
|
2587 |
setArray(getArray().set(index, value, strict)); |
|
2588 |
} |
|
2589 |
||
2590 |
if (longIndex > oldLength) { |
|
2591 |
ArrayData array = getArray(); |
|
2592 |
||
2593 |
if (array.canDelete(oldLength, (longIndex - 1), strict)) { |
|
2594 |
array = array.delete(oldLength, (longIndex - 1)); |
|
2595 |
} |
|
2596 |
||
2597 |
setArray(array); |
|
2598 |
} |
|
2599 |
} |
|
2600 |
||
2601 |
/** |
|
2602 |
* This is the most generic of all Object setters. Most of the others use this in some form. |
|
2603 |
* TODO: should be further specialized |
|
2604 |
* |
|
2605 |
* @param find found property |
|
2606 |
* @param strict are we in strict mode |
|
2607 |
* @param key property key |
|
2608 |
* @param value property value |
|
2609 |
*/ |
|
2610 |
public final void setObject(final FindProperty find, final boolean strict, final String key, final Object value) { |
|
2611 |
FindProperty f = find; |
|
2612 |
||
2613 |
if (f != null && f.isInherited() && !(f.getProperty() instanceof UserAccessorProperty)) { |
|
2614 |
f = null; |
|
2615 |
} |
|
2616 |
||
2617 |
MethodHandle setter; |
|
2618 |
||
2619 |
if (f != null) { |
|
2620 |
if (!f.isWritable()) { |
|
2621 |
if (strict) { |
|
16188 | 2622 |
typeError("property.not.writable", key, ScriptRuntime.safeToString(this)); |
16147 | 2623 |
} |
2624 |
||
2625 |
return; |
|
2626 |
} |
|
2627 |
||
2628 |
setter = f.getSetter(Object.class, strict); //TODO specfields |
|
2629 |
try { |
|
2630 |
setter.invokeExact((Object)f.getOwner(), value); |
|
2631 |
} catch (final Error|RuntimeException e) { |
|
2632 |
throw e; |
|
2633 |
} catch (final Throwable e) { |
|
2634 |
throw new RuntimeException(e); |
|
2635 |
} |
|
2636 |
} else if (!isExtensible()) { |
|
2637 |
if (strict) { |
|
16188 | 2638 |
typeError("object.non.extensible", key, ScriptRuntime.safeToString(this)); |
16147 | 2639 |
} |
2640 |
} else { |
|
2641 |
spill(key, value); |
|
2642 |
} |
|
2643 |
} |
|
2644 |
||
2645 |
private void spill(final String key, final Object value) { |
|
2646 |
try { |
|
2647 |
addSpill(key).invokeExact((Object)this, value); |
|
2648 |
} catch (final Error|RuntimeException e) { |
|
2649 |
throw e; |
|
2650 |
} catch (final Throwable e) { |
|
2651 |
throw new RuntimeException(e); |
|
2652 |
} |
|
2653 |
} |
|
2654 |
||
2655 |
||
2656 |
@Override |
|
2657 |
public void set(final Object key, final int value, final boolean strict) { |
|
2658 |
final int index = getArrayIndexNoThrow(key); |
|
2659 |
||
2660 |
if (isValidArrayIndex(index)) { |
|
2661 |
if (getArray().has(index)) { |
|
2662 |
setArray(getArray().set(index, value, strict)); |
|
2663 |
} else { |
|
2664 |
doesNotHave(index, value, strict); |
|
2665 |
} |
|
2666 |
||
2667 |
return; |
|
2668 |
} |
|
2669 |
||
2670 |
set(key, JSType.toObject(value), strict); |
|
2671 |
} |
|
2672 |
||
2673 |
@Override |
|
2674 |
public void set(final Object key, final long value, final boolean strict) { |
|
2675 |
final int index = getArrayIndexNoThrow(key); |
|
2676 |
||
2677 |
if (isValidArrayIndex(index)) { |
|
2678 |
if (getArray().has(index)) { |
|
2679 |
setArray(getArray().set(index, value, strict)); |
|
2680 |
} else { |
|
2681 |
doesNotHave(index, value, strict); |
|
2682 |
} |
|
2683 |
||
2684 |
return; |
|
2685 |
} |
|
2686 |
||
2687 |
set(key, JSType.toObject(value), strict); |
|
2688 |
} |
|
2689 |
||
2690 |
@Override |
|
2691 |
public void set(final Object key, final double value, final boolean strict) { |
|
2692 |
final int index = getArrayIndexNoThrow(key); |
|
2693 |
||
2694 |
if (isValidArrayIndex(index)) { |
|
2695 |
if (getArray().has(index)) { |
|
2696 |
setArray(getArray().set(index, value, strict)); |
|
2697 |
} else { |
|
2698 |
doesNotHave(index, value, strict); |
|
2699 |
} |
|
2700 |
||
2701 |
return; |
|
2702 |
} |
|
2703 |
||
2704 |
set(key, JSType.toObject(value), strict); |
|
2705 |
} |
|
2706 |
||
2707 |
@Override |
|
2708 |
public void set(final Object key, final Object value, final boolean strict) { |
|
2709 |
final int index = getArrayIndexNoThrow(key); |
|
2710 |
||
2711 |
if (isValidArrayIndex(index)) { |
|
2712 |
if (getArray().has(index)) { |
|
2713 |
setArray(getArray().set(index, value, strict)); |
|
2714 |
} else { |
|
2715 |
doesNotHave(index, value, strict); |
|
2716 |
} |
|
2717 |
||
2718 |
return; |
|
2719 |
} |
|
2720 |
||
2721 |
final String propName = convertKey(key); |
|
2722 |
final FindProperty find = findProperty(propName, true); |
|
2723 |
||
2724 |
setObject(find, strict, propName, value); |
|
2725 |
} |
|
2726 |
||
2727 |
@Override |
|
2728 |
public void set(final double key, final int value, final boolean strict) { |
|
2729 |
final int index = getArrayIndexNoThrow(key); |
|
2730 |
||
2731 |
if (isValidArrayIndex(index)) { |
|
2732 |
if (getArray().has(index)) { |
|
2733 |
setArray(getArray().set(index, value, strict)); |
|
2734 |
} else { |
|
2735 |
doesNotHave(index, value, strict); |
|
2736 |
} |
|
2737 |
||
2738 |
return; |
|
2739 |
} |
|
2740 |
||
2741 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2742 |
} |
|
2743 |
||
2744 |
@Override |
|
2745 |
public void set(final double key, final long value, final boolean strict) { |
|
2746 |
final int index = getArrayIndexNoThrow(key); |
|
2747 |
||
2748 |
if (isValidArrayIndex(index)) { |
|
2749 |
if (getArray().has(index)) { |
|
2750 |
setArray(getArray().set(index, value, strict)); |
|
2751 |
} else { |
|
2752 |
doesNotHave(index, value, strict); |
|
2753 |
} |
|
2754 |
||
2755 |
return; |
|
2756 |
} |
|
2757 |
||
2758 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2759 |
} |
|
2760 |
||
2761 |
@Override |
|
2762 |
public void set(final double key, final double value, final boolean strict) { |
|
2763 |
final int index = getArrayIndexNoThrow(key); |
|
2764 |
||
2765 |
if (isValidArrayIndex(index)) { |
|
2766 |
if (getArray().has(index)) { |
|
2767 |
setArray(getArray().set(index, value, strict)); |
|
2768 |
} else { |
|
2769 |
doesNotHave(index, value, strict); |
|
2770 |
} |
|
2771 |
||
2772 |
return; |
|
2773 |
} |
|
2774 |
||
2775 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2776 |
} |
|
2777 |
||
2778 |
@Override |
|
2779 |
public void set(final double key, final Object value, final boolean strict) { |
|
2780 |
final int index = getArrayIndexNoThrow(key); |
|
2781 |
||
2782 |
if (isValidArrayIndex(index)) { |
|
2783 |
if (getArray().has(index)) { |
|
2784 |
setArray(getArray().set(index, value, strict)); |
|
2785 |
} else { |
|
2786 |
doesNotHave(index, value, strict); |
|
2787 |
} |
|
2788 |
||
2789 |
return; |
|
2790 |
} |
|
2791 |
||
2792 |
set(JSType.toObject(key), value, strict); |
|
2793 |
} |
|
2794 |
||
2795 |
@Override |
|
2796 |
public void set(final long key, final int value, final boolean strict) { |
|
2797 |
final int index = getArrayIndexNoThrow(key); |
|
2798 |
||
2799 |
if (isValidArrayIndex(index)) { |
|
2800 |
if (getArray().has(index)) { |
|
2801 |
setArray(getArray().set(index, value, strict)); |
|
2802 |
} else { |
|
2803 |
doesNotHave(index, value, strict); |
|
2804 |
} |
|
2805 |
||
2806 |
return; |
|
2807 |
} |
|
2808 |
||
2809 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2810 |
} |
|
2811 |
||
2812 |
@Override |
|
2813 |
public void set(final long key, final long value, final boolean strict) { |
|
2814 |
final int index = getArrayIndexNoThrow(key); |
|
2815 |
||
2816 |
if (isValidArrayIndex(index)) { |
|
2817 |
if (getArray().has(index)) { |
|
2818 |
setArray(getArray().set(index, value, strict)); |
|
2819 |
} else { |
|
2820 |
doesNotHave(index, value, strict); |
|
2821 |
} |
|
2822 |
||
2823 |
return; |
|
2824 |
} |
|
2825 |
||
2826 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2827 |
} |
|
2828 |
||
2829 |
@Override |
|
2830 |
public void set(final long key, final double value, final boolean strict) { |
|
2831 |
final int index = getArrayIndexNoThrow(key); |
|
2832 |
||
2833 |
if (isValidArrayIndex(index)) { |
|
2834 |
if (getArray().has(index)) { |
|
2835 |
setArray(getArray().set(index, value, strict)); |
|
2836 |
} else { |
|
2837 |
doesNotHave(index, value, strict); |
|
2838 |
} |
|
2839 |
||
2840 |
return; |
|
2841 |
} |
|
2842 |
||
2843 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2844 |
} |
|
2845 |
||
2846 |
@Override |
|
2847 |
public void set(final long key, final Object value, final boolean strict) { |
|
2848 |
final int index = getArrayIndexNoThrow(key); |
|
2849 |
||
2850 |
if (isValidArrayIndex(index)) { |
|
2851 |
if (getArray().has(index)) { |
|
2852 |
setArray(getArray().set(index, value, strict)); |
|
2853 |
} else { |
|
2854 |
doesNotHave(index, value, strict); |
|
2855 |
} |
|
2856 |
||
2857 |
return; |
|
2858 |
} |
|
2859 |
||
2860 |
set(JSType.toObject(key), value, strict); |
|
2861 |
} |
|
2862 |
||
2863 |
@Override |
|
2864 |
public void set(final int key, final int value, final boolean strict) { |
|
2865 |
final int index = getArrayIndexNoThrow(key); |
|
2866 |
||
2867 |
if (isValidArrayIndex(index)) { |
|
2868 |
if (getArray().has(index)) { |
|
2869 |
setArray(getArray().set(index, value, strict)); |
|
2870 |
} else { |
|
2871 |
doesNotHave(index, value, strict); |
|
2872 |
} |
|
2873 |
||
2874 |
return; |
|
2875 |
} |
|
2876 |
||
2877 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2878 |
} |
|
2879 |
||
2880 |
@Override |
|
2881 |
public void set(final int key, final long value, final boolean strict) { |
|
2882 |
final int index = getArrayIndexNoThrow(key); |
|
2883 |
||
2884 |
if (isValidArrayIndex(index)) { |
|
2885 |
if (getArray().has(index)) { |
|
2886 |
setArray(getArray().set(index, value, strict)); |
|
2887 |
} else { |
|
2888 |
doesNotHave(index, value, strict); |
|
2889 |
} |
|
2890 |
||
2891 |
return; |
|
2892 |
} |
|
2893 |
||
2894 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2895 |
} |
|
2896 |
||
2897 |
@Override |
|
2898 |
public void set(final int key, final double value, final boolean strict) { |
|
2899 |
final int index = getArrayIndexNoThrow(key); |
|
2900 |
||
2901 |
if (isValidArrayIndex(index)) { |
|
2902 |
if (getArray().has(index)) { |
|
2903 |
setArray(getArray().set(index, value, strict)); |
|
2904 |
} else { |
|
2905 |
doesNotHave(index, value, strict); |
|
2906 |
} |
|
2907 |
||
2908 |
return; |
|
2909 |
} |
|
2910 |
||
2911 |
set(JSType.toObject(key), JSType.toObject(value), strict); |
|
2912 |
} |
|
2913 |
||
2914 |
@Override |
|
2915 |
public void set(final int key, final Object value, final boolean strict) { |
|
2916 |
final int index = getArrayIndexNoThrow(key); |
|
2917 |
||
2918 |
if (isValidArrayIndex(index)) { |
|
2919 |
if (getArray().has(index)) { |
|
2920 |
setArray(getArray().set(index, value, strict)); |
|
2921 |
} else { |
|
2922 |
doesNotHave(index, value, strict); |
|
2923 |
} |
|
2924 |
||
2925 |
return; |
|
2926 |
} |
|
2927 |
||
2928 |
set(JSType.toObject(key), value, strict); |
|
2929 |
} |
|
2930 |
||
2931 |
@Override |
|
2932 |
public boolean has(final Object key) { |
|
2933 |
final int index = getArrayIndexNoThrow(key); |
|
2934 |
||
2935 |
if (isValidArrayIndex(index)) { |
|
2936 |
for (ScriptObject self = this; self != null; self = self.getProto()) { |
|
2937 |
if (self.getArray().has(index)) { |
|
2938 |
return true; |
|
2939 |
} |
|
2940 |
} |
|
2941 |
} |
|
2942 |
||
2943 |
final FindProperty find = findProperty(convertKey(key), true); |
|
2944 |
||
2945 |
return find != null; |
|
2946 |
} |
|
2947 |
||
2948 |
@Override |
|
2949 |
public boolean has(final double key) { |
|
2950 |
final int index = getArrayIndexNoThrow(key); |
|
2951 |
||
2952 |
if (isValidArrayIndex(index)) { |
|
2953 |
for (ScriptObject self = this; self != null; self = self.getProto()) { |
|
2954 |
if (self.getArray().has(index)) { |
|
2955 |
return true; |
|
2956 |
} |
|
2957 |
} |
|
2958 |
} |
|
2959 |
||
2960 |
final FindProperty find = findProperty(convertKey(key), true); |
|
2961 |
||
2962 |
return find != null; |
|
2963 |
} |
|
2964 |
||
2965 |
@Override |
|
2966 |
public boolean has(final long key) { |
|
2967 |
final int index = getArrayIndexNoThrow(key); |
|
2968 |
||
2969 |
if (isValidArrayIndex(index)) { |
|
2970 |
for (ScriptObject self = this; self != null; self = self.getProto()) { |
|
2971 |
if (self.getArray().has(index)) { |
|
2972 |
return true; |
|
2973 |
} |
|
2974 |
} |
|
2975 |
} |
|
2976 |
||
2977 |
final FindProperty find = findProperty(convertKey(key), true); |
|
2978 |
||
2979 |
return find != null; |
|
2980 |
} |
|
2981 |
||
2982 |
@Override |
|
2983 |
public boolean has(final int key) { |
|
2984 |
final int index = getArrayIndexNoThrow(key); |
|
2985 |
||
2986 |
if (isValidArrayIndex(index)) { |
|
2987 |
for (ScriptObject self = this; self != null; self = self.getProto()) { |
|
2988 |
if (self.getArray().has(index)) { |
|
2989 |
return true; |
|
2990 |
} |
|
2991 |
} |
|
2992 |
} |
|
2993 |
||
2994 |
final FindProperty find = findProperty(convertKey(key), true); |
|
2995 |
||
2996 |
return find != null; |
|
2997 |
} |
|
2998 |
||
2999 |
@Override |
|
3000 |
public boolean hasOwnProperty(final Object key) { |
|
3001 |
final int index = getArrayIndexNoThrow(key); |
|
3002 |
||
3003 |
if (getArray().has(index)) { |
|
3004 |
return true; |
|
3005 |
} |
|
3006 |
||
3007 |
final FindProperty find = findProperty(convertKey(key), false); |
|
3008 |
||
3009 |
return find != null; |
|
3010 |
} |
|
3011 |
||
3012 |
@Override |
|
3013 |
public boolean hasOwnProperty(final int key) { |
|
3014 |
final int index = getArrayIndexNoThrow(key); |
|
3015 |
||
3016 |
if (getArray().has(index)) { |
|
3017 |
return true; |
|
3018 |
} |
|
3019 |
||
3020 |
final FindProperty find = findProperty(convertKey(key), false); |
|
3021 |
||
3022 |
return find != null; |
|
3023 |
} |
|
3024 |
||
3025 |
@Override |
|
3026 |
public boolean hasOwnProperty(final long key) { |
|
3027 |
final int index = getArrayIndexNoThrow(key); |
|
3028 |
||
3029 |
if (getArray().has(index)) { |
|
3030 |
return true; |
|
3031 |
} |
|
3032 |
||
3033 |
final FindProperty find = findProperty(convertKey(key), false); |
|
3034 |
||
3035 |
return find != null; |
|
3036 |
} |
|
3037 |
||
3038 |
@Override |
|
3039 |
public boolean hasOwnProperty(final double key) { |
|
3040 |
final int index = getArrayIndexNoThrow(key); |
|
3041 |
||
3042 |
if (getArray().has(index)) { |
|
3043 |
return true; |
|
3044 |
} |
|
3045 |
||
3046 |
final FindProperty find = findProperty(convertKey(key), false); |
|
3047 |
||
3048 |
return find != null; |
|
3049 |
} |
|
3050 |
||
3051 |
@Override |
|
3052 |
public boolean delete(final int key, final boolean strict) { |
|
3053 |
final int index = getArrayIndexNoThrow(key); |
|
3054 |
final ArrayData array = getArray(); |
|
3055 |
||
3056 |
if (array.has(index)) { |
|
3057 |
if (array.canDelete(index, strict)) { |
|
3058 |
setArray(array.delete(index)); |
|
3059 |
return true; |
|
3060 |
} |
|
3061 |
return false; |
|
3062 |
} |
|
3063 |
||
3064 |
return deleteObject(JSType.toObject(key), strict); |
|
3065 |
} |
|
3066 |
||
3067 |
@Override |
|
3068 |
public boolean delete(final long key, final boolean strict) { |
|
3069 |
final int index = getArrayIndexNoThrow(key); |
|
3070 |
final ArrayData array = getArray(); |
|
3071 |
||
3072 |
if (array.has(index)) { |
|
3073 |
if (array.canDelete(index, strict)) { |
|
3074 |
setArray(array.delete(index)); |
|
3075 |
return true; |
|
3076 |
} |
|
3077 |
return false; |
|
3078 |
} |
|
3079 |
||
3080 |
return deleteObject(JSType.toObject(key), strict); |
|
3081 |
} |
|
3082 |
||
3083 |
@Override |
|
3084 |
public boolean delete(final double key, final boolean strict) { |
|
3085 |
final int index = getArrayIndexNoThrow(key); |
|
3086 |
final ArrayData array = getArray(); |
|
3087 |
||
3088 |
if (array.has(index)) { |
|
3089 |
if (array.canDelete(index, strict)) { |
|
3090 |
setArray(array.delete(index)); |
|
3091 |
return true; |
|
3092 |
} |
|
3093 |
return false; |
|
3094 |
} |
|
3095 |
||
3096 |
return deleteObject(JSType.toObject(key), strict); |
|
3097 |
} |
|
3098 |
||
3099 |
@Override |
|
3100 |
public boolean delete(final Object key, final boolean strict) { |
|
3101 |
final int index = getArrayIndexNoThrow(key); |
|
3102 |
final ArrayData array = getArray(); |
|
3103 |
||
3104 |
if (array.has(index)) { |
|
3105 |
if (array.canDelete(index, strict)) { |
|
3106 |
setArray(array.delete(index)); |
|
3107 |
return true; |
|
3108 |
} |
|
3109 |
return false; |
|
3110 |
} |
|
3111 |
||
3112 |
return deleteObject(key, strict); |
|
3113 |
} |
|
3114 |
||
3115 |
private boolean deleteObject(final Object key, final boolean strict) { |
|
3116 |
final String propName = convertKey(key); |
|
3117 |
final FindProperty find = findProperty(propName, false); |
|
3118 |
||
3119 |
if (find == null) { |
|
3120 |
return true; |
|
3121 |
} |
|
3122 |
||
3123 |
if (!find.isConfigurable()) { |
|
3124 |
if (strict) { |
|
16188 | 3125 |
typeError("cant.delete.property", propName, ScriptRuntime.safeToString(this)); |
16147 | 3126 |
} |
3127 |
return false; |
|
3128 |
} |
|
3129 |
||
3130 |
final Property prop = find.getProperty(); |
|
3131 |
notifyPropertyDeleted(this, prop); |
|
3132 |
deleteOwnProperty(prop); |
|
3133 |
||
3134 |
return true; |
|
3135 |
} |
|
3136 |
||
3137 |
/* |
|
3138 |
* Embed management |
|
3139 |
*/ |
|
3140 |
||
3141 |
/** Number of embed slots */ |
|
3142 |
public static final int EMBED_SIZE = 4; |
|
3143 |
/** Embed offset */ |
|
3144 |
public static final int EMBED_OFFSET = 32 - EMBED_SIZE; |
|
3145 |
||
16159 | 3146 |
static final MethodHandle[] GET_EMBED; |
3147 |
static final MethodHandle[] SET_EMBED; |
|
16147 | 3148 |
|
3149 |
static { |
|
3150 |
GET_EMBED = new MethodHandle[EMBED_SIZE]; |
|
3151 |
SET_EMBED = new MethodHandle[EMBED_SIZE]; |
|
3152 |
||
3153 |
for (int i = 0; i < EMBED_SIZE; i++) { |
|
3154 |
final String name = "embed" + i; |
|
3155 |
GET_EMBED[i] = MH.asType(MH.getter(MethodHandles.lookup(), ScriptObject.class, name, Object.class), Lookup.GET_OBJECT_TYPE); |
|
3156 |
SET_EMBED[i] = MH.asType(MH.setter(MethodHandles.lookup(), ScriptObject.class, name, Object.class), Lookup.SET_OBJECT_TYPE); |
|
3157 |
} |
|
3158 |
} |
|
3159 |
||
16159 | 3160 |
void useEmbed(final int i) { |
16147 | 3161 |
flags |= 1 << (EMBED_OFFSET + i); |
3162 |
} |
|
3163 |
||
16159 | 3164 |
int findEmbed() { |
16147 | 3165 |
final int bits = ~(flags >>> EMBED_OFFSET); |
3166 |
final int least = bits ^ -bits; |
|
3167 |
final int index = Integer.numberOfTrailingZeros(least) - 1; |
|
3168 |
||
3169 |
return index; |
|
3170 |
} |
|
3171 |
||
3172 |
/* |
|
3173 |
* Make a new UserAccessorProperty property. getter and setter functions are stored in |
|
3174 |
* this ScriptObject and slot values are used in property object. |
|
3175 |
*/ |
|
3176 |
private UserAccessorProperty newUserAccessors(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { |
|
3177 |
int oldSpillLength = getMap().getSpillLength(); |
|
3178 |
||
3179 |
int getterSlot = findEmbed(); |
|
3180 |
if (getterSlot >= EMBED_SIZE) { |
|
3181 |
getterSlot = oldSpillLength + EMBED_SIZE; |
|
3182 |
++oldSpillLength; |
|
3183 |
} else { |
|
3184 |
useEmbed(getterSlot); |
|
3185 |
} |
|
3186 |
setEmbedOrSpill(getterSlot, getter); |
|
3187 |
// if getter function is null, flag the slot to be negative (less by 1) |
|
3188 |
if (getter == null) { |
|
3189 |
getterSlot = -getterSlot - 1; |
|
3190 |
} |
|
3191 |
||
3192 |
int setterSlot = findEmbed(); |
|
3193 |
if (setterSlot >= EMBED_SIZE) { |
|
3194 |
setterSlot = oldSpillLength + EMBED_SIZE; |
|
3195 |
} else { |
|
3196 |
useEmbed(setterSlot); |
|
3197 |
} |
|
3198 |
setEmbedOrSpill(setterSlot, setter); |
|
3199 |
// if setter function is null, flag the slot to be negative (less by 1) |
|
3200 |
if (setter == null) { |
|
3201 |
setterSlot = -setterSlot - 1; |
|
3202 |
} |
|
3203 |
||
3204 |
return new UserAccessorProperty(key, propertyFlags, getterSlot, setterSlot); |
|
3205 |
} |
|
3206 |
||
3207 |
private void setEmbedOrSpill(final int slot, final Object value) { |
|
3208 |
switch (slot) { |
|
3209 |
case 0: |
|
3210 |
embed0 = value; |
|
3211 |
break; |
|
3212 |
case 1: |
|
3213 |
embed1 = value; |
|
3214 |
break; |
|
3215 |
case 2: |
|
3216 |
embed2 = value; |
|
3217 |
break; |
|
3218 |
case 3: |
|
3219 |
embed3 = value; |
|
3220 |
break; |
|
3221 |
default: |
|
3222 |
if (slot >= 0) { |
|
3223 |
final int index = (slot - EMBED_SIZE); |
|
3224 |
if (spill == null) { |
|
3225 |
// create new spill. |
|
3226 |
spill = new Object[Math.max(index + 1, SPILL_RATE)]; |
|
3227 |
} else if (index >= spill.length) { |
|
3228 |
// grow spill as needed |
|
3229 |
final Object[] newSpill = new Object[index + 1]; |
|
3230 |
System.arraycopy(spill, 0, newSpill, 0, spill.length); |
|
3231 |
spill = newSpill; |
|
3232 |
} |
|
3233 |
||
3234 |
spill[index] = value; |
|
3235 |
} |
|
3236 |
break; |
|
3237 |
} |
|
3238 |
} |
|
3239 |
||
3240 |
// user accessors are either stored in embed fields or spill array slots |
|
3241 |
// get the accessor value using slot number. Note that slot is either embed |
|
3242 |
// field number or (spill array index + embedSize). |
|
3243 |
Object getEmbedOrSpill(final int slot) { |
|
3244 |
switch (slot) { |
|
3245 |
case 0: |
|
3246 |
return embed0; |
|
3247 |
case 1: |
|
3248 |
return embed1; |
|
3249 |
case 2: |
|
3250 |
return embed2; |
|
3251 |
case 3: |
|
3252 |
return embed3; |
|
3253 |
default: |
|
3254 |
final int index = (slot - EMBED_SIZE); |
|
3255 |
return (index < 0 || (index >= spill.length)) ? null : spill[index]; |
|
3256 |
} |
|
3257 |
} |
|
3258 |
||
3259 |
// User defined getter and setter are always called by "dyn:call". Note that the user |
|
3260 |
// getter/setter may be inherited. If so, proto is bound during lookup. In either |
|
3261 |
// inherited or self case, slot is also bound during lookup. Actual ScriptFunction |
|
3262 |
// to be called is retrieved everytime and applied. |
|
3263 |
@SuppressWarnings("unused") |
|
3264 |
private static Object userAccessorGetter(final ScriptObject proto, final int slot, final Object self) { |
|
3265 |
final ScriptObject container = (proto != null) ? proto : (ScriptObject)self; |
|
3266 |
final Object func = container.getEmbedOrSpill(slot); |
|
3267 |
||
3268 |
if (func instanceof ScriptFunction) { |
|
3269 |
try { |
|
3270 |
return INVOKE_UA_GETTER.invokeExact(func, self); |
|
3271 |
} catch(final Error|RuntimeException t) { |
|
3272 |
throw t; |
|
3273 |
} catch(final Throwable t) { |
|
3274 |
throw new RuntimeException(t); |
|
3275 |
} |
|
3276 |
} |
|
3277 |
||
3278 |
return UNDEFINED; |
|
3279 |
} |
|
3280 |
||
3281 |
@SuppressWarnings("unused") |
|
3282 |
private static void userAccessorSetter(final ScriptObject proto, final int slot, final String name, final Object self, final Object value) { |
|
3283 |
final ScriptObject container = (proto != null) ? proto : (ScriptObject)self; |
|
3284 |
final Object func = container.getEmbedOrSpill(slot); |
|
3285 |
||
3286 |
if (func instanceof ScriptFunction) { |
|
3287 |
try { |
|
3288 |
INVOKE_UA_SETTER.invokeExact(func, self, value); |
|
3289 |
} catch(final Error|RuntimeException t) { |
|
3290 |
throw t; |
|
3291 |
} catch(final Throwable t) { |
|
3292 |
throw new RuntimeException(t); |
|
3293 |
} |
|
3294 |
} else if (name != null) { |
|
16188 | 3295 |
typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self)); |
16147 | 3296 |
} |
3297 |
} |
|
3298 |
||
3299 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
3300 |
final Class<?> own = ScriptObject.class; |
|
3301 |
final MethodType mt = MH.type(rtype, types); |
|
3302 |
try { |
|
3303 |
return MH.findStatic(MethodHandles.lookup(), own, name, mt); |
|
3304 |
} catch (final MethodHandleFactory.LookupException e) { |
|
3305 |
return MH.findVirtual(MethodHandles.lookup(), own, name, mt); |
|
3306 |
} |
|
3307 |
} |
|
3308 |
||
3309 |
private static MethodHandle getKnownFunctionPropertyGuard(final PropertyMap map, final MethodHandle getter, final Object where, final ScriptFunction func) { |
|
3310 |
return MH.insertArguments(KNOWNFUNCPROPGUARD, 1, map, getter, where, func); |
|
3311 |
} |
|
3312 |
||
3313 |
@SuppressWarnings("unused") |
|
3314 |
private static boolean knownFunctionPropertyGuard(final Object self, final PropertyMap map, final MethodHandle getter, final Object where, final ScriptFunction func) { |
|
3315 |
if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) { |
|
3316 |
try { |
|
3317 |
return getter.invokeExact(where) == func; |
|
3318 |
} catch (final RuntimeException | Error e) { |
|
3319 |
throw e; |
|
3320 |
} catch (final Throwable t) { |
|
3321 |
throw new RuntimeException(t); |
|
3322 |
} |
|
3323 |
} |
|
3324 |
||
3325 |
return false; |
|
3326 |
} |
|
3327 |
||
3328 |
/** This is updated only in debug mode - counts number of {@code ScriptObject} instances created */ |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
3329 |
private static int count; |
16147 | 3330 |
|
3331 |
/** This is updated only in debug mode - counts number of {@code ScriptObject} instances created that are scope */ |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
3332 |
private static int scopeCount; |
16147 | 3333 |
|
3334 |
/** |
|
3335 |
* Get number of {@code ScriptObject} instances created. If not running in debug |
|
3336 |
* mode this is always 0 |
|
3337 |
* |
|
3338 |
* @return number of ScriptObjects created |
|
3339 |
*/ |
|
3340 |
public static int getCount() { |
|
3341 |
return count; |
|
3342 |
} |
|
3343 |
||
3344 |
/** |
|
3345 |
* Get number of scope {@code ScriptObject} instances created. If not running in debug |
|
3346 |
* mode this is always 0 |
|
3347 |
* |
|
3348 |
* @return number of scope ScriptObjects created |
|
3349 |
*/ |
|
3350 |
public static int getScopeCount() { |
|
3351 |
return scopeCount; |
|
3352 |
} |
|
3353 |
||
3354 |
} |