2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #ifndef PropertySlot_h
22 #define PropertySlot_h
24 #include "JSCJSValue.h"
25 #include "PropertyName.h"
26 #include "PropertyOffset.h"
28 #include <wtf/Assertions.h>
29 #include <wtf/NotFound.h>
36 #define JSC_VALUE_MARKER 0
37 #define INDEX_GETTER_MARKER reinterpret_cast<GetValueFunc>(2)
38 #define GETTER_FUNCTION_MARKER reinterpret_cast<GetValueFunc>(3)
42 enum CachedPropertyType
{
50 : m_cachedPropertyType(Uncacheable
)
57 explicit PropertySlot(const JSValue base
)
59 , m_cachedPropertyType(Uncacheable
)
65 typedef JSValue (*GetValueFunc
)(ExecState
*, JSValue slotBase
, PropertyName
);
66 typedef JSValue (*GetIndexValueFunc
)(ExecState
*, JSValue slotBase
, unsigned);
68 JSValue
getValue(ExecState
* exec
, PropertyName propertyName
) const
70 if (m_getValue
== JSC_VALUE_MARKER
)
72 if (m_getValue
== INDEX_GETTER_MARKER
)
73 return m_getIndexValue(exec
, slotBase(), index());
74 if (m_getValue
== GETTER_FUNCTION_MARKER
)
75 return functionGetter(exec
);
76 return m_getValue(exec
, slotBase(), propertyName
);
79 JSValue
getValue(ExecState
* exec
, unsigned propertyName
) const
81 if (m_getValue
== JSC_VALUE_MARKER
)
83 if (m_getValue
== INDEX_GETTER_MARKER
)
84 return m_getIndexValue(exec
, m_slotBase
, m_data
.index
);
85 if (m_getValue
== GETTER_FUNCTION_MARKER
)
86 return functionGetter(exec
);
87 return m_getValue(exec
, slotBase(), Identifier::from(exec
, propertyName
));
90 CachedPropertyType
cachedPropertyType() const { return m_cachedPropertyType
; }
91 bool isCacheable() const { return m_cachedPropertyType
!= Uncacheable
; }
92 bool isCacheableValue() const { return m_cachedPropertyType
== Value
; }
93 PropertyOffset
cachedOffset() const
95 ASSERT(isCacheable());
99 void setValue(JSValue slotBase
, JSValue value
)
103 m_getValue
= JSC_VALUE_MARKER
;
104 m_slotBase
= slotBase
;
108 void setValue(JSValue slotBase
, JSValue value
, PropertyOffset offset
)
111 m_getValue
= JSC_VALUE_MARKER
;
112 m_slotBase
= slotBase
;
115 m_cachedPropertyType
= Value
;
118 void setValue(JSValue value
)
123 m_getValue
= JSC_VALUE_MARKER
;
127 void setCustom(JSValue slotBase
, GetValueFunc getValue
)
131 m_getValue
= getValue
;
133 m_slotBase
= slotBase
;
136 void setCacheableCustom(JSValue slotBase
, GetValueFunc getValue
)
140 m_getValue
= getValue
;
142 m_slotBase
= slotBase
;
143 m_cachedPropertyType
= Custom
;
146 void setCustomIndex(JSValue slotBase
, unsigned index
, GetIndexValueFunc getIndexValue
)
149 ASSERT(getIndexValue
);
150 m_getValue
= INDEX_GETTER_MARKER
;
151 m_getIndexValue
= getIndexValue
;
152 m_slotBase
= slotBase
;
153 m_data
.index
= index
;
156 void setGetterSlot(JSObject
* getterFunc
)
159 m_thisValue
= m_slotBase
;
160 m_getValue
= GETTER_FUNCTION_MARKER
;
161 m_data
.getterFunc
= getterFunc
;
164 void setCacheableGetterSlot(JSValue slotBase
, JSObject
* getterFunc
, PropertyOffset offset
)
167 m_getValue
= GETTER_FUNCTION_MARKER
;
168 m_thisValue
= m_slotBase
;
169 m_slotBase
= slotBase
;
170 m_data
.getterFunc
= getterFunc
;
172 m_cachedPropertyType
= Getter
;
177 setValue(jsUndefined());
180 JSValue
slotBase() const
185 void setBase(JSValue base
)
195 m_slotBase
= JSValue();
208 // Clear offset even in release builds, in case this PropertySlot has been used before.
209 // (For other data members, we don't need to clear anything because reuse would meaningfully overwrite them.)
210 m_offset
= invalidOffset
;
211 m_cachedPropertyType
= Uncacheable
;
214 unsigned index() const { return m_data
.index
; }
216 GetValueFunc
customGetter() const
218 ASSERT(m_cachedPropertyType
== Custom
);
222 JS_EXPORT_PRIVATE JSValue
functionGetter(ExecState
*) const;
224 GetValueFunc m_getValue
;
225 GetIndexValueFunc m_getIndexValue
;
229 JSObject
* getterFunc
;
236 PropertyOffset m_offset
;
237 CachedPropertyType m_cachedPropertyType
;
242 #endif // PropertySlot_h