qtdeclarative/qmljs_runtime.cpp

621 lines
19 KiB
C++
Raw Normal View History

2012-04-16 19:23:25 +00:00
#include "qmljs_runtime.h"
#include "qmljs_objects.h"
2012-05-13 11:50:55 +00:00
#include "qv4ir_p.h"
2012-05-07 14:05:05 +00:00
#include <QtCore/QDebug>
2012-04-16 19:23:25 +00:00
#include <cstdio>
#include <cassert>
2012-05-04 18:22:04 +00:00
namespace QQmlJS {
namespace VM {
2012-05-04 13:28:04 +00:00
2012-05-15 08:53:20 +00:00
Value Value::fromString(Context *ctx, const QString &s)
2012-04-16 19:23:25 +00:00
{
2012-05-15 08:53:20 +00:00
return fromString(ctx->engine->newString(s));
2012-04-16 19:23:25 +00:00
}
2012-05-14 14:03:10 +00:00
int Value::toInt32(double number)
{
if (! number || isnan(number) || isinf(number))
return +0;
return (int) trunc(number); // ###
}
int Value::toInteger(double number)
{
if (isnan(number))
return +0;
else if (! number || isinf(number))
return number;
const double v = floor(fabs(number));
return signbit(number) ? -v : v;
}
int Value::toUInt16(Context *ctx)
{
return __qmljs_to_uint16(ctx, this);
}
int Value::toInt32(Context *ctx)
{
return __qmljs_to_int32(ctx, this);
}
uint Value::toUInt32(Context *ctx)
{
return __qmljs_to_int32(ctx, this);
}
bool Value::toBoolean(Context *ctx) const
{
return __qmljs_to_boolean(ctx, this);
}
double Value::toInteger(Context *ctx) const
{
return __qmljs_to_integer(ctx, this);
}
double Value::toNumber(Context *ctx) const
{
return __qmljs_to_number(ctx, this);
}
String *Value::toString(Context *ctx) const
{
Value v;
__qmljs_to_string(ctx, &v, this);
assert(v.is(STRING_TYPE));
return v.stringValue;
}
2012-04-16 19:23:25 +00:00
extern "C" {
void __qmljs_init_closure(Context *ctx, Value *result, IR::Function *clos)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_object(result, ctx->engine->newScriptFunction(ctx, clos));
}
2012-04-16 19:23:25 +00:00
void __qmljs_string_literal_undefined(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("undefined")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_null(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("null")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_true(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("true")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_false(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("false")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_object(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("object")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_boolean(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("boolean")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_number(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("number")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_string(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("string")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_literal_function(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, ctx->engine->identifier(QLatin1String("function")));
2012-04-16 19:23:25 +00:00
}
void __qmljs_delete(Context *ctx, Value *result, const Value *value)
{
2012-05-04 13:12:37 +00:00
Q_UNIMPLEMENTED();
2012-04-16 19:23:25 +00:00
(void) ctx;
(void) result;
(void) value;
2012-05-04 13:12:37 +00:00
assert(!"TODO");
2012-04-16 19:23:25 +00:00
}
void __qmljs_instanceof(Context *ctx, Value *result, const Value *left, const Value *right)
{
if (right->type == OBJECT_TYPE) {
if (FunctionObject *function = right->objectValue->asFunctionObject()) {
bool r = function->hasInstance(*left);
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(result, r);
2012-04-16 19:23:25 +00:00
return;
}
}
__qmljs_throw_type_error(ctx, result);
}
void __qmljs_in(Context *ctx, Value *result, const Value *left, const Value *right)
{
if (right->type == OBJECT_TYPE) {
Value s;
__qmljs_to_string(ctx, &s, left);
bool r = right->objectValue->hasProperty(s.stringValue);
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(result, r);
2012-04-16 19:23:25 +00:00
} else {
__qmljs_throw_type_error(ctx, result);
}
}
int __qmljs_string_length(Context *, String *string)
{
2012-05-14 14:03:10 +00:00
return string->toQString().length();
2012-04-16 19:23:25 +00:00
}
double __qmljs_string_to_number(Context *, String *string)
{
bool ok;
2012-05-14 14:03:10 +00:00
return string->toQString().toDouble(&ok); // ### TODO
2012-04-16 19:23:25 +00:00
}
void __qmljs_string_from_number(Context *ctx, Value *result, double number)
{
String *string = String::get(ctx, QString::number(number, 'g', 16));
2012-05-15 08:53:20 +00:00
__qmljs_init_string(result, string);
2012-04-16 19:23:25 +00:00
}
bool __qmljs_string_compare(Context *, String *left, String *right)
{
2012-05-14 14:03:10 +00:00
return left->toQString() < right->toQString();
2012-04-16 19:23:25 +00:00
}
bool __qmljs_string_equal(Context *, String *left, String *right)
{
return left == right ||
(left->hashValue() == right->hashValue() &&
2012-05-14 14:03:10 +00:00
left->toQString() == right->toQString());
2012-04-16 19:23:25 +00:00
}
String *__qmljs_string_concat(Context *ctx, String *first, String *second)
{
2012-05-14 14:03:10 +00:00
return String::get(ctx, first->toQString() + second->toQString());
2012-04-16 19:23:25 +00:00
}
bool __qmljs_is_function(Context *, const Value *value)
{
return value->objectValue->asFunctionObject() != 0;
}
void __qmljs_object_default_value(Context *ctx, Value *result, Object *object, int typeHint)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(ctx);
2012-05-14 15:12:25 +00:00
object->defaultValue(ctx, result, typeHint);
2012-04-16 19:23:25 +00:00
}
void __qmljs_throw_type_error(Context *ctx, Value *result)
{
2012-05-15 08:53:20 +00:00
__qmljs_init_object(result, ctx->engine->newErrorObject(Value::fromString(ctx, QLatin1String("type error"))));
2012-04-16 19:23:25 +00:00
}
void __qmljs_new_boolean_object(Context *ctx, Value *result, bool boolean)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(&value, boolean);
__qmljs_init_object(result, ctx->engine->newBooleanObject(value));
2012-04-16 19:23:25 +00:00
}
void __qmljs_new_number_object(Context *ctx, Value *result, double number)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&value, number);
__qmljs_init_object(result, ctx->engine->newNumberObject(value));
2012-05-14 15:12:25 +00:00
result->objectValue->prototype = ctx->engine->numberPrototype.objectValue;
2012-04-16 19:23:25 +00:00
}
void __qmljs_new_string_object(Context *ctx, Value *result, String *string)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_string(&value, string);
__qmljs_init_object(result, ctx->engine->newStringObject(value));
2012-05-14 15:12:25 +00:00
result->objectValue->prototype = ctx->engine->stringPrototype.objectValue;
2012-04-16 19:23:25 +00:00
}
void __qmljs_set_property(Context *ctx, Value *object, String *name, Value *value)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(ctx);
2012-04-16 19:23:25 +00:00
object->objectValue->put(name, *value, /*flags*/ 0);
}
2012-05-03 16:06:23 +00:00
void __qmljs_set_property_boolean(Context *ctx, Value *object, String *name, bool number)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(&value, number);
2012-05-03 16:06:23 +00:00
object->objectValue->put(name, value, /*flag*/ 0);
}
2012-04-16 19:23:25 +00:00
void __qmljs_set_property_number(Context *ctx, Value *object, String *name, double number)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&value, number);
2012-04-16 19:23:25 +00:00
object->objectValue->put(name, value, /*flag*/ 0);
}
void __qmljs_set_property_string(Context *ctx, Value *object, String *name, String *s)
{
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_string(&value, s);
2012-04-16 19:23:25 +00:00
object->objectValue->put(name, value, /*flag*/ 0);
}
2012-05-09 09:28:57 +00:00
void __qmljs_set_property_closure(Context *ctx, Value *object, String *name, IR::Function *function)
{
Value value;
2012-05-13 11:50:55 +00:00
__qmljs_init_closure(ctx, &value, function);
2012-05-09 09:28:57 +00:00
object->objectValue->put(name, value, /*flag*/ 0);
}
2012-04-16 19:23:25 +00:00
void __qmljs_set_activation_property(Context *ctx, String *name, Value *value)
{
2012-05-13 11:50:55 +00:00
if (Value *prop = ctx->lookup(name))
__qmljs_copy(prop, value);
else
ctx->activation.objectValue->put(name, *value);
2012-04-16 19:23:25 +00:00
}
void __qmljs_copy_activation_property(Context *ctx, String *name, String *other)
{
2012-05-13 11:50:55 +00:00
if (Value *source = ctx->lookup(other))
__qmljs_set_activation_property(ctx, name, source);
else
assert(!"reference error");
}
2012-05-13 11:50:55 +00:00
void __qmljs_set_activation_property_boolean(Context *ctx, String *name, bool b)
2012-05-03 16:06:23 +00:00
{
2012-05-13 11:50:55 +00:00
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(&value, b);
2012-05-13 11:50:55 +00:00
__qmljs_set_activation_property(ctx, name, &value);
2012-05-03 16:06:23 +00:00
}
2012-05-13 11:50:55 +00:00
void __qmljs_set_activation_property_number(Context *ctx, String *name, double number)
2012-04-16 19:23:25 +00:00
{
2012-05-13 11:50:55 +00:00
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&value, number);
2012-05-13 11:50:55 +00:00
__qmljs_set_activation_property(ctx, name, &value);
2012-04-16 19:23:25 +00:00
}
2012-05-13 11:50:55 +00:00
void __qmljs_set_activation_property_string(Context *ctx, String *name, String *string)
2012-04-16 19:23:25 +00:00
{
2012-05-13 11:50:55 +00:00
Value value;
2012-05-15 08:53:20 +00:00
__qmljs_init_string(&value, string);
2012-05-13 11:50:55 +00:00
__qmljs_set_activation_property(ctx, name, &value);
2012-04-16 19:23:25 +00:00
}
2012-05-13 11:50:55 +00:00
void __qmljs_set_activation_property_closure(Context *ctx, String *name, IR::Function *clos)
2012-05-09 09:28:57 +00:00
{
2012-05-13 11:50:55 +00:00
Value value;
__qmljs_init_closure(ctx, &value, clos);
__qmljs_set_activation_property(ctx, name, &value);
2012-05-09 09:28:57 +00:00
}
2012-04-16 19:23:25 +00:00
void __qmljs_get_property(Context *ctx, Value *result, Value *object, String *name)
{
2012-05-09 09:04:57 +00:00
if (object->type == OBJECT_TYPE) {
object->objectValue->get(name, result);
} else {
Value o;
__qmljs_to_object(ctx, &o, object);
assert(o.type == OBJECT_TYPE);
__qmljs_get_property(ctx, result, &o, name);
}
2012-04-16 19:23:25 +00:00
}
void __qmljs_get_activation_property(Context *ctx, Value *result, String *name)
{
2012-05-13 11:50:55 +00:00
if (Value *prop = ctx->lookup(name))
*result = *prop;
else
assert(!"reference error");
2012-04-16 19:23:25 +00:00
}
void __qmljs_get_activation(Context *ctx, Value *result)
{
*result = ctx->activation;
}
void __qmljs_get_thisObject(Context *ctx, Value *result)
{
*result = ctx->thisObject;
}
void __qmljs_copy_property(Context *ctx, Value *target, String *name, Value *source, String *other)
{
2012-05-04 13:12:37 +00:00
Q_UNUSED(ctx);
Value v;
source->objectValue->get(other, &v);
target->objectValue->put(name, v);
}
2012-04-16 19:23:25 +00:00
void __qmljs_compare(Context *ctx, Value *result, const Value *x, const Value *y, bool leftFirst)
{
Value px, py;
if (leftFirst) {
__qmljs_to_primitive(ctx, &px, x, NUMBER_HINT);
__qmljs_to_primitive(ctx, &py, y, NUMBER_HINT);
} else {
__qmljs_to_primitive(ctx, &py, x, NUMBER_HINT);
__qmljs_to_primitive(ctx, &px, y, NUMBER_HINT);
}
if (px.type == STRING_TYPE && py.type == STRING_TYPE) {
bool r = __qmljs_string_compare(ctx, px.stringValue, py.stringValue);
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(result, r);
2012-04-16 19:23:25 +00:00
} else {
double nx = __qmljs_to_number(ctx, &px);
double ny = __qmljs_to_number(ctx, &py);
if (isnan(nx) || isnan(ny)) {
2012-05-15 08:53:20 +00:00
__qmljs_init_undefined(result);
2012-04-16 19:23:25 +00:00
} else {
2012-05-15 08:53:20 +00:00
__qmljs_init_boolean(result, nx < ny);
2012-04-16 19:23:25 +00:00
}
}
}
bool __qmljs_equal(Context *ctx, const Value *x, const Value *y)
{
if (x->type == y->type) {
switch ((ValueType) x->type) {
case UNDEFINED_TYPE:
return true;
case NULL_TYPE:
return true;
case BOOLEAN_TYPE:
return x->booleanValue == y->booleanValue;
break;
case NUMBER_TYPE:
return x->numberValue == y->numberValue;
case STRING_TYPE:
return __qmljs_string_equal(ctx, x->stringValue, y->stringValue);
case OBJECT_TYPE:
return x->objectValue == y->objectValue;
}
// unreachable
} else {
if (x->type == NULL_TYPE && y->type == UNDEFINED_TYPE) {
return true;
} else if (x->type == UNDEFINED_TYPE && y->type == NULL_TYPE) {
return true;
} else if (x->type == NUMBER_TYPE && y->type == STRING_TYPE) {
Value ny;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&ny, __qmljs_to_number(ctx, y));
2012-04-16 19:23:25 +00:00
return __qmljs_equal(ctx, x, &ny);
} else if (x->type == STRING_TYPE && y->type == NUMBER_TYPE) {
Value nx;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&nx, __qmljs_to_number(ctx, x));
2012-04-16 19:23:25 +00:00
return __qmljs_equal(ctx, &nx, y);
} else if (x->type == BOOLEAN_TYPE) {
Value nx;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&nx, (double) x->booleanValue);
2012-04-16 19:23:25 +00:00
return __qmljs_equal(ctx, &nx, y);
} else if (y->type == BOOLEAN_TYPE) {
Value ny;
2012-05-15 08:53:20 +00:00
__qmljs_init_number(&ny, (double) y->booleanValue);
2012-04-16 19:23:25 +00:00
return __qmljs_equal(ctx, x, &ny);
} else if ((x->type == NUMBER_TYPE || x->type == STRING_TYPE) && y->type == OBJECT_TYPE) {
Value py;
__qmljs_to_primitive(ctx, &py, y, PREFERREDTYPE_HINT);
return __qmljs_equal(ctx, x, &py);
} else if (x->type == OBJECT_TYPE && (y->type == NUMBER_TYPE || y->type == STRING_TYPE)) {
Value px;
__qmljs_to_primitive(ctx, &px, x, PREFERREDTYPE_HINT);
return __qmljs_equal(ctx, &px, y);
}
}
return false;
}
2012-05-13 11:50:55 +00:00
void __qmljs_call_activation_property(Context *context, Value *result, String *name, Value *args, int argc)
2012-04-16 19:23:25 +00:00
{
2012-05-13 11:50:55 +00:00
Value *func = context->lookup(name);
if (! func)
assert(!"reference error");
2012-04-16 19:23:25 +00:00
2012-05-13 11:50:55 +00:00
__qmljs_call_value(context, result, func, args, argc);
2012-05-09 10:00:30 +00:00
}
2012-05-13 11:50:55 +00:00
void __qmljs_call_property(Context *context, Value *result, const Value *base, String *name, Value *args, int argc)
2012-05-07 14:05:05 +00:00
{
2012-05-09 15:04:25 +00:00
Value baseObject;
Value thisObject;
if (base) {
2012-05-09 15:28:55 +00:00
baseObject = *base;
2012-05-09 15:04:25 +00:00
if (baseObject.type != OBJECT_TYPE)
2012-05-09 15:28:55 +00:00
__qmljs_to_object(context, &baseObject, &baseObject);
assert(baseObject.type == OBJECT_TYPE);
2012-05-09 15:04:25 +00:00
thisObject = baseObject;
} else {
baseObject = context->activation;
2012-05-15 08:53:20 +00:00
__qmljs_init_null(&thisObject);
2012-05-09 15:04:25 +00:00
}
2012-05-07 14:05:05 +00:00
Value func;
2012-05-09 15:04:25 +00:00
baseObject.objectValue->get(name, &func);
2012-05-07 14:05:05 +00:00
if (func.type == OBJECT_TYPE) {
if (FunctionObject *f = func.objectValue->asFunctionObject()) {
Context k;
2012-05-15 08:35:19 +00:00
Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
2012-05-14 15:12:25 +00:00
ctx->init(context->engine);
2012-05-13 11:50:55 +00:00
ctx->parent = f->scope;
if (f->needsActivation)
2012-05-15 08:53:20 +00:00
__qmljs_init_object(&ctx->activation, ctx->engine->newArgumentsObject(ctx));
else
2012-05-15 08:53:20 +00:00
__qmljs_init_null(&ctx->activation);
2012-05-13 11:50:55 +00:00
ctx->thisObject = thisObject;
ctx->formals = f->formalParameterList;
ctx->formalCount = f->formalParameterCount;
ctx->arguments = args;
ctx->argumentCount = argc;
if (argc && f->needsActivation) {
ctx->arguments = new Value[argc];
std::copy(args, args + argc, ctx->arguments);
}
2012-05-13 11:50:55 +00:00
f->call(ctx);
2012-05-09 13:47:55 +00:00
if (result)
2012-05-13 11:50:55 +00:00
__qmljs_copy(result, &ctx->result);
2012-05-09 13:47:55 +00:00
} else {
assert(!"not a function");
}
} else {
assert(!"not a callable object");
}
}
2012-05-13 11:50:55 +00:00
void __qmljs_call_value(Context *context, Value *result, const Value *func, Value *args, int argc)
2012-05-09 13:47:55 +00:00
{
Q_UNUSED(context);
2012-05-09 13:47:55 +00:00
if (func->type == OBJECT_TYPE) {
if (FunctionObject *f = func->objectValue->asFunctionObject()) {
Context k;
2012-05-15 08:35:19 +00:00
Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
2012-05-14 15:12:25 +00:00
ctx->init(context->engine);
2012-05-13 11:50:55 +00:00
ctx->parent = f->scope;
if (f->needsActivation)
2012-05-15 08:53:20 +00:00
__qmljs_init_object(&ctx->activation, ctx->engine->newArgumentsObject(ctx));
else
2012-05-15 08:53:20 +00:00
__qmljs_init_null(&ctx->activation);
__qmljs_init_null(&ctx->thisObject);
2012-05-13 11:50:55 +00:00
ctx->formals = f->formalParameterList;
ctx->formalCount = f->formalParameterCount;
ctx->arguments = args;
ctx->argumentCount = argc;
if (argc && f->needsActivation) {
2012-05-13 11:50:55 +00:00
ctx->arguments = new Value[argc];
std::copy(args, args + argc, ctx->arguments);
}
f->call(ctx);
if (result)
2012-05-13 11:50:55 +00:00
__qmljs_copy(result, &ctx->result);
2012-05-07 14:05:05 +00:00
} else {
2012-05-09 09:04:57 +00:00
assert(!"not a function");
}
} else {
assert(!"not a callable object");
}
}
2012-05-13 11:50:55 +00:00
void __qmljs_construct_activation_property(Context *context, Value *result, String *name, Value *args, int argc)
2012-05-09 13:06:30 +00:00
{
2012-05-13 11:50:55 +00:00
Value *func = context->lookup(name);
if (! func)
assert(!"reference error");
__qmljs_construct_value(context, result, func, args, argc);
}
void __qmljs_construct_value(Context *context, Value *result, const Value *func, Value *args, int argc)
{
Q_UNUSED(context);
2012-05-13 11:50:55 +00:00
if (func->type == OBJECT_TYPE) {
if (FunctionObject *f = func->objectValue->asFunctionObject()) {
Context k;
2012-05-15 08:35:19 +00:00
Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
2012-05-14 15:12:25 +00:00
ctx->init(context->engine);
2012-05-13 11:50:55 +00:00
ctx->parent = f->scope;
if (f->needsActivation)
2012-05-15 08:53:20 +00:00
__qmljs_init_object(&ctx->activation, ctx->engine->newArgumentsObject(ctx));
else
2012-05-15 08:53:20 +00:00
__qmljs_init_null(&ctx->activation);
__qmljs_init_null(&ctx->thisObject);
2012-05-13 11:50:55 +00:00
ctx->formals = f->formalParameterList;
ctx->formalCount = f->formalParameterCount;
ctx->arguments = args;
ctx->argumentCount = argc;
if (argc && f->needsActivation) {
ctx->arguments = new Value[argc];
std::copy(args, args + argc, ctx->arguments);
}
2012-05-13 11:50:55 +00:00
ctx->calledAsConstructor = true;
f->construct(ctx);
assert(ctx->thisObject.is(OBJECT_TYPE));
ctx->result = ctx->thisObject;
Value proto;
2012-05-14 15:12:25 +00:00
if (f->get(ctx->engine->identifier(QLatin1String("prototype")), &proto)) { // ### `prototype' should be a unique symbol
2012-05-13 11:50:55 +00:00
if (proto.type == OBJECT_TYPE)
ctx->thisObject.objectValue->prototype = proto.objectValue;
}
if (result)
__qmljs_copy(result, &ctx->thisObject);
} else {
assert(!"not a function");
}
} else {
assert(!"not a callable object");
}
2012-05-09 13:06:30 +00:00
}
2012-05-13 11:50:55 +00:00
void __qmljs_construct_property(Context *context, Value *result, const Value *base, String *name, Value *args, int argc)
2012-05-09 09:04:57 +00:00
{
Value func;
2012-05-09 13:06:30 +00:00
Value thisObject = *base;
if (thisObject.type != OBJECT_TYPE)
__qmljs_to_object(context, &thisObject, base);
assert(thisObject.type == OBJECT_TYPE);
thisObject.objectValue->get(name, &func);
2012-05-09 09:04:57 +00:00
if (func.type == OBJECT_TYPE) {
if (FunctionObject *f = func.objectValue->asFunctionObject()) {
Context k;
2012-05-15 08:35:19 +00:00
Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
2012-05-14 15:12:25 +00:00
ctx->init(context->engine);
2012-05-13 11:50:55 +00:00
ctx->parent = f->scope;
if (f->needsActivation)
2012-05-15 08:53:20 +00:00
__qmljs_init_object(&ctx->activation, ctx->engine->newArgumentsObject(ctx));
else
2012-05-15 08:53:20 +00:00
__qmljs_init_null(&ctx->activation);
2012-05-13 11:50:55 +00:00
ctx->thisObject = thisObject;
ctx->formals = f->formalParameterList;
ctx->formalCount = f->formalParameterCount;
ctx->arguments = args;
ctx->argumentCount = argc;
if (argc && f->needsActivation) {
ctx->arguments = new Value[argc];
std::copy(args, args + argc, ctx->arguments);
}
2012-05-13 11:50:55 +00:00
ctx->calledAsConstructor = true;
f->construct(ctx);
assert(ctx->thisObject.is(OBJECT_TYPE));
Value proto;
2012-05-14 15:12:25 +00:00
if (f->get(ctx->engine->identifier(QLatin1String("prototype")), &proto)) { // ### `prototype' should be a unique symbol
if (proto.type == OBJECT_TYPE)
2012-05-13 11:50:55 +00:00
ctx->thisObject.objectValue->prototype = proto.objectValue;
}
2012-05-09 09:04:57 +00:00
if (result)
2012-05-13 11:50:55 +00:00
__qmljs_copy(result, &ctx->thisObject);
2012-05-09 09:04:57 +00:00
} else {
assert(!"not a function");
2012-05-07 14:05:05 +00:00
}
} else {
2012-05-09 09:04:57 +00:00
assert(!"not a callable object");
2012-05-07 14:05:05 +00:00
}
}
2012-04-16 19:23:25 +00:00
} // extern "C"
2012-05-04 18:22:04 +00:00
} // namespace VM
} // namespace QQmlJS