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