1 | // -*- c-basic-offset: 4 -*- |
2 | /* |
3 | * This file is part of the KDE libraries |
4 | * Copyright (C) 2005 Apple Computer, Inc. |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public License |
17 | * along with this library; see the file COPYING.LIB. If not, write to |
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | * |
21 | */ |
22 | |
23 | #ifndef KJS_PROPERTY_SLOT_H |
24 | #define KJS_PROPERTY_SLOT_H |
25 | |
26 | #include <assert.h> |
27 | #include "identifier.h" |
28 | #include "value.h" |
29 | |
30 | namespace KJS { |
31 | |
32 | struct HashEntry; |
33 | class ExecState; |
34 | class JSObject; |
35 | |
36 | class KJS_EXPORT PropertySlot |
37 | { |
38 | public: |
39 | typedef JSValue *(*GetValueFunc)(ExecState *, JSObject *originalObject, const Identifier&, const PropertySlot&); |
40 | typedef JSValue *(*GetValueNumberFunc)(ExecState *, JSObject *originalObject, unsigned, const PropertySlot&); |
41 | |
42 | JSValue *getValue(ExecState *exec, JSObject *originalObject, const Identifier& propertyName) const |
43 | { |
44 | switch (m_getType) { |
45 | case ValueType: |
46 | return m_data.jsValue; |
47 | case ValueSlotType: |
48 | return *m_data.valueSlot; |
49 | case StringFunction: |
50 | default: |
51 | return m_func.m_getValue(exec, originalObject, propertyName, *this); |
52 | } |
53 | } |
54 | |
55 | JSValue *getValue(ExecState *exec, JSObject *originalObject, unsigned propertyName) const |
56 | { |
57 | switch (m_getType) { |
58 | case ValueType: |
59 | return m_data.jsValue; |
60 | case ValueSlotType: |
61 | return *m_data.valueSlot; |
62 | case NumberFunction: |
63 | return m_func.m_getValueNumber(exec, originalObject, propertyName, *this); |
64 | case StringFunction: |
65 | default: |
66 | return m_func.m_getValue(exec, originalObject, Identifier::from(propertyName), *this); |
67 | } |
68 | } |
69 | |
70 | void setValueSlot(JSObject *slotBase, JSValue **valueSlot) |
71 | { |
72 | m_slotBase = slotBase; |
73 | m_data.valueSlot = valueSlot; |
74 | m_getType = ValueSlotType; |
75 | } |
76 | |
77 | void setValue(JSObject *slotBase, JSValue *value) |
78 | { |
79 | m_slotBase = slotBase; |
80 | m_data.jsValue = value; |
81 | m_getType = ValueType; |
82 | } |
83 | |
84 | void setStaticEntry(JSObject *slotBase, const HashEntry *staticEntry, GetValueFunc getValue) |
85 | { |
86 | assert(getValue); |
87 | m_slotBase = slotBase; |
88 | m_data.staticEntry = staticEntry; |
89 | m_func.m_getValue = getValue; |
90 | m_getType = StringFunction; |
91 | } |
92 | |
93 | void setCustom(JSObject *slotBase, GetValueFunc getValue) |
94 | { |
95 | assert(getValue); |
96 | m_slotBase = slotBase; |
97 | m_func.m_getValue = getValue; |
98 | m_getType = StringFunction; |
99 | } |
100 | |
101 | void setCustomIndex(JSObject *slotBase, unsigned index, GetValueFunc getValue) |
102 | { |
103 | assert(getValue); |
104 | m_slotBase = slotBase; |
105 | m_data.index = index; |
106 | m_func.m_getValue = getValue; |
107 | m_getType = StringFunction; |
108 | } |
109 | |
110 | void setCustomIndex(JSObject *slotBase, unsigned index, GetValueNumberFunc getValue) |
111 | { |
112 | assert(getValue); |
113 | m_slotBase = slotBase; |
114 | m_data.index = index; |
115 | m_func.m_getValueNumber = getValue; |
116 | m_getType = NumberFunction; |
117 | } |
118 | |
119 | void setCustomValue(JSObject *slotBase, void* value, GetValueFunc getValue) |
120 | { |
121 | assert(getValue); |
122 | m_slotBase = slotBase; |
123 | m_data.value = value; |
124 | m_func.m_getValue = getValue; |
125 | m_getType = StringFunction; |
126 | } |
127 | |
128 | void setGetterSlot(JSObject *slotBase, JSObject *getterFunc) |
129 | { |
130 | m_func.m_getValue = functionGetter; |
131 | m_slotBase = slotBase; |
132 | m_data.getterFunc = getterFunc; |
133 | m_getType = StringFunction; |
134 | } |
135 | |
136 | void setUndefined(JSObject *slotBase) |
137 | { |
138 | m_slotBase = slotBase; |
139 | m_func.m_getValue = undefinedGetter; |
140 | m_getType = StringFunction; |
141 | } |
142 | |
143 | JSObject *slotBase() const { return m_slotBase; } |
144 | |
145 | const HashEntry *staticEntry() const { return m_data.staticEntry; } |
146 | unsigned index() const { return m_data.index; } |
147 | void* customValue() const { return m_data.value; } |
148 | |
149 | private: |
150 | static JSValue *undefinedGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&); |
151 | static JSValue *functionGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&); |
152 | |
153 | union { |
154 | GetValueFunc m_getValue; |
155 | GetValueNumberFunc m_getValueNumber; |
156 | } m_func; |
157 | |
158 | JSObject *m_slotBase; |
159 | union { |
160 | JSObject *getterFunc; |
161 | JSValue **valueSlot; |
162 | const HashEntry *staticEntry; |
163 | unsigned index; |
164 | void* value; |
165 | JSValue* jsValue; |
166 | } m_data; |
167 | |
168 | enum GetType { |
169 | ValueType, |
170 | ValueSlotType, |
171 | StringFunction, |
172 | NumberFunction |
173 | }; |
174 | GetType m_getType; |
175 | }; |
176 | |
177 | } |
178 | |
179 | #endif // KJS_PROPERTY_SLOT_H |
180 | |