lookup.h
// -*- c-basic-offset: 2 -*-
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _KJSLOOKUP_H_
#define _KJSLOOKUP_H_
#include "identifier.h"
#include "value.h"
#include "object.h"
#include "interpreter.h"
#include <stdio.h>
namespace KJS {
struct HashEntry {
unsigned short soffset;
short int value;
unsigned char attr;
unsigned char params;
short next;
};
struct HashTable {
int type;
int size;
const HashEntry *const entries;
int hashSize;
const char* const sbase;
};
class KJS_EXPORT Lookup {
public:
static int find(const struct HashTable *table, const Identifier &s);
static int find(const struct HashTable *table,
const UChar *c, unsigned int len);
static const HashEntry* findEntry(const struct HashTable *table,
const Identifier &s);
static const HashEntry* findEntry(const struct HashTable *table,
const UChar *c, unsigned int len);
static unsigned int hash(const Identifier &key);
static unsigned int hash(const UChar *c, unsigned int len);
static unsigned int hash(const char *s);
};
class ExecState;
class UString;
template <class FuncImp>
inline Value lookupOrCreateFunction(ExecState *exec, const Identifier &propertyName,
const ObjectImp *thisObj, int token, int params, int attr)
{
// Look for cached value in dynamic map of properties (in ObjectImp)
ValueImp * cachedVal = thisObj->ObjectImp::getDirect(propertyName);
/*if (cachedVal)
fprintf(stderr, "lookupOrCreateFunction: Function -> looked up in ObjectImp, found type=%d\n", cachedVal->type());*/
if (cachedVal)
return Value(cachedVal);
ObjectImp* func = new FuncImp( exec, token, params );
Value val( func );
func->setFunctionName( propertyName );
ObjectImp *thatObj = const_cast<ObjectImp *>(thisObj);
thatObj->ObjectImp::put(exec, propertyName, val, attr);
return val;
}
template <class FuncImp, class ThisImp, class ParentImp>
inline Value lookupGet(ExecState *exec, const Identifier &propertyName,
const HashTable* table, const ThisImp* thisObj)
{
const HashEntry* entry = Lookup::findEntry(table, propertyName);
if (!entry) // not found, forward to parent
return thisObj->ParentImp::get(exec, propertyName);
//fprintf(stderr, "lookupGet: found value=%d attr=%d\n", entry->value, entry->attr);
if (entry->attr & Function)
return lookupOrCreateFunction<FuncImp>(exec, propertyName, thisObj, entry->value, entry->params, entry->attr);
return thisObj->getValueProperty(exec, entry->value);
}
template <class FuncImp, class ParentImp>
inline Value lookupGetFunction(ExecState *exec, const Identifier &propertyName,
const HashTable* table, const ObjectImp* thisObj)
{
const HashEntry* entry = Lookup::findEntry(table, propertyName);
if (!entry) // not found, forward to parent
return static_cast<const ParentImp *>(thisObj)->ParentImp::get(exec, propertyName);
if (entry->attr & Function)
return lookupOrCreateFunction<FuncImp>(exec, propertyName, thisObj, entry->value, entry->params, entry->attr);
fprintf(stderr, "Function bit not set! Shouldn't happen in lookupGetFunction!\n" );
return Undefined();
}
template <class ThisImp, class ParentImp>
inline Value lookupGetValue(ExecState *exec, const Identifier &propertyName,
const HashTable* table, const ThisImp* thisObj)
{
const HashEntry* entry = Lookup::findEntry(table, propertyName);
if (!entry) // not found, forward to parent
return thisObj->ParentImp::get(exec, propertyName);
if (entry->attr & Function)
fprintf(stderr, "Function bit set! Shouldn't happen in lookupGetValue! propertyName was %s\n", propertyName.ascii() );
return thisObj->getValueProperty(exec, entry->value);
}
template <class ThisImp, class ParentImp>
inline void lookupPut(ExecState *exec, const Identifier &propertyName,
const Value& value, int attr,
const HashTable* table, ThisImp* thisObj)
{
const HashEntry* entry = Lookup::findEntry(table, propertyName);
if (!entry) // not found: forward to parent
thisObj->ParentImp::put(exec, propertyName, value, attr);
else if (entry->attr & Function) // function: put as override property
thisObj->ObjectImp::put(exec, propertyName, value, attr);
else if (entry->attr & ReadOnly) // readonly! Can't put!
#ifdef KJS_VERBOSE
fprintf(stderr,"WARNING: Attempt to change value of readonly property '%s'\n",propertyName.ascii());
#else
; // do nothing
#endif
else
thisObj->putValueProperty(exec, entry->value, value, attr);
}
template <class ClassCtor>
inline KJS::Object cacheGlobalObject(ExecState *exec, const Identifier &propertyName)
{
ValueImp *obj = static_cast<KJS::ObjectImp*>(exec->interpreter()->globalObject().imp())->getDirect(propertyName);
if (obj)
return KJS::Object::dynamicCast(Value(obj));
else
{
KJS::Object newObject(new ClassCtor(exec));
exec->interpreter()->globalObject().put(exec, propertyName, newObject, Internal);
return newObject;
}
}
#define PUBLIC_DEFINE_PROTOTYPE(ClassName,ClassProto) \
namespace KJS { \
class ClassProto : public KJS::ObjectImp { \
friend KJS::Object cacheGlobalObject<ClassProto>(KJS::ExecState *exec, const KJS::Identifier &propertyName); \
public: \
static KJS::Object self(KJS::ExecState *exec) \
{ \
return cacheGlobalObject<ClassProto>( exec, "[[" ClassName ".prototype]]" ); \
} \
protected: \
ClassProto( KJS::ExecState *exec ) \
: KJS::ObjectImp( exec->interpreter()->builtinObjectPrototype() ) {} \
\
public: \
virtual const KJS::ClassInfo *classInfo() const { return &info; } \
static const KJS::ClassInfo info; \
KJS::Value get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
bool hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const; \
}; \
}
#define IMPLEMENT_CLASSINFO(ClassName,ClassProto) \
namespace KJS {\
const KJS::ClassInfo ClassProto::info = { ClassName, 0, &ClassProto##Table, 0 }; \
}
#define DEFINE_PROTOTYPE(ClassName,ClassProto) \
PUBLIC_DEFINE_PROTOTYPE(ClassName,ClassProto) \
IMPLEMENT_CLASSINFO(ClassName,ClassProto)
#define IMPLEMENT_PROTOTYPE(ClassProto,ClassFunc) \
KJS::Value KJS::ClassProto::get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
{ \
/*fprintf( stderr, "%sProto::get(%s) [in macro, no parent]\n", info.className, propertyName.ascii());*/ \
return lookupGetFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &ClassProto##Table, this ); \
} \
bool KJS::ClassProto::hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
{ /*stupid but we need this to have a common macro for the declaration*/ \
return KJS::ObjectImp::hasProperty(exec, propertyName); \
}
#define PUBLIC_IMPLEMENT_PROTOTYPE(ClassProto,ClassName,ClassFunc) \
IMPLEMENT_PROTOTYPE(ClassProto,ClassFunc)\
IMPLEMENT_CLASSINFO(ClassName,ClassProto)
#define IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassFunc,ParentProto) \
KJS::Value KJS::ClassProto::get(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
{ \
/*fprintf( stderr, "%sProto::get(%s) [in macro]\n", info.className, propertyName.ascii());*/ \
KJS::Value val = lookupGetFunction<ClassFunc,KJS::ObjectImp>(exec, propertyName, &ClassProto##Table, this ); \
if ( val.type() != UndefinedType ) return val; \
/* Not found -> forward request to "parent" prototype */ \
return ParentProto::self(exec).get( exec, propertyName ); \
} \
bool KJS::ClassProto::hasProperty(KJS::ExecState *exec, const KJS::Identifier &propertyName) const \
{ \
if (KJS::ObjectImp::hasProperty(exec, propertyName)) \
return true; \
return ParentProto::self(exec).hasProperty(exec, propertyName); \
}
#define PUBLIC_IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassName,ClassFunc,ParentProto) \
IMPLEMENT_PROTOTYPE_WITH_PARENT(ClassProto,ClassFunc,ParentProto) \
IMPLEMENT_CLASSINFO(ClassName,ClassProto)
#define IMPLEMENT_PROTOFUNC(ClassFunc) \
namespace KJS { \
class ClassFunc : public ObjectImp { \
public: \
ClassFunc(KJS::ExecState *exec, int i, int len) \
: ObjectImp( /*proto? */ ), id(i) { \
KJS::Value protect(this); \
put(exec,lengthPropertyName,Number(len),DontDelete|ReadOnly|DontEnum); \
} \
virtual bool implementsCall() const { return true; } \
\
virtual KJS::Value call(KJS::ExecState *exec, KJS::Object &thisObj, const KJS::List &args); \
private: \
int id; \
}; \
}
// To be used in all call() implementations, before casting the type of thisObj
#define KJS_CHECK_THIS( ClassName, theObj ) \
if (!theObj.isValid() || !theObj.inherits(&ClassName::info)) { \
KJS::UString errMsg = "Attempt at calling a function that expects a "; \
errMsg += ClassName::info.className; \
errMsg += " on a "; \
errMsg += thisObj.className(); \
KJS::Object err = KJS::Error::create(exec, KJS::TypeError, errMsg.ascii()); \
exec->setException(err); \
return err; \
}
/*
* List of things to do when porting an objectimp to the 'static hashtable' mechanism:
* - write the hashtable source, between @begin and @end
* - add a rule to build the .lut.h
* - include the .lut.h
* - mention the table in the classinfo (add a classinfo if necessary)
* - write/update the class enum (for the tokens)
* - turn get() into getValueProperty(), put() into putValueProperty(), using a switch and removing funcs
* - write get() and/or put() using a template method
* - cleanup old stuff (e.g. hasProperty)
* - compile, test, commit ;)
*/
} // namespace
#endif
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::Lookup
Fast keyword lookup.
Definition: lookup.h:102
KJS::HashTable::sbase
const char *const sbase
pointer to the string table.
Definition: lookup.h:96
KJS::HashEntry::value
short int value
value is the result value (usually an enum value)
Definition: lookup.h:45
KJS::HashTable::type
int type
type is a version number.
Definition: lookup.h:76
KJS::HashTable::size
int size
size is the total number of entries in the hashtable, including the null entries, i...
Definition: lookup.h:82
KJS::HashEntry
An entry in a hash table.
Definition: lookup.h:37
KJS::HashTable::hashSize
int hashSize
the maximum value for the hash.
Definition: lookup.h:91
KJS::HashTable
A hash table Usually the hashtable is generated by the create_hash_table script, from a ...
Definition: lookup.h:72
KJS::Object
Represents an Object.
Definition: object.h:82
KJS::HashEntry::attr
unsigned char attr
attr is a set for flags (e.g.
Definition: lookup.h:49
KJS::HashEntry::soffset
unsigned short soffset
s is the offset to the string key (e.g.
Definition: lookup.h:41
KJS::HashEntry::next
short next
next is the index to the next entry for the same hash value
Definition: lookup.h:58
KJS::HashEntry::params
unsigned char params
params is another number.
Definition: lookup.h:54
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::UChar
Unicode character.
Definition: ustring.h:51
KJS::Object::dynamicCast
static Object dynamicCast(const Value &v)
Converts a Value into an Object.
Definition: object.cpp:46
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:79
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:72
KJS::HashTable::entries
const HashEntry *const entries
pointer to the array of entries Mind that some entries in the array are null (0,0,0,0).
Definition: lookup.h:87
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

KDE Home | KDE Accessibility Home | Description of Access Keys