ScriptObject.java revision 1314:a24cb0bf79bc
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
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
26package jdk.nashorn.internal.runtime;
27
28import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
29import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCall;
30import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
31import static jdk.nashorn.internal.lookup.Lookup.MH;
32import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
33import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
34import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_DOUBLE;
35import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT;
36import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_LONG;
37import static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE;
38import static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE;
39import static jdk.nashorn.internal.runtime.PropertyDescriptor.GET;
40import static jdk.nashorn.internal.runtime.PropertyDescriptor.SET;
41import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE;
42import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
43import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
44import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
45import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
46import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex;
47import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
48import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isScopeFlag;
49import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isStrictFlag;
50import static jdk.nashorn.internal.runtime.linker.NashornGuards.explicitInstanceOfCheck;
51
52import java.lang.invoke.MethodHandle;
53import java.lang.invoke.MethodHandles;
54import java.lang.invoke.MethodType;
55import java.lang.invoke.SwitchPoint;
56import java.util.AbstractMap;
57import java.util.ArrayList;
58import java.util.Arrays;
59import java.util.Collection;
60import java.util.Collections;
61import java.util.HashSet;
62import java.util.Iterator;
63import java.util.LinkedHashSet;
64import java.util.List;
65import java.util.Map;
66import java.util.Set;
67import jdk.internal.dynalink.CallSiteDescriptor;
68import jdk.internal.dynalink.linker.GuardedInvocation;
69import jdk.internal.dynalink.linker.LinkRequest;
70import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
71import jdk.nashorn.internal.codegen.CompilerConstants.Call;
72import jdk.nashorn.internal.codegen.ObjectClassGenerator;
73import jdk.nashorn.internal.codegen.types.Type;
74import jdk.nashorn.internal.lookup.Lookup;
75import jdk.nashorn.internal.objects.AccessorPropertyDescriptor;
76import jdk.nashorn.internal.objects.DataPropertyDescriptor;
77import jdk.nashorn.internal.objects.Global;
78import jdk.nashorn.internal.objects.NativeArray;
79import jdk.nashorn.internal.runtime.arrays.ArrayData;
80import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
81import jdk.nashorn.internal.runtime.linker.Bootstrap;
82import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
83import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
84import jdk.nashorn.internal.runtime.linker.NashornGuards;
85
86/**
87 * Base class for generic JavaScript objects.
88 * <p>
89 * Notes:
90 * <ul>
91 * <li>The map is used to identify properties in the object.</li>
92 * <li>If the map is modified then it must be cloned and replaced.  This notifies
93 *     any code that made assumptions about the object that things have changed.
94 *     Ex. CallSites that have been validated must check to see if the map has
95 *     changed (or a map from a different object type) and hence relink the method
96 *     to call.</li>
97 * <li>Modifications of the map include adding/deleting attributes or changing a
98 *     function field value.</li>
99 * </ul>
100 */
101
102public abstract class ScriptObject implements PropertyAccess, Cloneable {
103    /** __proto__ special property name inside object literals. ES6 draft. */
104    public static final String PROTO_PROPERTY_NAME   = "__proto__";
105
106    /** Search fall back routine name for "no such method" */
107    public static final String NO_SUCH_METHOD_NAME   = "__noSuchMethod__";
108
109    /** Search fall back routine name for "no such property" */
110    public static final String NO_SUCH_PROPERTY_NAME = "__noSuchProperty__";
111
112    /** Per ScriptObject flag - is this a scope object? */
113    public static final int IS_SCOPE       = 1 << 0;
114
115    /** Per ScriptObject flag - is this an array object? */
116    public static final int IS_ARRAY       = 1 << 1;
117
118    /** Per ScriptObject flag - is this an arguments object? */
119    public static final int IS_ARGUMENTS   = 1 << 2;
120
121    /** Is length property not-writable? */
122    public static final int IS_LENGTH_NOT_WRITABLE = 1 << 3;
123
124    /** Is this a builtin object? */
125    public static final int IS_BUILTIN = 1 << 4;
126
127    /**
128     * Spill growth rate - by how many elements does {@link ScriptObject#primitiveSpill} and
129     * {@link ScriptObject#objectSpill} when full
130     */
131    public static final int SPILL_RATE = 8;
132
133    /** Map to property information and accessor functions. Ordered by insertion. */
134    private PropertyMap map;
135
136    /** objects proto. */
137    private ScriptObject proto;
138
139    /** Object flags. */
140    private int flags;
141
142    /** Area for primitive properties added to object after instantiation, see {@link AccessorProperty} */
143    protected long[]   primitiveSpill;
144
145    /** Area for reference properties added to object after instantiation, see {@link AccessorProperty} */
146    protected Object[] objectSpill;
147
148    /** Indexed array data. */
149    private ArrayData arrayData;
150
151    /** Method handle to retrieve prototype of this object */
152    public static final MethodHandle GETPROTO      = findOwnMH_V("getProto", ScriptObject.class);
153
154    static final MethodHandle MEGAMORPHIC_GET    = findOwnMH_V("megamorphicGet", Object.class, String.class, boolean.class);
155    static final MethodHandle GLOBALFILTER       = findOwnMH_S("globalFilter", Object.class, Object.class);
156    static final MethodHandle DECLARE_AND_SET    = findOwnMH_V("declareAndSet", void.class, String.class, Object.class);
157
158    private static final MethodHandle TRUNCATINGFILTER   = findOwnMH_S("truncatingFilter", Object[].class, int.class, Object[].class);
159    private static final MethodHandle KNOWNFUNCPROPGUARDSELF = findOwnMH_S("knownFunctionPropertyGuardSelf", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, ScriptFunction.class);
160    private static final MethodHandle KNOWNFUNCPROPGUARDPROTO = findOwnMH_S("knownFunctionPropertyGuardProto", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, int.class, ScriptFunction.class);
161
162    private static final ArrayList<MethodHandle> PROTO_FILTERS = new ArrayList<>();
163
164    /** Method handle for getting the array data */
165    public static final Call GET_ARRAY          = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArray", ArrayData.class);
166
167    /** Method handle for getting a function argument at a given index. Used from MapCreator */
168    public static final Call GET_ARGUMENT       = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArgument", Object.class, int.class);
169
170    /** Method handle for setting a function argument at a given index. Used from MapCreator */
171    public static final Call SET_ARGUMENT       = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setArgument", void.class, int.class, Object.class);
172
173    /** Method handle for getting the proto of a ScriptObject */
174    public static final Call GET_PROTO          = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class);
175
176    /** Method handle for getting the proto of a ScriptObject */
177    public static final Call GET_PROTO_DEPTH    = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class, int.class);
178
179    /** Method handle for setting the proto of a ScriptObject */
180    public static final Call SET_GLOBAL_OBJECT_PROTO = staticCallNoLookup(ScriptObject.class, "setGlobalObjectProto", void.class, ScriptObject.class);
181
182    /** Method handle for setting the proto of a ScriptObject after checking argument */
183    public static final Call SET_PROTO_FROM_LITERAL    = virtualCallNoLookup(ScriptObject.class, "setProtoFromLiteral", void.class, Object.class);
184
185    /** Method handle for setting the user accessors of a ScriptObject */
186    //TODO fastpath this
187    public static final Call SET_USER_ACCESSORS = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class);
188
189    static final MethodHandle[] SET_SLOW = new MethodHandle[] {
190        findOwnMH_V("set", void.class, Object.class, int.class, int.class),
191        findOwnMH_V("set", void.class, Object.class, long.class, int.class),
192        findOwnMH_V("set", void.class, Object.class, double.class, int.class),
193        findOwnMH_V("set", void.class, Object.class, Object.class, int.class)
194    };
195
196    /** Method handle to reset the map of this ScriptObject */
197    public static final Call SET_MAP = virtualCallNoLookup(ScriptObject.class, "setMap", void.class, PropertyMap.class);
198
199    static final MethodHandle CAS_MAP           = findOwnMH_V("compareAndSetMap", boolean.class, PropertyMap.class, PropertyMap.class);
200    static final MethodHandle EXTENSION_CHECK   = findOwnMH_V("extensionCheck", boolean.class, boolean.class, String.class);
201    static final MethodHandle ENSURE_SPILL_SIZE = findOwnMH_V("ensureSpillSize", Object.class, int.class);
202
203    /**
204     * Constructor
205     */
206    public ScriptObject() {
207        this(null);
208    }
209
210    /**
211    * Constructor
212    *
213    * @param map {@link PropertyMap} used to create the initial object
214    */
215    public ScriptObject(final PropertyMap map) {
216        if (Context.DEBUG) {
217            ScriptObject.count++;
218        }
219        this.arrayData = ArrayData.EMPTY_ARRAY;
220        this.setMap(map == null ? PropertyMap.newMap() : map);
221    }
222
223    /**
224     * Constructor that directly sets the prototype to {@code proto} and property map to
225     * {@code map} without invalidating the map as calling {@link #setProto(ScriptObject)}
226     * would do. This should only be used for objects that are always constructed with the
227     * same combination of prototype and property map.
228     *
229     * @param proto the prototype object
230     * @param map intial {@link PropertyMap}
231     */
232    protected ScriptObject(final ScriptObject proto, final PropertyMap map) {
233        this(map);
234        this.proto = proto;
235    }
236
237    /**
238     * Constructor used to instantiate spill properties directly. Used from
239     * SpillObjectCreator.
240     *
241     * @param map            property maps
242     * @param primitiveSpill primitive spills
243     * @param objectSpill    reference spills
244     */
245    public ScriptObject(final PropertyMap map, final long[] primitiveSpill, final Object[] objectSpill) {
246        this(map);
247        this.primitiveSpill = primitiveSpill;
248        this.objectSpill    = objectSpill;
249        assert primitiveSpill == null || primitiveSpill.length == objectSpill.length : " primitive spill pool size is not the same length as object spill pool size";
250    }
251
252    /**
253     * Check whether this is a global object
254     * @return true if global
255     */
256    protected boolean isGlobal() {
257        return false;
258    }
259
260    private static int alignUp(final int size, final int alignment) {
261        return size + alignment - 1 & ~(alignment - 1);
262    }
263
264    /**
265     * Given a number of properties, return the aligned to SPILL_RATE
266     * buffer size required for the smallest spill pool needed to
267     * house them
268     * @param nProperties number of properties
269     * @return property buffer length, a multiple of SPILL_RATE
270     */
271    public static int spillAllocationLength(final int nProperties) {
272        return alignUp(nProperties, SPILL_RATE);
273    }
274
275    /**
276     * Copy all properties from the source object with their receiver bound to the source.
277     * This function was known as mergeMap
278     *
279     * @param source The source object to copy from.
280     */
281    public void addBoundProperties(final ScriptObject source) {
282        addBoundProperties(source, source.getMap().getProperties());
283    }
284
285    /**
286     * Copy all properties from the array with their receiver bound to the source.
287     *
288     * @param source The source object to copy from.
289     * @param properties The array of properties to copy.
290     */
291    public void addBoundProperties(final ScriptObject source, final Property[] properties) {
292        PropertyMap newMap = this.getMap();
293
294        for (final Property property : properties) {
295            newMap = addBoundProperty(newMap, source, property);
296        }
297
298        this.setMap(newMap);
299    }
300
301    /**
302     * Add a bound property from {@code source}, using the interim property map {@code propMap}, and return the
303     * new interim property map.
304     *
305     * @param propMap the property map
306     * @param source the source object
307     * @param property the property to be added
308     * @return the new property map
309     */
310    protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final Property property) {
311        PropertyMap newMap = propMap;
312        final String key = property.getKey();
313        final Property oldProp = newMap.findProperty(key);
314        if (oldProp == null) {
315            if (property instanceof UserAccessorProperty) {
316                // Note: we copy accessor functions to this object which is semantically different from binding.
317                final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source));
318                newMap = newMap.addPropertyNoHistory(prop);
319            } else {
320                newMap = newMap.addPropertyBind((AccessorProperty)property, source);
321            }
322        } else {
323            // See ECMA section 10.5 Declaration Binding Instantiation
324            // step 5 processing each function declaration.
325            if (property.isFunctionDeclaration() && !oldProp.isConfigurable()) {
326                if (oldProp instanceof UserAccessorProperty ||
327                        !(oldProp.isWritable() && oldProp.isEnumerable())) {
328                    throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
329                }
330            }
331        }
332        return newMap;
333    }
334
335    /**
336     * Copy all properties from the array with their receiver bound to the source.
337     *
338     * @param source The source object to copy from.
339     * @param properties The collection of accessor properties to copy.
340     */
341    public void addBoundProperties(final Object source, final AccessorProperty[] properties) {
342        PropertyMap newMap = this.getMap();
343
344        for (final AccessorProperty property : properties) {
345            final String key = property.getKey();
346
347            if (newMap.findProperty(key) == null) {
348                newMap = newMap.addPropertyBind(property, source);
349            }
350        }
351
352        this.setMap(newMap);
353    }
354
355    /**
356     * Bind the method handle to the specified receiver, while preserving its original type (it will just ignore the
357     * first argument in lieu of the bound argument).
358     * @param methodHandle Method handle to bind to.
359     * @param receiver     Object to bind.
360     * @return Bound method handle.
361     */
362    static MethodHandle bindTo(final MethodHandle methodHandle, final Object receiver) {
363        return MH.dropArguments(MH.bindTo(methodHandle, receiver), 0, methodHandle.type().parameterType(0));
364    }
365
366    /**
367     * Return a property iterator.
368     * @return Property iterator.
369     */
370    public Iterator<String> propertyIterator() {
371        return new KeyIterator(this);
372    }
373
374    /**
375     * Return a property value iterator.
376     * @return Property value iterator.
377     */
378    public Iterator<Object> valueIterator() {
379        return new ValueIterator(this);
380    }
381
382    /**
383     * ECMA 8.10.1 IsAccessorDescriptor ( Desc )
384     * @return true if this has a {@link AccessorPropertyDescriptor} with a getter or a setter
385     */
386    public final boolean isAccessorDescriptor() {
387        return has(GET) || has(SET);
388    }
389
390    /**
391     * ECMA 8.10.2 IsDataDescriptor ( Desc )
392     * @return true if this has a {@link DataPropertyDescriptor}, i.e. the object has a property value and is writable
393     */
394    public final boolean isDataDescriptor() {
395        return has(VALUE) || has(WRITABLE);
396    }
397
398    /**
399     * ECMA 8.10.3 IsGenericDescriptor ( Desc )
400     * @return true if this has a descriptor describing an {@link AccessorPropertyDescriptor} or {@link DataPropertyDescriptor}
401     */
402    public final boolean isGenericDescriptor() {
403        return isAccessorDescriptor() || isDataDescriptor();
404    }
405
406    /**
407      * ECMA 8.10.5 ToPropertyDescriptor ( Obj )
408      *
409      * @return property descriptor
410      */
411    public final PropertyDescriptor toPropertyDescriptor() {
412        final Global global = Context.getGlobal();
413
414        final PropertyDescriptor desc;
415        if (isDataDescriptor()) {
416            if (has(SET) || has(GET)) {
417                throw typeError(global, "inconsistent.property.descriptor");
418            }
419
420            desc = global.newDataDescriptor(UNDEFINED, false, false, false);
421        } else if (isAccessorDescriptor()) {
422            if (has(VALUE) || has(WRITABLE)) {
423                throw typeError(global, "inconsistent.property.descriptor");
424            }
425
426            desc = global.newAccessorDescriptor(UNDEFINED, UNDEFINED, false, false);
427        } else {
428            desc = global.newGenericDescriptor(false, false);
429        }
430
431        return desc.fillFrom(this);
432    }
433
434    /**
435     * ECMA 8.10.5 ToPropertyDescriptor ( Obj )
436     *
437     * @param global  global scope object
438     * @param obj object to create property descriptor from
439     *
440     * @return property descriptor
441     */
442    public static PropertyDescriptor toPropertyDescriptor(final Global global, final Object obj) {
443        if (obj instanceof ScriptObject) {
444            return ((ScriptObject)obj).toPropertyDescriptor();
445        }
446
447        throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj));
448    }
449
450    /**
451     * ECMA 8.12.1 [[GetOwnProperty]] (P)
452     *
453     * @param key property key
454     *
455     * @return Returns the Property Descriptor of the named own property of this
456     * object, or undefined if absent.
457     */
458    public Object getOwnPropertyDescriptor(final String key) {
459        final Property property = getMap().findProperty(key);
460
461        final Global global = Context.getGlobal();
462
463        if (property != null) {
464            final ScriptFunction get   = property.getGetterFunction(this);
465            final ScriptFunction set   = property.getSetterFunction(this);
466
467            final boolean configurable = property.isConfigurable();
468            final boolean enumerable   = property.isEnumerable();
469            final boolean writable     = property.isWritable();
470
471            if (property instanceof UserAccessorProperty) {
472                return global.newAccessorDescriptor(
473                    get != null ?
474                        get :
475                        UNDEFINED,
476                    set != null ?
477                        set :
478                        UNDEFINED,
479                    configurable,
480                    enumerable);
481            }
482
483            return global.newDataDescriptor(getWithProperty(property), configurable, enumerable, writable);
484        }
485
486        final int index = getArrayIndex(key);
487        final ArrayData array = getArray();
488
489        if (array.has(index)) {
490            return array.getDescriptor(global, index);
491        }
492
493        return UNDEFINED;
494    }
495
496    /**
497     * ECMA 8.12.2 [[GetProperty]] (P)
498     *
499     * @param key property key
500     *
501     * @return Returns the fully populated Property Descriptor of the named property
502     * of this object, or undefined if absent.
503     */
504    public Object getPropertyDescriptor(final String key) {
505        final Object res = getOwnPropertyDescriptor(key);
506
507        if (res != UNDEFINED) {
508            return res;
509        } else if (getProto() != null) {
510            return getProto().getOwnPropertyDescriptor(key);
511        } else {
512            return UNDEFINED;
513        }
514    }
515
516    /**
517     * Invalidate any existing global constant method handles that may exist for {@code key}.
518     * @param key the property name
519     */
520    protected void invalidateGlobalConstant(final String key) {
521        final GlobalConstants globalConstants = getGlobalConstants();
522        if (globalConstants != null) {
523            globalConstants.delete(key);
524        }
525    }
526
527    /**
528     * ECMA 8.12.9 [[DefineOwnProperty]] (P, Desc, Throw)
529     *
530     * @param key the property key
531     * @param propertyDesc the property descriptor
532     * @param reject is the property extensible - true means new definitions are rejected
533     *
534     * @return true if property was successfully defined
535     */
536    public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
537        final Global             global  = Context.getGlobal();
538        final PropertyDescriptor desc    = toPropertyDescriptor(global, propertyDesc);
539        final Object             current = getOwnPropertyDescriptor(key);
540        final String             name    = JSType.toString(key);
541
542        invalidateGlobalConstant(key);
543
544        if (current == UNDEFINED) {
545            if (isExtensible()) {
546                // add a new own property
547                addOwnProperty(key, desc);
548                return true;
549            }
550            // new property added to non-extensible object
551            if (reject) {
552                throw typeError(global, "object.non.extensible", name, ScriptRuntime.safeToString(this));
553            }
554            return false;
555        }
556
557        // modifying an existing property
558        final PropertyDescriptor currentDesc = (PropertyDescriptor)current;
559        final PropertyDescriptor newDesc     = desc;
560
561        if (newDesc.type() == PropertyDescriptor.GENERIC && !newDesc.has(CONFIGURABLE) && !newDesc.has(ENUMERABLE)) {
562            // every descriptor field is absent
563            return true;
564        }
565
566        if (newDesc.hasAndEquals(currentDesc)) {
567            // every descriptor field of the new is same as the current
568            return true;
569        }
570
571        if (!currentDesc.isConfigurable()) {
572            if (newDesc.has(CONFIGURABLE) && newDesc.isConfigurable()) {
573                // not configurable can not be made configurable
574                if (reject) {
575                    throw typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this));
576                }
577                return false;
578            }
579
580            if (newDesc.has(ENUMERABLE) &&
581                currentDesc.isEnumerable() != newDesc.isEnumerable()) {
582                // cannot make non-enumerable as enumerable or vice-versa
583                if (reject) {
584                    throw typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this));
585                }
586                return false;
587            }
588        }
589
590        int propFlags = Property.mergeFlags(currentDesc, newDesc);
591        Property property = getMap().findProperty(key);
592
593        if (currentDesc.type() == PropertyDescriptor.DATA &&
594                (newDesc.type() == PropertyDescriptor.DATA ||
595                 newDesc.type() == PropertyDescriptor.GENERIC)) {
596            if (!currentDesc.isConfigurable() && !currentDesc.isWritable()) {
597                if (newDesc.has(WRITABLE) && newDesc.isWritable() ||
598                    newDesc.has(VALUE) && !ScriptRuntime.sameValue(currentDesc.getValue(), newDesc.getValue())) {
599                    if (reject) {
600                        throw typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this));
601                    }
602                    return false;
603                }
604            }
605
606            final boolean newValue = newDesc.has(VALUE);
607            final Object value     = newValue ? newDesc.getValue() : currentDesc.getValue();
608
609            if (newValue && property != null) {
610                // Temporarily clear flags.
611                property = modifyOwnProperty(property, 0);
612                set(key, value, 0);
613                //this might change the map if we change types of the property
614                //hence we need to read it again. note that we should probably
615                //have the setter return the new property throughout and in
616                //general respect Property return values from modify and add
617                //functions - which we don't seem to do at all here :-(
618                //There is already a bug filed to generify PropertyAccess so we
619                //can have the setter return e.g. a Property
620                property = getMap().findProperty(key);
621            }
622
623            if (property == null) {
624                // promoting an arrayData value to actual property
625                addOwnProperty(key, propFlags, value);
626                checkIntegerKey(key);
627            } else {
628                // Now set the new flags
629                modifyOwnProperty(property, propFlags);
630            }
631        } else if (currentDesc.type() == PropertyDescriptor.ACCESSOR &&
632                   (newDesc.type() == PropertyDescriptor.ACCESSOR ||
633                    newDesc.type() == PropertyDescriptor.GENERIC)) {
634            if (!currentDesc.isConfigurable()) {
635                if (newDesc.has(PropertyDescriptor.GET) && !ScriptRuntime.sameValue(currentDesc.getGetter(), newDesc.getGetter()) ||
636                    newDesc.has(PropertyDescriptor.SET) && !ScriptRuntime.sameValue(currentDesc.getSetter(), newDesc.getSetter())) {
637                    if (reject) {
638                        throw typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this));
639                    }
640                    return false;
641                }
642            }
643            // New set the new features.
644            modifyOwnProperty(property, propFlags,
645                                      newDesc.has(GET) ? newDesc.getGetter() : currentDesc.getGetter(),
646                                      newDesc.has(SET) ? newDesc.getSetter() : currentDesc.getSetter());
647        } else {
648            // changing descriptor type
649            if (!currentDesc.isConfigurable()) {
650                // not configurable can not be made configurable
651                if (reject) {
652                    throw typeError(global, "cant.redefine.property", name, ScriptRuntime.safeToString(this));
653                }
654                return false;
655            }
656
657            propFlags = 0;
658
659            // Preserve only configurable and enumerable from current desc
660            // if those are not overridden in the new property descriptor.
661            boolean value = newDesc.has(CONFIGURABLE) ? newDesc.isConfigurable() : currentDesc.isConfigurable();
662            if (!value) {
663                propFlags |= Property.NOT_CONFIGURABLE;
664            }
665            value = newDesc.has(ENUMERABLE)? newDesc.isEnumerable() : currentDesc.isEnumerable();
666            if (!value) {
667                propFlags |= Property.NOT_ENUMERABLE;
668            }
669
670            final int type = newDesc.type();
671            if (type == PropertyDescriptor.DATA) {
672                // get writable from the new descriptor
673                value = newDesc.has(WRITABLE) && newDesc.isWritable();
674                if (!value) {
675                    propFlags |= Property.NOT_WRITABLE;
676                }
677
678                // delete the old property
679                deleteOwnProperty(property);
680                // add new data property
681                addOwnProperty(key, propFlags, newDesc.getValue());
682            } else if (type == PropertyDescriptor.ACCESSOR) {
683                if (property == null) {
684                    addOwnProperty(key, propFlags,
685                                     newDesc.has(GET) ? newDesc.getGetter() : null,
686                                     newDesc.has(SET) ? newDesc.getSetter() : null);
687                } else {
688                    // Modify old property with the new features.
689                    modifyOwnProperty(property, propFlags,
690                                        newDesc.has(GET) ? newDesc.getGetter() : null,
691                                        newDesc.has(SET) ? newDesc.getSetter() : null);
692                }
693            }
694        }
695
696        checkIntegerKey(key);
697
698        return true;
699    }
700
701    /**
702     * Almost like defineOwnProperty(int,Object) for arrays this one does
703     * not add 'gap' elements (like the array one does).
704     *
705     * @param index key for property
706     * @param value value to define
707     */
708    public void defineOwnProperty(final int index, final Object value) {
709        assert isValidArrayIndex(index) : "invalid array index";
710        final long longIndex = ArrayIndex.toLongIndex(index);
711        final long oldLength = getArray().length();
712        if (longIndex >= oldLength) {
713            setArray(getArray().ensure(longIndex));
714            doesNotHaveEnsureDelete(longIndex, oldLength, false);
715        }
716        setArray(getArray().set(index, value, false));
717    }
718
719    private void checkIntegerKey(final String key) {
720        final int index = getArrayIndex(key);
721
722        if (isValidArrayIndex(index)) {
723            final ArrayData data = getArray();
724
725            if (data.has(index)) {
726                setArray(data.delete(index));
727            }
728        }
729    }
730
731    /**
732      * Add a new property to the object.
733      *
734      * @param key          property key
735      * @param propertyDesc property descriptor for property
736      */
737    public final void addOwnProperty(final String key, final PropertyDescriptor propertyDesc) {
738        // Already checked that there is no own property with that key.
739        PropertyDescriptor pdesc = propertyDesc;
740
741        final int propFlags = Property.toFlags(pdesc);
742
743        if (pdesc.type() == PropertyDescriptor.GENERIC) {
744            final Global global = Context.getGlobal();
745            final PropertyDescriptor dDesc = global.newDataDescriptor(UNDEFINED, false, false, false);
746
747            dDesc.fillFrom((ScriptObject)pdesc);
748            pdesc = dDesc;
749        }
750
751        final int type = pdesc.type();
752        if (type == PropertyDescriptor.DATA) {
753            addOwnProperty(key, propFlags, pdesc.getValue());
754        } else if (type == PropertyDescriptor.ACCESSOR) {
755            addOwnProperty(key, propFlags,
756                    pdesc.has(GET) ? pdesc.getGetter() : null,
757                    pdesc.has(SET) ? pdesc.getSetter() : null);
758        }
759
760        checkIntegerKey(key);
761    }
762
763    /**
764     * Low level property API (not using property descriptors)
765     * <p>
766     * Find a property in the prototype hierarchy. Note: this is final and not
767     * a good idea to override. If you have to, use
768     * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or
769     * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the
770     * overriding way to find array properties
771     *
772     * @see jdk.nashorn.internal.objects.NativeArray
773     *
774     * @param key  Property key.
775     * @param deep Whether the search should look up proto chain.
776     *
777     * @return FindPropertyData or null if not found.
778     */
779    public final FindProperty findProperty(final String key, final boolean deep) {
780        return findProperty(key, deep, this);
781    }
782
783    /**
784     * Low level property API (not using property descriptors)
785     * <p>
786     * Find a property in the prototype hierarchy. Note: this is not a good idea
787     * to override except as it was done in {@link WithObject}.
788     * If you have to, use
789     * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or
790     * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the
791     * overriding way to find array properties
792     *
793     * @see jdk.nashorn.internal.objects.NativeArray
794     *
795     * @param key  Property key.
796     * @param deep Whether the search should look up proto chain.
797     * @param start the object on which the lookup was originally initiated
798     *
799     * @return FindPropertyData or null if not found.
800     */
801    protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) {
802
803        final PropertyMap selfMap  = getMap();
804        final Property    property = selfMap.findProperty(key);
805
806        if (property != null) {
807            return new FindProperty(start, this, property);
808        }
809
810        if (deep) {
811            final ScriptObject myProto = getProto();
812            if (myProto != null) {
813                return myProto.findProperty(key, deep, start);
814            }
815        }
816
817        return null;
818    }
819
820    /**
821     * Low level property API. This is similar to {@link #findProperty(String, boolean)} but returns a
822     * {@code boolean} value instead of a {@link FindProperty} object.
823     * @param key  Property key.
824     * @param deep Whether the search should look up proto chain.
825     * @return true if the property was found.
826     */
827    boolean hasProperty(final String key, final boolean deep) {
828        if (getMap().findProperty(key) != null) {
829            return true;
830        }
831
832        if (deep) {
833            final ScriptObject myProto = getProto();
834            if (myProto != null) {
835                return myProto.hasProperty(key, deep);
836            }
837        }
838
839        return false;
840    }
841
842    private SwitchPoint findBuiltinSwitchPoint(final String key) {
843        for (ScriptObject myProto = getProto(); myProto != null; myProto = myProto.getProto()) {
844            final Property prop = myProto.getMap().findProperty(key);
845            if (prop != null) {
846                final SwitchPoint sp = prop.getBuiltinSwitchPoint();
847                if (sp != null && !sp.hasBeenInvalidated()) {
848                    return sp;
849                }
850            }
851        }
852        return null;
853    }
854
855    /**
856     * Add a new property to the object.
857     * <p>
858     * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s
859     *
860     * @param key             Property key.
861     * @param propertyFlags   Property flags.
862     * @param getter          Property getter, or null if not defined
863     * @param setter          Property setter, or null if not defined
864     *
865     * @return New property.
866     */
867    public final Property addOwnProperty(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) {
868        return addOwnProperty(newUserAccessors(key, propertyFlags, getter, setter));
869    }
870
871    /**
872     * Add a new property to the object.
873     * <p>
874     * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s
875     *
876     * @param key             Property key.
877     * @param propertyFlags   Property flags.
878     * @param value           Value of property
879     *
880     * @return New property.
881     */
882    public final Property addOwnProperty(final String key, final int propertyFlags, final Object value) {
883        return addSpillProperty(key, propertyFlags, value, true);
884    }
885
886    /**
887     * Add a new property to the object.
888     * <p>
889     * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s
890     *
891     * @param newProperty property to add
892     *
893     * @return New property.
894     */
895    public final Property addOwnProperty(final Property newProperty) {
896        PropertyMap oldMap = getMap();
897        while (true) {
898            final PropertyMap newMap = oldMap.addProperty(newProperty);
899            if (!compareAndSetMap(oldMap, newMap)) {
900                oldMap = getMap();
901                final Property oldProperty = oldMap.findProperty(newProperty.getKey());
902
903                if (oldProperty != null) {
904                    return oldProperty;
905                }
906            } else {
907                return newProperty;
908            }
909        }
910    }
911
912    private void erasePropertyValue(final Property property) {
913        // Erase the property field value with undefined. If the property is defined
914        // by user-defined accessors, we don't want to call the setter!!
915        if (!(property instanceof UserAccessorProperty)) {
916            assert property != null;
917            property.setValue(this, this, UNDEFINED, false);
918        }
919    }
920
921    /**
922     * Delete a property from the object.
923     *
924     * @param property Property to delete.
925     *
926     * @return true if deleted.
927     */
928    public final boolean deleteOwnProperty(final Property property) {
929        erasePropertyValue(property);
930        PropertyMap oldMap = getMap();
931
932        while (true) {
933            final PropertyMap newMap = oldMap.deleteProperty(property);
934
935            if (newMap == null) {
936                return false;
937            }
938
939            if (!compareAndSetMap(oldMap, newMap)) {
940                oldMap = getMap();
941            } else {
942                // delete getter and setter function references so that we don't leak
943                if (property instanceof UserAccessorProperty) {
944                    ((UserAccessorProperty)property).setAccessors(this, getMap(), null);
945                }
946
947                invalidateGlobalConstant(property.getKey());
948                return true;
949            }
950        }
951
952    }
953
954    /**
955     * Fast initialization functions for ScriptFunctions that are strict, to avoid
956     * creating setters that probably aren't used. Inject directly into the spill pool
957     * the defaults for "arguments" and "caller"
958     *
959     * @param key           property key
960     * @param propertyFlags flags
961     * @param getter        getter for {@link UserAccessorProperty}, null if not present or N/A
962     * @param setter        setter for {@link UserAccessorProperty}, null if not present or N/A
963     */
964    protected final void initUserAccessors(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) {
965        final PropertyMap oldMap = getMap();
966        final int slot = oldMap.getFreeSpillSlot();
967        ensureSpillSize(slot);
968        objectSpill[slot] = new UserAccessorProperty.Accessors(getter, setter);
969        Property    newProperty;
970        PropertyMap newMap;
971        do {
972            newProperty = new UserAccessorProperty(key, propertyFlags, slot);
973            newMap = oldMap.addProperty(newProperty);
974        } while (!compareAndSetMap(oldMap, newMap));
975    }
976
977    /**
978     * Modify a property in the object
979     *
980     * @param oldProperty    property to modify
981     * @param propertyFlags  new property flags
982     * @param getter         getter for {@link UserAccessorProperty}, null if not present or N/A
983     * @param setter         setter for {@link UserAccessorProperty}, null if not present or N/A
984     *
985     * @return new property
986     */
987    public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) {
988        Property newProperty;
989
990        if (oldProperty instanceof UserAccessorProperty) {
991            final UserAccessorProperty uc = (UserAccessorProperty)oldProperty;
992            final int slot = uc.getSlot();
993
994            assert uc.getLocalType() == Object.class;
995            final UserAccessorProperty.Accessors gs = uc.getAccessors(this); //this crashes
996            assert gs != null;
997            //reuse existing getter setter for speed
998            gs.set(getter, setter);
999            if (uc.getFlags() == propertyFlags) {
1000                return oldProperty;
1001            }
1002            newProperty = new UserAccessorProperty(uc.getKey(), propertyFlags, slot);
1003        } else {
1004            // erase old property value and create new user accessor property
1005            erasePropertyValue(oldProperty);
1006            newProperty = newUserAccessors(oldProperty.getKey(), propertyFlags, getter, setter);
1007        }
1008
1009        return modifyOwnProperty(oldProperty, newProperty);
1010    }
1011
1012    /**
1013      * Modify a property in the object
1014      *
1015      * @param oldProperty    property to modify
1016      * @param propertyFlags  new property flags
1017      *
1018      * @return new property
1019      */
1020    public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags) {
1021        return modifyOwnProperty(oldProperty, oldProperty.setFlags(propertyFlags));
1022    }
1023
1024    /**
1025     * Modify a property in the object, replacing a property with a new one
1026     *
1027     * @param oldProperty   property to replace
1028     * @param newProperty   property to replace it with
1029     *
1030     * @return new property
1031     */
1032    private Property modifyOwnProperty(final Property oldProperty, final Property newProperty) {
1033        if (oldProperty == newProperty) {
1034            return newProperty; //nop
1035        }
1036
1037        assert newProperty.getKey().equals(oldProperty.getKey()) : "replacing property with different key";
1038
1039        PropertyMap oldMap = getMap();
1040
1041        while (true) {
1042            final PropertyMap newMap = oldMap.replaceProperty(oldProperty, newProperty);
1043
1044            if (!compareAndSetMap(oldMap, newMap)) {
1045                oldMap = getMap();
1046                final Property oldPropertyLookup = oldMap.findProperty(oldProperty.getKey());
1047
1048                if (oldPropertyLookup != null && oldPropertyLookup.equals(newProperty)) {
1049                    return oldPropertyLookup;
1050                }
1051            } else {
1052                return newProperty;
1053            }
1054        }
1055    }
1056
1057    /**
1058     * Update getter and setter in an object literal.
1059     *
1060     * @param key    Property key.
1061     * @param getter {@link UserAccessorProperty} defined getter, or null if none
1062     * @param setter {@link UserAccessorProperty} defined setter, or null if none
1063     */
1064    public final void setUserAccessors(final String key, final ScriptFunction getter, final ScriptFunction setter) {
1065        final Property oldProperty = getMap().findProperty(key);
1066        if (oldProperty instanceof UserAccessorProperty) {
1067            modifyOwnProperty(oldProperty, oldProperty.getFlags(), getter, setter);
1068        } else {
1069            addOwnProperty(newUserAccessors(key, oldProperty != null ? oldProperty.getFlags() : 0, getter, setter));
1070        }
1071    }
1072
1073    private static int getIntValue(final FindProperty find, final int programPoint) {
1074        final MethodHandle getter = find.getGetter(int.class, programPoint, null);
1075        if (getter != null) {
1076            try {
1077                return (int)getter.invokeExact((Object)find.getGetterReceiver());
1078            } catch (final Error|RuntimeException e) {
1079                throw e;
1080            } catch (final Throwable e) {
1081                throw new RuntimeException(e);
1082            }
1083        }
1084
1085        return UNDEFINED_INT;
1086    }
1087
1088    private static long getLongValue(final FindProperty find, final int programPoint) {
1089        final MethodHandle getter = find.getGetter(long.class, programPoint, null);
1090        if (getter != null) {
1091            try {
1092                return (long)getter.invokeExact((Object)find.getGetterReceiver());
1093            } catch (final Error|RuntimeException e) {
1094                throw e;
1095            } catch (final Throwable e) {
1096                throw new RuntimeException(e);
1097            }
1098        }
1099
1100        return UNDEFINED_LONG;
1101    }
1102
1103    private static double getDoubleValue(final FindProperty find, final int programPoint) {
1104        final MethodHandle getter = find.getGetter(double.class, programPoint, null);
1105        if (getter != null) {
1106            try {
1107                return (double)getter.invokeExact((Object)find.getGetterReceiver());
1108            } catch (final Error|RuntimeException e) {
1109                throw e;
1110            } catch (final Throwable e) {
1111                throw new RuntimeException(e);
1112            }
1113        }
1114
1115        return UNDEFINED_DOUBLE;
1116    }
1117
1118    /**
1119     * Return methodHandle of value function for call.
1120     *
1121     * @param find      data from find property.
1122     * @param type      method type of function.
1123     * @param bindName  null or name to bind to second argument (property not found method.)
1124     *
1125     * @return value of property as a MethodHandle or null.
1126     */
1127    protected MethodHandle getCallMethodHandle(final FindProperty find, final MethodType type, final String bindName) {
1128        return getCallMethodHandle(find.getObjectValue(), type, bindName);
1129    }
1130
1131    /**
1132     * Return methodHandle of value function for call.
1133     *
1134     * @param value     value of receiver, it not a {@link ScriptFunction} this will return null.
1135     * @param type      method type of function.
1136     * @param bindName  null or name to bind to second argument (property not found method.)
1137     *
1138     * @return value of property as a MethodHandle or null.
1139     */
1140    protected static MethodHandle getCallMethodHandle(final Object value, final MethodType type, final String bindName) {
1141        return value instanceof ScriptFunction ? ((ScriptFunction)value).getCallMethodHandle(type, bindName) : null;
1142    }
1143
1144    /**
1145     * Get value using found property.
1146     *
1147     * @param property Found property.
1148     *
1149     * @return Value of property.
1150     */
1151    public final Object getWithProperty(final Property property) {
1152        return new FindProperty(this, this, property).getObjectValue();
1153    }
1154
1155    /**
1156     * Get a property given a key
1157     *
1158     * @param key property key
1159     *
1160     * @return property for key
1161     */
1162    public final Property getProperty(final String key) {
1163        return getMap().findProperty(key);
1164    }
1165
1166    /**
1167     * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.)
1168     * Used for argument access in a vararg function using parameter name.
1169     * Returns the argument at a given key (index)
1170     *
1171     * @param key argument index
1172     *
1173     * @return the argument at the given position, or undefined if not present
1174     */
1175    public Object getArgument(final int key) {
1176        return get(key);
1177    }
1178
1179    /**
1180     * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.)
1181     * Used for argument access in a vararg function using parameter name.
1182     * Returns the argument at a given key (index)
1183     *
1184     * @param key   argument index
1185     * @param value the value to write at the given index
1186     */
1187    public void setArgument(final int key, final Object value) {
1188        set(key, value, 0);
1189    }
1190
1191    /**
1192     * Return the current context from the object's map.
1193     * @return Current context.
1194     */
1195    protected Context getContext() {
1196        return Context.fromClass(getClass());
1197    }
1198
1199    /**
1200     * Return the map of an object.
1201     * @return PropertyMap object.
1202     */
1203    public final PropertyMap getMap() {
1204        return map;
1205    }
1206
1207    /**
1208     * Set the initial map.
1209     * @param map Initial map.
1210     */
1211    public final void setMap(final PropertyMap map) {
1212        this.map = map;
1213    }
1214
1215    /**
1216     * Conditionally set the new map if the old map is the same.
1217     * @param oldMap Map prior to manipulation.
1218     * @param newMap Replacement map.
1219     * @return true if the operation succeeded.
1220     */
1221    protected final boolean compareAndSetMap(final PropertyMap oldMap, final PropertyMap newMap) {
1222        if (oldMap == this.map) {
1223            this.map = newMap;
1224            return true;
1225        }
1226        return false;
1227     }
1228
1229    /**
1230     * Return the __proto__ of an object.
1231     * @return __proto__ object.
1232     */
1233    public final ScriptObject getProto() {
1234        return proto;
1235    }
1236
1237    /**
1238     * Get the proto of a specific depth
1239     * @param n depth
1240     * @return proto at given depth
1241     */
1242    public final ScriptObject getProto(final int n) {
1243        assert n > 0;
1244        ScriptObject p = getProto();
1245        for (int i = n; i-- > 0;) {
1246            p = p.getProto();
1247        }
1248        return p;
1249    }
1250
1251    /**
1252     * Set the __proto__ of an object.
1253     * @param newProto new __proto__ to set.
1254     */
1255    public final void setProto(final ScriptObject newProto) {
1256        final ScriptObject oldProto = proto;
1257
1258        if (oldProto != newProto) {
1259            proto = newProto;
1260
1261            // Let current listeners know that the protototype has changed and set our map
1262            final PropertyListeners listeners = getMap().getListeners();
1263            if (listeners != null) {
1264                listeners.protoChanged();
1265            }
1266            // Replace our current allocator map with one that is associated with the new prototype.
1267            setMap(getMap().changeProto(newProto));
1268        }
1269    }
1270
1271    /**
1272     * Set the initial __proto__ of this object. This should be used instead of
1273     * {@link #setProto} if it is known that the current property map will not be
1274     * used on a new object with any other parent property map, so we can pass over
1275     * property map invalidation/evolution.
1276     *
1277     * @param initialProto the initial __proto__ to set.
1278     */
1279    public void setInitialProto(final ScriptObject initialProto) {
1280        this.proto = initialProto;
1281    }
1282
1283    /**
1284     * Invoked from generated bytecode to initialize the prototype of object literals to the global Object prototype.
1285     * @param obj the object literal that needs to have its prototype initialized to the global Object prototype.
1286     */
1287    public static void setGlobalObjectProto(final ScriptObject obj) {
1288        obj.setInitialProto(Global.objectPrototype());
1289    }
1290
1291    /**
1292     * Set the __proto__ of an object with checks.
1293     * This is the built-in operation [[SetPrototypeOf]]
1294     * See ES6 draft spec: 9.1.2 [[SetPrototypeOf]] (V)
1295     *
1296     * @param newProto Prototype to set.
1297     */
1298    public final void setPrototypeOf(final Object newProto) {
1299        if (newProto == null || newProto instanceof ScriptObject) {
1300            if (! isExtensible()) {
1301                // okay to set same proto again - even if non-extensible
1302
1303                if (newProto == getProto()) {
1304                    return;
1305                }
1306                throw typeError("__proto__.set.non.extensible", ScriptRuntime.safeToString(this));
1307            }
1308
1309            // check for circularity
1310            ScriptObject p = (ScriptObject)newProto;
1311            while (p != null) {
1312                if (p == this) {
1313                    throw typeError("circular.__proto__.set", ScriptRuntime.safeToString(this));
1314                }
1315                p = p.getProto();
1316            }
1317            setProto((ScriptObject)newProto);
1318        } else {
1319            throw typeError("cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto));
1320        }
1321    }
1322
1323    /**
1324     * Set the __proto__ of an object from an object literal.
1325     * See ES6 draft spec: B.3.1 __proto__ Property Names in
1326     * Object Initializers. Step 6 handling of "__proto__".
1327     *
1328     * @param newProto Prototype to set.
1329     */
1330    public final void setProtoFromLiteral(final Object newProto) {
1331        if (newProto == null || newProto instanceof ScriptObject) {
1332            setPrototypeOf(newProto);
1333        } else {
1334            // Some non-object, non-null. Then, we need to set
1335            // Object.prototype as the new __proto__
1336            //
1337            // var obj = { __proto__ : 34 };
1338            // print(obj.__proto__ === Object.prototype); // => true
1339            setPrototypeOf(Global.objectPrototype());
1340        }
1341    }
1342
1343    /**
1344     * return an array of own property keys associated with the object.
1345     *
1346     * @param all True if to include non-enumerable keys.
1347     * @return Array of keys.
1348     */
1349    public final String[] getOwnKeys(final boolean all) {
1350        return getOwnKeys(all, null);
1351    }
1352
1353    /**
1354     * return an array of own property keys associated with the object.
1355     *
1356     * @param all True if to include non-enumerable keys.
1357     * @param nonEnumerable set of non-enumerable properties seen already.Used
1358       to filter out shadowed, but enumerable properties from proto children.
1359     * @return Array of keys.
1360     */
1361    protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
1362        final List<Object> keys    = new ArrayList<>();
1363        final PropertyMap  selfMap = this.getMap();
1364
1365        final ArrayData array  = getArray();
1366
1367        for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
1368            keys.add(JSType.toString(iter.next().longValue()));
1369        }
1370
1371        for (final Property property : selfMap.getProperties()) {
1372            final boolean enumerable = property.isEnumerable();
1373            final String key = property.getKey();
1374            if (all) {
1375                keys.add(key);
1376            } else if (enumerable) {
1377                // either we don't have non-enumerable filter set or filter set
1378                // does not contain the current property.
1379                if (nonEnumerable == null || !nonEnumerable.contains(key)) {
1380                    keys.add(key);
1381                }
1382            } else {
1383                // store this non-enumerable property for later proto walk
1384                if (nonEnumerable != null) {
1385                    nonEnumerable.add(key);
1386                }
1387            }
1388        }
1389
1390        return keys.toArray(new String[keys.size()]);
1391    }
1392
1393    /**
1394     * Check if this ScriptObject has array entries. This means that someone has
1395     * set values with numeric keys in the object.
1396     *
1397     * @return true if array entries exists.
1398     */
1399    public boolean hasArrayEntries() {
1400        return getArray().length() > 0 || getMap().containsArrayKeys();
1401    }
1402
1403    /**
1404     * Return the valid JavaScript type name descriptor
1405     *
1406     * @return "Object"
1407     */
1408    public String getClassName() {
1409        return "Object";
1410    }
1411
1412    /**
1413     * {@code length} is a well known property. This is its getter.
1414     * Note that this *may* be optimized by other classes
1415     *
1416     * @return length property value for this ScriptObject
1417     */
1418    public Object getLength() {
1419        return get("length");
1420    }
1421
1422    /**
1423     * Stateless toString for ScriptObjects.
1424     *
1425     * @return string description of this object, e.g. {@code [object Object]}
1426     */
1427    public String safeToString() {
1428        return "[object " + getClassName() + "]";
1429    }
1430
1431    /**
1432     * Return the default value of the object with a given preferred type hint.
1433     * The preferred type hints are String.class for type String, Number.class
1434     * for type Number. <p>
1435     *
1436     * A <code>hint</code> of null means "no hint".
1437     *
1438     * ECMA 8.12.8 [[DefaultValue]](hint)
1439     *
1440     * @param typeHint the preferred type hint
1441     * @return the default value
1442     */
1443    public Object getDefaultValue(final Class<?> typeHint) {
1444        // We delegate to Global, as the implementation uses dynamic call sites to invoke object's "toString" and
1445        // "valueOf" methods, and in order to avoid those call sites from becoming megamorphic when multiple contexts
1446        // are being executed in a long-running program, we move the code and their associated dynamic call sites
1447        // (Global.TO_STRING and Global.VALUE_OF) into per-context code.
1448        return Context.getGlobal().getDefaultValue(this, typeHint);
1449    }
1450
1451    /**
1452     * Checking whether a script object is an instance of another. Used
1453     * in {@link ScriptFunction} for hasInstance implementation, walks
1454     * the proto chain
1455     *
1456     * @param instance instace to check
1457     * @return true if 'instance' is an instance of this object
1458     */
1459    public boolean isInstance(final ScriptObject instance) {
1460        return false;
1461    }
1462
1463    /**
1464     * Flag this ScriptObject as non extensible
1465     *
1466     * @return the object after being made non extensible
1467     */
1468    public ScriptObject preventExtensions() {
1469        PropertyMap oldMap = getMap();
1470        while (!compareAndSetMap(oldMap,  getMap().preventExtensions())) {
1471            oldMap = getMap();
1472        }
1473
1474        //invalidate any fast array setters
1475        final ArrayData array = getArray();
1476        assert array != null;
1477        setArray(ArrayData.preventExtension(array));
1478        return this;
1479    }
1480
1481    /**
1482     * Check whether if an Object (not just a ScriptObject) represents JavaScript array
1483     *
1484     * @param obj object to check
1485     *
1486     * @return true if array
1487     */
1488    public static boolean isArray(final Object obj) {
1489        return obj instanceof ScriptObject && ((ScriptObject)obj).isArray();
1490    }
1491
1492    /**
1493     * Check if this ScriptObject is an array
1494     * @return true if array
1495     */
1496    public final boolean isArray() {
1497        return (flags & IS_ARRAY) != 0;
1498    }
1499
1500    /**
1501     * Flag this ScriptObject as being an array
1502     */
1503    public final void setIsArray() {
1504        flags |= IS_ARRAY;
1505    }
1506
1507    /**
1508     * Check if this ScriptObject is an {@code arguments} vector
1509     * @return true if arguments vector
1510     */
1511    public final boolean isArguments() {
1512        return (flags & IS_ARGUMENTS) != 0;
1513    }
1514
1515    /**
1516     * Flag this ScriptObject as being an {@code arguments} vector
1517     */
1518    public final void setIsArguments() {
1519        flags |= IS_ARGUMENTS;
1520    }
1521
1522    /**
1523     * Check if this object has non-writable length property
1524     *
1525     * @return {@code true} if 'length' property is non-writable
1526     */
1527    public boolean isLengthNotWritable() {
1528        return (flags & IS_LENGTH_NOT_WRITABLE) != 0;
1529    }
1530
1531    /**
1532     * Flag this object as having non-writable length property.
1533     */
1534    public void setIsLengthNotWritable() {
1535        flags |= IS_LENGTH_NOT_WRITABLE;
1536    }
1537
1538    /**
1539     * Get the {@link ArrayData}, for this ScriptObject, ensuring it is of a type
1540     * that can handle elementType
1541     * @param elementType elementType
1542     * @return array data
1543     */
1544    public final ArrayData getArray(final Class<?> elementType) {
1545        if (elementType == null) {
1546            return arrayData;
1547        }
1548        final ArrayData newArrayData = arrayData.convert(elementType);
1549        if (newArrayData != arrayData) {
1550            arrayData = newArrayData;
1551        }
1552        return newArrayData;
1553    }
1554
1555    /**
1556     * Get the {@link ArrayData} for this ScriptObject if it is an array
1557     * @return array data
1558     */
1559    public final ArrayData getArray() {
1560        return arrayData;
1561    }
1562
1563    /**
1564     * Set the {@link ArrayData} for this ScriptObject if it is to be an array
1565     * @param arrayData the array data
1566     */
1567    public final void setArray(final ArrayData arrayData) {
1568        this.arrayData = arrayData;
1569    }
1570
1571    /**
1572     * Check if this ScriptObject is extensible
1573     * @return true if extensible
1574     */
1575    public boolean isExtensible() {
1576        return getMap().isExtensible();
1577    }
1578
1579    /**
1580     * ECMAScript 15.2.3.8 - seal implementation
1581     * @return the sealed ScriptObject
1582     */
1583    public ScriptObject seal() {
1584        PropertyMap oldMap = getMap();
1585
1586        while (true) {
1587            final PropertyMap newMap = getMap().seal();
1588
1589            if (!compareAndSetMap(oldMap, newMap)) {
1590                oldMap = getMap();
1591            } else {
1592                setArray(ArrayData.seal(getArray()));
1593                return this;
1594            }
1595        }
1596    }
1597
1598    /**
1599     * Check whether this ScriptObject is sealed
1600     * @return true if sealed
1601     */
1602    public boolean isSealed() {
1603        return getMap().isSealed();
1604    }
1605
1606    /**
1607     * ECMA 15.2.39 - freeze implementation. Freeze this ScriptObject
1608     * @return the frozen ScriptObject
1609     */
1610    public ScriptObject freeze() {
1611        PropertyMap oldMap = getMap();
1612
1613        while (true) {
1614            final PropertyMap newMap = getMap().freeze();
1615
1616            if (!compareAndSetMap(oldMap, newMap)) {
1617                oldMap = getMap();
1618            } else {
1619                setArray(ArrayData.freeze(getArray()));
1620                return this;
1621            }
1622        }
1623    }
1624
1625    /**
1626     * Check whether this ScriptObject is frozen
1627     * @return true if frozen
1628     */
1629    public boolean isFrozen() {
1630        return getMap().isFrozen();
1631    }
1632
1633
1634    /**
1635     * Flag this ScriptObject as scope
1636     */
1637    public final void setIsScope() {
1638        if (Context.DEBUG) {
1639            scopeCount++;
1640        }
1641        flags |= IS_SCOPE;
1642    }
1643
1644    /**
1645     * Check whether this ScriptObject is scope
1646     * @return true if scope
1647     */
1648    public final boolean isScope() {
1649        return (flags & IS_SCOPE) != 0;
1650    }
1651
1652    /**
1653     * Tag this script object as built in
1654     */
1655    public final void setIsBuiltin() {
1656        flags |= IS_BUILTIN;
1657    }
1658
1659    /**
1660     * Check if this script object is built in
1661     * @return true if build in
1662     */
1663    public final boolean isBuiltin() {
1664        return (flags & IS_BUILTIN) != 0;
1665    }
1666
1667    /**
1668     * Clears the properties from a ScriptObject
1669     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1670     *
1671     * @param strict strict mode or not
1672     */
1673    public void clear(final boolean strict) {
1674        final Iterator<String> iter = propertyIterator();
1675        while (iter.hasNext()) {
1676            delete(iter.next(), strict);
1677        }
1678    }
1679
1680    /**
1681     * Checks if a property with a given key is present in a ScriptObject
1682     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1683     *
1684     * @param key the key to check for
1685     * @return true if a property with the given key exists, false otherwise
1686     */
1687    public boolean containsKey(final Object key) {
1688        return has(key);
1689    }
1690
1691    /**
1692     * Checks if a property with a given value is present in a ScriptObject
1693     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1694     *
1695     * @param value value to check for
1696     * @return true if a property with the given value exists, false otherwise
1697     */
1698    public boolean containsValue(final Object value) {
1699        final Iterator<Object> iter = valueIterator();
1700        while (iter.hasNext()) {
1701            if (iter.next().equals(value)) {
1702                return true;
1703            }
1704        }
1705        return false;
1706    }
1707
1708    /**
1709     * Returns the set of {@literal <property, value>} entries that make up this
1710     * ScriptObject's properties
1711     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1712     *
1713     * @return an entry set of all the properties in this object
1714     */
1715    public Set<Map.Entry<Object, Object>> entrySet() {
1716        final Iterator<String> iter = propertyIterator();
1717        final Set<Map.Entry<Object, Object>> entries = new HashSet<>();
1718        while (iter.hasNext()) {
1719            final Object key = iter.next();
1720            entries.add(new AbstractMap.SimpleImmutableEntry<>(key, get(key)));
1721        }
1722        return Collections.unmodifiableSet(entries);
1723    }
1724
1725    /**
1726     * Check whether a ScriptObject contains no properties
1727     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1728     *
1729     * @return true if object has no properties
1730     */
1731    public boolean isEmpty() {
1732        return !propertyIterator().hasNext();
1733    }
1734
1735    /**
1736     * Return the set of keys (property names) for all properties
1737     * in this ScriptObject
1738     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1739     *
1740     * @return keySet of this ScriptObject
1741     */
1742    public Set<Object> keySet() {
1743        final Iterator<String> iter = propertyIterator();
1744        final Set<Object> keySet = new HashSet<>();
1745        while (iter.hasNext()) {
1746            keySet.add(iter.next());
1747        }
1748        return Collections.unmodifiableSet(keySet);
1749    }
1750
1751    /**
1752     * Put a property in the ScriptObject
1753     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1754     *
1755     * @param key property key
1756     * @param value property value
1757     * @param strict strict mode or not
1758     * @return oldValue if property with same key existed already
1759     */
1760    public Object put(final Object key, final Object value, final boolean strict) {
1761        final Object oldValue = get(key);
1762        final int scriptObjectFlags = strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0;
1763        set(key, value, scriptObjectFlags);
1764        return oldValue;
1765    }
1766
1767    /**
1768     * Put several properties in the ScriptObject given a mapping
1769     * of their keys to their values
1770     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1771     *
1772     * @param otherMap a {@literal <key,value>} map of properties to add
1773     * @param strict strict mode or not
1774     */
1775    public void putAll(final Map<?, ?> otherMap, final boolean strict) {
1776        final int scriptObjectFlags = strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0;
1777        for (final Map.Entry<?, ?> entry : otherMap.entrySet()) {
1778            set(entry.getKey(), entry.getValue(), scriptObjectFlags);
1779        }
1780    }
1781
1782    /**
1783     * Remove a property from the ScriptObject.
1784     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1785     *
1786     * @param key the key of the property
1787     * @param strict strict mode or not
1788     * @return the oldValue of the removed property
1789     */
1790    public Object remove(final Object key, final boolean strict) {
1791        final Object oldValue = get(key);
1792        delete(key, strict);
1793        return oldValue;
1794    }
1795
1796    /**
1797     * Return the size of the ScriptObject - i.e. the number of properties
1798     * it contains
1799     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1800     *
1801     * @return number of properties in ScriptObject
1802     */
1803    public int size() {
1804        int n = 0;
1805        for (final Iterator<String> iter = propertyIterator(); iter.hasNext(); iter.next()) {
1806            n++;
1807        }
1808        return n;
1809    }
1810
1811    /**
1812     * Return the values of the properties in the ScriptObject
1813     * (java.util.Map-like method to help ScriptObjectMirror implementation)
1814     *
1815     * @return collection of values for the properties in this ScriptObject
1816     */
1817    public Collection<Object> values() {
1818        final List<Object>     values = new ArrayList<>(size());
1819        final Iterator<Object> iter   = valueIterator();
1820        while (iter.hasNext()) {
1821            values.add(iter.next());
1822        }
1823        return Collections.unmodifiableList(values);
1824    }
1825
1826    /**
1827     * Lookup method that, given a CallSiteDescriptor, looks up the target
1828     * MethodHandle and creates a GuardedInvocation
1829     * with the appropriate guard(s).
1830     *
1831     * @param desc call site descriptor
1832     * @param request the link request
1833     *
1834     * @return GuardedInvocation for the callsite
1835     */
1836    public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
1837        final int c = desc.getNameTokenCount();
1838        // JavaScript is "immune" to all currently defined Dynalink composite operation - getProp is the same as getElem
1839        // is the same as getMethod as JavaScript objects have a single namespace for all three. Therefore, we don't
1840        // care about them, and just link to whatever is the first operation.
1841        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
1842        // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself
1843        // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are
1844        // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
1845        // operation has an associated name or not.
1846        switch (operator) {
1847        case "getProp":
1848        case "getElem":
1849        case "getMethod":
1850            return c > 2 ? findGetMethod(desc, request, operator) : findGetIndexMethod(desc, request);
1851        case "setProp":
1852        case "setElem":
1853            return c > 2 ? findSetMethod(desc, request) : findSetIndexMethod(desc, request);
1854        case "call":
1855            return findCallMethod(desc, request);
1856        case "new":
1857            return findNewMethod(desc, request);
1858        case "callMethod":
1859            return findCallMethodMethod(desc, request);
1860        default:
1861            return null;
1862        }
1863    }
1864
1865    /**
1866     * Find the appropriate New method for an invoke dynamic call.
1867     *
1868     * @param desc The invoke dynamic call site descriptor.
1869     * @param request The link request
1870     *
1871     * @return GuardedInvocation to be invoked at call site.
1872     */
1873    protected GuardedInvocation findNewMethod(final CallSiteDescriptor desc, final LinkRequest request) {
1874        return notAFunction();
1875    }
1876
1877    /**
1878     * Find the appropriate CALL method for an invoke dynamic call.
1879     * This generates "not a function" always
1880     *
1881     * @param desc    the call site descriptor.
1882     * @param request the link request
1883     *
1884     * @return GuardedInvocation to be invoed at call site.
1885     */
1886    protected GuardedInvocation findCallMethod(final CallSiteDescriptor desc, final LinkRequest request) {
1887        return notAFunction();
1888    }
1889
1890    private GuardedInvocation notAFunction() {
1891        throw typeError("not.a.function", ScriptRuntime.safeToString(this));
1892    }
1893
1894    /**
1895     * Find an implementation for a "dyn:callMethod" operation. Note that Nashorn internally never uses
1896     * "dyn:callMethod", but instead always emits two call sites in bytecode, one for "dyn:getMethod", and then another
1897     * one for "dyn:call". Explicit support for "dyn:callMethod" is provided for the benefit of potential external
1898     * callers. The implementation itself actually folds a "dyn:getMethod" method handle into a "dyn:call" method handle.
1899     *
1900     * @param desc    the call site descriptor.
1901     * @param request the link request
1902     *
1903     * @return GuardedInvocation to be invoked at call site.
1904     */
1905    protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) {
1906        // R(P0, P1, ...)
1907        final MethodType callType = desc.getMethodType();
1908        // use type Object(P0) for the getter
1909        final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0)));
1910        final GuardedInvocation getter = findGetMethod(getterType, request, "getMethod");
1911
1912        // Object(P0) => Object(P0, P1, ...)
1913        final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount()));
1914        // R(Object, P0, P1, ...)
1915        final MethodHandle invoker = Bootstrap.createDynamicInvoker("dyn:call", callType.insertParameterTypes(0, argDroppingGetter.type().returnType()));
1916        // Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...)
1917        return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard());
1918    }
1919
1920    /**
1921     * Test whether this object contains in its prototype chain or is itself a with-object.
1922     * @return true if a with-object was found
1923     */
1924    final boolean hasWithScope() {
1925        if (isScope()) {
1926            for (ScriptObject obj = this; obj != null; obj = obj.getProto()) {
1927                if (obj instanceof WithObject) {
1928                    return true;
1929                }
1930            }
1931        }
1932        return false;
1933    }
1934
1935    /**
1936     * Add a filter to the first argument of {@code methodHandle} that calls its {@link #getProto()} method
1937     * {@code depth} times.
1938     * @param methodHandle a method handle
1939     * @param depth        distance to target prototype
1940     * @return the filtered method handle
1941     */
1942    static MethodHandle addProtoFilter(final MethodHandle methodHandle, final int depth) {
1943        if (depth == 0) {
1944            return methodHandle;
1945        }
1946        final int listIndex = depth - 1; // We don't need 0-deep walker
1947        MethodHandle filter = listIndex < PROTO_FILTERS.size() ? PROTO_FILTERS.get(listIndex) : null;
1948
1949        if (filter == null) {
1950            filter = addProtoFilter(GETPROTO, depth - 1);
1951            PROTO_FILTERS.add(null);
1952            PROTO_FILTERS.set(listIndex, filter);
1953        }
1954
1955        return MH.filterArguments(methodHandle, 0, filter.asType(filter.type().changeReturnType(methodHandle.type().parameterType(0))));
1956    }
1957
1958    /**
1959     * Find the appropriate GET method for an invoke dynamic call.
1960     *
1961     * @param desc     the call site descriptor
1962     * @param request  the link request
1963     * @param operator operator for get: getProp, getMethod, getElem etc
1964     *
1965     * @return GuardedInvocation to be invoked at call site.
1966     */
1967    protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
1968        final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);
1969
1970        String name;
1971        name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
1972        if (NashornCallSiteDescriptor.isApplyToCall(desc)) {
1973            if (Global.isBuiltinFunctionPrototypeApply()) {
1974                name = "call";
1975            }
1976        }
1977
1978        if (request.isCallSiteUnstable() || hasWithScope()) {
1979            return findMegaMorphicGetMethod(desc, name, "getMethod".equals(operator));
1980        }
1981
1982        final FindProperty find = findProperty(name, true);
1983        MethodHandle mh;
1984
1985        if (find == null) {
1986            switch (operator) {
1987            case "getElem": // getElem only gets here if element name is constant, so treat it like a property access
1988            case "getProp":
1989                return noSuchProperty(desc, request);
1990            case "getMethod":
1991                return noSuchMethod(desc, request);
1992            default:
1993                throw new AssertionError(operator); // never invoked with any other operation
1994            }
1995        }
1996
1997        final GlobalConstants globalConstants = getGlobalConstants();
1998        if (globalConstants != null) {
1999            final GuardedInvocation cinv = globalConstants.findGetMethod(find, this, desc);
2000            if (cinv != null) {
2001                return cinv;
2002            }
2003        }
2004
2005        final Class<?> returnType = desc.getMethodType().returnType();
2006        final Property property   = find.getProperty();
2007
2008        final int programPoint = NashornCallSiteDescriptor.isOptimistic(desc) ?
2009                NashornCallSiteDescriptor.getProgramPoint(desc) :
2010                UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
2011
2012        mh = find.getGetter(returnType, programPoint, request);
2013        // Get the appropriate guard for this callsite and property.
2014        final MethodHandle guard = NashornGuards.getGuard(this, property, desc, explicitInstanceOfCheck);
2015        final ScriptObject owner = find.getOwner();
2016        final Class<ClassCastException> exception = explicitInstanceOfCheck ? null : ClassCastException.class;
2017
2018        final SwitchPoint protoSwitchPoint;
2019
2020        if (mh == null) {
2021            mh = Lookup.emptyGetter(returnType);
2022            protoSwitchPoint = getProtoSwitchPoint(name, owner);
2023        } else if (!find.isSelf()) {
2024            assert mh.type().returnType().equals(returnType) :
2025                    "return type mismatch for getter " + mh.type().returnType() + " != " + returnType;
2026            if (!(property instanceof UserAccessorProperty)) {
2027                // Add a filter that replaces the self object with the prototype owning the property.
2028                mh = addProtoFilter(mh, find.getProtoChainLength());
2029            }
2030            protoSwitchPoint = getProtoSwitchPoint(name, owner);
2031        } else {
2032            protoSwitchPoint = null;
2033        }
2034
2035        final GuardedInvocation inv = new GuardedInvocation(mh, guard, protoSwitchPoint, exception);
2036        return inv.addSwitchPoint(findBuiltinSwitchPoint(name));
2037    }
2038
2039    private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name, final boolean isMethod) {
2040        Context.getContextTrusted().getLogger(ObjectClassGenerator.class).warning("Megamorphic getter: " + desc + " " + name + " " +isMethod);
2041        final MethodHandle invoker = MH.insertArguments(MEGAMORPHIC_GET, 1, name, isMethod);
2042        final MethodHandle guard   = getScriptObjectGuard(desc.getMethodType(), true);
2043        return new GuardedInvocation(invoker, guard);
2044    }
2045
2046    @SuppressWarnings("unused")
2047    private Object megamorphicGet(final String key, final boolean isMethod) {
2048        final FindProperty find = findProperty(key, true);
2049        if (find != null) {
2050            return find.getObjectValue();
2051        }
2052
2053        return isMethod ? getNoSuchMethod(key, INVALID_PROGRAM_POINT) : invokeNoSuchProperty(key, INVALID_PROGRAM_POINT);
2054    }
2055
2056    // Marks a property as declared and sets its value. Used as slow path for block-scoped LET and CONST
2057    @SuppressWarnings("unused")
2058    private void declareAndSet(final String key, final Object value) {
2059        final PropertyMap oldMap = getMap();
2060        final FindProperty find = findProperty(key, false);
2061        assert find != null;
2062
2063        final Property property = find.getProperty();
2064        assert property != null;
2065        assert property.needsDeclaration();
2066
2067        final PropertyMap newMap = oldMap.replaceProperty(property, property.removeFlags(Property.NEEDS_DECLARATION));
2068        setMap(newMap);
2069        set(key, value, 0);
2070    }
2071
2072    /**
2073     * Find the appropriate GETINDEX method for an invoke dynamic call.
2074     *
2075     * @param desc    the call site descriptor
2076     * @param request the link request
2077     *
2078     * @return GuardedInvocation to be invoked at call site.
2079     */
2080    protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
2081        final MethodType callType                = desc.getMethodType();
2082        final Class<?>   returnType              = callType.returnType();
2083        final Class<?>   returnClass             = returnType.isPrimitive() ? returnType : Object.class;
2084        final Class<?>   keyClass                = callType.parameterType(1);
2085        final boolean    explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);
2086
2087        final String name;
2088        if (returnClass.isPrimitive()) {
2089            //turn e.g. get with a double into getDouble
2090            final String returnTypeName = returnClass.getName();
2091            name = "get" + Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length());
2092        } else {
2093            name = "get";
2094        }
2095
2096        final MethodHandle mh = findGetIndexMethodHandle(returnClass, name, keyClass, desc);
2097        return new GuardedInvocation(mh, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class);
2098    }
2099
2100    private static MethodHandle getScriptObjectGuard(final MethodType type, final boolean explicitInstanceOfCheck) {
2101        return ScriptObject.class.isAssignableFrom(type.parameterType(0)) ? null : NashornGuards.getScriptObjectGuard(explicitInstanceOfCheck);
2102    }
2103
2104    /**
2105     * Find a handle for a getIndex method
2106     * @param returnType     return type for getter
2107     * @param name           name
2108     * @param elementType    index type for getter
2109     * @param desc           call site descriptor
2110     * @return method handle for getter
2111     */
2112    protected MethodHandle findGetIndexMethodHandle(final Class<?> returnType, final String name, final Class<?> elementType, final CallSiteDescriptor desc) {
2113        if (!returnType.isPrimitive()) {
2114            return findOwnMH_V(getClass(), name, returnType, elementType);
2115        }
2116
2117        return MH.insertArguments(
2118                findOwnMH_V(getClass(), name, returnType, elementType, int.class),
2119                2,
2120                NashornCallSiteDescriptor.isOptimistic(desc) ?
2121                        NashornCallSiteDescriptor.getProgramPoint(desc) :
2122                        INVALID_PROGRAM_POINT);
2123    }
2124
2125    /**
2126     * Get a switch point for a property with the given {@code name} that will be invalidated when
2127     * the property definition is changed in this object's prototype chain. Returns {@code null} if
2128     * the property is defined in this object itself.
2129     *
2130     * @param name the property name
2131     * @param owner the property owner, null if property is not defined
2132     * @return a SwitchPoint or null
2133     */
2134    public final SwitchPoint getProtoSwitchPoint(final String name, final ScriptObject owner) {
2135        if (owner == this || getProto() == null) {
2136            return null;
2137        }
2138
2139        for (ScriptObject obj = this; obj != owner && obj.getProto() != null; obj = obj.getProto()) {
2140            final ScriptObject parent = obj.getProto();
2141            parent.getMap().addListener(name, obj.getMap());
2142        }
2143
2144        return getMap().getSwitchPoint(name);
2145    }
2146
2147    /**
2148     * Find the appropriate SET method for an invoke dynamic call.
2149     *
2150     * @param desc    the call site descriptor
2151     * @param request the link request
2152     *
2153     * @return GuardedInvocation to be invoked at call site.
2154     */
2155    protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
2156        final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
2157
2158        if (request.isCallSiteUnstable() || hasWithScope()) {
2159            return findMegaMorphicSetMethod(desc, name);
2160        }
2161
2162        final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);
2163
2164        /*
2165         * If doing property set on a scope object, we should stop proto search on the first
2166         * non-scope object. Without this, for example, when assigning "toString" on global scope,
2167         * we'll end up assigning it on it's proto - which is Object.prototype.toString !!
2168         *
2169         * toString = function() { print("global toString"); } // don't affect Object.prototype.toString
2170         */
2171        FindProperty find = findProperty(name, true, this);
2172
2173        // If it's not a scope search, then we don't want any inherited properties except those with user defined accessors.
2174        if (find != null && find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) {
2175            // We should still check if inherited data property is not writable
2176            if (isExtensible() && !find.getProperty().isWritable()) {
2177                return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true);
2178            }
2179            // Otherwise, forget the found property unless this is a scope callsite and the owner is a scope object as well.
2180            if (!NashornCallSiteDescriptor.isScope(desc) || !find.getOwner().isScope()) {
2181                find = null;
2182            }
2183        }
2184
2185        if (find != null) {
2186            if (!find.getProperty().isWritable() && !NashornCallSiteDescriptor.isDeclaration(desc)) {
2187                if (NashornCallSiteDescriptor.isScope(desc) && find.getProperty().isLexicalBinding()) {
2188                    throw typeError("assign.constant", name); // Overwriting ES6 const should throw also in non-strict mode.
2189                }
2190                // Existing, non-writable property
2191                return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true);
2192            }
2193        } else {
2194            if (!isExtensible()) {
2195                return createEmptySetMethod(desc, explicitInstanceOfCheck, "object.non.extensible", false);
2196            }
2197        }
2198
2199        final GuardedInvocation inv = new SetMethodCreator(this, find, desc, request).createGuardedInvocation(findBuiltinSwitchPoint(name));
2200
2201        final GlobalConstants globalConstants = getGlobalConstants();
2202        if (globalConstants != null) {
2203            final GuardedInvocation cinv = globalConstants.findSetMethod(find, this, inv, desc, request);
2204            if (cinv != null) {
2205                return cinv;
2206            }
2207        }
2208
2209        return inv;
2210    }
2211
2212    private GlobalConstants getGlobalConstants() {
2213        // Avoid hitting getContext() which might be costly for a non-Global unless needed.
2214        return GlobalConstants.GLOBAL_ONLY && !isGlobal() ? null : getContext().getGlobalConstants();
2215    }
2216
2217    private GuardedInvocation createEmptySetMethod(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String strictErrorMessage, final boolean canBeFastScope) {
2218        final String  name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
2219        if (NashornCallSiteDescriptor.isStrict(desc)) {
2220            throw typeError(strictErrorMessage, name, ScriptRuntime.safeToString(this));
2221        }
2222        assert canBeFastScope || !NashornCallSiteDescriptor.isFastScope(desc);
2223        return new GuardedInvocation(
2224                Lookup.EMPTY_SETTER,
2225                NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck),
2226                getProtoSwitchPoint(name, null),
2227                explicitInstanceOfCheck ? null : ClassCastException.class);
2228    }
2229
2230    @SuppressWarnings("unused")
2231    private boolean extensionCheck(final boolean isStrict, final String name) {
2232        if (isExtensible()) {
2233            return true; //go on and do the set. this is our guard
2234        } else if (isStrict) {
2235            //throw an error for attempting to do the set in strict mode
2236            throw typeError("object.non.extensible", name, ScriptRuntime.safeToString(this));
2237        } else {
2238            //not extensible, non strict - this is a nop
2239            return false;
2240        }
2241    }
2242
2243    private GuardedInvocation findMegaMorphicSetMethod(final CallSiteDescriptor desc, final String name) {
2244        final MethodType        type = desc.getMethodType().insertParameterTypes(1, Object.class);
2245        //never bother with ClassCastExceptionGuard for megamorphic callsites
2246        final GuardedInvocation inv = findSetIndexMethod(getClass(), desc, false, type);
2247        return inv.replaceMethods(MH.insertArguments(inv.getInvocation(), 1, name), inv.getGuard());
2248    }
2249
2250    @SuppressWarnings("unused")
2251    private static Object globalFilter(final Object object) {
2252        ScriptObject sobj = (ScriptObject) object;
2253        while (sobj != null && !(sobj instanceof Global)) {
2254            sobj = sobj.getProto();
2255        }
2256        return sobj;
2257    }
2258
2259    /**
2260     * Lookup function for the set index method, available for subclasses as well, e.g. {@link NativeArray}
2261     * provides special quick accessor linkage for continuous arrays that are represented as Java arrays
2262     *
2263     * @param desc    call site descriptor
2264     * @param request link request
2265     *
2266     * @return GuardedInvocation to be invoked at call site.
2267     */
2268    protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value
2269        return findSetIndexMethod(getClass(), desc, explicitInstanceOfCheck(desc, request), desc.getMethodType());
2270    }
2271
2272    /**
2273     * Find the appropriate SETINDEX method for an invoke dynamic call.
2274     *
2275     * @param clazz the receiver class
2276     * @param desc  the call site descriptor
2277     * @param explicitInstanceOfCheck add an explicit instanceof check?
2278     * @param callType the method type at the call site
2279     *
2280     * @return GuardedInvocation to be invoked at call site.
2281     */
2282    private static GuardedInvocation findSetIndexMethod(final Class<? extends ScriptObject> clazz, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final MethodType callType) {
2283        assert callType.parameterCount() == 3;
2284        final Class<?> keyClass   = callType.parameterType(1);
2285        final Class<?> valueClass = callType.parameterType(2);
2286
2287        MethodHandle methodHandle = findOwnMH_V(clazz, "set", void.class, keyClass, valueClass, int.class);
2288        methodHandle = MH.insertArguments(methodHandle, 3, NashornCallSiteDescriptor.getFlags(desc));
2289
2290        return new GuardedInvocation(methodHandle, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class);
2291    }
2292
2293    /**
2294     * Fall back if a function property is not found.
2295     * @param desc The call site descriptor
2296     * @param request the link request
2297     * @return GuardedInvocation to be invoked at call site.
2298     */
2299    public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
2300        final String       name      = desc.getNameToken(2);
2301        final FindProperty find      = findProperty(NO_SUCH_METHOD_NAME, true);
2302        final boolean      scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc);
2303
2304        if (find == null) {
2305            return noSuchProperty(desc, request);
2306        }
2307
2308        final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);
2309
2310        final Object value = find.getObjectValue();
2311        if (!(value instanceof ScriptFunction)) {
2312            return createEmptyGetter(desc, explicitInstanceOfCheck, name);
2313        }
2314
2315        final ScriptFunction func = (ScriptFunction)value;
2316        final Object         thiz = scopeCall && func.isStrict() ? UNDEFINED : this;
2317        // TODO: It'd be awesome if we could bind "name" without binding "this".
2318        // Since we're binding this we must use an identity guard here.
2319        return new GuardedInvocation(
2320                MH.dropArguments(
2321                        MH.constant(
2322                                ScriptFunction.class,
2323                                func.makeBoundFunction(thiz, new Object[] { name })),
2324                        0,
2325                        Object.class),
2326                NashornGuards.combineGuards(
2327                        NashornGuards.getIdentityGuard(this),
2328                        NashornGuards.getMapGuard(getMap(), true)));
2329    }
2330
2331    /**
2332     * Fall back if a property is not found.
2333     * @param desc the call site descriptor.
2334     * @param request the link request
2335     * @return GuardedInvocation to be invoked at call site.
2336     */
2337    public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
2338        final String       name        = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
2339        final FindProperty find        = findProperty(NO_SUCH_PROPERTY_NAME, true);
2340        final boolean      scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc);
2341
2342        if (find != null) {
2343            final Object   value = find.getObjectValue();
2344            ScriptFunction func  = null;
2345            MethodHandle   mh    = null;
2346
2347            if (value instanceof ScriptFunction) {
2348                func = (ScriptFunction)value;
2349                mh   = getCallMethodHandle(func, desc.getMethodType(), name);
2350            }
2351
2352            if (mh != null) {
2353                assert func != null;
2354                if (scopeAccess && func.isStrict()) {
2355                    mh = bindTo(mh, UNDEFINED);
2356                }
2357
2358                return new GuardedInvocation(
2359                        mh,
2360                        find.isSelf()?
2361                            getKnownFunctionPropertyGuardSelf(
2362                                getMap(),
2363                                find.getGetter(Object.class, INVALID_PROGRAM_POINT, request),
2364                                func)
2365                            :
2366                            //TODO this always does a scriptobject check
2367                            getKnownFunctionPropertyGuardProto(
2368                                getMap(),
2369                                find.getGetter(Object.class, INVALID_PROGRAM_POINT, request),
2370                                find.getProtoChainLength(),
2371                                func),
2372                        getProtoSwitchPoint(NO_SUCH_PROPERTY_NAME, find.getOwner()),
2373                        //TODO this doesn't need a ClassCastException as guard always checks script object
2374                        null);
2375            }
2376        }
2377
2378        if (scopeAccess) {
2379            throw referenceError("not.defined", name);
2380        }
2381
2382        return createEmptyGetter(desc, explicitInstanceOfCheck(desc, request), name);
2383    }
2384
2385    /**
2386     * Invoke fall back if a property is not found.
2387     * @param name Name of property.
2388     * @param programPoint program point
2389     * @return Result from call.
2390     */
2391    protected Object invokeNoSuchProperty(final String name, final int programPoint) {
2392        final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true);
2393
2394        Object ret = UNDEFINED;
2395
2396        if (find != null) {
2397            final Object func = find.getObjectValue();
2398
2399            if (func instanceof ScriptFunction) {
2400                ret = ScriptRuntime.apply((ScriptFunction)func, this, name);
2401            }
2402        }
2403
2404        if (isValid(programPoint)) {
2405            throw new UnwarrantedOptimismException(ret, programPoint);
2406        }
2407
2408        return ret;
2409    }
2410
2411
2412    /**
2413     * Get __noSuchMethod__ as a function bound to this object and {@code name} if it is defined.
2414     * @param name the method name
2415     * @return the bound function, or undefined
2416     */
2417    private Object getNoSuchMethod(final String name, final int programPoint) {
2418        final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true);
2419
2420        if (find == null) {
2421            return invokeNoSuchProperty(name, programPoint);
2422        }
2423
2424        final Object value = find.getObjectValue();
2425        if (!(value instanceof ScriptFunction)) {
2426            return UNDEFINED;
2427        }
2428
2429        return ((ScriptFunction)value).makeBoundFunction(this, new Object[] {name});
2430    }
2431
2432    private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) {
2433        if (NashornCallSiteDescriptor.isOptimistic(desc)) {
2434            throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
2435        }
2436
2437        return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()),
2438                NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoint(name, null),
2439                explicitInstanceOfCheck ? null : ClassCastException.class);
2440    }
2441
2442    private abstract static class ScriptObjectIterator <T extends Object> implements Iterator<T> {
2443        protected T[] values;
2444        protected final ScriptObject object;
2445        private int index;
2446
2447        ScriptObjectIterator(final ScriptObject object) {
2448            this.object = object;
2449        }
2450
2451        protected abstract void init();
2452
2453        @Override
2454        public boolean hasNext() {
2455            if (values == null) {
2456                init();
2457            }
2458            return index < values.length;
2459        }
2460
2461        @Override
2462        public T next() {
2463            if (values == null) {
2464                init();
2465            }
2466            return values[index++];
2467        }
2468
2469        @Override
2470        public void remove() {
2471            throw new UnsupportedOperationException("remove");
2472        }
2473    }
2474
2475    private static class KeyIterator extends ScriptObjectIterator<String> {
2476        KeyIterator(final ScriptObject object) {
2477            super(object);
2478        }
2479
2480        @Override
2481        protected void init() {
2482            final Set<String> keys = new LinkedHashSet<>();
2483            final Set<String> nonEnumerable = new HashSet<>();
2484            for (ScriptObject self = object; self != null; self = self.getProto()) {
2485                keys.addAll(Arrays.asList(self.getOwnKeys(false, nonEnumerable)));
2486            }
2487            this.values = keys.toArray(new String[keys.size()]);
2488        }
2489    }
2490
2491    private static class ValueIterator extends ScriptObjectIterator<Object> {
2492        ValueIterator(final ScriptObject object) {
2493            super(object);
2494        }
2495
2496        @Override
2497        protected void init() {
2498            final ArrayList<Object> valueList = new ArrayList<>();
2499            final Set<String> nonEnumerable = new HashSet<>();
2500            for (ScriptObject self = object; self != null; self = self.getProto()) {
2501                for (final String key : self.getOwnKeys(false, nonEnumerable)) {
2502                    valueList.add(self.get(key));
2503                }
2504            }
2505            this.values = valueList.toArray(new Object[valueList.size()]);
2506        }
2507    }
2508
2509    /**
2510     * Add a spill property for the given key.
2511     * @param key    Property key.
2512     * @param flags  Property flags.
2513     * @return Added property.
2514     */
2515    private Property addSpillProperty(final String key, final int flags, final Object value, final boolean hasInitialValue) {
2516        final PropertyMap propertyMap = getMap();
2517        final int fieldSlot  = propertyMap.getFreeFieldSlot();
2518        final int propertyFlags = flags | (useDualFields() ? Property.DUAL_FIELDS : 0);
2519
2520        Property property;
2521        if (fieldSlot > -1) {
2522            property = hasInitialValue ?
2523                new AccessorProperty(key, propertyFlags, fieldSlot, this, value) :
2524                new AccessorProperty(key, propertyFlags, getClass(), fieldSlot);
2525            property = addOwnProperty(property);
2526        } else {
2527            final int spillSlot = propertyMap.getFreeSpillSlot();
2528            property = hasInitialValue ?
2529                new SpillProperty(key, propertyFlags, spillSlot, this, value) :
2530                new SpillProperty(key, propertyFlags, spillSlot);
2531            property = addOwnProperty(property);
2532            ensureSpillSize(property.getSlot());
2533        }
2534        return property;
2535    }
2536
2537    /**
2538     * Add a spill entry for the given key.
2539     * @param key Property key.
2540     * @return Setter method handle.
2541     */
2542    MethodHandle addSpill(final Class<?> type, final String key) {
2543        return addSpillProperty(key, 0, null, false).getSetter(type, getMap());
2544    }
2545
2546    /**
2547     * Make sure arguments are paired correctly, with respect to more parameters than declared,
2548     * fewer parameters than declared and other things that JavaScript allows. This might involve
2549     * creating collectors.
2550     *
2551     * @param methodHandle method handle for invoke
2552     * @param callType     type of the call
2553     *
2554     * @return method handle with adjusted arguments
2555     */
2556    protected static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType) {
2557        return pairArguments(methodHandle, callType, null);
2558    }
2559
2560    /**
2561     * Make sure arguments are paired correctly, with respect to more parameters than declared,
2562     * fewer parameters than declared and other things that JavaScript allows. This might involve
2563     * creating collectors.
2564     *
2565     * Make sure arguments are paired correctly.
2566     * @param methodHandle MethodHandle to adjust.
2567     * @param callType     MethodType of the call site.
2568     * @param callerVarArg true if the caller is vararg, false otherwise, null if it should be inferred from the
2569     * {@code callType}; basically, if the last parameter type of the call site is an array, it'll be considered a
2570     * variable arity call site. These are ordinarily rare; Nashorn code generator creates variable arity call sites
2571     * when the call has more than {@link LinkerCallSite#ARGLIMIT} parameters.
2572     *
2573     * @return method handle with adjusted arguments
2574     */
2575    public static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType, final Boolean callerVarArg) {
2576        final MethodType methodType = methodHandle.type();
2577        if (methodType.equals(callType.changeReturnType(methodType.returnType()))) {
2578            return methodHandle;
2579        }
2580
2581        final int parameterCount = methodType.parameterCount();
2582        final int callCount      = callType.parameterCount();
2583        final int pdiff          = callCount - parameterCount + 1;
2584
2585        final boolean isCalleeVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
2586        final boolean isCallerVarArg = callerVarArg != null ? callerVarArg : callCount > 0 &&
2587                callType.parameterType(callCount - 1).isArray();
2588
2589        // A value of pdiff < 0 means that there are additional normal arguments in the callee that must not be consumed
2590        // by the vararg collector. No vararg collector is required in that case, and no varargs are passed.
2591        if (isCalleeVarArg && pdiff >= 0) {
2592            return isCallerVarArg ?
2593                methodHandle :
2594                MH.asCollector(methodHandle, Object[].class, pdiff);
2595        }
2596
2597        if (isCallerVarArg) {
2598            return adaptHandleToVarArgCallSite(methodHandle, callCount);
2599        }
2600
2601        if (callCount < parameterCount) {
2602            final int      missingArgs = parameterCount - callCount;
2603            final Object[] fillers     = new Object[missingArgs];
2604
2605            Arrays.fill(fillers, UNDEFINED);
2606
2607            if (isCalleeVarArg) {
2608                fillers[missingArgs - 1] = ScriptRuntime.EMPTY_ARRAY;
2609            }
2610
2611            return MH.insertArguments(
2612                methodHandle,
2613                parameterCount - missingArgs,
2614                fillers);
2615        }
2616
2617        if (callCount > parameterCount) {
2618            final int discardedArgs = callCount - parameterCount;
2619
2620            final Class<?>[] discards = new Class<?>[discardedArgs];
2621            Arrays.fill(discards, Object.class);
2622
2623            return MH.dropArguments(methodHandle, callCount - discardedArgs, discards);
2624        }
2625
2626        return methodHandle;
2627    }
2628
2629    static MethodHandle adaptHandleToVarArgCallSite(final MethodHandle mh, final int callSiteParamCount) {
2630        final int spreadArgs = mh.type().parameterCount() - callSiteParamCount + 1;
2631        return MH.filterArguments(
2632            MH.asSpreader(
2633                mh,
2634                Object[].class,
2635                spreadArgs),
2636            callSiteParamCount - 1,
2637            MH.insertArguments(
2638                TRUNCATINGFILTER,
2639                0,
2640                spreadArgs)
2641            );
2642    }
2643
2644    @SuppressWarnings("unused")
2645    private static Object[] truncatingFilter(final int n, final Object[] array) {
2646        final int length = array == null ? 0 : array.length;
2647        if (n == length) {
2648            return array == null ? ScriptRuntime.EMPTY_ARRAY : array;
2649        }
2650
2651        final Object[] newArray = new Object[n];
2652
2653        if (array != null) {
2654            System.arraycopy(array, 0, newArray, 0, Math.min(n, length));
2655        }
2656
2657        if (length < n) {
2658            final Object fill = UNDEFINED;
2659
2660            for (int i = length; i < n; i++) {
2661                newArray[i] = fill;
2662            }
2663        }
2664
2665        return newArray;
2666    }
2667
2668    /**
2669      * Numeric length setter for length property
2670      *
2671      * @param newLength new length to set
2672      */
2673    public final void setLength(final long newLength) {
2674        ArrayData data = getArray();
2675        final long arrayLength = data.length();
2676        if (newLength == arrayLength) {
2677            return;
2678        }
2679
2680        if (newLength > arrayLength) {
2681            data = data.ensure(newLength - 1);
2682            if (data.canDelete(arrayLength, newLength - 1, false)) {
2683               data = data.delete(arrayLength, newLength - 1);
2684            }
2685            setArray(data);
2686            return;
2687        }
2688
2689        if (newLength < arrayLength) {
2690           long actualLength = newLength;
2691
2692           // Check for numeric keys in property map and delete them or adjust length, depending on whether
2693           // they're defined as configurable. See ES5 #15.4.5.2
2694           if (getMap().containsArrayKeys()) {
2695
2696               for (long l = arrayLength - 1; l >= newLength; l--) {
2697                   final FindProperty find = findProperty(JSType.toString(l), false);
2698
2699                   if (find != null) {
2700
2701                       if (find.getProperty().isConfigurable()) {
2702                           deleteOwnProperty(find.getProperty());
2703                       } else {
2704                           actualLength = l + 1;
2705                           break;
2706                       }
2707                   }
2708               }
2709           }
2710
2711           setArray(data.shrink(actualLength));
2712           data.setLength(actualLength);
2713       }
2714    }
2715
2716    private int getInt(final int index, final String key, final int programPoint) {
2717        if (isValidArrayIndex(index)) {
2718            for (ScriptObject object = this; ; ) {
2719                if (object.getMap().containsArrayKeys()) {
2720                    final FindProperty find = object.findProperty(key, false, this);
2721
2722                    if (find != null) {
2723                        return getIntValue(find, programPoint);
2724                    }
2725                }
2726
2727                if ((object = object.getProto()) == null) {
2728                    break;
2729                }
2730
2731                final ArrayData array = object.getArray();
2732
2733                if (array.has(index)) {
2734                    return isValid(programPoint) ?
2735                        array.getIntOptimistic(index, programPoint) :
2736                        array.getInt(index);
2737                }
2738            }
2739        } else {
2740            final FindProperty find = findProperty(key, true);
2741
2742            if (find != null) {
2743                return getIntValue(find, programPoint);
2744            }
2745        }
2746
2747        return JSType.toInt32(invokeNoSuchProperty(key, programPoint));
2748    }
2749
2750    @Override
2751    public int getInt(final Object key, final int programPoint) {
2752        final Object    primitiveKey = JSType.toPrimitive(key, String.class);
2753        final int       index        = getArrayIndex(primitiveKey);
2754        final ArrayData array        = getArray();
2755
2756        if (array.has(index)) {
2757            return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index);
2758        }
2759
2760        return getInt(index, JSType.toString(primitiveKey), programPoint);
2761    }
2762
2763    @Override
2764    public int getInt(final double key, final int programPoint) {
2765        final int       index = getArrayIndex(key);
2766        final ArrayData array = getArray();
2767
2768        if (array.has(index)) {
2769            return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index);
2770        }
2771
2772        return getInt(index, JSType.toString(key), programPoint);
2773    }
2774
2775    @Override
2776    public int getInt(final long key, final int programPoint) {
2777        final int       index = getArrayIndex(key);
2778        final ArrayData array = getArray();
2779
2780        if (array.has(index)) {
2781            return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index);
2782        }
2783
2784        return getInt(index, JSType.toString(key), programPoint);
2785    }
2786
2787    @Override
2788    public int getInt(final int key, final int programPoint) {
2789        final int       index = getArrayIndex(key);
2790        final ArrayData array = getArray();
2791
2792        if (array.has(index)) {
2793            return isValid(programPoint) ? array.getIntOptimistic(key, programPoint) : array.getInt(key);
2794        }
2795
2796        return getInt(index, JSType.toString(key), programPoint);
2797    }
2798
2799    private long getLong(final int index, final String key, final int programPoint) {
2800        if (isValidArrayIndex(index)) {
2801            for (ScriptObject object = this; ; ) {
2802                if (object.getMap().containsArrayKeys()) {
2803                    final FindProperty find = object.findProperty(key, false, this);
2804                    if (find != null) {
2805                        return getLongValue(find, programPoint);
2806                    }
2807                }
2808
2809                if ((object = object.getProto()) == null) {
2810                    break;
2811                }
2812
2813                final ArrayData array = object.getArray();
2814
2815                if (array.has(index)) {
2816                    return isValid(programPoint) ?
2817                        array.getLongOptimistic(index, programPoint) :
2818                        array.getLong(index);
2819                }
2820            }
2821        } else {
2822            final FindProperty find = findProperty(key, true);
2823
2824            if (find != null) {
2825                return getLongValue(find, programPoint);
2826            }
2827        }
2828
2829        return JSType.toLong(invokeNoSuchProperty(key, programPoint));
2830    }
2831
2832    @Override
2833    public long getLong(final Object key, final int programPoint) {
2834        final Object    primitiveKey = JSType.toPrimitive(key, String.class);
2835        final int       index        = getArrayIndex(primitiveKey);
2836        final ArrayData array        = getArray();
2837
2838        if (array.has(index)) {
2839            return isValid(programPoint) ? array.getLongOptimistic(index, programPoint) : array.getLong(index);
2840        }
2841
2842        return getLong(index, JSType.toString(primitiveKey), programPoint);
2843    }
2844
2845    @Override
2846    public long getLong(final double key, final int programPoint) {
2847        final int       index = getArrayIndex(key);
2848        final ArrayData array = getArray();
2849
2850        if (array.has(index)) {
2851            return isValid(programPoint) ? array.getLongOptimistic(index, programPoint) : array.getLong(index);
2852        }
2853
2854        return getLong(index, JSType.toString(key), programPoint);
2855    }
2856
2857    @Override
2858    public long getLong(final long key, final int programPoint) {
2859        final int       index = getArrayIndex(key);
2860        final ArrayData array = getArray();
2861
2862        if (array.has(index)) {
2863            return isValid(programPoint) ? array.getLongOptimistic(index, programPoint) : array.getLong(index);
2864        }
2865
2866        return getLong(index, JSType.toString(key), programPoint);
2867    }
2868
2869    @Override
2870    public long getLong(final int key, final int programPoint) {
2871        final int       index = getArrayIndex(key);
2872        final ArrayData array = getArray();
2873
2874        if (array.has(index)) {
2875            return isValid(programPoint) ? array.getLongOptimistic(key, programPoint) : array.getLong(key);
2876        }
2877
2878        return getLong(index, JSType.toString(key), programPoint);
2879    }
2880
2881    private double getDouble(final int index, final String key, final int programPoint) {
2882        if (isValidArrayIndex(index)) {
2883            for (ScriptObject object = this; ; ) {
2884                if (object.getMap().containsArrayKeys()) {
2885                    final FindProperty find = object.findProperty(key, false, this);
2886                    if (find != null) {
2887                        return getDoubleValue(find, programPoint);
2888                    }
2889                }
2890
2891                if ((object = object.getProto()) == null) {
2892                    break;
2893                }
2894
2895                final ArrayData array = object.getArray();
2896
2897                if (array.has(index)) {
2898                    return isValid(programPoint) ?
2899                        array.getDoubleOptimistic(index, programPoint) :
2900                        array.getDouble(index);
2901                }
2902            }
2903        } else {
2904            final FindProperty find = findProperty(key, true);
2905
2906            if (find != null) {
2907                return getDoubleValue(find, programPoint);
2908            }
2909        }
2910
2911        return JSType.toNumber(invokeNoSuchProperty(key, INVALID_PROGRAM_POINT));
2912    }
2913
2914    @Override
2915    public double getDouble(final Object key, final int programPoint) {
2916        final Object    primitiveKey = JSType.toPrimitive(key, String.class);
2917        final int       index        = getArrayIndex(primitiveKey);
2918        final ArrayData array        = getArray();
2919
2920        if (array.has(index)) {
2921            return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index);
2922        }
2923
2924        return getDouble(index, JSType.toString(primitiveKey), programPoint);
2925    }
2926
2927    @Override
2928    public double getDouble(final double key, final int programPoint) {
2929        final int       index = getArrayIndex(key);
2930        final ArrayData array = getArray();
2931
2932        if (array.has(index)) {
2933            return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index);
2934        }
2935
2936        return getDouble(index, JSType.toString(key), programPoint);
2937    }
2938
2939    @Override
2940    public double getDouble(final long key, final int programPoint) {
2941        final int       index = getArrayIndex(key);
2942        final ArrayData array = getArray();
2943
2944        if (array.has(index)) {
2945            return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index);
2946        }
2947
2948        return getDouble(index, JSType.toString(key), programPoint);
2949    }
2950
2951    @Override
2952    public double getDouble(final int key, final int programPoint) {
2953        final int       index = getArrayIndex(key);
2954        final ArrayData array = getArray();
2955
2956        if (array.has(index)) {
2957            return isValid(programPoint) ? array.getDoubleOptimistic(key, programPoint) : array.getDouble(key);
2958        }
2959
2960        return getDouble(index, JSType.toString(key), programPoint);
2961    }
2962
2963    private Object get(final int index, final String key) {
2964        if (isValidArrayIndex(index)) {
2965            for (ScriptObject object = this; ; ) {
2966                if (object.getMap().containsArrayKeys()) {
2967                    final FindProperty find = object.findProperty(key, false, this);
2968
2969                    if (find != null) {
2970                        return find.getObjectValue();
2971                    }
2972                }
2973
2974                if ((object = object.getProto()) == null) {
2975                    break;
2976                }
2977
2978                final ArrayData array = object.getArray();
2979
2980                if (array.has(index)) {
2981                    return array.getObject(index);
2982                }
2983            }
2984        } else {
2985            final FindProperty find = findProperty(key, true);
2986
2987            if (find != null) {
2988                return find.getObjectValue();
2989            }
2990        }
2991
2992        return invokeNoSuchProperty(key, INVALID_PROGRAM_POINT);
2993    }
2994
2995    @Override
2996    public Object get(final Object key) {
2997        final Object    primitiveKey = JSType.toPrimitive(key, String.class);
2998        final int       index        = getArrayIndex(primitiveKey);
2999        final ArrayData array        = getArray();
3000
3001        if (array.has(index)) {
3002            return array.getObject(index);
3003        }
3004
3005        return get(index, JSType.toString(primitiveKey));
3006    }
3007
3008    @Override
3009    public Object get(final double key) {
3010        final int index = getArrayIndex(key);
3011        final ArrayData array = getArray();
3012
3013        if (array.has(index)) {
3014            return array.getObject(index);
3015        }
3016
3017        return get(index, JSType.toString(key));
3018    }
3019
3020    @Override
3021    public Object get(final long key) {
3022        final int index = getArrayIndex(key);
3023        final ArrayData array = getArray();
3024
3025        if (array.has(index)) {
3026            return array.getObject(index);
3027        }
3028
3029        return get(index, JSType.toString(key));
3030    }
3031
3032    @Override
3033    public Object get(final int key) {
3034        final int index = getArrayIndex(key);
3035        final ArrayData array = getArray();
3036
3037        if (array.has(index)) {
3038            return array.getObject(index);
3039        }
3040
3041        return get(index, JSType.toString(key));
3042    }
3043
3044    private boolean doesNotHaveCheckArrayKeys(final long longIndex, final int value, final int callSiteFlags) {
3045        if (getMap().containsArrayKeys()) {
3046            final String       key  = JSType.toString(longIndex);
3047            final FindProperty find = findProperty(key, true);
3048            if (find != null) {
3049                setObject(find, callSiteFlags, key, value);
3050                return true;
3051            }
3052        }
3053        return false;
3054    }
3055
3056    private boolean doesNotHaveCheckArrayKeys(final long longIndex, final long value, final int callSiteFlags) {
3057        if (getMap().containsArrayKeys()) {
3058            final String       key  = JSType.toString(longIndex);
3059            final FindProperty find = findProperty(key, true);
3060            if (find != null) {
3061                setObject(find, callSiteFlags, key, value);
3062                return true;
3063            }
3064        }
3065        return false;
3066    }
3067
3068    private boolean doesNotHaveCheckArrayKeys(final long longIndex, final double value, final int callSiteFlags) {
3069         if (getMap().containsArrayKeys()) {
3070            final String       key  = JSType.toString(longIndex);
3071            final FindProperty find = findProperty(key, true);
3072            if (find != null) {
3073                setObject(find, callSiteFlags, key, value);
3074                return true;
3075            }
3076        }
3077        return false;
3078    }
3079
3080    private boolean doesNotHaveCheckArrayKeys(final long longIndex, final Object value, final int callSiteFlags) {
3081        if (getMap().containsArrayKeys()) {
3082            final String       key  = JSType.toString(longIndex);
3083            final FindProperty find = findProperty(key, true);
3084            if (find != null) {
3085                setObject(find, callSiteFlags, key, value);
3086                return true;
3087            }
3088        }
3089        return false;
3090    }
3091
3092    //value agnostic
3093    private boolean doesNotHaveEnsureLength(final long longIndex, final long oldLength, final int callSiteFlags) {
3094        if (longIndex >= oldLength) {
3095            if (!isExtensible()) {
3096                if (isStrictFlag(callSiteFlags)) {
3097                    throw typeError("object.non.extensible", JSType.toString(longIndex), ScriptRuntime.safeToString(this));
3098                }
3099                return true;
3100            }
3101            setArray(getArray().ensure(longIndex));
3102        }
3103        return false;
3104    }
3105
3106    private void doesNotHaveEnsureDelete(final long longIndex, final long oldLength, final boolean strict) {
3107        if (longIndex > oldLength) {
3108            ArrayData array = getArray();
3109            if (array.canDelete(oldLength, longIndex - 1, strict)) {
3110                array = array.delete(oldLength, longIndex - 1);
3111            }
3112            setArray(array);
3113        }
3114    }
3115
3116    private void doesNotHave(final int index, final int value, final int callSiteFlags) {
3117        final long oldLength = getArray().length();
3118        final long longIndex = ArrayIndex.toLongIndex(index);
3119        if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) {
3120            final boolean strict = isStrictFlag(callSiteFlags);
3121            setArray(getArray().set(index, value, strict));
3122            doesNotHaveEnsureDelete(longIndex, oldLength, strict);
3123        }
3124    }
3125
3126    private void doesNotHave(final int index, final long value, final int callSiteFlags) {
3127        final long oldLength = getArray().length();
3128        final long longIndex = ArrayIndex.toLongIndex(index);
3129        if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) {
3130            final boolean strict = isStrictFlag(callSiteFlags);
3131            setArray(getArray().set(index, value, strict));
3132            doesNotHaveEnsureDelete(longIndex, oldLength, strict);
3133        }
3134    }
3135
3136    private void doesNotHave(final int index, final double value, final int callSiteFlags) {
3137        final long oldLength = getArray().length();
3138        final long longIndex = ArrayIndex.toLongIndex(index);
3139        if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) {
3140            final boolean strict = isStrictFlag(callSiteFlags);
3141            setArray(getArray().set(index, value, strict));
3142            doesNotHaveEnsureDelete(longIndex, oldLength, strict);
3143        }
3144    }
3145
3146    private void doesNotHave(final int index, final Object value, final int callSiteFlags) {
3147        final long oldLength = getArray().length();
3148        final long longIndex = ArrayIndex.toLongIndex(index);
3149        if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) {
3150            final boolean strict = isStrictFlag(callSiteFlags);
3151            setArray(getArray().set(index, value, strict));
3152            doesNotHaveEnsureDelete(longIndex, oldLength, strict);
3153        }
3154    }
3155
3156    /**
3157     * This is the most generic of all Object setters. Most of the others use this in some form.
3158     * TODO: should be further specialized
3159     *
3160     * @param find          found property
3161     * @param callSiteFlags callsite flags
3162     * @param key           property key
3163     * @param value         property value
3164     */
3165    public final void setObject(final FindProperty find, final int callSiteFlags, final String key, final Object value) {
3166        FindProperty f = find;
3167
3168        invalidateGlobalConstant(key);
3169
3170        if (f != null && f.isInherited() && !(f.getProperty() instanceof UserAccessorProperty)) {
3171            final boolean isScope = isScopeFlag(callSiteFlags);
3172            // If the start object of the find is not this object it means the property was found inside a
3173            // 'with' statement expression (see WithObject.findProperty()). In this case we forward the 'set'
3174            // to the 'with' object.
3175            // Note that although a 'set' operation involving a with statement follows scope rules outside
3176            // the 'with' expression (the 'set' operation is performed on the owning prototype if it exists),
3177            // it follows non-scope rules inside the 'with' expression (set is performed on the top level object).
3178            // This is why we clear the callsite flags and FindProperty in the forward call to the 'with' object.
3179            if (isScope && f.getSelf() != this) {
3180                f.getSelf().setObject(null, 0, key, value);
3181                return;
3182            }
3183            // Setting a property should not modify the property in prototype unless this is a scope callsite
3184            // and the owner is a scope object as well (with the exception of 'with' statement handled above).
3185            if (!isScope || !f.getOwner().isScope()) {
3186                f = null;
3187            }
3188        }
3189
3190        if (f != null) {
3191            if (!f.getProperty().isWritable()) {
3192                if (isScopeFlag(callSiteFlags) && f.getProperty().isLexicalBinding()) {
3193                    throw typeError("assign.constant", key); // Overwriting ES6 const should throw also in non-strict mode.
3194                }
3195                if (isStrictFlag(callSiteFlags)) {
3196                    throw typeError("property.not.writable", key, ScriptRuntime.safeToString(this));
3197                }
3198                return;
3199            }
3200
3201            f.setValue(value, isStrictFlag(callSiteFlags));
3202
3203        } else if (!isExtensible()) {
3204            if (isStrictFlag(callSiteFlags)) {
3205                throw typeError("object.non.extensible", key, ScriptRuntime.safeToString(this));
3206            }
3207        } else {
3208            ScriptObject sobj = this;
3209            // undefined scope properties are set in the global object.
3210            if (isScope()) {
3211                while (sobj != null && !(sobj instanceof Global)) {
3212                    sobj = sobj.getProto();
3213                }
3214                assert sobj != null : "no parent global object in scope";
3215            }
3216            //this will unbox any Number object to its primitive type in case the
3217            //property supports primitive types, so it doesn't matter that it comes
3218            //in as an Object.
3219            sobj.addSpillProperty(key, 0, value, true);
3220        }
3221    }
3222
3223    @Override
3224    public void set(final Object key, final int value, final int callSiteFlags) {
3225        final Object primitiveKey = JSType.toPrimitive(key, String.class);
3226        final int    index        = getArrayIndex(primitiveKey);
3227
3228        if (isValidArrayIndex(index)) {
3229            final ArrayData data = getArray();
3230            if (data.has(index)) {
3231                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3232            } else {
3233                doesNotHave(index, value, callSiteFlags);
3234            }
3235
3236            return;
3237        }
3238
3239        final String propName = JSType.toString(primitiveKey);
3240        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3241    }
3242
3243    @Override
3244    public void set(final Object key, final long value, final int callSiteFlags) {
3245        final Object primitiveKey = JSType.toPrimitive(key, String.class);
3246        final int    index        = getArrayIndex(primitiveKey);
3247
3248        if (isValidArrayIndex(index)) {
3249            final ArrayData data = getArray();
3250            if (data.has(index)) {
3251                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3252            } else {
3253                doesNotHave(index, value, callSiteFlags);
3254            }
3255
3256            return;
3257        }
3258
3259        final String propName = JSType.toString(primitiveKey);
3260        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3261    }
3262
3263    @Override
3264    public void set(final Object key, final double value, final int callSiteFlags) {
3265        final Object primitiveKey = JSType.toPrimitive(key, String.class);
3266        final int    index        = getArrayIndex(primitiveKey);
3267
3268        if (isValidArrayIndex(index)) {
3269            final ArrayData data = getArray();
3270            if (data.has(index)) {
3271                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3272            } else {
3273                doesNotHave(index, value, callSiteFlags);
3274            }
3275
3276            return;
3277        }
3278
3279        final String propName = JSType.toString(primitiveKey);
3280        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3281    }
3282
3283    @Override
3284    public void set(final Object key, final Object value, final int callSiteFlags) {
3285        final Object primitiveKey = JSType.toPrimitive(key, String.class);
3286        final int    index        = getArrayIndex(primitiveKey);
3287
3288        if (isValidArrayIndex(index)) {
3289            final ArrayData data = getArray();
3290            if (data.has(index)) {
3291                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3292            } else {
3293                doesNotHave(index, value, callSiteFlags);
3294            }
3295
3296            return;
3297        }
3298
3299        final String propName = JSType.toString(primitiveKey);
3300        setObject(findProperty(propName, true), callSiteFlags, propName, value);
3301    }
3302
3303    @Override
3304    public void set(final double key, final int value, final int callSiteFlags) {
3305        final int index = getArrayIndex(key);
3306
3307        if (isValidArrayIndex(index)) {
3308            final ArrayData data = getArray();
3309            if (data.has(index)) {
3310                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3311            } else {
3312                doesNotHave(index, value, callSiteFlags);
3313            }
3314
3315            return;
3316        }
3317
3318        final String propName = JSType.toString(key);
3319        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3320    }
3321
3322    @Override
3323    public void set(final double key, final long value, final int callSiteFlags) {
3324        final int index = getArrayIndex(key);
3325
3326        if (isValidArrayIndex(index)) {
3327            final ArrayData data = getArray();
3328            if (data.has(index)) {
3329                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3330            } else {
3331                doesNotHave(index, value, callSiteFlags);
3332            }
3333
3334            return;
3335        }
3336
3337        final String propName = JSType.toString(key);
3338        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3339    }
3340
3341    @Override
3342    public void set(final double key, final double value, final int callSiteFlags) {
3343        final int index = getArrayIndex(key);
3344
3345        if (isValidArrayIndex(index)) {
3346            final ArrayData data = getArray();
3347            if (data.has(index)) {
3348                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3349            } else {
3350                doesNotHave(index, value, callSiteFlags);
3351            }
3352
3353            return;
3354        }
3355
3356        final String propName = JSType.toString(key);
3357        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3358    }
3359
3360    @Override
3361    public void set(final double key, final Object value, final int callSiteFlags) {
3362        final int index = getArrayIndex(key);
3363
3364        if (isValidArrayIndex(index)) {
3365            final ArrayData data = getArray();
3366            if (data.has(index)) {
3367                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3368            } else {
3369                doesNotHave(index, value, callSiteFlags);
3370            }
3371
3372            return;
3373        }
3374
3375        final String propName = JSType.toString(key);
3376        setObject(findProperty(propName, true), callSiteFlags, propName, value);
3377    }
3378
3379    @Override
3380    public void set(final long key, final int value, final int callSiteFlags) {
3381        final int index = getArrayIndex(key);
3382
3383        if (isValidArrayIndex(index)) {
3384            final ArrayData data = getArray();
3385            if (data.has(index)) {
3386                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3387            } else {
3388                doesNotHave(index, value, callSiteFlags);
3389            }
3390
3391            return;
3392        }
3393
3394        final String propName = JSType.toString(key);
3395        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3396    }
3397
3398    @Override
3399    public void set(final long key, final long value, final int callSiteFlags) {
3400        final int index = getArrayIndex(key);
3401
3402        if (isValidArrayIndex(index)) {
3403            final ArrayData data = getArray();
3404            if (data.has(index)) {
3405                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3406            } else {
3407                doesNotHave(index, value, callSiteFlags);
3408            }
3409
3410            return;
3411        }
3412
3413        final String propName = JSType.toString(key);
3414        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3415    }
3416
3417    @Override
3418    public void set(final long key, final double value, final int callSiteFlags) {
3419        final int index = getArrayIndex(key);
3420
3421        if (isValidArrayIndex(index)) {
3422            final ArrayData data = getArray();
3423            if (data.has(index)) {
3424                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3425            } else {
3426                doesNotHave(index, value, callSiteFlags);
3427            }
3428
3429            return;
3430        }
3431
3432        final String propName = JSType.toString(key);
3433        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3434    }
3435
3436    @Override
3437    public void set(final long key, final Object value, final int callSiteFlags) {
3438        final int index = getArrayIndex(key);
3439
3440        if (isValidArrayIndex(index)) {
3441            final ArrayData data = getArray();
3442            if (data.has(index)) {
3443                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3444            } else {
3445                doesNotHave(index, value, callSiteFlags);
3446            }
3447
3448            return;
3449        }
3450
3451        final String propName = JSType.toString(key);
3452        setObject(findProperty(propName, true), callSiteFlags, propName, value);
3453    }
3454
3455    @Override
3456    public void set(final int key, final int value, final int callSiteFlags) {
3457        final int index = getArrayIndex(key);
3458        if (isValidArrayIndex(index)) {
3459            if (getArray().has(index)) {
3460                final ArrayData data = getArray();
3461                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3462            } else {
3463                doesNotHave(index, value, callSiteFlags);
3464            }
3465            return;
3466        }
3467
3468        final String propName = JSType.toString(key);
3469        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3470    }
3471
3472    @Override
3473    public void set(final int key, final long value, final int callSiteFlags) {
3474        final int index = getArrayIndex(key);
3475
3476        if (isValidArrayIndex(index)) {
3477            final ArrayData data = getArray();
3478            if (data.has(index)) {
3479                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3480            } else {
3481                doesNotHave(index, value, callSiteFlags);
3482            }
3483
3484            return;
3485        }
3486
3487        final String propName = JSType.toString(key);
3488        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3489    }
3490
3491    @Override
3492    public void set(final int key, final double value, final int callSiteFlags) {
3493        final int index = getArrayIndex(key);
3494
3495        if (isValidArrayIndex(index)) {
3496            final ArrayData data = getArray();
3497            if (data.has(index)) {
3498                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3499            } else {
3500                doesNotHave(index, value, callSiteFlags);
3501            }
3502
3503            return;
3504        }
3505
3506        final String propName = JSType.toString(key);
3507        setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value));
3508    }
3509
3510    @Override
3511    public void set(final int key, final Object value, final int callSiteFlags) {
3512        final int index = getArrayIndex(key);
3513
3514        if (isValidArrayIndex(index)) {
3515            final ArrayData data = getArray();
3516            if (data.has(index)) {
3517                setArray(data.set(index, value, isStrictFlag(callSiteFlags)));
3518            } else {
3519                doesNotHave(index, value, callSiteFlags);
3520            }
3521
3522            return;
3523        }
3524
3525        final String propName = JSType.toString(key);
3526        setObject(findProperty(propName, true), callSiteFlags, propName, value);
3527    }
3528
3529    @Override
3530    public boolean has(final Object key) {
3531        final Object primitiveKey = JSType.toPrimitive(key);
3532        final int    index        = getArrayIndex(primitiveKey);
3533        return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(primitiveKey), true);
3534    }
3535
3536    @Override
3537    public boolean has(final double key) {
3538        final int index = getArrayIndex(key);
3539        return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true);
3540    }
3541
3542    @Override
3543    public boolean has(final long key) {
3544        final int index = getArrayIndex(key);
3545        return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true);
3546    }
3547
3548    @Override
3549    public boolean has(final int key) {
3550        final int index = getArrayIndex(key);
3551        return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true);
3552    }
3553
3554    private boolean hasArrayProperty(final int index) {
3555        boolean hasArrayKeys = false;
3556
3557        for (ScriptObject self = this; self != null; self = self.getProto()) {
3558            if (self.getArray().has(index)) {
3559                return true;
3560            }
3561            hasArrayKeys = hasArrayKeys || self.getMap().containsArrayKeys();
3562        }
3563
3564        return hasArrayKeys && hasProperty(ArrayIndex.toKey(index), true);
3565    }
3566
3567    @Override
3568    public boolean hasOwnProperty(final Object key) {
3569        final Object primitiveKey = JSType.toPrimitive(key, String.class);
3570        final int    index        = getArrayIndex(primitiveKey);
3571        return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(primitiveKey), false);
3572    }
3573
3574    @Override
3575    public boolean hasOwnProperty(final int key) {
3576        final int index = getArrayIndex(key);
3577        return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false);
3578    }
3579
3580    @Override
3581    public boolean hasOwnProperty(final long key) {
3582        final int index = getArrayIndex(key);
3583        return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false);
3584    }
3585
3586    @Override
3587    public boolean hasOwnProperty(final double key) {
3588        final int index = getArrayIndex(key);
3589        return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false);
3590    }
3591
3592    private boolean hasOwnArrayProperty(final int index) {
3593        return getArray().has(index) || getMap().containsArrayKeys() && hasProperty(ArrayIndex.toKey(index), false);
3594    }
3595
3596    @Override
3597    public boolean delete(final int key, final boolean strict) {
3598        final int index = getArrayIndex(key);
3599        final ArrayData array = getArray();
3600
3601        if (array.has(index)) {
3602            if (array.canDelete(index, strict)) {
3603                setArray(array.delete(index));
3604                return true;
3605            }
3606            return false;
3607        }
3608        return deleteObject(JSType.toObject(key), strict);
3609    }
3610
3611    @Override
3612    public boolean delete(final long key, final boolean strict) {
3613        final int index = getArrayIndex(key);
3614        final ArrayData array = getArray();
3615
3616        if (array.has(index)) {
3617            if (array.canDelete(index, strict)) {
3618                setArray(array.delete(index));
3619                return true;
3620            }
3621            return false;
3622        }
3623
3624        return deleteObject(JSType.toObject(key), strict);
3625    }
3626
3627    @Override
3628    public boolean delete(final double key, final boolean strict) {
3629        final int index = getArrayIndex(key);
3630        final ArrayData array = getArray();
3631
3632        if (array.has(index)) {
3633            if (array.canDelete(index, strict)) {
3634                setArray(array.delete(index));
3635                return true;
3636            }
3637            return false;
3638        }
3639
3640        return deleteObject(JSType.toObject(key), strict);
3641    }
3642
3643    @Override
3644    public boolean delete(final Object key, final boolean strict) {
3645        final Object    primitiveKey = JSType.toPrimitive(key, String.class);
3646        final int       index        = getArrayIndex(primitiveKey);
3647        final ArrayData array        = getArray();
3648
3649        if (array.has(index)) {
3650            if (array.canDelete(index, strict)) {
3651                setArray(array.delete(index));
3652                return true;
3653            }
3654            return false;
3655        }
3656
3657        return deleteObject(primitiveKey, strict);
3658    }
3659
3660    private boolean deleteObject(final Object key, final boolean strict) {
3661        final String propName = JSType.toString(key);
3662        final FindProperty find = findProperty(propName, false);
3663
3664        if (find == null) {
3665            return true;
3666        }
3667
3668        if (!find.getProperty().isConfigurable()) {
3669            if (strict) {
3670                throw typeError("cant.delete.property", propName, ScriptRuntime.safeToString(this));
3671            }
3672            return false;
3673        }
3674
3675        final Property prop = find.getProperty();
3676        deleteOwnProperty(prop);
3677
3678        return true;
3679    }
3680
3681    /**
3682     * Return a shallow copy of this ScriptObject.
3683     * @return a shallow copy.
3684     */
3685    public final ScriptObject copy() {
3686        try {
3687            return clone();
3688        } catch (final CloneNotSupportedException e) {
3689            throw new RuntimeException(e);
3690        }
3691    }
3692
3693    @Override
3694    protected ScriptObject clone() throws CloneNotSupportedException {
3695        final ScriptObject clone = (ScriptObject) super.clone();
3696        if (objectSpill != null) {
3697            clone.objectSpill = objectSpill.clone();
3698            if (primitiveSpill != null) {
3699                clone.primitiveSpill = primitiveSpill.clone();
3700            }
3701        }
3702        clone.arrayData = arrayData.copy();
3703        return clone;
3704    }
3705
3706    /**
3707     * Make a new UserAccessorProperty property. getter and setter functions are stored in
3708     * this ScriptObject and slot values are used in property object.
3709     *
3710     * @param key the property name
3711     * @param propertyFlags attribute flags of the property
3712     * @param getter getter function for the property
3713     * @param setter setter function for the property
3714     * @return the newly created UserAccessorProperty
3715     */
3716    protected final UserAccessorProperty newUserAccessors(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) {
3717        final UserAccessorProperty uc = getMap().newUserAccessors(key, propertyFlags);
3718        //property.getSetter(Object.class, getMap());
3719        uc.setAccessors(this, getMap(), new UserAccessorProperty.Accessors(getter, setter));
3720        return uc;
3721    }
3722
3723    /**
3724     * Returns {@code true} if properties for this object should use dual field mode, {@code false} otherwise.
3725     * @return {@code true} if dual fields should be used.
3726     */
3727    protected boolean useDualFields() {
3728        return !StructureLoader.isSingleFieldStructure(getClass().getName());
3729    }
3730
3731    Object ensureSpillSize(final int slot) {
3732        final int oldLength = objectSpill == null ? 0 : objectSpill.length;
3733        if (slot < oldLength) {
3734            return this;
3735        }
3736        final int newLength = alignUp(slot + 1, SPILL_RATE);
3737        final Object[] newObjectSpill    = new Object[newLength];
3738        final long[]   newPrimitiveSpill = useDualFields() ? new long[newLength] : null;
3739
3740        if (objectSpill != null) {
3741            System.arraycopy(objectSpill, 0, newObjectSpill, 0, oldLength);
3742            if (primitiveSpill != null && newPrimitiveSpill != null) {
3743                System.arraycopy(primitiveSpill, 0, newPrimitiveSpill, 0, oldLength);
3744            }
3745        }
3746
3747        this.primitiveSpill = newPrimitiveSpill;
3748        this.objectSpill    = newObjectSpill;
3749
3750        return this;
3751    }
3752
3753    private static MethodHandle findOwnMH_V(final Class<? extends ScriptObject> clazz, final String name, final Class<?> rtype, final Class<?>... types) {
3754        // TODO: figure out how can it work for NativeArray$Prototype etc.
3755        return MH.findVirtual(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types));
3756    }
3757
3758    private static MethodHandle findOwnMH_V(final String name, final Class<?> rtype, final Class<?>... types) {
3759        return findOwnMH_V(ScriptObject.class, name, rtype, types);
3760    }
3761
3762    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
3763        return MH.findStatic(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types));
3764    }
3765
3766    private static MethodHandle getKnownFunctionPropertyGuardSelf(final PropertyMap map, final MethodHandle getter, final ScriptFunction func) {
3767        return MH.insertArguments(KNOWNFUNCPROPGUARDSELF, 1, map, getter, func);
3768    }
3769
3770    @SuppressWarnings("unused")
3771    private static boolean knownFunctionPropertyGuardSelf(final Object self, final PropertyMap map, final MethodHandle getter, final ScriptFunction func) {
3772        if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) {
3773            try {
3774                return getter.invokeExact(self) == func;
3775            } catch (final RuntimeException | Error e) {
3776                throw e;
3777            } catch (final Throwable t) {
3778                throw new RuntimeException(t);
3779            }
3780        }
3781
3782        return false;
3783    }
3784
3785    private static MethodHandle getKnownFunctionPropertyGuardProto(final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) {
3786        return MH.insertArguments(KNOWNFUNCPROPGUARDPROTO, 1, map, getter, depth, func);
3787    }
3788
3789    private static ScriptObject getProto(final ScriptObject self, final int depth) {
3790        ScriptObject proto = self;
3791        for (int d = 0; d < depth; d++) {
3792            proto = proto.getProto();
3793            if (proto == null) {
3794                return null;
3795            }
3796        }
3797
3798        return proto;
3799    }
3800
3801    @SuppressWarnings("unused")
3802    private static boolean knownFunctionPropertyGuardProto(final Object self, final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) {
3803        if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) {
3804            final ScriptObject proto = getProto((ScriptObject)self, depth);
3805            if (proto == null) {
3806                return false;
3807            }
3808            try {
3809                return getter.invokeExact((Object)proto) == func;
3810            } catch (final RuntimeException | Error e) {
3811                throw e;
3812            } catch (final Throwable t) {
3813                throw new RuntimeException(t);
3814            }
3815        }
3816
3817        return false;
3818    }
3819
3820    /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created */
3821    private static int count;
3822
3823    /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created that are scope */
3824    private static int scopeCount;
3825
3826    /**
3827     * Get number of {@code ScriptObject} instances created. If not running in debug
3828     * mode this is always 0
3829     *
3830     * @return number of ScriptObjects created
3831     */
3832    public static int getCount() {
3833        return count;
3834    }
3835
3836    /**
3837     * Get number of scope {@code ScriptObject} instances created. If not running in debug
3838     * mode this is always 0
3839     *
3840     * @return number of scope ScriptObjects created
3841     */
3842    public static int getScopeCount() {
3843        return scopeCount;
3844    }
3845
3846}
3847