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