]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/PropertyDescriptor.cpp
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "PropertyDescriptor.h"
31 #include "GetterSetter.h"
33 #include "Operations.h"
36 unsigned PropertyDescriptor::defaultAttributes
= (DontDelete
<< 1) - 1;
38 bool PropertyDescriptor::writable() const
40 ASSERT(!isAccessorDescriptor());
41 return !(m_attributes
& ReadOnly
);
44 bool PropertyDescriptor::enumerable() const
46 return !(m_attributes
& DontEnum
);
49 bool PropertyDescriptor::configurable() const
51 return !(m_attributes
& DontDelete
);
54 bool PropertyDescriptor::isDataDescriptor() const
56 return m_value
|| (m_seenAttributes
& WritablePresent
);
59 bool PropertyDescriptor::isGenericDescriptor() const
61 return !isAccessorDescriptor() && !isDataDescriptor();
64 bool PropertyDescriptor::isAccessorDescriptor() const
66 return m_getter
|| m_setter
;
69 void PropertyDescriptor::setUndefined()
71 m_value
= jsUndefined();
72 m_attributes
= ReadOnly
| DontDelete
| DontEnum
;
75 JSValue
PropertyDescriptor::getter() const
77 ASSERT(isAccessorDescriptor());
81 JSValue
PropertyDescriptor::setter() const
83 ASSERT(isAccessorDescriptor());
87 void PropertyDescriptor::setDescriptor(JSValue value
, unsigned attributes
)
90 m_attributes
= attributes
;
91 if (attributes
& (Getter
| Setter
)) {
92 GetterSetter
* accessor
= asGetterSetter(value
);
93 m_getter
= accessor
->getter();
94 m_setter
= accessor
->setter();
95 ASSERT(m_getter
|| m_setter
);
96 m_seenAttributes
= EnumerablePresent
| ConfigurablePresent
;
97 m_attributes
&= ~ReadOnly
;
100 m_seenAttributes
= EnumerablePresent
| ConfigurablePresent
| WritablePresent
;
104 void PropertyDescriptor::setAccessorDescriptor(JSValue getter
, JSValue setter
, unsigned attributes
)
106 ASSERT(attributes
& (Getter
| Setter
));
107 ASSERT(getter
|| setter
);
108 m_attributes
= attributes
;
111 m_attributes
&= ~ReadOnly
;
112 m_seenAttributes
= EnumerablePresent
| ConfigurablePresent
;
115 void PropertyDescriptor::setWritable(bool writable
)
118 m_attributes
&= ~ReadOnly
;
120 m_attributes
|= ReadOnly
;
121 m_seenAttributes
|= WritablePresent
;
124 void PropertyDescriptor::setEnumerable(bool enumerable
)
127 m_attributes
&= ~DontEnum
;
129 m_attributes
|= DontEnum
;
130 m_seenAttributes
|= EnumerablePresent
;
133 void PropertyDescriptor::setConfigurable(bool configurable
)
136 m_attributes
&= ~DontDelete
;
138 m_attributes
|= DontDelete
;
139 m_seenAttributes
|= ConfigurablePresent
;
142 void PropertyDescriptor::setSetter(JSValue setter
)
145 m_attributes
|= Setter
;
146 m_attributes
&= ~ReadOnly
;
149 void PropertyDescriptor::setGetter(JSValue getter
)
152 m_attributes
|= Getter
;
153 m_attributes
&= ~ReadOnly
;
156 bool PropertyDescriptor::equalTo(ExecState
* exec
, const PropertyDescriptor
& other
) const
158 if (!other
.m_value
== m_value
||
159 !other
.m_getter
== m_getter
||
160 !other
.m_setter
== m_setter
)
162 return (!m_value
|| JSValue::strictEqual(exec
, other
.m_value
, m_value
)) &&
163 (!m_getter
|| JSValue::strictEqual(exec
, other
.m_getter
, m_getter
)) &&
164 (!m_setter
|| JSValue::strictEqual(exec
, other
.m_setter
, m_setter
)) &&
165 attributesEqual(other
);
168 bool PropertyDescriptor::attributesEqual(const PropertyDescriptor
& other
) const
170 unsigned mismatch
= other
.m_attributes
^ m_attributes
;
171 unsigned sharedSeen
= other
.m_seenAttributes
& m_seenAttributes
;
172 if (sharedSeen
& WritablePresent
&& mismatch
& ReadOnly
)
174 if (sharedSeen
& ConfigurablePresent
&& mismatch
& DontDelete
)
176 if (sharedSeen
& EnumerablePresent
&& mismatch
& DontEnum
)
181 unsigned PropertyDescriptor::attributesWithOverride(const PropertyDescriptor
& other
) const
183 unsigned mismatch
= other
.m_attributes
^ m_attributes
;
184 unsigned sharedSeen
= other
.m_seenAttributes
& m_seenAttributes
;
185 unsigned newAttributes
= m_attributes
& defaultAttributes
;
186 if (sharedSeen
& WritablePresent
&& mismatch
& ReadOnly
)
187 newAttributes
^= ReadOnly
;
188 if (sharedSeen
& ConfigurablePresent
&& mismatch
& DontDelete
)
189 newAttributes
^= DontDelete
;
190 if (sharedSeen
& EnumerablePresent
&& mismatch
& DontEnum
)
191 newAttributes
^= DontEnum
;
192 return newAttributes
;