DataPropertyDescriptor.java revision 953:221a84ef44c0
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.objects;
27
28import static jdk.nashorn.internal.runtime.ScriptRuntime.sameValue;
29
30import java.util.Objects;
31import jdk.nashorn.internal.objects.annotations.Property;
32import jdk.nashorn.internal.objects.annotations.ScriptClass;
33import jdk.nashorn.internal.runtime.JSType;
34import jdk.nashorn.internal.runtime.PropertyDescriptor;
35import jdk.nashorn.internal.runtime.PropertyMap;
36import jdk.nashorn.internal.runtime.ScriptFunction;
37import jdk.nashorn.internal.runtime.ScriptObject;
38
39/**
40 * Data Property descriptor is used to represent attributes an object property
41 * that has data value (instead of a getter or setter function).
42 *
43 * See ECMA 8.10 The Property Descriptor and Property Identifier Specification Types
44 *
45 */
46@ScriptClass("DataPropertyDescriptor")
47public final class DataPropertyDescriptor extends ScriptObject implements PropertyDescriptor {
48    /** is this property configurable */
49    @Property
50    public Object configurable;
51
52    /** is this property enumerable */
53    @Property
54    public Object enumerable;
55
56    /** is this property writable */
57    @Property
58    public Object writable;
59
60    /** value of this property */
61    @Property
62    public Object value;
63
64    // initialized by nasgen
65    private static PropertyMap $nasgenmap$;
66
67    DataPropertyDescriptor(final boolean configurable, final boolean enumerable, final boolean writable, final Object value, final Global global) {
68        super(global.getObjectPrototype(), $nasgenmap$);
69        this.configurable = configurable;
70        this.enumerable   = enumerable;
71        this.writable     = writable;
72        this.value        = value;
73    }
74
75
76    @Override
77    public boolean isConfigurable() {
78        return JSType.toBoolean(configurable);
79    }
80
81    @Override
82    public boolean isEnumerable() {
83        return JSType.toBoolean(enumerable);
84    }
85
86    @Override
87    public boolean isWritable() {
88        return JSType.toBoolean(writable);
89    }
90
91    @Override
92    public Object getValue() {
93        return value;
94    }
95
96    @Override
97    public ScriptFunction getGetter() {
98        throw new UnsupportedOperationException("getter");
99    }
100
101    @Override
102    public ScriptFunction getSetter() {
103        throw new UnsupportedOperationException("setter");
104    }
105
106    @Override
107    public void setConfigurable(final boolean flag) {
108        this.configurable = flag;
109    }
110
111    @Override
112    public void setEnumerable(final boolean flag) {
113        this.enumerable = flag;
114    }
115
116    @Override
117    public void setWritable(final boolean flag) {
118        this.writable = flag;
119    }
120
121    @Override
122    public void setValue(final Object value) {
123        this.value = value;
124    }
125
126    @Override
127    public void setGetter(final Object getter) {
128        throw new UnsupportedOperationException("set getter");
129    }
130
131    @Override
132    public void setSetter(final Object setter) {
133        throw new UnsupportedOperationException("set setter");
134    }
135
136    @Override
137    public PropertyDescriptor fillFrom(final ScriptObject sobj) {
138        if (sobj.has(CONFIGURABLE)) {
139            this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
140        } else {
141            delete(CONFIGURABLE, false);
142        }
143
144        if (sobj.has(ENUMERABLE)) {
145            this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
146        } else {
147            delete(ENUMERABLE, false);
148        }
149
150        if (sobj.has(WRITABLE)) {
151            this.writable = JSType.toBoolean(sobj.get(WRITABLE));
152        } else {
153            delete(WRITABLE, false);
154        }
155
156        if (sobj.has(VALUE)) {
157            this.value = sobj.get(VALUE);
158        } else {
159            delete(VALUE, false);
160        }
161
162        return this;
163    }
164
165    @Override
166    public int type() {
167        return DATA;
168    }
169
170    @Override
171    public boolean hasAndEquals(final PropertyDescriptor otherDesc) {
172        if (! (otherDesc instanceof DataPropertyDescriptor)) {
173            return false;
174        }
175
176        final DataPropertyDescriptor other = (DataPropertyDescriptor)otherDesc;
177        return (!has(CONFIGURABLE) || sameValue(configurable, other.configurable)) &&
178               (!has(ENUMERABLE) || sameValue(enumerable, other.enumerable)) &&
179               (!has(WRITABLE) || sameValue(writable, other.writable)) &&
180               (!has(VALUE) || sameValue(value, other.value));
181    }
182
183    @Override
184    public boolean equals(final Object obj) {
185        if (this == obj) {
186            return true;
187        }
188        if (! (obj instanceof DataPropertyDescriptor)) {
189            return false;
190        }
191
192        final DataPropertyDescriptor other = (DataPropertyDescriptor)obj;
193        return sameValue(configurable, other.configurable) &&
194               sameValue(enumerable, other.enumerable) &&
195               sameValue(writable, other.writable) &&
196               sameValue(value, other.value);
197    }
198
199    @Override
200    public String toString() {
201        return '[' + getClass().getSimpleName() + " {configurable=" + configurable + " enumerable=" + enumerable + " writable=" + writable + " value=" + value + "}]";
202    }
203
204    @Override
205    public int hashCode() {
206        int hash = 5;
207        hash = 43 * hash + Objects.hashCode(this.configurable);
208        hash = 43 * hash + Objects.hashCode(this.enumerable);
209        hash = 43 * hash + Objects.hashCode(this.writable);
210        hash = 43 * hash + Objects.hashCode(this.value);
211        return hash;
212    }
213}
214