• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
lookup.h
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003 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 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.
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 * Lesser General Public License for more details.
15 *
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
19 *
20 */
21
22#ifndef _KJSLOOKUP_H_
23#define _KJSLOOKUP_H_
24
25#include "identifier.h"
26#include "value.h"
27#include "object.h"
28#include "interpreter.h"
29#include <stdio.h>
30
31namespace KJS {
32
36 struct HashEntry {
40 unsigned short soffset;
44 short int value;
48 unsigned char attr;
53 unsigned char params;
57 short next;
58 };
59
71 struct HashTable {
75 int type;
81 int size;
86 const HashEntry *const entries;
90 int hashSize;
91
95 const char* const sbase;
96 };
97
101 class KJS_EXPORT Lookup {
102 public:
106 static int find(const struct HashTable *table, const Identifier &s);
107 static int find(const struct HashTable *table,
108 const UChar *c, unsigned int len);
109
115 static const HashEntry* findEntry(const struct HashTable *table,
116 const Identifier &s);
117 static const HashEntry* findEntry(const struct HashTable *table,
118 const UChar *c, unsigned int len);
119
123 static unsigned int hash(const Identifier &key);
124 static unsigned int hash(const UChar *c, unsigned int len);
125 static unsigned int hash(const char *s);
126 };
127
128 class ExecState;
129 class UString;
134 template <class FuncImp>
135 inline Value lookupOrCreateFunction(ExecState *exec, const Identifier &propertyName,
136 const ObjectImp *thisObj, int token, int params, int attr)
137 {
138 // Look for cached value in dynamic map of properties (in ObjectImp)
139 ValueImp * cachedVal = thisObj->ObjectImp::getDirect(propertyName);
140 /*if (cachedVal)
141 fprintf(stderr, "lookupOrCreateFunction: Function -> looked up in ObjectImp, found type=%d\n", cachedVal->type());*/
142 if (cachedVal)
143 return Value(cachedVal);
144
145 ObjectImp* func = new FuncImp( exec, token, params );
146 Value val( func );
147 func->setFunctionName( propertyName );
148 ObjectImp *thatObj = const_cast<ObjectImp *>(thisObj);
149 thatObj->ObjectImp::put(exec, propertyName, val, attr);
150 return val;
151 }
152
173 template <class FuncImp, class ThisImp, class ParentImp>
174 inline Value lookupGet(ExecState *exec, const Identifier &propertyName,
175 const HashTable* table, const ThisImp* thisObj)
176 {
177 const HashEntry* entry = Lookup::findEntry(table, propertyName);
178
179 if (!entry) // not found, forward to parent
180 return thisObj->ParentImp::get(exec, propertyName);
181
182 //fprintf(stderr, "lookupGet: found value=%d attr=%d\n", entry->value, entry->attr);
183 if (entry->attr & Function)
184 return lookupOrCreateFunction<FuncImp>(exec, propertyName, thisObj, entry->value, entry->params, entry->attr);
185 return thisObj->getValueProperty(exec, entry->value);
186 }
187
192 template <class FuncImp, class ParentImp>
193 inline Value lookupGetFunction(ExecState *exec, const Identifier &propertyName,
194 const HashTable* table, const ObjectImp* thisObj)
195 {
196 const HashEntry* entry = Lookup::findEntry(table, propertyName);
197
198 if (!entry) // not found, forward to parent
199 return static_cast<const ParentImp *>(thisObj)->ParentImp::get(exec, propertyName);
200
201 if (entry->attr & Function)
202 return lookupOrCreateFunction<FuncImp>(exec, propertyName, thisObj, entry->value, entry->params, entry->attr);
203
204 fprintf(stderr, "Function bit not set! Shouldn't happen in lookupGetFunction!\n" );
205 return Undefined();
206 }
207
212 template <class ThisImp, class ParentImp>
213 inline Value lookupGetValue(ExecState *exec, const Identifier &propertyName,
214 const HashTable* table, const ThisImp* thisObj)
215 {
216 const HashEntry* entry = Lookup::findEntry(table, propertyName);
217
218 if (!entry) // not found, forward to parent
219 return thisObj->ParentImp::get(exec, propertyName);
220
221 if (entry->attr & Function)
222 fprintf(stderr, "Function bit set! Shouldn't happen in lookupGetValue! propertyName was %s\n", propertyName.ascii() );
223 return thisObj->getValueProperty(exec, entry->value);
224 }
225
230 template <class ThisImp, class ParentImp>
231 inline void lookupPut(ExecState *exec, const Identifier &propertyName,
232 const Value& value, int attr,
233 const HashTable* table, ThisImp* thisObj)
234 {
235 const HashEntry* entry = Lookup::findEntry(table, propertyName);
236
237 if (!entry) // not found: forward to parent
238 thisObj->ParentImp::put(exec, propertyName, value, attr);
239 else if (entry->attr & Function) // function: put as override property
240 thisObj->ObjectImp::put(exec, propertyName, value, attr);
241 else if (entry->attr & ReadOnly) // readonly! Can't put!
242#ifdef KJS_VERBOSE
243 fprintf(stderr,"WARNING: Attempt to change value of readonly property '%s'\n",propertyName.ascii());
244#else
245 ; // do nothing
246#endif
247 else
248 thisObj->putValueProperty(exec, entry->value, value, attr);
249 }
250
251
259 template <class ClassCtor>
260 inline KJS::Object cacheGlobalObject(ExecState *exec, const Identifier &propertyName)
261 {
262 ValueImp *obj = static_cast<KJS::ObjectImp*>(exec->interpreter()->globalObject().imp())->getDirect(propertyName);
263 if (obj)
264 return KJS::Object::dynamicCast(Value(obj));
265 else
266 {
267 KJS::Object newObject(new ClassCtor(exec));
268 exec->interpreter()->globalObject().put(exec, propertyName, newObject, Internal);
269 return newObject;
270 }
271 }
272
273
292#define PUBLIC_DEFINE_PROTOTYPE(ClassName,ClassProto) \
293 namespace KJS { \
294 class ClassProto : public KJS::ObjectImp { \
295 friend KJS::Object cacheGlobalObject<ClassProto>(KJS::ExecState *exec, const KJS::Identifier &propertyName); \
296 public: \
297 static KJS::Object self(KJS::ExecState *exec) \
298 { \
299 return cacheGlobalObject<ClassProto>( exec, "[[" ClassName ".prototype]]" ); \
300 } \
301 protected: \
302 ClassProto( KJS::ExecState *exec ) \
303 : KJS::ObjectImp( exec->interpreter()->builtinObjectPrototype() ) {} \
304 \
305 public: \
306 virtual const KJS::ClassInfo *classInfo() const { return &info; } \
307 static const KJS::ClassInfo info; \
308 KJS::Value get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
309 bool hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
310 }; \
311 }
312
313#define IMPLEMENT_CLASSINFO(ClassName,ClassProto) \
314 namespace KJS {\
315 const KJS::ClassInfo ClassProto::info = { ClassName, 0, &ClassProto##Table, 0 }; \
316 }
317
318#define DEFINE_PROTOTYPE(ClassName,ClassProto) \
319 PUBLIC_DEFINE_PROTOTYPE(ClassName,ClassProto) \
320 IMPLEMENT_CLASSINFO(ClassName,ClassProto)
321
322#define IMPLEMENT_PROTOTYPE(ClassProto,ClassFunc) \
323 KJS::Value KJS::ClassProto::get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
324 { \
325 /*fprintf( stderr, "%sProto::get(%s) [in macro, no parent]\n", info.className, propertyName.ascii());*/ \
326 return lookupGetFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &ClassProto##Table, this ); \
327 } \
328 bool KJS::ClassProto::hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
329 { /*stupid but we need this to have a common macro for the declaration*/ \
330 return KJS::ObjectImp::hasProperty(exec, propertyName); \
331 }
332
333#define PUBLIC_IMPLEMENT_PROTOTYPE(ClassProto,ClassName,ClassFunc) \
334 IMPLEMENT_PROTOTYPE(ClassProto,ClassFunc)\
335 IMPLEMENT_CLASSINFO(ClassName,ClassProto)
336
337#define IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassFunc,ParentProto) \
338 KJS::Value KJS::ClassProto::get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
339 { \
340 /*fprintf( stderr, "%sProto::get(%s) [in macro]\n", info.className, propertyName.ascii());*/ \
341 KJS::Value val = lookupGetFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &ClassProto##Table, this ); \
342 if ( val.type() != UndefinedType ) return val; \
343 /* Not found -> forward request to "parent" prototype */ \
344 return ParentProto::self(exec).get( exec, propertyName ); \
345 } \
346 bool KJS::ClassProto::hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
347 { \
348 if (KJS::ObjectImp::hasProperty(exec, propertyName)) \
349 return true; \
350 return ParentProto::self(exec).hasProperty(exec, propertyName); \
351 }
352
353#define PUBLIC_IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassName,ClassFunc,ParentProto) \
354 IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassFunc,ParentProto) \
355 IMPLEMENT_CLASSINFO(ClassName,ClassProto)
356
357#define IMPLEMENT_PROTOFUNC(ClassFunc) \
358 namespace KJS { \
359 class ClassFunc : public ObjectImp { \
360 public: \
361 ClassFunc(KJS::ExecState *exec, int i, int len) \
362 : ObjectImp( /*proto? */ ), id(i) { \
363 KJS::Value protect(this); \
364 put(exec,lengthPropertyName,Number(len),DontDelete|ReadOnly|DontEnum); \
365 } \
366 virtual bool implementsCall() const { return true; } \
367 \
368 virtual KJS::Value call(KJS::ExecState *exec, KJS::Object &thisObj, const KJS::List &args); \
369 private: \
370 int id; \
371 }; \
372 }
373
374 // To be used in all call() implementations, before casting the type of thisObj
375#define KJS_CHECK_THIS( ClassName, theObj ) \
376 if (!theObj.isValid() || !theObj.inherits(&ClassName::info)) { \
377 KJS::UString errMsg = "Attempt at calling a function that expects a "; \
378 errMsg += ClassName::info.className; \
379 errMsg += " on a "; \
380 errMsg += thisObj.className(); \
381 KJS::Object err = KJS::Error::create(exec, KJS::TypeError, errMsg.ascii()); \
382 exec->setException(err); \
383 return err; \
384 }
385
386 /*
387 * List of things to do when porting an objectimp to the 'static hashtable' mechanism:
388 * - write the hashtable source, between @begin and @end
389 * - add a rule to build the .lut.h
390 * - include the .lut.h
391 * - mention the table in the classinfo (add a classinfo if necessary)
392 * - write/update the class enum (for the tokens)
393 * - turn get() into getValueProperty(), put() into putValueProperty(), using a switch and removing funcs
394 * - write get() and/or put() using a template method
395 * - cleanup old stuff (e.g. hasProperty)
396 * - compile, test, commit ;)
397 */
398} // namespace
399
400#endif
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Lookup
Fast keyword lookup.
Definition: lookup.h:101
KJS::Lookup::findEntry
static const HashEntry * findEntry(const struct HashTable *table, const Identifier &s)
Find an entry in the table, and return the entry This variant gives access to the other attributes of...
Definition: lookup.cpp:71
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Object::dynamicCast
static Object dynamicCast(const Value &v)
Converts a Value into an Object.
Definition: object.cpp:45
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:78
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::HashEntry
An entry in a hash table.
Definition: lookup.h:36
KJS::HashEntry::attr
unsigned char attr
attr is a set for flags (e.g.
Definition: lookup.h:48
KJS::HashEntry::params
unsigned char params
params is another number.
Definition: lookup.h:53
KJS::HashEntry::next
short next
next is the index to the next entry for the same hash value
Definition: lookup.h:57
KJS::HashEntry::value
short int value
value is the result value (usually an enum value)
Definition: lookup.h:44
KJS::HashEntry::soffset
unsigned short soffset
s is the offset to the string key (e.g.
Definition: lookup.h:40
KJS::HashTable
A hash table Usually the hashtable is generated by the create_hash_table script, from a ....
Definition: lookup.h:71
KJS::HashTable::sbase
const char *const sbase
pointer to the string table.
Definition: lookup.h:95
KJS::HashTable::entries
const HashEntry *const entries
pointer to the array of entries Mind that some entries in the array are null (0,0,...
Definition: lookup.h:86
KJS::HashTable::size
int size
size is the total number of entries in the hashtable, including the null entries, i....
Definition: lookup.h:81
KJS::HashTable::hashSize
int hashSize
the maximum value for the hash.
Definition: lookup.h:90
KJS::HashTable::type
int type
type is a version number.
Definition: lookup.h:75
KJS::UChar
Unicode character.
Definition: ustring.h:51

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.4
This website is maintained by Timothy Pearson.