2 Copyright (C) 2004 Nikolas Zimmermann <wildfox@kde.org>
3 2004 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 #ifndef KDOM_DOMLookup_H
24 #define KDOM_DOMLookup_H
27 #include <kjs/value.h>
28 #include <kjs/object.h>
29 #include <kjs/lookup.h>
30 #include <kjs/interpreter.h> // for ExecState
32 #include <kdom/ecma/ScriptInterpreter.h>
34 // NOTE: Those macros are probably very hard to understand first,
35 // so don't hesitate to contact us - whenever someone plans
36 // to reuse this code - except for KSVG :)
38 #define KDOM_GET_COMMON \
41 /* The standard hasProperty call, auto-generated. Looks in hashtable, forwards to parents. */ \
42 bool hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
44 /* get() method, called by DOMBridge::get */ \
45 KJS::Value get(KJS::ExecState *exec, const KJS::Identifier &propertyName, const KJS::ObjectImp* bridge) const; \
47 /* Called by lookupGet(). Auto-generated. Forwards to the parent which has the given property. */ \
48 KJS::Value getInParents(KJS::ExecState *exec, const KJS::Identifier &propertyName, const KJS::ObjectImp* bridge) const; \
50 KJS::Object prototype(KJS::ExecState *exec) const;\
52 KJS::ObjectImp *bridge(KJS::ExecState *) const; \
54 static const KJS::ClassInfo s_classInfo; \
56 static const struct KJS::HashTable s_hashTable;
59 * For classes with properties to read, and a hashtable.
63 KJS::Value cache(KJS::ExecState *exec) const;
66 * Same as KDOM_GET, but for base classes(kalyptus helps finding them). The
67 * difference is that cache() is virtual.
69 #define KDOM_BASECLASS_GET \
71 virtual KJS::Value cache(KJS::ExecState *exec) const;
74 * For read-write classes only, i.e. those which support put().
78 /* put() method, called by DOMBridge::put */ \
79 bool put(KJS::ExecState *exec, const KJS::Identifier &propertyName, \
80 const KJS::Value &value, int attr); \
82 /* Called by lookupPut. Auto-generated. Looks in hashtable, forwards to parents. */ \
83 bool putInParents(KJS::ExecState *exec, const KJS::Identifier &propertyName, \
84 const KJS::Value &value, int attr);
87 * For classes which inherit a read-write class,
88 * but have no read-write property themselves.
90 #define KDOM_FORWARDPUT \
92 /* put() method, called by DOMBridge::put */ \
93 bool put(KJS::ExecState *exec, const KJS::Identifier &propertyName, const KJS::Value &value, int attr);
96 * Used in generatedata.cpp
99 * @param p2 propertyName
103 #define GET_METHOD_ARGS KJS::ExecState *p1, const KJS::Identifier &p2, const KJS::ObjectImp *p3
106 * Used in generated.cpp
109 * @param p2 propertyName
114 #define PUT_METHOD_ARGS KJS::ExecState *p1, const KJS::Identifier &p2, const KJS::Value &p3, int p4
119 * Helper method for property lookups.
121 * This method does it all (looking in the hashtable, checking for function
122 * overrides, creating the function or retrieving from cache, calling
123 * getValueProperty in case of a non-function property, forwarding to parent[s] if
126 * Template arguments:
127 * @param FuncImp the class which implements this object's functions
128 * @param ThisImp the class of "this". It must implement the getValueProperty(exec,token) method,
129 * for non-function properties, and the getInParents() method (auto-generated).
132 * @param exec execution state, as usual
133 * @param propertyName the property we're looking for
134 * @param table the static hashtable for this class
135 * @param thisObj "this"
137 template<class FuncImp, class ThisImp>
138 inline KJS::Value lookupGet(KJS::ExecState *exec,
139 const KJS::Identifier &propertyName,
140 const KJS::HashTable *table,
141 const ThisImp *thisObj, // the 'impl' object
142 const KJS::ObjectImp *bridge)
144 const KJS::HashEntry *entry = KJS::Lookup::findEntry(table, propertyName);
146 if(!entry) // not found, forward to parents
147 return thisObj->getInParents(exec, propertyName, bridge);
149 if(entry->attr & KJS::Function)
150 return KJS::lookupOrCreateFunction<FuncImp>(exec, propertyName,
151 const_cast<KJS::ObjectImp *>(bridge),
152 entry->value, entry->params, entry->attr);
154 return thisObj->getValueProperty(exec, entry->value);
158 * Simplified version of lookupGet in case there are no functions, only "values".
159 * Using this instead of lookupGet removes the need for a FuncImp class.
161 template <class ThisImp>
162 inline KJS::Value lookupGetValue(KJS::ExecState *exec,
163 const KJS::Identifier &propertyName,
164 const KJS::HashTable *table,
165 const ThisImp *thisObj, // the 'impl' object
166 const KJS::ObjectImp *bridge)
168 const KJS::HashEntry *entry = KJS::Lookup::findEntry(table, propertyName);
170 if(!entry) // not found, forward to parents
171 return thisObj->getInParents(exec, propertyName, bridge);
173 if(entry->attr & KJS::Function)
174 kdError(26004) << "Function bit set! Shouldn't happen in lookupGetValue! propertyName was " << propertyName.qstring() << endl;
176 return thisObj->getValueProperty(exec, entry->value);
180 * This one is for "put".
181 * Lookup hash entry for property to be set, and set the value.
182 * The "this" class must implement putValueProperty.
183 * If it returns false, put() will return false, and KDOMRequest will set a dynamic property in ObjectImp
185 template <class ThisImp>
186 inline bool lookupPut(KJS::ExecState *exec,
187 const KJS::Identifier &propertyName,
188 const KJS::Value &value,
190 const KJS::HashTable *table,
193 const KJS::HashEntry *entry = KJS::Lookup::findEntry(table, propertyName);
195 if(!entry) // not found, forward to parents
196 return thisObj->putInParents(exec, propertyName, value, attr);
197 else if(entry->attr & KJS::Function) // Function: put as override property
199 else if(entry->attr & KJS::ReadOnly && !(attr & KJS::Internal)) // readonly! Can't put!
202 kdWarning(26004) <<" Attempt to change value of readonly property '" << propertyName.qstring() << "'" << endl;
204 return true; // "we did it" -> don't put override property
208 thisObj->putValueProperty(exec, entry->value, value, attr);
214 // Useful macros for dealing with ecma constructors (enum types, for example)
215 #define ECMA_IMPLEMENT_CONSTRUCTOR(Space, ClassName, Class) \
216 KJS::Value ClassName::get(KJS::ExecState *, const KJS::Identifier &propertyName, const KJS::ObjectImp *) const { \
217 const KJS::HashEntry *entry = KJS::Lookup::findEntry(&s_hashTable, propertyName); \
219 return KJS::Number(entry->value); \
220 return KJS::Undefined(); \
222 bool ClassName::hasProperty(KJS::ExecState *, const KJS::Identifier &propertyName) const { \
223 return KJS::Lookup::findEntry(&s_hashTable, propertyName); \
225 KJS::Object ClassName::prototype(KJS::ExecState *exec) const { \
226 if(exec) return exec->interpreter()->builtinObjectPrototype(); \
227 return KJS::Object::dynamicCast(KJS::Null()); \
229 const KJS::ClassInfo ClassName::s_classInfo = { Class "Constructor", 0, &s_hashTable, 0 }; \
230 KJS::Value Space::get##ClassName(KJS::ExecState *exec) { \
231 return KDOM::cacheGlobalBridge<ClassName>(exec, "[[" Class ".constructor]]"); \
234 // Just defines the constructor prototype (which will be called by the GlobalObject)
235 #define ECMA_DEFINE_CONSTRUCTOR(Space, ClassName) \
237 class ClassName : public KJS::ObjectImp { \
239 ClassName(KJS::ExecState *exec) : KJS::ObjectImp() {} \
240 KJS::Value get(KJS::ExecState *exec, const KJS::Identifier &propertyName, const KJS::ObjectImp *obj) const; \
241 bool hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
242 KJS::Object prototype(KJS::ExecState *exec) const; \
243 static const KJS::ClassInfo s_classInfo; \
244 static const struct KJS::HashTable s_hashTable; \
246 KJS::Value get##ClassName(KJS::ExecState *exec); \
249 // Same as kjs' DEFINE_PROTOTYPE, but with a pointer to the hashtable too, and no ClassName here
250 // The ClassProto ctor(exec) must be public, so we can use KJS::cacheGlobalObject... (Niko)
251 #define ECMA_DEFINE_PROTOTYPE(Space, ClassProto) \
253 class ClassProto : public KJS::ObjectImp { \
255 static KJS::Object self(KJS::ExecState *exec); \
256 ClassProto( KJS::ExecState *exec ) \
257 : KJS::ObjectImp( exec->interpreter()->builtinObjectPrototype() ) {} \
258 virtual const KJS::ClassInfo *classInfo() const { return &info; } \
259 static const KJS::ClassInfo info; \
260 KJS::Value get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
261 bool hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
263 static const struct KJS::HashTable s_hashTable; \
267 // same as IMPLEMENT_PROTOTYPE but in the KDOM namespace, and with ClassName here
268 // so that KDOM_DEFINE_PROTOTYPE can be put in a header file ('info' defined here)
269 #define ECMA_IMPLEMENT_PROTOTYPE(Space,ClassName,ClassProto,ClassFunc) \
270 KJS::Value Space::ClassProto::get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
273 if (KJS::lookupGetOwnFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &s_hashTable, this, result)) \
275 return KJS::Undefined(); \
276 /* return KJS::lookupGetFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &s_hashTable, this ); */ \
278 bool Space::ClassProto::hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
279 { /*stupid but we need this to have a common macro for the declaration*/ \
280 return KJS::ObjectImp::hasProperty(exec, propertyName); \
282 KJS::Object Space::ClassProto::self(KJS::ExecState *exec) \
284 return KJS::cacheGlobalObject<ClassProto>( exec, "[[" ClassName ".prototype]]" ); \
286 const KJS::ClassInfo ClassProto::info = { ClassName, 0, &s_hashTable, 0 }; \
288 #define ECMA_IMPLEMENT_PROTOFUNC(Space,ClassFunc,Class) \
290 class ClassFunc : public KJS::ObjectImp { \
292 ClassFunc(KJS::ExecState *exec, int i, int len) \
293 : KJS::ObjectImp( /*proto? */ ), id(i) { \
294 KJS::Value protect(this); \
295 put(exec, "length", KJS::Number(len), KJS::DontDelete | KJS::ReadOnly | KJS::DontEnum); \
297 /** Used by call() to check the type of thisObj. Generated code */ \
298 Class cast(KJS::ExecState *exec, const KJS::ObjectImp *bridge) const; \
300 virtual bool implementsCall() const { return true; } \
302 /** You need to implement that one */ \
303 virtual KJS::Value call(KJS::ExecState *exec, KJS::Object &thisObj, const KJS::List &args); \
308 /* Is eventually generated, if KDOM_CAST is given */ \
309 Class to##Class(KJS::ExecState *exec, const KJS::ObjectImp *bridge); \
312 // To be used when casting the type of an argument
313 #define KDOM_CHECK(ClassName, theObj) \
314 ClassName obj = cast(exec, static_cast<KJS::ObjectImp *>(theObj.imp())); \
315 if(obj == ClassName::null) { \
316 kdDebug(26004) << k_funcinfo << " Wrong object type: expected " << ClassName::s_classInfo.className << " got " << thisObj.classInfo()->className << endl; \
317 Object err = Error::create(exec,TypeError); \
318 exec->setException(err); \
322 // To be used in all call() implementations!
323 // Can't use if (!thisObj.inherits(&ClassName::s_classInfo) since we don't
324 // use the (single-parent) inheritance of ClassInfo...
325 #define KDOM_CHECK_THIS(ClassName) KDOM_CHECK(ClassName, thisObj)
327 // Helpers to hide exception stuff...
328 // used within get/putValueProprety
329 #define KDOM_ENTER_SAFE try {
330 #define KDOM_LEAVE_SAFE(Exception) } catch(Exception::Private *e) { KJS::Object err = KJS::Error::create(exec, KJS::GeneralError, QString::fromLatin1("%1 %2").arg(QString::fromLatin1(Exception(e).s_classInfo.className)).arg(QString::number(e->code())).latin1()); err.put(exec, "code", KJS::Number(e->code())); exec->setException(err); }
331 #define KDOM_LEAVE_CALL_SAFE(Exception) } catch(Exception::Private *e) { KJS::Object err = KJS::Error::create(exec, KJS::GeneralError, QString::fromLatin1("%1 %2").arg(QString::fromLatin1(Exception(e).s_classInfo.className)).arg(QString::number(e->code())).latin1()); err.put(exec, "code", KJS::Number(e->code())); exec->setException(err); return err; }
333 // Just a marker for kalyptus
335 #define KDOM_DEFINE_CAST(Class) Class to##Class(KJS::ExecState *exec, const KJS::ObjectImp *bridge);
337 // KDOM Internal helpers - convenience functions
338 #define KDOM_DEFINE_CONSTRUCTOR(ClassName) ECMA_DEFINE_CONSTRUCTOR(KDOM, ClassName)
339 #define KDOM_IMPLEMENT_CONSTRUCTOR(ClassName, Class) ECMA_IMPLEMENT_CONSTRUCTOR(KDOM, ClassName, Class)
341 #define KDOM_DEFINE_PROTOTYPE(ClassName) ECMA_DEFINE_PROTOTYPE(KDOM, ClassName)
342 #define KDOM_IMPLEMENT_PROTOFUNC(ClassFunc, Class) ECMA_IMPLEMENT_PROTOFUNC(KDOM, ClassFunc, Class)
344 #define KDOM_IMPLEMENT_PROTOTYPE(ClassName,ClassProto,ClassFunc) ECMA_IMPLEMENT_PROTOTYPE(KDOM, ClassName, ClassProto, ClassFunc)