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 "Identifier.h"
27 #include <wtf/Assertions.h>
28 #include <wtf/NotFound.h>
35 #define JSC_VALUE_MARKER 0
36 #define INDEX_GETTER_MARKER reinterpret_cast<GetValueFunc>(2)
37 #define GETTER_FUNCTION_MARKER reinterpret_cast<GetValueFunc>(3)
41 enum CachedPropertyType {
49 : m_cachedPropertyType(Uncacheable)
56 explicit PropertySlot(const JSValue base)
58 , m_cachedPropertyType(Uncacheable)
64 typedef JSValue (*GetValueFunc)(ExecState*, JSValue slotBase, const Identifier&);
65 typedef JSValue (*GetIndexValueFunc)(ExecState*, JSValue slotBase, unsigned);
67 JSValue getValue(ExecState* exec, const Identifier& propertyName) const
69 if (m_getValue == JSC_VALUE_MARKER)
71 if (m_getValue == INDEX_GETTER_MARKER)
72 return m_getIndexValue(exec, slotBase(), index());
73 if (m_getValue == GETTER_FUNCTION_MARKER)
74 return functionGetter(exec);
75 return m_getValue(exec, slotBase(), propertyName);
78 JSValue getValue(ExecState* exec, unsigned propertyName) const
80 if (m_getValue == JSC_VALUE_MARKER)
82 if (m_getValue == INDEX_GETTER_MARKER)
83 return m_getIndexValue(exec, m_slotBase, m_data.index);
84 if (m_getValue == GETTER_FUNCTION_MARKER)
85 return functionGetter(exec);
86 return m_getValue(exec, slotBase(), Identifier::from(exec, propertyName));
89 CachedPropertyType cachedPropertyType() const { return m_cachedPropertyType; }
90 bool isCacheable() const { return m_cachedPropertyType != Uncacheable; }
91 bool isCacheableValue() const { return m_cachedPropertyType == Value; }
92 size_t cachedOffset() const
94 ASSERT(isCacheable());
98 void setValue(JSValue slotBase, JSValue value)
102 m_getValue = JSC_VALUE_MARKER;
103 m_slotBase = slotBase;
107 void setValue(JSValue slotBase, JSValue value, size_t offset)
110 m_getValue = JSC_VALUE_MARKER;
111 m_slotBase = slotBase;
114 m_cachedPropertyType = Value;
117 void setValue(JSValue value)
122 m_getValue = JSC_VALUE_MARKER;
126 void setCustom(JSValue slotBase, GetValueFunc getValue)
130 m_getValue = getValue;
132 m_slotBase = slotBase;
135 void setCacheableCustom(JSValue slotBase, GetValueFunc getValue)
139 m_getValue = getValue;
141 m_slotBase = slotBase;
142 m_cachedPropertyType = Custom;
145 void setCustomIndex(JSValue slotBase, unsigned index, GetIndexValueFunc getIndexValue)
148 ASSERT(getIndexValue);
149 m_getValue = INDEX_GETTER_MARKER;
150 m_getIndexValue = getIndexValue;
151 m_slotBase = slotBase;
152 m_data.index = index;
155 void setGetterSlot(JSObject* getterFunc)
158 m_thisValue = m_slotBase;
159 m_getValue = GETTER_FUNCTION_MARKER;
160 m_data.getterFunc = getterFunc;
163 void setCacheableGetterSlot(JSValue slotBase, JSObject* getterFunc, unsigned offset)
166 m_getValue = GETTER_FUNCTION_MARKER;
167 m_thisValue = m_slotBase;
168 m_slotBase = slotBase;
169 m_data.getterFunc = getterFunc;
171 m_cachedPropertyType = Getter;
176 setValue(jsUndefined());
179 JSValue slotBase() const
184 void setBase(JSValue base)
194 m_slotBase = JSValue();
207 // Clear offset even in release builds, in case this PropertySlot has been used before.
208 // (For other data members, we don't need to clear anything because reuse would meaningfully overwrite them.)
210 m_cachedPropertyType = Uncacheable;
213 unsigned index() const { return m_data.index; }
215 GetValueFunc customGetter() const
217 ASSERT(m_cachedPropertyType == Custom);
221 JSValue functionGetter(ExecState*) const;
223 GetValueFunc m_getValue;
224 GetIndexValueFunc m_getIndexValue;
228 JSObject* getterFunc;
236 CachedPropertyType m_cachedPropertyType;
241 #endif // PropertySlot_h