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