nashorn/src/jdk/nashorn/internal/runtime/PropertyMap.java
author attila
Fri, 21 Mar 2014 17:52:03 +0100
changeset 24727 611ba7e2101f
parent 24726 34410e0545b1
parent 23084 6c5c02d1023a
child 24763 20971b9687ec
permissions -rw-r--r--
Merge
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
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    28
import static jdk.nashorn.internal.runtime.PropertyHashMap.EMPTY_HASHMAP;
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    29
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex;
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    30
import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.lang.invoke.SwitchPoint;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    33
import java.lang.ref.SoftReference;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.util.Arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.util.Collection;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import java.util.HashMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import java.util.Iterator;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import java.util.NoSuchElementException;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import java.util.WeakHashMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
 * Map of object properties. The PropertyMap is the "template" for JavaScript object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
 * layouts. It contains a map with prototype names as keys and {@link Property} instances
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
 * as values. A PropertyMap is typically passed to the {@link ScriptObject} constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
 * to form the seed map for the ScriptObject.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
 * All property maps are immutable. If a property is added, modified or removed, the mutator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
 * will return a new map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    50
public final class PropertyMap implements Iterable<Object> {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
    /** Used for non extensible PropertyMaps, negative logic as the normal case is extensible. See {@link ScriptObject#preventExtensions()} */
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    52
    public static final int NOT_EXTENSIBLE        = 0b0000_0001;
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    53
    /** Does this map contain valid array keys? */
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    54
    public static final int CONTAINS_ARRAY_KEYS   = 0b0000_0010;
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    55
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    /** Map status flags. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    private int flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    /** Map of properties. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    private final PropertyHashMap properties;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    62
    /** Number of fields in use. */
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    63
    private int fieldCount;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    64
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    65
    /** Number of fields available. */
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
    66
    private final int fieldMaximum;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    /** Length of spill in use. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    private int spillLength;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
    /** {@link SwitchPoint}s for gets on inherited properties. */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    72
    private HashMap<String, SwitchPoint> protoGetSwitches;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    /** History of maps, used to limit map duplication. */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    75
    private WeakHashMap<Property, SoftReference<PropertyMap>> history;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    /** History of prototypes, used to limit map duplication. */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    78
    private WeakHashMap<PropertyMap, SoftReference<PropertyMap>> protoHistory;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    80
    /** property listeners */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
    81
    private PropertyListeners listeners;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
     * Constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
     *
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
    86
     * @param properties   A {@link PropertyHashMap} with initial contents.
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
    87
     * @param fieldCount   Number of fields in use.
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    88
     * @param fieldMaximum Number of fields available.
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
    89
     * @param spillLength  Number of spill slots used.
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    90
     * @param containsArrayKeys True if properties contain numeric keys
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
     */
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    92
    private PropertyMap(final PropertyHashMap properties, final int fieldCount, final int fieldMaximum, final int spillLength, final boolean containsArrayKeys) {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    93
        this.properties   = properties;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    94
        this.fieldCount   = fieldCount;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
    95
        this.fieldMaximum = fieldMaximum;
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
    96
        this.spillLength  = spillLength;
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    97
        if (containsArrayKeys) {
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    98
            setContainsArrayKeys();
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
    99
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
        if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
            count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
     * Cloning constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
     * @param propertyMap Existing property map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
     * @param properties  A {@link PropertyHashMap} with a new set of properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
    private PropertyMap(final PropertyMap propertyMap, final PropertyHashMap properties) {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   113
        this.properties   = properties;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   114
        this.flags        = propertyMap.flags;
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   115
        this.spillLength  = propertyMap.spillLength;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   116
        this.fieldCount   = propertyMap.fieldCount;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   117
        this.fieldMaximum = propertyMap.fieldMaximum;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   118
        // We inherit the parent property listeners instance. It will be cloned when a new listener is added.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   119
        this.listeners    = propertyMap.listeners;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
        if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
            count++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
            clonedCount++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    /**
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   128
     * Cloning constructor.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   129
     *
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   130
     * @param propertyMap Existing property map.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   131
      */
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   132
    private PropertyMap(final PropertyMap propertyMap) {
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   133
        this(propertyMap, propertyMap.properties);
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   134
    }
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   135
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   136
    /**
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   137
     * Duplicates this PropertyMap instance. This is used to duplicate 'shared'
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   138
     * maps {@link PropertyMap} used as process wide singletons. Shared maps are
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   139
     * duplicated for every global scope object. That way listeners, proto and property
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   140
     * histories are scoped within a global scope.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
     * @return Duplicated {@link PropertyMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    public PropertyMap duplicate() {
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   145
        if (Context.DEBUG) {
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   146
            duplicatedCount++;
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   147
        }
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   148
        return new PropertyMap(this.properties, 0, 0, 0, containsArrayKeys());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     * Public property map allocator.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
     *
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   154
     * <p>It is the caller's responsibility to make sure that {@code properties} does not contain
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   155
     * properties with keys that are valid array indices.</p>
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   156
     *
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
   157
     * @param properties   Collection of initial properties.
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
   158
     * @param fieldCount   Number of fields in use.
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   159
     * @param fieldMaximum Number of fields available.
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
   160
     * @param spillLength  Number of used spill slots.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
     * @return New {@link PropertyMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     */
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
   163
    public static PropertyMap newMap(final Collection<Property> properties, final int fieldCount, final int fieldMaximum,  final int spillLength) {
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   164
        final PropertyHashMap newProperties = EMPTY_HASHMAP.immutableAdd(properties);
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   165
        return new PropertyMap(newProperties, fieldCount, fieldMaximum, spillLength, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    /**
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   169
     * Public property map allocator. Used by nasgen generated code.
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   170
     *
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   171
     * <p>It is the caller's responsibility to make sure that {@code properties} does not contain
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   172
     * properties with keys that are valid array indices.</p>
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   173
     *
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   174
     * @param properties Collection of initial properties.
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   175
     * @return New {@link PropertyMap}.
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   176
     */
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   177
    public static PropertyMap newMap(final Collection<Property> properties) {
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   178
        return properties == null || properties.isEmpty()? newMap() : newMap(properties, 0, 0, 0);
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   179
    }
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   180
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   181
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
     * Return a sharable empty map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
     * @return New empty {@link PropertyMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
     */
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 18618
diff changeset
   186
    public static PropertyMap newMap() {
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   187
        return new PropertyMap(EMPTY_HASHMAP, 0, 0, 0, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
     * Return number of properties in the map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
     * @return Number of properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
    public int size() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
        return properties.size();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
    /**
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   200
     * Get the listeners of this map, or null if none exists
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   201
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   202
     * @return the listeners
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   203
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   204
    public PropertyListeners getListeners() {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   205
        return listeners;
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   206
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   207
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   208
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   209
     * Add {@code listenerMap} as a listener to this property map for the given {@code key}.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   210
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   211
     * @param key the property name
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   212
     * @param listenerMap the listener map
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   213
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   214
    public void addListener(final String key, final PropertyMap listenerMap) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   215
        if (listenerMap != this) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   216
            // We need to clone listener instance when adding a new listener since we share
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   217
            // the listeners instance with our parent maps that don't need to see the new listener.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   218
            listeners = PropertyListeners.addListener(listeners, key, listenerMap);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   219
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   220
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   221
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   222
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   223
     * A new property is being added.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   224
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   225
     * @param property The new Property added.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   226
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   227
    public void propertyAdded(final Property property) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   228
        invalidateProtoGetSwitchPoint(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   229
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   230
            listeners.propertyAdded(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   231
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   232
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   233
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   234
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   235
     * An existing property is being deleted.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   236
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   237
     * @param property The property being deleted.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   238
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   239
    public void propertyDeleted(final Property property) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   240
        invalidateProtoGetSwitchPoint(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   241
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   242
            listeners.propertyDeleted(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   243
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   244
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   245
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   246
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   247
     * An existing property is being redefined.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   248
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   249
     * @param oldProperty The old property
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   250
     * @param newProperty The new property
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   251
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   252
    public void propertyModified(final Property oldProperty, final Property newProperty) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   253
        invalidateProtoGetSwitchPoint(oldProperty);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   254
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   255
            listeners.propertyModified(oldProperty, newProperty);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   256
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   257
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   258
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   259
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   260
     * The prototype of an object associated with this {@link PropertyMap} is changed.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   261
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   262
    public void protoChanged() {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   263
        invalidateAllProtoGetSwitchPoints();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   264
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   265
            listeners.protoChanged();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   266
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   267
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   268
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   269
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * Return a SwitchPoint used to track changes of a property in a prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     *
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   272
     * @param key Property key.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
     * @return A shared {@link SwitchPoint} for the property.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   275
    public synchronized SwitchPoint getSwitchPoint(final String key) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   276
        if (protoGetSwitches == null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   277
            protoGetSwitches = new HashMap<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   280
        SwitchPoint switchPoint = protoGetSwitches.get(key);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   281
        if (switchPoint == null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   282
            switchPoint = new SwitchPoint();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   283
            protoGetSwitches.put(key, switchPoint);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
        return switchPoint;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    /**
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   290
     * Indicate that a prototype property has changed.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     * @param property {@link Property} to invalidate.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   294
    synchronized void invalidateProtoGetSwitchPoint(final Property property) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   295
        if (protoGetSwitches != null) {
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   296
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
            final String key = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
            final SwitchPoint sp = protoGetSwitches.get(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
            if (sp != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   300
                protoGetSwitches.remove(key);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
                if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
                    protoInvalidations++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
                SwitchPoint.invalidateAll(new SwitchPoint[] { sp });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    /**
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   310
     * Indicate that proto itself has changed in hierarchy somewhere.
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   311
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   312
    synchronized void invalidateAllProtoGetSwitchPoints() {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   313
        if (protoGetSwitches != null && !protoGetSwitches.isEmpty()) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   314
            if (Context.DEBUG) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   315
                protoInvalidations += protoGetSwitches.size();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   316
            }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   317
            SwitchPoint.invalidateAll(protoGetSwitches.values().toArray(new SwitchPoint[protoGetSwitches.values().size()]));
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   318
            protoGetSwitches.clear();
19619
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   319
        }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   320
    }
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   321
4085b74056ee 8023368: Instance __proto__ property should exist and be writable.
sundar
parents: 19097
diff changeset
   322
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * Add a property to the map, re-binding its getters and setters,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     * if available, to a given receiver. This is typically the global scope. See
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
     * {@link ScriptObject#addBoundProperties(ScriptObject)}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
     * @param property {@link Property} being added.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
     * @param bindTo   Object to bind to.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
     * @return New {@link PropertyMap} with {@link Property} added.
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
    PropertyMap addPropertyBind(final AccessorProperty property, final Object bindTo) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   333
        // No need to store bound property in the history as bound properties can't be reused.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   334
        return addPropertyNoHistory(new AccessorProperty(property, bindTo));
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   335
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   336
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   337
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   338
     * Add a property to the map without adding it to the history. This should be used for properties that
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   339
     * can't be shared such as bound properties, or properties that are expected to be added only once.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   340
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   341
     * @param property {@link Property} being added.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   342
     * @return New {@link PropertyMap} with {@link Property} added.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   343
     */
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   344
    public PropertyMap addPropertyNoHistory(final Property property) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   345
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   346
            listeners.propertyAdded(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   347
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   348
        final PropertyHashMap newProperties = properties.immutableAdd(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   349
        final PropertyMap newMap = new PropertyMap(this, newProperties);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   350
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   351
        if(!property.isSpill()) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   352
            newMap.fieldCount = Math.max(newMap.fieldCount, property.getSlot() + 1);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   353
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   354
        if (isValidArrayIndex(getArrayIndex(property.getKey()))) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   355
            newMap.setContainsArrayKeys();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   356
        }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   357
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   358
        newMap.spillLength += property.getSpillCount();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   359
        return newMap;
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
     * Add a property to the map.  Cloning or using an existing map if available.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * @param property {@link Property} being added.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
     * @return New {@link PropertyMap} with {@link Property} added.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
     */
18617
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   369
    public PropertyMap addProperty(final Property property) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   370
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   371
            listeners.propertyAdded(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   372
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
        PropertyMap newMap = checkHistory(property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
        if (newMap == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
            final PropertyHashMap newProperties = properties.immutableAdd(property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
            newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
            addToHistory(property, newMap);
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   379
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   380
            if (!property.isSpill()) {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   381
                newMap.fieldCount = Math.max(newMap.fieldCount, property.getSlot() + 1);
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   382
            }
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   383
            if (isValidArrayIndex(getArrayIndex(property.getKey()))) {
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   384
                newMap.setContainsArrayKeys();
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   385
            }
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   386
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
            newMap.spillLength += property.getSpillCount();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
    }
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
     * Remove a property from a map. Cloning or using an existing map if available.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
     * @param property {@link Property} being removed.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
     * @return New {@link PropertyMap} with {@link Property} removed or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
     */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16224
diff changeset
   400
    public PropertyMap deleteProperty(final Property property) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   401
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   402
            listeners.propertyDeleted(property);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   403
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
        PropertyMap newMap = checkHistory(property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
        final String key = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
        if (newMap == null && properties.containsKey(key)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
            final PropertyHashMap newProperties = properties.immutableRemove(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
            newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
            addToHistory(property, newMap);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
        return newMap;
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
     * Replace an existing property with a new one.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
     * @param oldProperty Property to replace.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
     * @param newProperty New {@link Property}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
     * @return New {@link PropertyMap} with {@link Property} replaced.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
    PropertyMap replaceProperty(final Property oldProperty, final Property newProperty) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   425
        if (listeners != null) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   426
            listeners.propertyModified(oldProperty, newProperty);
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   427
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
        // Add replaces existing property.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   429
        final PropertyHashMap newProperties = properties.immutableReplace(oldProperty, newProperty);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
        final PropertyMap newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
        /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
         * See ScriptObject.modifyProperty and ScriptObject.setUserAccessors methods.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
         *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
         * This replaceProperty method is called only for the following three cases:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
         *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
         *   1. To change flags OR TYPE of an old (cloned) property. We use the same spill slots.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
         *   2. To change one UserAccessor property with another - user getter or setter changed via
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
         *      Object.defineProperty function. Again, same spill slots are re-used.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
         *   3. Via ScriptObject.setUserAccessors method to set user getter and setter functions
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
         *      replacing the dummy AccessorProperty with null method handles (added during map init).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
         *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
         * In case (1) and case(2), the property type of old and new property is same. For case (3),
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
         * the old property is an AccessorProperty and the new one is a UserAccessorProperty property.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   446
        final boolean sameType = oldProperty.getClass() == newProperty.getClass();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
        assert sameType ||
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   448
                oldProperty instanceof AccessorProperty &&
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   449
                newProperty instanceof UserAccessorProperty :
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   450
            "arbitrary replaceProperty attempted " + sameType + " oldProperty=" + oldProperty.getClass() + " newProperty=" + newProperty.getClass() + " [" + oldProperty.getCurrentType() + " => " + newProperty.getCurrentType() + "]";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   452
        newMap.flags = flags;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
        /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
         * spillLength remains same in case (1) and (2) because of slot reuse. Only for case (3), we need
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
         * to add spill count of the newly added UserAccessorProperty property.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
         */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   458
        newMap.spillLength = spillLength;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 18860
diff changeset
   462
    /**
18617
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   463
     * Make a new UserAccessorProperty property. getter and setter functions are stored in
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   464
     * this ScriptObject and slot values are used in property object. Note that slots
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   465
     * are assigned speculatively and should be added to map before adding other
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   466
     * properties.
19097
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 18860
diff changeset
   467
     *
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 18860
diff changeset
   468
     * @param key the property name
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 18860
diff changeset
   469
     * @param propertyFlags attribute flags of the property
f544a2ea40ef 8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents: 18860
diff changeset
   470
     * @return the newly created UserAccessorProperty
18617
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   471
     */
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   472
    public UserAccessorProperty newUserAccessors(final String key, final int propertyFlags) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   473
        return new UserAccessorProperty(key, propertyFlags, spillLength);
18617
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   474
    }
f6fe338f62c3 8008458: Strict functions dont share property map
jlaskey
parents: 18325
diff changeset
   475
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
     * Find a property in the map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
     * @param key Key to search for.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
     * @return {@link Property} matching key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
    public Property findProperty(final String key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
        return properties.find(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
     * Adds all map properties from another map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
     * @param other The source of properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   492
     * @return New {@link PropertyMap} with added properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
    public PropertyMap addAll(final PropertyMap other) {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16201
diff changeset
   495
        assert this != other : "adding property map to itself";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
        final Property[] otherProperties = other.properties.getProperties();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   497
        final PropertyHashMap newProperties = properties.immutableAdd(otherProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
        final PropertyMap newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   500
        for (final Property property : otherProperties) {
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   501
            if (isValidArrayIndex(getArrayIndex(property.getKey()))) {
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   502
                newMap.setContainsArrayKeys();
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   503
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
            newMap.spillLength += property.getSpillCount();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
    }
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
     * Return an array of all properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
     * @return Properties as an array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   514
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   515
    public Property[] getProperties() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   516
        return properties.getProperties();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
     * Prevents the map from having additional properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
     * @return New map with {@link #NOT_EXTENSIBLE} flag set.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
    PropertyMap preventExtensions() {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   525
        final PropertyMap newMap = new PropertyMap(this);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
        newMap.flags |= NOT_EXTENSIBLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   528
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   531
     * Prevents properties in map from being modified.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
     *
16154
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16152
diff changeset
   533
     * @return New map with {@link #NOT_EXTENSIBLE} flag set and properties with
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16152
diff changeset
   534
     * {@link Property#NOT_CONFIGURABLE} set.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
    PropertyMap seal() {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   537
        PropertyHashMap newProperties = EMPTY_HASHMAP;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
        for (final Property oldProperty :  properties.getProperties()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
            newProperties = newProperties.immutableAdd(oldProperty.addFlags(Property.NOT_CONFIGURABLE));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
        final PropertyMap newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
        newMap.flags |= NOT_EXTENSIBLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
     * Prevents properties in map from being modified or written to.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
     * @return New map with {@link #NOT_EXTENSIBLE} flag set and properties with
16154
de44634fa4ec 8005782: get rid of javadoc errors, warnings in nashorn build
sundar
parents: 16152
diff changeset
   553
     * {@link Property#NOT_CONFIGURABLE} and {@link Property#NOT_WRITABLE} set.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
    PropertyMap freeze() {
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   556
        PropertyHashMap newProperties = EMPTY_HASHMAP;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
24721
81f70e23cd3b 8036127: Prototype filter needs to be applied to getter guard as well, not just getter
lagergren
parents: 24719
diff changeset
   558
        for (final Property oldProperty : properties.getProperties()) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
            int propertyFlags = Property.NOT_CONFIGURABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
            if (!(oldProperty instanceof UserAccessorProperty)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
                propertyFlags |= Property.NOT_WRITABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
            newProperties = newProperties.immutableAdd(oldProperty.addFlags(propertyFlags));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
        final PropertyMap newMap = new PropertyMap(this, newProperties);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   569
        newMap.flags |= NOT_EXTENSIBLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   570
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   571
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
     * Check for any configurable properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
     * @return {@code true} if any configurable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
    private boolean anyConfigurable() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
        for (final Property property : properties.getProperties()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
            if (property.isConfigurable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
               return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   583
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   585
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
        return false;
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
     * Check if all properties are frozen.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
     * @return {@code true} if all are frozen.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
    private boolean allFrozen() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
        for (final Property property : properties.getProperties()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
            // check if it is a data descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
            if (!(property instanceof UserAccessorProperty)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
                if (property.isWritable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
                    return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
            if (property.isConfigurable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
               return false;
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
        return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
     * Check prototype history for an existing property map with specified prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
     *
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   613
     * @param parentMap New prototype object.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
     * @return Existing {@link PropertyMap} or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   617
    private PropertyMap checkProtoHistory(final PropertyMap parentMap) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   618
        final PropertyMap cachedMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
        if (protoHistory != null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   620
            final SoftReference<PropertyMap> weakMap = protoHistory.get(parentMap);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
            cachedMap = (weakMap != null ? weakMap.get() : null);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   622
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   623
            cachedMap = null;
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
        if (Context.DEBUG && cachedMap != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   627
            protoHistoryHit++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   628
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   629
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   630
        return cachedMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   631
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   632
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   633
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   634
     * Add a map to the prototype history.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   635
     *
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   636
     * @param parentMap Prototype to add (key.)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   637
     * @param newMap   {@link PropertyMap} associated with prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   638
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   639
    private void addToProtoHistory(final PropertyMap parentMap, final PropertyMap newMap) {
16759
ecf99910fc31 8011219: Regression with recent PropertyMap history changes
hannesw
parents: 16758
diff changeset
   640
        if (protoHistory == null) {
ecf99910fc31 8011219: Regression with recent PropertyMap history changes
hannesw
parents: 16758
diff changeset
   641
            protoHistory = new WeakHashMap<>();
ecf99910fc31 8011219: Regression with recent PropertyMap history changes
hannesw
parents: 16758
diff changeset
   642
        }
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16275
diff changeset
   643
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   644
        protoHistory.put(parentMap, new SoftReference<>(newMap));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   646
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
     * Track the modification of the map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
     * @param property Mapping property.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   651
     * @param newMap   Modified {@link PropertyMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
    private void addToHistory(final Property property, final PropertyMap newMap) {
16774
745fe7d2536d 8011540: PropertyMap histories should not begin with empty map
jlaskey
parents: 16759
diff changeset
   654
        if (!properties.isEmpty()) {
745fe7d2536d 8011540: PropertyMap histories should not begin with empty map
jlaskey
parents: 16759
diff changeset
   655
            if (history == null) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   656
                history = new WeakHashMap<>();
16774
745fe7d2536d 8011540: PropertyMap histories should not begin with empty map
jlaskey
parents: 16759
diff changeset
   657
            }
745fe7d2536d 8011540: PropertyMap histories should not begin with empty map
jlaskey
parents: 16759
diff changeset
   658
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   659
            history.put(property, new SoftReference<>(newMap));
16759
ecf99910fc31 8011219: Regression with recent PropertyMap history changes
hannesw
parents: 16758
diff changeset
   660
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
     * Check the history for a map that already has the given property added.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
     * @param property {@link Property} to add.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   668
     * @return Existing map or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   669
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   670
    private PropertyMap checkHistory(final Property property) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   671
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   672
        if (history != null) {
24727
attila
parents: 24726 23084
diff changeset
   673
            final SoftReference<PropertyMap> ref = history.get(property);
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   674
            final PropertyMap historicMap = ref == null ? null : ref.get();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   675
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   676
            if (historicMap != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   677
                if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   678
                    historyHit++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   679
                }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   680
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   681
                return historicMap;
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   685
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   686
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   689
     * Returns true if the two maps have identical properties in the same order, but allows the properties to differ in
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   690
     * their types. This method is mostly useful for tests.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   691
     * @param otherMap the other map
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   692
     * @return true if this map has identical properties in the same order as the other map, allowing the properties to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   693
     * differ in type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   694
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   695
    public boolean equalsWithoutType(final PropertyMap otherMap) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   696
        if (properties.size() != otherMap.properties.size()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   697
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   698
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   699
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   700
        final Iterator<Property> iter      = properties.values().iterator();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   701
        final Iterator<Property> otherIter = otherMap.properties.values().iterator();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   702
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   703
        while (iter.hasNext() && otherIter.hasNext()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   704
            if (!iter.next().equalsWithoutType(otherIter.next())) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   705
                return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   706
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   707
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   708
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   709
        return true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   710
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   711
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   712
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   713
    public String toString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   714
        final StringBuilder sb = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   715
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
   716
        sb.append(Debug.id(this));
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
   717
        sb.append(" = {\n");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
24726
34410e0545b1 8037967: Broke the build, by commiting without saving the last review comment
lagergren
parents: 24725
diff changeset
   719
        for (final Property property : getProperties()) {
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
   720
            sb.append('\t');
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   721
            sb.append(property);
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
   722
            sb.append('\n');
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
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
   725
        sb.append('}');
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   726
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   728
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   729
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   730
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   731
    public Iterator<Object> iterator() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   732
        return new PropertyMapIterator(this);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   733
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
    /**
21441
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   736
     * Check if this map contains properties with valid array keys
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   737
     *
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   738
     * @return {@code true} if this map contains properties with valid array keys
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   739
     */
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   740
    public final boolean containsArrayKeys() {
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   741
        return (flags & CONTAINS_ARRAY_KEYS) != 0;
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   742
    }
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   743
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   744
    /**
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   745
     * Flag this object as having array keys in defined properties
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   746
     */
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   747
    private void setContainsArrayKeys() {
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   748
        flags |= CONTAINS_ARRAY_KEYS;
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   749
    }
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   750
0b98be59e3cb 8026858: Array length does not handle defined properties correctly
hannesw
parents: 19619
diff changeset
   751
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   752
     * Test to see if {@link PropertyMap} is extensible.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   753
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   754
     * @return {@code true} if {@link PropertyMap} can be added to.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   755
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   756
    boolean isExtensible() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   757
        return (flags & NOT_EXTENSIBLE) == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   758
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   759
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   760
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   761
     * Test to see if {@link PropertyMap} is not extensible or any properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   762
     * can not be modified.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   763
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   764
     * @return {@code true} if {@link PropertyMap} is sealed.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   765
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   766
    boolean isSealed() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   767
        return !isExtensible() && !anyConfigurable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   768
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   769
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   770
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   771
     * Test to see if {@link PropertyMap} is not extensible or all properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   772
     * can not be modified.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   773
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   774
     * @return {@code true} if {@link PropertyMap} is frozen.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   775
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   776
    boolean isFrozen() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   777
        return !isExtensible() && allFrozen();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   778
    }
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   779
    /**
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   780
     * Get the number of fields allocated for this {@link PropertyMap}.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   781
     *
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   782
     * @return Number of fields allocated.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   783
     */
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   784
    int getFieldCount() {
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   785
        return fieldCount;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   786
    }
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   787
    /**
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   788
     * Get maximum number of fields available for this {@link PropertyMap}.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   789
     *
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   790
     * @return Number of fields available.
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   791
     */
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   792
    int getFieldMaximum() {
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   793
        return fieldMaximum;
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   794
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   795
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   796
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   797
     * Get length of spill area associated with this {@link PropertyMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   798
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   799
     * @return Length of spill area.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   800
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   801
    int getSpillLength() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   802
        return spillLength;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   803
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   804
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   805
    /**
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   806
     * Return a property map with the same layout that is associated with the new prototype object.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   807
     *
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   808
     * @param newProto New prototype object to replace oldProto.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   809
     * @return New {@link PropertyMap} with prototype changed.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   810
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   811
    public PropertyMap changeProto(final ScriptObject newProto) {
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   812
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   813
        final PropertyMap parentMap = newProto == null ? null : newProto.getMap();
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   814
        final PropertyMap nextMap = checkProtoHistory(parentMap);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   815
        if (nextMap != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   816
            return nextMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   817
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   818
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   819
        if (Context.DEBUG) {
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   820
            setProtoNewMapCount++;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   821
        }
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   822
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16774
diff changeset
   823
        final PropertyMap newMap = new PropertyMap(this);
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21441
diff changeset
   824
        addToProtoHistory(parentMap, newMap);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   825
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   826
        return newMap;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   827
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   828
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   829
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   830
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   831
     * {@link PropertyMap} iterator.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   832
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   833
    private static class PropertyMapIterator implements Iterator<Object> {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   834
        /** Property iterator. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   835
        final Iterator<Property> iter;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   836
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   837
        /** Current Property. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   838
        Property property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   839
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   840
        /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   841
         * Constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   842
         *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   843
         * @param propertyMap {@link PropertyMap} to iterate over.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   844
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   845
        PropertyMapIterator(final PropertyMap propertyMap) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   846
            iter = Arrays.asList(propertyMap.properties.getProperties()).iterator();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   847
            property = iter.hasNext() ? iter.next() : null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   848
            skipNotEnumerable();
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
         * Ignore properties that are not enumerable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   853
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   854
        private void skipNotEnumerable() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   855
            while (property != null && !property.isEnumerable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   856
                property = iter.hasNext() ? iter.next() : null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   857
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   858
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   859
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   860
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   861
        public boolean hasNext() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   862
            return property != null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   863
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   864
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   865
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   866
        public Object next() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   867
            if (property == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   868
                throw new NoSuchElementException();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   869
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   870
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   871
            final Object key = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   872
            property = iter.next();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   873
            skipNotEnumerable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   874
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   875
            return key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
        public void remove() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   880
            throw new UnsupportedOperationException();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   881
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   882
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   883
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   884
    /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   885
     * Debugging and statistics.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   887
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   888
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   889
     * Debug helper function that returns the diff of two property maps, only
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   890
     * displaying the information that is different and in which map it exists
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   891
     * compared to the other map. Can be used to e.g. debug map guards and
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   892
     * investigate why they fail, causing relink
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   893
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   894
     * @param map0 the first property map
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   895
     * @param map1 the second property map
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   896
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   897
     * @return property map diff as string
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   898
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   899
    public static String diff(final PropertyMap map0, final PropertyMap map1) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   900
        final StringBuilder sb = new StringBuilder();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   901
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   902
        if (map0 != map1) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   903
           sb.append(">>> START: Map diff");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   904
           boolean found = false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   905
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   906
           for (final Property p : map0.getProperties()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   907
               final Property p2 = map1.findProperty(p.getKey());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   908
               if (p2 == null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   909
                   sb.append("FIRST ONLY : [" + p + "]");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   910
                   found = true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   911
               } else if (p2 != p) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   912
                   sb.append("DIFFERENT  : [" + p + "] != [" + p2 + "]");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   913
                   found = true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   914
               }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   915
           }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   916
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   917
           for (final Property p2 : map1.getProperties()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   918
               final Property p1 = map0.findProperty(p2.getKey());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   919
               if (p1 == null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   920
                   sb.append("SECOND ONLY: [" + p2 + "]");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   921
                   found = true;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   922
               }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   923
           }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   924
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   925
           //assert found;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   926
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   927
           if (!found) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   928
                sb.append(map0).
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   929
                    append("!=").
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   930
                    append(map1);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   931
           }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   932
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   933
           sb.append("<<< END: Map diff\n");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   934
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   935
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   936
        return sb.toString();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   937
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21441
diff changeset
   938
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
    // counters updated only in debug mode
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
    private static int count;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   941
    private static int clonedCount;
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   942
    private static int duplicatedCount;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
    private static int historyHit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   944
    private static int protoInvalidations;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   945
    private static int protoHistoryHit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   946
    private static int setProtoNewMapCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   947
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   948
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   949
     * @return Total number of maps.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   950
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   951
    public static int getCount() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   952
        return count;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   953
    }
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
     * @return The number of maps that were cloned.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   957
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   958
    public static int getClonedCount() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
        return clonedCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
    /**
18855
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   963
     * @return The number of maps that are duplicated.
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   964
     */
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   965
    public static int getDuplicatedCount() {
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   966
        return duplicatedCount;
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   967
    }
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   968
408663ef8f66 8020015: shared PropertyMaps should not be used without duplication
sundar
parents: 18852
diff changeset
   969
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   970
     * @return The number of times history was successfully used.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
    public static int getHistoryHit() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   973
        return historyHit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   974
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
     * @return The number of times prototype changes caused invalidation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   979
    public static int getProtoInvalidations() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   980
        return protoInvalidations;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   981
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   982
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   983
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   984
     * @return The number of times proto history was successfully used.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   985
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   986
    public static int getProtoHistoryHit() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   987
        return protoHistoryHit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   988
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   989
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   990
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   991
     * @return The number of times prototypes were modified.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   992
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   993
    public static int getSetProtoNewMapCount() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   994
        return setProtoNewMapCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   995
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   996
}