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