1 // -*- c-basic-offset: 2 -*-
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "ExecState.h"
27 #include "identifier.h"
28 #include "JSGlobalObject.h"
31 #include <wtf/Assertions.h>
36 * An entry in a hash table.
40 * s is the key (e.g. a property name)
45 * value is the result value (enum value for properties and a function pointer to a constructor factory for functions)
49 PrototypeFunction::JSMemberFunction functionValue
;
53 * attr is a set for flags (e.g. the property flags, see object.h)
57 * params is another number. For property hashtables, it is used to
58 * denote the number of argument of the function
62 * next is the pointer to the next entry for the same hash value
64 const HashEntry
* next
;
69 * Usually the hashtable is generated by the create_hash_table script, from a .table file.
71 * The implementation uses an array of entries, "size" is the total size of that array.
72 * The entries between 0 and hashSize-1 are the entry points
73 * for each hash value, and the entries between hashSize and size-1
74 * are the overflow entries for the hash values that need one.
75 * The "next" pointer of the entry links entry points to overflow entries,
76 * and links overflow entries between them.
80 * type is a version number. Currently always 2
84 * size is the total number of entries in the hashtable, including the null entries,
85 * i.e. the size of the "entries" array.
86 * Used to iterate over all entries in the table
90 * pointer to the array of entries
91 * Mind that some entries in the array are null (0,0,0,0).
93 const HashEntry
* entries
;
95 * the maximum value for the hash minus 1. Always smaller than size.
101 * @short Fast keyword lookup.
106 * Find an entry in the table, and return its value (i.e. the value field of HashEntry)
108 static int find(const struct HashTable
*, const Identifier
&);
109 static int find(const struct HashTable
*, const UChar
*, unsigned int len
);
112 * Find an entry in the table, and return the entry
113 * This variant gives access to the other attributes of the entry,
114 * especially the attr field.
116 static const HashEntry
* findEntry(const struct HashTable
*, const Identifier
&);
124 * Helper for getStaticFunctionSlot and getStaticPropertySlot
126 inline JSValue
* staticFunctionGetter(ExecState
* exec
, JSObject
*, const Identifier
& propertyName
, const PropertySlot
& slot
)
128 // Look for cached value in dynamic map of properties (in JSObject)
129 JSObject
* thisObj
= slot
.slotBase();
130 JSValue
* cachedVal
= thisObj
->getDirect(propertyName
);
134 const HashEntry
* entry
= slot
.staticEntry();
135 JSValue
* val
= new PrototypeFunction(exec
, entry
->params
, propertyName
, entry
->value
.functionValue
);
136 thisObj
->putDirect(propertyName
, val
, entry
->attr
);
142 * Helper for getStaticValueSlot and getStaticPropertySlot
144 template <class ThisImp
>
145 inline JSValue
* staticValueGetter(ExecState
* exec
, JSObject
*, const Identifier
&, const PropertySlot
& slot
)
147 ThisImp
* thisObj
= static_cast<ThisImp
*>(slot
.slotBase());
148 const HashEntry
* entry
= slot
.staticEntry();
149 return thisObj
->getValueProperty(exec
, entry
->value
.intValue
);
153 * Helper method for property lookups
155 * This method does it all (looking in the hashtable, checking for function
156 * overrides, creating the function or retrieving from cache, calling
157 * getValueProperty in case of a non-function property, forwarding to parent if
160 * Template arguments:
161 * @param FuncImp the class which implements this object's functions
162 * @param ThisImp the class of "this". It must implement the getValueProperty(exec,token) method,
163 * for non-function properties.
164 * @param ParentImp the class of the parent, to propagate the lookup.
167 * @param exec execution state, as usual
168 * @param propertyName the property we're looking for
169 * @param table the static hashtable for this class
170 * @param thisObj "this"
172 template <class ThisImp
, class ParentImp
>
173 inline bool getStaticPropertySlot(ExecState
* exec
, const HashTable
* table
,
174 ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
176 const HashEntry
* entry
= Lookup::findEntry(table
, propertyName
);
178 if (!entry
) // not found, forward to parent
179 return thisObj
->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
);
181 if (entry
->attr
& Function
)
182 slot
.setStaticEntry(thisObj
, entry
, staticFunctionGetter
);
184 slot
.setStaticEntry(thisObj
, entry
, staticValueGetter
<ThisImp
>);
190 * Simplified version of getStaticPropertySlot in case there are only functions.
191 * Using this instead of getStaticPropertySlot allows 'this' to avoid implementing
192 * a dummy getValueProperty.
194 template <class ParentImp
>
195 inline bool getStaticFunctionSlot(ExecState
* exec
, const HashTable
* table
,
196 JSObject
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
198 const HashEntry
* entry
= Lookup::findEntry(table
, propertyName
);
200 if (!entry
) // not found, forward to parent
201 return static_cast<ParentImp
*>(thisObj
)->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
);
203 ASSERT(entry
->attr
& Function
);
205 slot
.setStaticEntry(thisObj
, entry
, staticFunctionGetter
);
210 * Simplified version of getStaticPropertySlot in case there are no functions, only "values".
211 * Using this instead of getStaticPropertySlot removes the need for a FuncImp class.
213 template <class ThisImp
, class ParentImp
>
214 inline bool getStaticValueSlot(ExecState
* exec
, const HashTable
* table
,
215 ThisImp
* thisObj
, const Identifier
& propertyName
, PropertySlot
& slot
)
217 const HashEntry
* entry
= Lookup::findEntry(table
, propertyName
);
219 if (!entry
) // not found, forward to parent
220 return thisObj
->ParentImp::getOwnPropertySlot(exec
, propertyName
, slot
);
222 ASSERT(!(entry
->attr
& Function
));
224 slot
.setStaticEntry(thisObj
, entry
, staticValueGetter
<ThisImp
>);
229 * This one is for "put".
230 * It looks up a hash entry for the property to be set. If an entry
231 * is found it sets the value and returns true, else it returns false.
233 template <class ThisImp
>
234 inline bool lookupPut(ExecState
* exec
, const Identifier
& propertyName
,
235 JSValue
* value
, int attr
,
236 const HashTable
* table
, ThisImp
* thisObj
)
238 const HashEntry
* entry
= Lookup::findEntry(table
, propertyName
);
243 if (entry
->attr
& Function
) // function: put as override property
244 thisObj
->JSObject::put(exec
, propertyName
, value
, attr
);
245 else if (!(entry
->attr
& ReadOnly
))
246 thisObj
->putValueProperty(exec
, entry
->value
.intValue
, value
, attr
);
252 * This one is for "put".
253 * It calls lookupPut<ThisImp>() to set the value. If that call
254 * returns false (meaning no entry in the hash table was found),
255 * then it calls put() on the ParentImp class.
257 template <class ThisImp
, class ParentImp
>
258 inline void lookupPut(ExecState
* exec
, const Identifier
& propertyName
,
259 JSValue
* value
, int attr
,
260 const HashTable
* table
, ThisImp
* thisObj
)
262 if (!lookupPut
<ThisImp
>(exec
, propertyName
, value
, attr
, table
, thisObj
))
263 thisObj
->ParentImp::put(exec
, propertyName
, value
, attr
); // not found: forward to parent
267 * This template method retrieves or create an object that is unique
268 * (for a given global object) The first time this is called (for a given
269 * property name), the Object will be constructed, and set as a property
270 * of the global object. Later calls will simply retrieve that cached object.
271 * Note that the object constructor must take 1 argument, exec.
273 template <class ClassCtor
>
274 inline JSObject
* cacheGlobalObject(ExecState
* exec
, const Identifier
& propertyName
)
276 JSGlobalObject
* globalObject
= exec
->lexicalGlobalObject();
277 JSValue
* obj
= globalObject
->getDirect(propertyName
);
279 ASSERT(obj
->isObject());
280 return static_cast<JSObject
* >(obj
);
282 JSObject
* newObject
= new ClassCtor(exec
);
283 globalObject
->putDirect(propertyName
, newObject
, Internal
| DontEnum
);
290 * Helpers to define prototype objects (each of which simply implements
291 * the functions for a type of objects).
292 * Sorry for this not being very readable, but it actually saves much copy-n-paste.
293 * ParentPrototype is not our base class, it's the object we use as fallback.
294 * The reason for this is that there should only be ONE DOMNode.hasAttributes (e.g.),
295 * not one in each derived class. So we link the (unique) prototypes between them.
297 * Using those macros is very simple: define the hashtable (e.g. "DOMNodePrototypeTable"), then
298 * KJS_DEFINE_PROTOTYPE(DOMNodePrototype)
299 * KJS_IMPLEMENT_PROTOTYPE("DOMNode", DOMNodePrototype, DOMNodePrototypeFunction)
300 * and use DOMNodePrototype::self(exec) as prototype in the DOMNode constructor.
301 * If the prototype has a "parent prototype", e.g. DOMElementPrototype falls back on DOMNodePrototype,
302 * then the first line will use KJS_DEFINE_PROTOTYPE_WITH_PROTOTYPE, with DOMNodePrototype as the second argument.
305 // These macros assume that a prototype's only properties are functions
306 #define KJS_DEFINE_PROTOTYPE(ClassPrototype) \
307 class ClassPrototype : public KJS::JSObject { \
309 static KJS::JSObject* self(KJS::ExecState* exec); \
310 virtual const KJS::ClassInfo* classInfo() const { return &info; } \
311 static const KJS::ClassInfo info; \
312 bool getOwnPropertySlot(KJS::ExecState* , const KJS::Identifier&, KJS::PropertySlot&); \
313 ClassPrototype(KJS::ExecState* exec) \
314 : KJS::JSObject(exec->lexicalGlobalObject()->objectPrototype()) { } \
318 #define KJS_DEFINE_PROTOTYPE_WITH_PROTOTYPE(ClassPrototype, ClassPrototypePrototype) \
319 class ClassPrototype : public KJS::JSObject { \
321 static KJS::JSObject* self(KJS::ExecState* exec); \
322 virtual const KJS::ClassInfo* classInfo() const { return &info; } \
323 static const KJS::ClassInfo info; \
324 bool getOwnPropertySlot(KJS::ExecState*, const KJS::Identifier&, KJS::PropertySlot&); \
325 ClassPrototype(KJS::ExecState* exec) \
326 : KJS::JSObject(ClassPrototypePrototype::self(exec)) { } \
330 #define KJS_IMPLEMENT_PROTOTYPE(ClassName, ClassPrototype) \
331 const ClassInfo ClassPrototype::info = { ClassName"Prototype", 0, &ClassPrototype##Table }; \
332 JSObject* ClassPrototype::self(ExecState* exec) \
334 static Identifier* prototypeIdentifier = new Identifier("[[" ClassName ".prototype]]"); \
335 return KJS::cacheGlobalObject<ClassPrototype>(exec, *prototypeIdentifier); \
337 bool ClassPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) \
339 return getStaticFunctionSlot<JSObject>(exec, &ClassPrototype##Table, this, propertyName, slot); \
342 #endif // KJS_lookup_h