qtdeclarative/qmljs_objects.h

329 lines
8.0 KiB
C
Raw Normal View History

2012-04-16 19:23:25 +00:00
#ifndef QMLJS_OBJECTS_H
#define QMLJS_OBJECTS_H
#include "qmljs_runtime.h"
#include <QtCore/QString>
#include <QtCore/QHash>
2012-05-04 13:12:37 +00:00
#include <cassert>
2012-04-16 19:23:25 +00:00
2012-05-04 13:28:04 +00:00
namespace QQmlJS {
2012-05-07 14:05:05 +00:00
namespace IR {
struct Function;
}
2012-05-04 13:28:04 +00:00
namespace VM {
2012-04-16 19:23:25 +00:00
struct Value;
struct Object;
struct BooleanObject;
struct NumberObject;
struct StringObject;
struct ArrayObject;
struct FunctionObject;
struct ErrorObject;
2012-05-04 13:12:37 +00:00
struct String {
2012-04-16 19:23:25 +00:00
String(const QString &text)
: _text(text), _hashValue(0) {}
inline const QString &text() const {
return _text;
}
inline unsigned hashValue() const {
if (! _hashValue)
_hashValue = qHash(_text);
return _hashValue;
}
static String *get(Context *ctx, const QString &s) {
2012-05-04 13:12:37 +00:00
Q_UNUSED(ctx);
return new String(s);
2012-04-16 19:23:25 +00:00
}
private:
QString _text;
mutable unsigned _hashValue;
};
2012-05-04 13:12:37 +00:00
struct Property {
2012-04-16 19:23:25 +00:00
String *name;
Value value;
2012-05-08 09:13:02 +00:00
PropertyAttributes attributes;
2012-04-16 19:23:25 +00:00
Property *next;
2012-05-04 13:12:37 +00:00
int index;
2012-04-16 19:23:25 +00:00
Property(String *name, const Value &value, PropertyAttributes flags = NoAttributes)
2012-05-08 09:13:02 +00:00
: name(name), value(value), attributes(flags), next(0), index(-1) {}
2012-04-16 19:23:25 +00:00
2012-05-08 09:13:02 +00:00
inline bool isWritable() const { return attributes & WritableAttribute; }
inline bool isEnumerable() const { return attributes & EnumerableAttribute; }
inline bool isConfigurable() const { return attributes & ConfigurableAttribute; }
2012-04-16 19:23:25 +00:00
inline bool hasName(String *n) const {
2012-05-04 13:12:37 +00:00
if (name == n) {
2012-04-16 19:23:25 +00:00
return true;
2012-05-04 13:12:37 +00:00
} else if (name->hashValue() == n->hashValue() && name->text() == n->text()) {
return true;
}
2012-04-16 19:23:25 +00:00
return false;
}
inline unsigned hashValue() const {
return name->hashValue();
}
};
2012-05-04 13:12:37 +00:00
class Table
2012-04-16 19:23:25 +00:00
{
2012-05-04 13:12:37 +00:00
Q_DISABLE_COPY(Table)
2012-04-16 19:23:25 +00:00
public:
Table()
: _properties(0)
, _buckets(0)
, _propertyCount(-1)
, _bucketCount(11)
, _allocated(0) {}
2012-05-04 13:12:37 +00:00
~Table()
{
qDeleteAll(_properties, _properties + _propertyCount + 1);
delete[] _properties;
delete[] _buckets;
}
inline bool isEmpty() const { return _propertyCount == -1; }
2012-04-16 19:23:25 +00:00
typedef Property **iterator;
2012-05-04 13:12:37 +00:00
inline iterator begin() const { return _properties; }
inline iterator end() const { return _properties + (_propertyCount + 1); }
2012-04-16 19:23:25 +00:00
bool remove(String *name)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(name);
assert(!"TODO");
2012-04-16 19:23:25 +00:00
return false;
}
Property *find(String *name) const
{
2012-05-04 13:12:37 +00:00
if (_properties) {
for (Property *prop = _buckets[name->hashValue() % _bucketCount]; prop; prop = prop->next) {
if (prop->hasName(name))
return prop;
}
2012-04-16 19:23:25 +00:00
}
return 0;
}
Property *insert(String *name, const Value &value)
{
if (Property *prop = find(name)) {
prop->value = value;
return prop;
}
if (++_propertyCount == _allocated) {
if (! _allocated)
_allocated = 4;
else
_allocated *= 2;
2012-05-04 13:12:37 +00:00
Property **properties = new Property*[_allocated];
2012-04-16 19:23:25 +00:00
std::copy(_properties, _properties + _propertyCount, properties);
2012-05-04 13:12:37 +00:00
delete[] _properties;
2012-04-16 19:23:25 +00:00
_properties = properties;
}
2012-05-04 13:12:37 +00:00
Property *prop = new Property(name, value);
prop->index = _propertyCount;
2012-04-16 19:23:25 +00:00
_properties[_propertyCount] = prop;
if (! _buckets || 3 * _propertyCount >= 2 * _bucketCount) {
rehash();
} else {
Property *&bucket = _buckets[prop->hashValue() % _bucketCount];
prop->next = bucket;
bucket = prop;
}
return prop;
}
private:
void rehash()
{
if (_bucketCount)
_bucketCount *= 2; // ### next prime
2012-05-04 13:12:37 +00:00
if (_buckets)
delete[] _buckets;
_buckets = new Property *[_bucketCount];
2012-04-16 19:23:25 +00:00
std::fill(_buckets, _buckets + _bucketCount, (Property *) 0);
for (int i = 0; i <= _propertyCount; ++i) {
Property *prop = _properties[i];
2012-05-04 13:12:37 +00:00
Property *&bucket = _buckets[prop->hashValue() % _bucketCount];
2012-04-16 19:23:25 +00:00
prop->next = bucket;
bucket = prop;
}
}
2012-05-04 13:12:37 +00:00
private:
Property **_properties;
Property **_buckets;
int _propertyCount;
int _bucketCount;
int _allocated;
2012-04-16 19:23:25 +00:00
};
2012-05-04 13:12:37 +00:00
struct Object {
2012-04-16 19:23:25 +00:00
Object *prototype;
String *klass;
Table *members;
bool extensible;
Object()
: prototype(0)
, klass(0)
, members(0)
, extensible(true) {}
2012-05-04 13:12:37 +00:00
virtual ~Object();
2012-04-16 19:23:25 +00:00
virtual FunctionObject *asFunctionObject() { return 0; }
2012-05-08 09:13:02 +00:00
bool get(String *name, Value *result);
virtual Value *getOwnProperty(String *name, PropertyAttributes *attributes = 0);
virtual Value *getProperty(String *name, PropertyAttributes *attributes = 0);
2012-04-16 19:23:25 +00:00
virtual void put(String *name, const Value &value, bool flag = 0);
virtual bool canPut(String *name);
virtual bool hasProperty(String *name) const;
virtual bool deleteProperty(String *name, bool flag);
virtual void defaultValue(Value *result, int typeHint);
// ### TODO: defineOwnProperty(name, descriptor, boolean) -> boolean
};
struct BooleanObject: Object {
Value value;
BooleanObject(const Value &value): value(value) {}
2012-05-04 13:12:37 +00:00
virtual void defaultValue(Value *result, int /*typehint*/) { *result = value; }
2012-04-16 19:23:25 +00:00
};
struct NumberObject: Object {
Value value;
NumberObject(const Value &value): value(value) {}
2012-05-04 13:12:37 +00:00
virtual void defaultValue(Value *result, int /*typehint*/) { *result = value; }
2012-04-16 19:23:25 +00:00
};
struct StringObject: Object {
Value value;
StringObject(const Value &value): value(value) {}
2012-05-04 13:12:37 +00:00
virtual void defaultValue(Value *result, int /*typehint*/) { *result = value; }
2012-04-16 19:23:25 +00:00
};
struct ArrayObject: Object {
};
struct FunctionObject: Object {
2012-05-13 11:50:55 +00:00
Context *scope;
2012-04-16 19:23:25 +00:00
String **formalParameterList;
2012-05-08 09:13:02 +00:00
size_t formalParameterCount;
2012-04-16 19:23:25 +00:00
2012-05-13 11:50:55 +00:00
FunctionObject(Context *scope): scope(scope), formalParameterList(0), formalParameterCount(0) {}
2012-04-16 19:23:25 +00:00
virtual FunctionObject *asFunctionObject() { return this; }
virtual bool hasInstance(const Value &value) const;
2012-05-07 14:05:05 +00:00
virtual void call(Context *ctx);
virtual void construct(Context *ctx);
};
struct NativeFunction: FunctionObject {
void (*code)(Context *);
2012-05-13 11:50:55 +00:00
NativeFunction(Context *scope, void (*code)(Context *)): FunctionObject(scope), code(code) {}
virtual void call(Context *ctx) { code(ctx); }
virtual void construct(Context *ctx) { code(ctx); }
};
2012-05-07 14:05:05 +00:00
struct ScriptFunction: FunctionObject {
IR::Function *function;
2012-05-13 11:50:55 +00:00
ScriptFunction(Context *scope, IR::Function *function);
2012-05-08 09:13:02 +00:00
virtual ~ScriptFunction();
2012-05-07 14:05:05 +00:00
virtual void call(Context *ctx);
virtual void construct(Context *ctx);
2012-04-16 19:23:25 +00:00
};
struct ErrorObject: Object {
String *message;
ErrorObject(String *message): message(message) {}
};
struct ArgumentsObject: Object {
2012-05-07 14:05:05 +00:00
Context *context;
ArgumentsObject(Context *context): context(context) {}
2012-05-08 09:13:02 +00:00
virtual Value *getProperty(String *name, PropertyAttributes *attributes);
2012-04-16 19:23:25 +00:00
};
2012-05-04 13:12:37 +00:00
struct Context {
2012-05-07 14:05:05 +00:00
Context *parent;
2012-04-16 19:23:25 +00:00
Value activation;
Value thisObject;
Value *arguments;
2012-05-07 14:05:05 +00:00
size_t argumentCount;
Value result;
2012-05-08 09:13:02 +00:00
String **formals;
size_t formalCount;
bool calledAsConstructor;
2012-04-16 19:23:25 +00:00
2012-05-13 11:50:55 +00:00
Value *lookup(String *name) {
if (activation.is(OBJECT_TYPE)) {
if (Value *prop = activation.objectValue->getProperty(name))
return prop;
}
return parent ? parent->lookup(name) : 0;
}
2012-05-10 08:56:02 +00:00
inline Value argument(size_t index = 0)
{
Value arg;
getArgument(&arg, index);
return arg;
}
inline void getArgument(Value *result, size_t index)
{
if (index < argumentCount)
*result = arguments[index];
else
__qmljs_init_undefined(this, result);
}
2012-05-08 08:34:57 +00:00
void init()
2012-04-16 19:23:25 +00:00
{
2012-05-08 08:34:57 +00:00
parent = 0;
arguments = 0;
argumentCount = 0;
2012-04-16 19:23:25 +00:00
activation.type = NULL_TYPE;
thisObject.type = NULL_TYPE;
2012-05-07 14:05:05 +00:00
result.type = UNDEFINED_TYPE;
2012-05-08 09:13:02 +00:00
formals = 0;
formalCount = 0;
calledAsConstructor = false;
2012-04-16 19:23:25 +00:00
}
};
2012-05-04 13:28:04 +00:00
} // namespace VM
} // namespace QQmlJS
2012-04-16 19:23:25 +00:00
#endif // QMLJS_OBJECTS_H