#ifndef KJS_PROPERTY_MAP_H_
#define KJS_PROPERTY_MAP_H_
#include "identifier.h"
#include "protect.h"
#include <wtf/OwnArrayPtr.h>
namespace KJS {
class JSObject;
class JSValue;
class PropertyNameArray;
struct PropertyMapEntry;
struct PropertyMapHashTable;
class SavedProperty : Noncopyable {
public:
SavedProperty();
void init(UString::Rep* name, JSValue*, unsigned attributes);
~SavedProperty();
UString::Rep* name() const;
JSValue* value() const;
unsigned attributes() const;
private:
UString::Rep* m_name;
JSValue* m_value;
unsigned m_attributes;
};
struct SavedProperties {
SavedProperties();
~SavedProperties();
unsigned count;
OwnArrayPtr<SavedProperty> properties;
};
class PropertyMap : Noncopyable {
public:
PropertyMap();
~PropertyMap();
void clear();
void put(const Identifier&, JSValue*, unsigned attributes, bool checkReadOnly = false);
void remove(const Identifier&);
JSValue* get(const Identifier&) const;
JSValue* get(const Identifier&, unsigned& attributes) const;
JSValue** getLocation(const Identifier& name);
void mark() const;
void getEnumerablePropertyNames(PropertyNameArray&) const;
void save(SavedProperties&) const;
void restore(const SavedProperties&);
bool hasGetterSetterProperties() const { return m_getterSetterFlag; }
void setHasGetterSetterProperties(bool f) { m_getterSetterFlag = f; }
bool containsGettersOrSetters() const;
private:
typedef PropertyMapEntry Entry;
typedef PropertyMapHashTable Table;
static bool keysMatch(const UString::Rep*, const UString::Rep*);
void expand();
void rehash();
void rehash(unsigned newTableSize);
void createTable();
void insert(const Entry&);
void checkConsistency();
UString::Rep* m_singleEntryKey;
union {
JSValue* singleEntryValue;
Table* table;
} m_u;
short m_singleEntryAttributes;
bool m_getterSetterFlag : 1;
bool m_usingTable : 1;
};
inline PropertyMap::PropertyMap()
: m_singleEntryKey(0)
, m_getterSetterFlag(false)
, m_usingTable(false)
{
}
inline SavedProperty::SavedProperty()
#ifndef NDEBUG
: m_name(0)
, m_value(0)
, m_attributes(0)
#endif
{
}
inline void SavedProperty::init(UString::Rep* name, JSValue* value, unsigned attributes)
{
ASSERT(name);
ASSERT(value);
ASSERT(!m_name);
ASSERT(!m_value);
ASSERT(!m_attributes);
m_name = name;
m_value = value;
m_attributes = attributes;
name->ref();
gcProtect(value);
}
inline SavedProperty::~SavedProperty()
{
ASSERT(m_name);
ASSERT(m_value);
m_name->deref();
gcUnprotect(m_value);
}
inline UString::Rep* SavedProperty::name() const
{
ASSERT(m_name);
ASSERT(m_value);
return m_name;
}
inline JSValue* SavedProperty::value() const
{
ASSERT(m_name);
ASSERT(m_value);
return m_value;
}
inline unsigned SavedProperty::attributes() const
{
ASSERT(m_name);
ASSERT(m_value);
return m_attributes;
}
}
#endif // _KJS_PROPERTY_MAP_H_