1
// -*- c-basic-offset: 2 -*-
3
* This file is part of the KDE libraries
4
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5
* Copyright (C) 2003 Apple Computer, Inc.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
#include "interpreter.h"
27
#include "identifier.h"
33
class FunctionPrototype;
36
* An entry in a hash table.
40
* s is the key (e.g. a property name)
45
* value is the result value (usually an enum value)
49
* attr is a set for flags (e.g. the property flags, see object.h)
53
* params is another number. For property hashtables, it is used to
54
* denote the number of argument of the function
58
* next is the pointer to the next entry for the same hash value
60
const HashEntry* next;
65
* Usually the hashtable is generated by the create_hash_table script, from a .table file.
67
* The implementation uses an array of entries, "size" is the total size of that array.
68
* The entries between 0 and hashSize-1 are the entry points
69
* for each hash value, and the entries between hashSize and size-1
70
* are the overflow entries for the hash values that need one.
71
* The "next" pointer of the entry links entry points to overflow entries,
72
* and links overflow entries between them.
76
* type is a version number. Currently always 2
80
* size is the total number of entries in the hashtable, including the null entries,
81
* i.e. the size of the "entries" array.
82
* Used to iterate over all entries in the table
86
* pointer to the array of entries
87
* Mind that some entries in the array are null (0,0,0,0).
89
const HashEntry* entries;
91
* the maximum value for the hash. Always smaller than size.
97
* @short Fast keyword lookup.
102
* Find an entry in the table, and return its value (i.e. the value field of HashEntry)
104
static int find(const struct HashTable*, const Identifier&);
105
static int find(const struct HashTable*, const UChar*, unsigned int len);
109
* Find an entry in the table, and return the entry
110
* This variant gives access to the other attributes of the entry,
111
* especially the attr field.
113
static const HashEntry* findEntry(const struct HashTable*, const Identifier&);
121
* Helper for getStaticFunctionSlot and getStaticPropertySlot
123
template <class FuncImp>
124
inline JSValue* staticFunctionGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot)
126
// Look for cached value in dynamic map of properties (in JSObject)
127
JSObject* thisObj = slot.slotBase();
128
JSValue* cachedVal = thisObj->getDirect(propertyName);
132
const HashEntry* entry = slot.staticEntry();
133
JSValue* val = new FuncImp(exec, entry->value, entry->params, propertyName);
134
thisObj->putDirect(propertyName, val, entry->attr);
140
* Helper for getStaticValueSlot and getStaticPropertySlot
142
template <class ThisImp>
143
inline JSValue* staticValueGetter(ExecState* exec, JSObject*, const Identifier&, const PropertySlot& slot)
145
ThisImp* thisObj = static_cast<ThisImp*>(slot.slotBase());
146
const HashEntry* entry = slot.staticEntry();
147
return thisObj->getValueProperty(exec, entry->value);
151
* Helper method for property lookups
153
* This method does it all (looking in the hashtable, checking for function
154
* overrides, creating the function or retrieving from cache, calling
155
* getValueProperty in case of a non-function property, forwarding to parent if
158
* Template arguments:
159
* @param FuncImp the class which implements this object's functions
160
* @param ThisImp the class of "this". It must implement the getValueProperty(exec,token) method,
161
* for non-function properties.
162
* @param ParentImp the class of the parent, to propagate the lookup.
165
* @param exec execution state, as usual
166
* @param propertyName the property we're looking for
167
* @param table the static hashtable for this class
168
* @param thisObj "this"
170
template <class FuncImp, class ThisImp, class ParentImp>
171
inline bool getStaticPropertySlot(ExecState* exec, const HashTable* table,
172
ThisImp* thisObj, const Identifier& propertyName, PropertySlot& slot)
174
const HashEntry* entry = Lookup::findEntry(table, propertyName);
176
if (!entry) // not found, forward to parent
177
return thisObj->ParentImp::getOwnPropertySlot(exec, propertyName, slot);
179
if (entry->attr & Function)
180
slot.setStaticEntry(thisObj, entry, staticFunctionGetter<FuncImp>);
182
slot.setStaticEntry(thisObj, entry, staticValueGetter<ThisImp>);
188
* Simplified version of getStaticPropertySlot in case there are only functions.
189
* Using this instead of getStaticPropertySlot allows 'this' to avoid implementing
190
* a dummy getValueProperty.
192
template <class FuncImp, class ParentImp>
193
inline bool getStaticFunctionSlot(ExecState* exec, const HashTable* table,
194
JSObject* thisObj, const Identifier& propertyName, PropertySlot& slot)
196
const HashEntry* entry = Lookup::findEntry(table, propertyName);
198
if (!entry) // not found, forward to parent
199
return static_cast<ParentImp*>(thisObj)->ParentImp::getOwnPropertySlot(exec, propertyName, slot);
201
assert(entry->attr & Function);
203
slot.setStaticEntry(thisObj, entry, staticFunctionGetter<FuncImp>);
208
* Simplified version of getStaticPropertySlot in case there are no functions, only "values".
209
* Using this instead of getStaticPropertySlot removes the need for a FuncImp class.
211
template <class ThisImp, class ParentImp>
212
inline bool getStaticValueSlot(ExecState* exec, const HashTable* table,
213
ThisImp* thisObj, const Identifier& propertyName, PropertySlot& slot)
215
const HashEntry* entry = Lookup::findEntry(table, propertyName);
217
if (!entry) // not found, forward to parent
218
return thisObj->ParentImp::getOwnPropertySlot(exec, propertyName, slot);
220
assert(!(entry->attr & Function));
222
slot.setStaticEntry(thisObj, entry, staticValueGetter<ThisImp>);
227
* This one is for "put".
228
* It looks up a hash entry for the property to be set. If an entry
229
* is found it sets the value and returns true, else it returns false.
231
template <class ThisImp>
232
inline bool lookupPut(ExecState* exec, const Identifier& propertyName,
233
JSValue* value, int attr,
234
const HashTable* table, ThisImp* thisObj)
236
const HashEntry* entry = Lookup::findEntry(table, propertyName);
241
if (entry->attr & Function) // function: put as override property
242
thisObj->JSObject::put(exec, propertyName, value, attr);
243
else if (entry->attr & ReadOnly) // readonly! Can't put!
245
fprintf(stderr,"WARNING: Attempt to change value of readonly property '%s'\n",propertyName.ascii());
250
thisObj->putValueProperty(exec, entry->value, value, attr);
256
* This one is for "put".
257
* It calls lookupPut<ThisImp>() to set the value. If that call
258
* returns false (meaning no entry in the hash table was found),
259
* then it calls put() on the ParentImp class.
261
template <class ThisImp, class ParentImp>
262
inline void lookupPut(ExecState* exec, const Identifier& propertyName,
263
JSValue* value, int attr,
264
const HashTable* table, ThisImp* thisObj)
266
if (!lookupPut<ThisImp>(exec, propertyName, value, attr, table, thisObj))
267
thisObj->ParentImp::put(exec, propertyName, value, attr); // not found: forward to parent
271
* This template method retrieves or create an object that is unique
272
* (for a given interpreter) The first time this is called (for a given
273
* property name), the Object will be constructed, and set as a property
274
* of the interpreter's global object. Later calls will simply retrieve
275
* that cached object. Note that the object constructor must take 1 argument, exec.
277
template <class ClassCtor>
278
inline JSObject* cacheGlobalObject(ExecState* exec, const Identifier& propertyName)
280
JSObject* globalObject = static_cast<JSObject*>(exec->lexicalInterpreter()->globalObject());
281
JSValue* obj = globalObject->getDirect(propertyName);
283
assert(obj->isObject());
284
return static_cast<JSObject* >(obj);
286
JSObject* newObject = new ClassCtor(exec);
287
globalObject->put(exec, propertyName, newObject, Internal | DontEnum);
294
* Helpers to define prototype objects (each of which simply implements
295
* the functions for a type of objects).
296
* Sorry for this not being very readable, but it actually saves much copy-n-paste.
297
* ParentPrototype is not our base class, it's the object we use as fallback.
298
* The reason for this is that there should only be ONE DOMNode.hasAttributes (e.g.),
299
* not one in each derived class. So we link the (unique) prototypes between them.
301
* Using those macros is very simple: define the hashtable (e.g. "DOMNodePrototypeTable"), then
302
* KJS_DEFINE_PROTOTYPE(DOMNodePrototype)
303
* KJS_IMPLEMENT_PROTOFUNC(DOMNodePrototypeFunction)
304
* KJS_IMPLEMENT_PROTOTYPE("DOMNode", DOMNodePrototype, DOMNodePrototypeFunction)
305
* and use DOMNodePrototype::self(exec) as prototype in the DOMNode constructor.
306
* If the prototype has a "parent prototype", e.g. DOMElementPrototype falls back on DOMNodePrototype,
307
* then the first line will use KJS_DEFINE_PROTOTYPE_WITH_PROTOTYPE, with DOMNodePrototype as the second argument.
310
// These macros assume that a prototype's only properties are functions
311
#define KJS_DEFINE_PROTOTYPE(ClassPrototype) \
312
class ClassPrototype : public KJS::JSObject { \
314
static KJS::JSObject* self(KJS::ExecState* exec); \
315
virtual const KJS::ClassInfo* classInfo() const { return &info; } \
316
static const KJS::ClassInfo info; \
317
bool getOwnPropertySlot(KJS::ExecState* , const KJS::Identifier&, KJS::PropertySlot&); \
318
ClassPrototype(KJS::ExecState* exec) \
319
: KJS::JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()) { } \
323
#define KJS_DEFINE_PROTOTYPE_WITH_PROTOTYPE(ClassPrototype, ClassPrototypePrototype) \
324
class ClassPrototype : public KJS::JSObject { \
326
static KJS::JSObject* self(KJS::ExecState* exec); \
327
virtual const KJS::ClassInfo* classInfo() const { return &info; } \
328
static const KJS::ClassInfo info; \
329
bool getOwnPropertySlot(KJS::ExecState*, const KJS::Identifier&, KJS::PropertySlot&); \
330
ClassPrototype(KJS::ExecState* exec) \
331
: KJS::JSObject(ClassPrototypePrototype::self(exec)) { } \
335
#define KJS_IMPLEMENT_PROTOTYPE(ClassName, ClassPrototype, ClassFunction) \
336
const ClassInfo ClassPrototype::info = { ClassName"Prototype", 0, &ClassPrototype##Table, 0 }; \
337
JSObject* ClassPrototype::self(ExecState* exec) \
339
return KJS::cacheGlobalObject<ClassPrototype>(exec, "[[" ClassName ".prototype]]"); \
341
bool ClassPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) \
343
return getStaticFunctionSlot<ClassFunction, JSObject>(exec, &ClassPrototype##Table, this, propertyName, slot); \
346
#define KJS_IMPLEMENT_PROTOTYPE_FUNCTION(ClassFunction) \
347
class ClassFunction : public InternalFunctionImp { \
349
ClassFunction(ExecState* exec, int i, int len, const Identifier& name) \
350
: InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name) \
353
put(exec, exec->propertyNames().length, jsNumber(len), DontDelete|ReadOnly|DontEnum); \
355
/* Macro user needs to implement the callAsFunction function. */ \
356
virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List& args); \