nashorn/src/jdk/nashorn/internal/runtime/Property.java
author jlaskey
Fri, 04 Jan 2013 09:58:33 -0400
changeset 16151 97c1e756ae1e
parent 16147 e63b63819133
child 16224 0c49ad4e3b55
permissions -rw-r--r--
8005663: Update copyright year to 2013 Reviewed-by: sundar Contributed-by: james.laskey@oracle.com
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
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.Objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import jdk.nashorn.internal.codegen.objects.ObjectClassGenerator;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import jdk.nashorn.internal.codegen.types.Type;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * This is the abstract superclass representing a JavaScript Property.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * The {@link PropertyMap} map links keys to properties, and consequently
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
 * instances of this class make up the values in the PropertyMap
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
 * @see PropertyMap
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
 * @see AccessorProperty
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
 * @see SpillProperty
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
 * @see UserAccessorProperty
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
public abstract class Property {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
    /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     * ECMA 8.6.1 Property Attributes
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
     * We use negative flags because most properties are expected to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
     * be 'writable', 'configurable' and 'enumerable'. With negative flags,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
     * we can use leave flag byte initialized with (the default) zero value.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    /** ECMA 8.6.1 - Is this property not writable? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    public static final int NOT_WRITABLE     = 0b0000_0000_0001;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    /** ECMA 8.6.1 - Is this property not enumerable? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    public static final int NOT_ENUMERABLE   = 0b0000_0000_0010;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    /** ECMA 8.6.1 - Is this property not configurable? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    public static final int NOT_CONFIGURABLE = 0b0000_0000_0100;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
    private static final int MODIFY_MASK     = 0b0000_0000_1111;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    /** Is this a spill property? See {@link SpillProperty} */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    public static final int IS_SPILL         = 0b0000_0001_0000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    /** Is this a function parameter ? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
    public static final int IS_PARAMETER     = 0b0000_0010_0000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    /** Is this property always represented as an Object? See {@link ObjectClassGenerator} and dual fields flag. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    public static final int IS_ALWAYS_OBJECT = 0b0000_0100_0000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    /** Can this property be primitive? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    public static final int CAN_BE_PRIMITIVE = 0b0000_1000_0000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
    /** Can this property be undefined? */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    public static final int CAN_BE_UNDEFINED = 0b0001_0000_0000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    /** Property key. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
    private final String key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
    /** Property flags. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
    protected int flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
     * @param key   property key
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
     * @param flags property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
    public Property(final String key, final int flags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
        this.key   = key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
        this.flags = flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
     * Copy constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
     * @param property source property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    protected Property(final Property property) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
        this.key   = property.key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
        this.flags = property.flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
     * Copy function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
     * @return cloned property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
    protected abstract Property copy();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
     * Property flag utility method for {@link PropertyDescriptor}s. Given two property descriptors,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
     * return the result of merging their flags.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
     * @param oldDesc  first property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
     * @param newDesc  second property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
     * @return merged flags.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
    static int mergeFlags(final PropertyDescriptor oldDesc, final PropertyDescriptor newDesc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
        int     propFlags = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
        boolean value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
        value = newDesc.has(CONFIGURABLE) ? newDesc.isConfigurable() : oldDesc.isConfigurable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
        if (!value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
            propFlags |= NOT_CONFIGURABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        value = newDesc.has(ENUMERABLE) ? newDesc.isEnumerable() : oldDesc.isEnumerable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
        if (!value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
            propFlags |= NOT_ENUMERABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
        value = newDesc.has(WRITABLE) ? newDesc.isWritable() : oldDesc.isWritable();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
        if (!value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
            propFlags |= NOT_WRITABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
        return propFlags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
     * Property flag utility method for {@link PropertyDescriptor}. Get the property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     * conforming to any Property using this PropertyDescriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
     * @param desc property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     * @return flags for properties that conform to property descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    static int toFlags(final PropertyDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        int propFlags = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
        if (!desc.isConfigurable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
            propFlags |= NOT_CONFIGURABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        if (!desc.isEnumerable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
            propFlags |= NOT_ENUMERABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
        if (!desc.isWritable()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
            propFlags |= NOT_WRITABLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
        return propFlags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * Check whether this property has a user defined getter function. See {@link UserAccessorProperty}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     * @return true if getter function exists, false is default
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    public boolean hasGetterFunction() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * Check whether this property has a user defined setter function. See {@link UserAccessorProperty}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     * @return true if getter function exists, false is default
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
    public boolean hasSetterFunction() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
     * Check whether this property is writable (see ECMA 8.6.1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
     * @return true if writable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
    public boolean isWritable() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
        return (flags & NOT_WRITABLE) == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     * Check whether this property is writable (see ECMA 8.6.1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
     * @return true if configurable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
    public boolean isConfigurable() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
        return (flags & NOT_CONFIGURABLE) == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
     * Check whether this property is enumerable (see ECMA 8.6.1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
     * @return true if enumerable
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    public boolean isEnumerable() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
        return (flags & NOT_ENUMERABLE) == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
     * Check whether this property is used as a function parameter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
     * @return true if parameter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
    public boolean isParameter() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
        return (flags & IS_PARAMETER) == IS_PARAMETER;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
     * Check whether this is a spill property, i.e. one that will not
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
     * be stored in a specially generated field in the property class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
     * The spill pool is maintained separately, as a growing Object array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
     * in the {@link ScriptObject}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     * @return true if spill property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    public boolean isSpill() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
        return (flags & IS_SPILL) == IS_SPILL;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
     * Does this property use any slots in the spill array described in
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
     * {@link Property#isSpill}? In that case how many. Currently a property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
     * only uses max one spill slot, but this may change in future representations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
     * Only {@link SpillProperty} instances use spill slots
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
     * @return number of spill slots a property is using
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
    public int getSpillCount() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
        return isSpill() ? 1 : 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
     * Add more property flags to the property. Properties are immutable here,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     * so any property change that results in a larger flag set results in the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
     * property being cloned. Use only the return value
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     * @param propertyFlags flags to be OR:ed to the existing property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     * @return new property if property set was changed, {@code this} otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    public Property addFlags(final int propertyFlags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
        if ((this.flags & propertyFlags) != propertyFlags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
            final Property cloned = this.copy();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
            cloned.flags |= propertyFlags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
            return cloned;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * Get the flags for this property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     * @return property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
    public int getFlags() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
        return flags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
     * Get the modify flags for this property. The modify flags are the ECMA 8.6.1
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
     * flags that decide if the Property is writable, configurable and/or enumerable.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * @return modify flags for property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    public int getModifyFlags() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
        return flags & MODIFY_MASK;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     * Remove property flags from the property. Properties are immutable here,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
     * so any property change that results in a smaller flag set results in the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     * property being cloned. Use only the return value
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
     * @param propertyFlags flags to be subtracted from the existing property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
     * @return new property if property set was changed, {@code this} otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
    public Property removeFlags(final int propertyFlags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
        if ((this.flags & propertyFlags) != 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
            final Property cloned = this.copy();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
            cloned.flags &= ~propertyFlags;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
            return cloned;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
     * Reset the property for this property. Properties are immutable here,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
     * so any property change that results in a different flag sets results in the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     * property being cloned. Use only the return value
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     * @param propertyFlags flags that are replacing from the existing property flags
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     * @return new property if property set was changed, {@code this} otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
    public Property setFlags(final int propertyFlags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
        if (this.flags != propertyFlags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
            final Property cloned = this.copy();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
            cloned.flags &= ~MODIFY_MASK;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
            cloned.flags |= propertyFlags & MODIFY_MASK;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
            return cloned;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * Abstract method for retrieving the getter for the property. We do not know
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     * anything about the internal representation when we request the getter, we only
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     * know that the getter will return the property as the given type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
     * @param type getter return value type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
     * @return a getter for this property as {@code type}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
    public abstract MethodHandle getGetter(final Class<?> type);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
     * Get the key for this property. This key is an ordinary string. The "name".
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * @return key for property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
    public String getKey() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
        return key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
     * Abstract method for retrieving the setter for the property. We do not know
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
     * anything about the internal representation when we request the setter, we only
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
     * know that the setter will take the property as a parameter of the given type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   333
     * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   334
     * Note that we have to pass the current property map from which we retrieved
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
     * the property here. This is necessary for map guards if, e.g. the internal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
     * representation of the field, and consequently also the setter, changes. Then
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
     * we automatically get a map guard that relinks the call site so that the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
     * older setter will never be used again.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
     * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
     * see {@link ObjectClassGenerator#createSetter(Class, Class, MethodHandle, MethodHandle)}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
     * if you are interested in the internal details of this. Note that if you
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
     * are running in default mode, with {@code -Dnashorn.fields.dual=true}, disabled, the setters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
     * will currently never change, as all properties are represented as Object field,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
     * the Object fields are Initialized to {@code ScriptRuntime.UNDEFINED} and primitives are
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
     * boxed/unboxed upon every access, which is not necessarily optimal
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
     * @param type setter parameter type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
     * @param currentMap current property map for property
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   349
     * @return a getter for this property as {@code type}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
    public abstract MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
     * Get the user defined getter function if one exists. Only {@link UserAccessorProperty} instances
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     * can have user defined getters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
     * @param obj the script object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
     * @return user defined getter function, or {@code null} if none exists
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
    public ScriptFunction getGetterFunction(final ScriptObject obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
        return null;
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     * Get the user defined setter function if one exists. Only {@link UserAccessorProperty} instances
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * can have user defined getters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     * @param obj the script object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
     * @return user defined getter function, or {@code null} if none exists
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
    public ScriptFunction getSetterFunction(final ScriptObject obj) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
     * Get the spill slot as described in {@link Property#getSpillCount()}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
     * @return spill slot, -1 if none exists
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
    public int getSlot() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
        return -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
    public int hashCode() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
        final Class<?> type = getCurrentType();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
        return Objects.hashCode(this.key) ^ flags ^ (type == null ? 0 : type.hashCode());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
    public boolean equals(final Object other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
        if (this == other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
            return true;
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
        if (!(other instanceof Property)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
            return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
        final Property otherProperty = (Property)other;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
        final Object otherKey = otherProperty.key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
        final Class<?> otherType = otherProperty.getCurrentType();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
        return flags == otherProperty.flags &&
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
               (key == null ? otherKey == null : key.equals(otherKey)) &&
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
               (getCurrentType() == otherType);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
    public String toString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
        final StringBuilder sb   = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
        final Class<?>      type = getCurrentType();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
        sb.append(getKey()).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
            append("(0x").
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
            append(Integer.toHexString(flags)).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
            append(") ").
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
            append(getClass().getSimpleName()).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
            append(" {").
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
            append(type == null ? "UNDEFINED" : Type.typeFor(type).getDescriptor()).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
            append('}');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
     * Get the current type of this field. If you are not running with dual fields enabled,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
     * this will always be Object.class. See the value representation explanation in
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
     * {@link Property#getSetter(Class, PropertyMap)} and {@link ObjectClassGenerator}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
     * for more information.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
     * @return current type of property, null means undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
    public Class<?> getCurrentType() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
        return Object.class;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
     * Check whether this Property can ever change its type. The default is false, and if
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
     * you are not running with dual fields, the type is always object and can never change
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
     * @return true if this property can change types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
    public boolean canChangeType() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
     * Check whether this Property is ever used as anything but an Object. If this is used only
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
     * as an object, dual fields mode need not even try to represent it as a primitive at any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
     * callsite, saving map rewrites for performance.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
     * @return true if representation should always be an object field
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
    public boolean isAlwaysObject() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
        return (flags & IS_ALWAYS_OBJECT) == IS_ALWAYS_OBJECT;
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
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
     * Check whether this property can be primitive. This is a conservative
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
     * analysis result, so {@code false} might mean that it can still be
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
     * primitive
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
     * @return can be primitive status
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
    public boolean canBePrimitive() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
        return (flags & CAN_BE_PRIMITIVE) == CAN_BE_PRIMITIVE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
     * Check whether this property can be primitive. This is a conservative
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
     * analysis result, so {@code true} might mean that it can still be
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
     * defined, but it will never say that a property can not be undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
     * if it can
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
     * @return can be undefined status
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
    public boolean canBeUndefined() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
        return (flags & CAN_BE_UNDEFINED) == CAN_BE_UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
}