qtdeclarative/qmljs_objects.cpp

166 lines
3.7 KiB
C++
Raw Normal View History

2012-04-16 19:23:25 +00:00
#include "qmljs_objects.h"
2012-05-07 14:05:05 +00:00
#include "qv4ir_p.h"
#include <QtCore/QDebug>
2012-04-16 19:23:25 +00:00
#include <cassert>
2012-05-04 13:28:04 +00:00
using namespace QQmlJS::VM;
2012-05-04 13:12:37 +00:00
Object::~Object()
{
delete members;
}
2012-04-16 19:23:25 +00:00
bool Object::get(String *name, Value *result)
{
2012-05-08 09:13:02 +00:00
if (Value *prop = getProperty(name)) {
*result = *prop;
2012-04-16 19:23:25 +00:00
return true;
}
__qmljs_init_undefined(0, result);
return false;
}
2012-05-08 09:13:02 +00:00
Value *Object::getOwnProperty(String *name, PropertyAttributes *attributes)
2012-04-16 19:23:25 +00:00
{
if (members) {
if (Property *prop = members->find(name)) {
2012-05-08 09:13:02 +00:00
if (attributes)
*attributes = prop->attributes;
return &prop->value;
2012-04-16 19:23:25 +00:00
}
}
return 0;
}
2012-05-08 09:13:02 +00:00
Value *Object::getProperty(String *name, PropertyAttributes *attributes)
2012-04-16 19:23:25 +00:00
{
2012-05-08 09:13:02 +00:00
if (Value *prop = getOwnProperty(name, attributes))
2012-04-16 19:23:25 +00:00
return prop;
else if (prototype)
2012-05-08 09:13:02 +00:00
return prototype->getProperty(name, attributes);
2012-04-16 19:23:25 +00:00
return 0;
}
void Object::put(String *name, const Value &value, bool flag)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(flag);
2012-04-16 19:23:25 +00:00
if (! members)
2012-05-04 13:12:37 +00:00
members = new Table();
2012-04-16 19:23:25 +00:00
members->insert(name, value);
}
bool Object::canPut(String *name)
{
2012-05-08 09:13:02 +00:00
PropertyAttributes attrs = PropertyAttributes();
if (getOwnProperty(name, &attrs)) {
return attrs & WritableAttribute;
2012-05-04 13:12:37 +00:00
} else if (! prototype) {
2012-04-16 19:23:25 +00:00
return extensible;
2012-05-08 09:13:02 +00:00
} else if (prototype->getProperty(name, &attrs)) {
return attrs & WritableAttribute;
2012-04-16 19:23:25 +00:00
} else {
return extensible;
}
return true;
}
bool Object::hasProperty(String *name) const
{
if (members)
return members->find(name) != 0;
return false;
}
bool Object::deleteProperty(String *name, bool flag)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(flag);
2012-04-16 19:23:25 +00:00
if (members)
return members->remove(name);
return false;
}
void Object::defaultValue(Value *result, int typeHint)
{
Context *ctx = 0; // ###
if (typeHint == STRING_HINT) {
if (asFunctionObject() != 0)
__qmljs_init_string(ctx, result, String::get(ctx, QLatin1String("function")));
else
__qmljs_init_string(ctx, result, String::get(ctx, QLatin1String("object")));
} else {
__qmljs_init_undefined(ctx, result);
}
}
bool FunctionObject::hasInstance(const Value &value) const
{
Q_UNUSED(value);
return false;
}
2012-05-07 14:05:05 +00:00
void FunctionObject::call(Context *ctx)
2012-04-16 19:23:25 +00:00
{
2012-05-07 14:05:05 +00:00
Q_UNUSED(ctx);
}
void FunctionObject::construct(Context *ctx)
{
2012-05-09 11:30:22 +00:00
__qmljs_init_object(ctx, &ctx->thisObject, new Object());
call(ctx);
2012-05-07 14:05:05 +00:00
}
2012-04-16 19:23:25 +00:00
2012-05-13 11:50:55 +00:00
ScriptFunction::ScriptFunction(Context *scope, IR::Function *function)
: FunctionObject(scope)
2012-05-09 13:47:55 +00:00
, function(function)
2012-05-07 14:05:05 +00:00
{
2012-05-08 09:13:02 +00:00
formalParameterCount = function->formals.size();
if (formalParameterCount) {
formalParameterList = new String*[formalParameterCount];
for (size_t i = 0; i < formalParameterCount; ++i) {
formalParameterList[i] = String::get(0, *function->formals.at(i)); // ### unique
}
2012-05-07 14:05:05 +00:00
}
2012-05-08 09:13:02 +00:00
}
ScriptFunction::~ScriptFunction()
{
delete[] formalParameterList;
}
void ScriptFunction::call(VM::Context *ctx)
{
2012-05-07 14:05:05 +00:00
function->code(ctx);
2012-04-16 19:23:25 +00:00
}
void ScriptFunction::construct(VM::Context *ctx)
{
2012-05-13 11:50:55 +00:00
__qmljs_init_object(ctx, &ctx->thisObject, new Object());
function->code(ctx);
}
2012-05-08 09:13:02 +00:00
Value *ArgumentsObject::getProperty(String *name, PropertyAttributes *attributes)
2012-04-16 19:23:25 +00:00
{
2012-05-08 09:13:02 +00:00
if (context) {
for (size_t i = 0; i < context->formalCount; ++i) {
String *formal = context->formals[i];
if (__qmljs_string_equal(context, formal, name)) {
if (attributes)
*attributes = PropertyAttributes(*attributes | WritableAttribute);
return &context->arguments[i];
}
}
}
if (Value *prop = Object::getProperty(name, attributes))
2012-05-07 14:05:05 +00:00
return prop;
return 0;
2012-04-16 19:23:25 +00:00
}