qtdeclarative/qmljs_objects.h

456 lines
12 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"
2012-05-20 17:59:47 +00:00
#include "qv4array_p.h"
2012-04-16 19:23:25 +00:00
#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;
2012-05-18 12:10:02 +00:00
struct DateObject;
2012-04-16 19:23:25 +00:00
struct FunctionObject;
struct ErrorObject;
2012-05-18 12:10:02 +00:00
struct ArgumentsObject;
2012-05-14 15:12:25 +00:00
struct Context;
struct ExecutionEngine;
2012-04-16 19:23:25 +00:00
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) {}
2012-05-14 14:03:10 +00:00
inline const QString &toQString() const {
2012-04-16 19:23:25 +00:00
return _text;
}
inline unsigned hashValue() const {
if (! _hashValue)
_hashValue = qHash(_text);
return _hashValue;
}
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-14 14:03:10 +00:00
} else if (name->hashValue() == n->hashValue() && name->toQString() == n->toQString()) {
2012-05-04 13:12:37 +00:00
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
2012-05-18 12:10:02 +00:00
virtual BooleanObject *asBooleanObject() { return 0; }
virtual NumberObject *asNumberObject() { return 0; }
virtual StringObject *asStringObject() { return 0; }
virtual DateObject *asDateObject() { return 0; }
virtual ArrayObject *asArrayObject() { return 0; }
2012-04-16 19:23:25 +00:00
virtual FunctionObject *asFunctionObject() { return 0; }
2012-05-18 12:10:02 +00:00
virtual ErrorObject *asErrorObject() { return 0; }
virtual ArgumentsObject *asArgumentsObject() { return 0; }
2012-04-16 19:23:25 +00:00
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);
// ### TODO: defineOwnProperty(name, descriptor, boolean) -> boolean
2012-05-14 14:03:10 +00:00
//
// helpers
//
void setProperty(Context *ctx, const QString &name, const Value &value);
void setProperty(Context *ctx, const QString &name, void (*code)(Context *), int count = 0);
2012-04-16 19:23:25 +00:00
};
struct BooleanObject: Object {
Value value;
BooleanObject(const Value &value): value(value) {}
2012-05-18 12:10:02 +00:00
virtual BooleanObject *asBooleanObject() { return this; }
2012-04-16 19:23:25 +00:00
};
struct NumberObject: Object {
Value value;
NumberObject(const Value &value): value(value) {}
2012-05-18 12:10:02 +00:00
virtual NumberObject *asNumberObject() { return this; }
2012-04-16 19:23:25 +00:00
};
struct StringObject: Object {
Value value;
StringObject(const Value &value): value(value) {}
2012-05-18 12:10:02 +00:00
virtual StringObject *asStringObject() { return this; }
};
struct DateObject: Object {
Value value;
DateObject(const Value &value): value(value) {}
virtual DateObject *asDateObject() { return this; }
2012-04-16 19:23:25 +00:00
};
struct ArrayObject: Object {
2012-05-20 17:59:47 +00:00
Array value;
Value length;
ArrayObject() { length.type = NUMBER_TYPE; }
ArrayObject(const Array &value): value(value) { length.type = NUMBER_TYPE; }
2012-05-18 12:10:02 +00:00
virtual ArrayObject *asArrayObject() { return this; }
2012-05-20 17:59:47 +00:00
virtual Value *getOwnProperty(String *name, PropertyAttributes *attributes);
2012-04-16 19:23:25 +00:00
};
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-05-16 09:58:07 +00:00
String **varList;
size_t varCount;
bool needsActivation;
2012-04-16 19:23:25 +00:00
FunctionObject(Context *scope)
: scope(scope)
, formalParameterList(0)
, formalParameterCount(0)
2012-05-16 09:58:07 +00:00
, varList(0)
, varCount(0)
, needsActivation(true) {}
2012-04-16 19:23:25 +00:00
2012-05-18 12:10:02 +00:00
virtual FunctionObject *asFunctionObject() { return this; }
2012-04-16 19:23:25 +00:00
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 {
2012-05-15 08:35:19 +00:00
Value message;
ErrorObject(const Value &message): message(message) {}
2012-05-18 12:10:02 +00:00
virtual ErrorObject *asErrorObject() { return this; }
2012-04-16 19:23:25 +00:00
};
struct ArgumentsObject: Object {
2012-05-07 14:05:05 +00:00
Context *context;
ArgumentsObject(Context *context): context(context) {}
2012-05-18 12:10:02 +00:00
virtual ArgumentsObject *asArgumentsObject() { return this; }
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-14 15:12:25 +00:00
ExecutionEngine *engine;
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;
2012-05-16 09:58:07 +00:00
Value *locals;
2012-05-07 14:05:05 +00:00
Value result;
2012-05-08 09:13:02 +00:00
String **formals;
size_t formalCount;
2012-05-16 09:58:07 +00:00
String **vars;
size_t varCount;
2012-05-23 16:48:48 +00:00
int calledAsConstructor;
int hasUncaughtException;
2012-04-16 19:23:25 +00:00
Value *lookup(String *name)
{
for (Context *ctx = this; ctx; ctx = ctx->parent) {
if (ctx->activation.is(OBJECT_TYPE)) {
if (Value *prop = ctx->activation.objectValue->getProperty(name)) {
return prop;
}
2012-05-16 09:58:07 +00:00
}
2012-05-13 11:50:55 +00:00
}
return 0;
2012-05-13 11:50:55 +00:00
}
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
2012-05-15 08:53:20 +00:00
__qmljs_init_undefined(result);
2012-05-10 08:56:02 +00:00
}
2012-05-14 15:12:25 +00:00
void init(ExecutionEngine *eng)
2012-04-16 19:23:25 +00:00
{
2012-05-14 15:12:25 +00:00
engine = eng;
2012-05-08 08:34:57 +00:00
parent = 0;
arguments = 0;
argumentCount = 0;
2012-05-16 09:58:07 +00:00
locals = 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;
2012-05-16 09:58:07 +00:00
vars = 0;
varCount = 0;
calledAsConstructor = false;
2012-05-23 16:48:48 +00:00
hasUncaughtException = false;
2012-04-16 19:23:25 +00:00
}
2012-05-24 08:01:17 +00:00
void throwError(const Value &value);
void throwError(const QString &message);
void throwTypeError();
void throwUnimplemented(const QString &message);
void initCallContext(ExecutionEngine *e, const Value *object, FunctionObject *f, Value *args, int argc);
void leaveCallContext(FunctionObject *f, Value *r);
void initConstructorContext(ExecutionEngine *e, const Value *object, FunctionObject *f, Value *args, int argc);
void leaveConstructorContext(FunctionObject *f, Value *returnValue);
2012-04-16 19:23:25 +00:00
};
2012-05-14 15:12:25 +00:00
struct ExecutionEngine
{
Context *rootContext;
Value globalObject;
Value objectCtor;
Value stringCtor;
Value numberCtor;
2012-05-20 17:59:47 +00:00
Value booleanCtor;
Value arrayCtor;
Value functionCtor;
Value dateCtor;
2012-05-14 15:12:25 +00:00
Value objectPrototype;
Value stringPrototype;
Value numberPrototype;
2012-05-20 17:59:47 +00:00
Value booleanPrototype;
Value arrayPrototype;
Value functionPrototype;
Value datePrototype;
2012-05-14 15:12:25 +00:00
QHash<QString, String *> identifiers;
ExecutionEngine();
2012-05-15 08:35:19 +00:00
Context *newContext();
2012-05-14 15:12:25 +00:00
String *identifier(const QString &s);
2012-05-15 08:35:19 +00:00
FunctionObject *newNativeFunction(Context *scope, void (*code)(Context *));
FunctionObject *newScriptFunction(Context *scope, IR::Function *function);
Object *newObject();
FunctionObject *newObjectCtor(Context *ctx);
Object *newObjectPrototype(Context *ctx, FunctionObject *proto);
String *newString(const QString &s);
Object *newStringObject(const Value &value);
FunctionObject *newStringCtor(Context *ctx);
Object *newStringPrototype(Context *ctx, FunctionObject *proto);
Object *newNumberObject(const Value &value);
FunctionObject *newNumberCtor(Context *ctx);
Object *newNumberPrototype(Context *ctx, FunctionObject *proto);
Object *newBooleanObject(const Value &value);
2012-05-15 09:19:10 +00:00
FunctionObject *newBooleanCtor(Context *ctx);
Object *newBooleanPrototype(Context *ctx, FunctionObject *proto);
Object *newFunctionObject(Context *ctx);
FunctionObject *newFunctionCtor(Context *ctx);
Object *newFunctionPrototype(Context *ctx, FunctionObject *proto);
2012-05-20 17:59:47 +00:00
Object *newArrayObject();
Object *newArrayObject(const Array &value);
FunctionObject *newArrayCtor(Context *ctx);
Object *newArrayPrototype(Context *ctx, FunctionObject *proto);
Object *newDateObject(const Value &value);
FunctionObject *newDateCtor(Context *ctx);
Object *newDatePrototype(Context *ctx, FunctionObject *proto);
2012-05-15 08:35:19 +00:00
Object *newErrorObject(const Value &value);
Object *newMathObject(Context *ctx);
Object *newArgumentsObject(Context *ctx);
2012-05-14 15:12: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