Convert more objects to the new constructor scheme

Change-Id: I31b2a1ba4a93f0d4bde68eeb94f13e7224c0cd7b
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
This commit is contained in:
Lars Knoll 2014-05-09 14:32:59 +02:00 committed by Simon Hausmann
parent 0fd24cf96e
commit 133ea9d0f1
9 changed files with 69 additions and 76 deletions

View File

@ -324,12 +324,12 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory)
ScopedObject uRIErrorPrototype(scope, new (this) URIErrorPrototype::Data(errorClass));
uriErrorClass = InternalClass::create(this, URIErrorObject::staticVTable(), uRIErrorPrototype);
ScopedObject variantPrototype(scope, new (memoryManager) VariantPrototype(InternalClass::create(this, VariantPrototype::staticVTable(), objectPrototype)));
ScopedObject variantPrototype(scope, new (this) VariantPrototype::Data(InternalClass::create(this, VariantPrototype::staticVTable(), objectPrototype)));
variantClass = InternalClass::create(this, VariantObject::staticVTable(), variantPrototype);
Q_ASSERT(variantClass->prototype == variantPrototype);
Q_ASSERT(variantPrototype->internalClass()->prototype == objectPrototype);
sequencePrototype = new (memoryManager) SequencePrototype(arrayClass);
sequencePrototype = ScopedValue(scope, new (this) SequencePrototype::Data(arrayClass));
objectCtor = static_cast<HeapObject *>(new (this) ObjectCtor::Data(rootContext));
stringCtor = static_cast<HeapObject *>(new (this) StringCtor::Data(rootContext));
@ -390,8 +390,8 @@ ExecutionEngine::ExecutionEngine(EvalISelFactory *factory)
globalObject->defineDefaultProperty(QStringLiteral("TypeError"), typeErrorCtor);
globalObject->defineDefaultProperty(QStringLiteral("URIError"), uRIErrorCtor);
ScopedObject o(scope);
globalObject->defineDefaultProperty(QStringLiteral("Math"), (o = new (memoryManager) MathObject(QV4::InternalClass::create(this, MathObject::staticVTable(), objectPrototype))));
globalObject->defineDefaultProperty(QStringLiteral("JSON"), (o = new (memoryManager) JsonObject(QV4::InternalClass::create(this, JsonObject::staticVTable(), objectPrototype))));
globalObject->defineDefaultProperty(QStringLiteral("Math"), (o = new (this) MathObject::Data(QV4::InternalClass::create(this, MathObject::staticVTable(), objectPrototype))));
globalObject->defineDefaultProperty(QStringLiteral("JSON"), (o = new (this) JsonObject::Data(QV4::InternalClass::create(this, JsonObject::staticVTable(), objectPrototype))));
globalObject->defineReadonlyProperty(QStringLiteral("undefined"), Primitive::undefinedValue());
globalObject->defineReadonlyProperty(QStringLiteral("NaN"), Primitive::fromDouble(std::numeric_limits<double>::quiet_NaN()));
@ -658,7 +658,8 @@ Returned<Object> *ExecutionEngine::newURIErrorObject(const ValueRef message)
Returned<Object> *ExecutionEngine::newVariantObject(const QVariant &v)
{
Object *o = new (memoryManager) VariantObject(this, v);
Scope scope(this);
ScopedObject o(scope, new (this) VariantObject::Data(this, v));
return o->asReturned<Object>();
}

View File

@ -883,14 +883,14 @@ QString Stringify::JA(ArrayObject *a)
}
JsonObject::JsonObject(InternalClass *ic)
: Object(ic)
JsonObject::Data::Data(InternalClass *ic)
: Object::Data(ic)
{
Scope scope(ic->engine);
ScopedObject protectThis(scope, this);
ScopedObject o(scope, this);
defineDefaultProperty(QStringLiteral("parse"), method_parse, 2);
defineDefaultProperty(QStringLiteral("stringify"), method_stringify, 3);
o->defineDefaultProperty(QStringLiteral("parse"), method_parse, 2);
o->defineDefaultProperty(QStringLiteral("stringify"), method_stringify, 3);
}

View File

@ -51,12 +51,14 @@ QT_BEGIN_NAMESPACE
namespace QV4 {
struct JsonObject : Object {
struct Data : Object::Data {
Data(InternalClass *ic);
};
Q_MANAGED_TYPE(JsonObject)
V4_OBJECT
private:
typedef QSet<QV4::Object *> V4ObjectSet;
public:
JsonObject(InternalClass *ic);
static ReturnedValue method_parse(CallContext *ctx);
static ReturnedValue method_stringify(CallContext *ctx);

View File

@ -55,39 +55,39 @@ DEFINE_OBJECT_VTABLE(MathObject);
static const double qt_PI = 2.0 * ::asin(1.0);
MathObject::MathObject(InternalClass *ic)
: Object(ic)
MathObject::Data::Data(InternalClass *ic)
: Object::Data(ic)
{
Scope scope(ic->engine);
ScopedObject protectThis(scope, this);
ScopedObject m(scope, this);
defineReadonlyProperty(QStringLiteral("E"), Primitive::fromDouble(::exp(1.0)));
defineReadonlyProperty(QStringLiteral("LN2"), Primitive::fromDouble(::log(2.0)));
defineReadonlyProperty(QStringLiteral("LN10"), Primitive::fromDouble(::log(10.0)));
defineReadonlyProperty(QStringLiteral("LOG2E"), Primitive::fromDouble(1.0/::log(2.0)));
defineReadonlyProperty(QStringLiteral("LOG10E"), Primitive::fromDouble(1.0/::log(10.0)));
defineReadonlyProperty(QStringLiteral("PI"), Primitive::fromDouble(qt_PI));
defineReadonlyProperty(QStringLiteral("SQRT1_2"), Primitive::fromDouble(::sqrt(0.5)));
defineReadonlyProperty(QStringLiteral("SQRT2"), Primitive::fromDouble(::sqrt(2.0)));
m->defineReadonlyProperty(QStringLiteral("E"), Primitive::fromDouble(::exp(1.0)));
m->defineReadonlyProperty(QStringLiteral("LN2"), Primitive::fromDouble(::log(2.0)));
m->defineReadonlyProperty(QStringLiteral("LN10"), Primitive::fromDouble(::log(10.0)));
m->defineReadonlyProperty(QStringLiteral("LOG2E"), Primitive::fromDouble(1.0/::log(2.0)));
m->defineReadonlyProperty(QStringLiteral("LOG10E"), Primitive::fromDouble(1.0/::log(10.0)));
m->defineReadonlyProperty(QStringLiteral("PI"), Primitive::fromDouble(qt_PI));
m->defineReadonlyProperty(QStringLiteral("SQRT1_2"), Primitive::fromDouble(::sqrt(0.5)));
m->defineReadonlyProperty(QStringLiteral("SQRT2"), Primitive::fromDouble(::sqrt(2.0)));
defineDefaultProperty(QStringLiteral("abs"), method_abs, 1);
defineDefaultProperty(QStringLiteral("acos"), method_acos, 1);
defineDefaultProperty(QStringLiteral("asin"), method_asin, 0);
defineDefaultProperty(QStringLiteral("atan"), method_atan, 1);
defineDefaultProperty(QStringLiteral("atan2"), method_atan2, 2);
defineDefaultProperty(QStringLiteral("ceil"), method_ceil, 1);
defineDefaultProperty(QStringLiteral("cos"), method_cos, 1);
defineDefaultProperty(QStringLiteral("exp"), method_exp, 1);
defineDefaultProperty(QStringLiteral("floor"), method_floor, 1);
defineDefaultProperty(QStringLiteral("log"), method_log, 1);
defineDefaultProperty(QStringLiteral("max"), method_max, 2);
defineDefaultProperty(QStringLiteral("min"), method_min, 2);
defineDefaultProperty(QStringLiteral("pow"), method_pow, 2);
defineDefaultProperty(QStringLiteral("random"), method_random, 0);
defineDefaultProperty(QStringLiteral("round"), method_round, 1);
defineDefaultProperty(QStringLiteral("sin"), method_sin, 1);
defineDefaultProperty(QStringLiteral("sqrt"), method_sqrt, 1);
defineDefaultProperty(QStringLiteral("tan"), method_tan, 1);
m->defineDefaultProperty(QStringLiteral("abs"), method_abs, 1);
m->defineDefaultProperty(QStringLiteral("acos"), method_acos, 1);
m->defineDefaultProperty(QStringLiteral("asin"), method_asin, 0);
m->defineDefaultProperty(QStringLiteral("atan"), method_atan, 1);
m->defineDefaultProperty(QStringLiteral("atan2"), method_atan2, 2);
m->defineDefaultProperty(QStringLiteral("ceil"), method_ceil, 1);
m->defineDefaultProperty(QStringLiteral("cos"), method_cos, 1);
m->defineDefaultProperty(QStringLiteral("exp"), method_exp, 1);
m->defineDefaultProperty(QStringLiteral("floor"), method_floor, 1);
m->defineDefaultProperty(QStringLiteral("log"), method_log, 1);
m->defineDefaultProperty(QStringLiteral("max"), method_max, 2);
m->defineDefaultProperty(QStringLiteral("min"), method_min, 2);
m->defineDefaultProperty(QStringLiteral("pow"), method_pow, 2);
m->defineDefaultProperty(QStringLiteral("random"), method_random, 0);
m->defineDefaultProperty(QStringLiteral("round"), method_round, 1);
m->defineDefaultProperty(QStringLiteral("sin"), method_sin, 1);
m->defineDefaultProperty(QStringLiteral("sqrt"), method_sqrt, 1);
m->defineDefaultProperty(QStringLiteral("tan"), method_tan, 1);
}
/* copies the sign from y to x and returns the result */

View File

@ -49,9 +49,12 @@ namespace QV4 {
struct MathObject: Object
{
struct Data : Object::Data {
Data(InternalClass *ic);
};
V4_OBJECT
Q_MANAGED_TYPE(MathObject)
MathObject(InternalClass *ic);
static ReturnedValue method_abs(CallContext *context);
static ReturnedValue method_acos(CallContext *context);

View File

@ -548,18 +548,13 @@ template<>
DEFINE_OBJECT_VTABLE(QQmlRealList);
#define REGISTER_QML_SEQUENCE_METATYPE(unused, unused2, SequenceType, unused3) qRegisterMetaType<SequenceType>(#SequenceType);
SequencePrototype::SequencePrototype(InternalClass *ic)
: QV4::Object(ic)
{
FOREACH_QML_SEQUENCE_TYPE(REGISTER_QML_SEQUENCE_METATYPE)
}
#undef REGISTER_QML_SEQUENCE_METATYPE
void SequencePrototype::init()
{
FOREACH_QML_SEQUENCE_TYPE(REGISTER_QML_SEQUENCE_METATYPE)
defineDefaultProperty(QStringLiteral("sort"), method_sort, 1);
defineDefaultProperty(engine()->id_valueOf, method_valueOf, 0);
}
#undef REGISTER_QML_SEQUENCE_METATYPE
QV4::ReturnedValue SequencePrototype::method_sort(QV4::CallContext *ctx)
{

View File

@ -65,8 +65,6 @@ namespace QV4 {
struct SequencePrototype : public QV4::Object
{
SequencePrototype(QV4::InternalClass *ic);
void init();
static ReturnedValue method_valueOf(QV4::CallContext *ctx)

View File

@ -51,17 +51,17 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(VariantObject);
VariantObject::VariantObject(InternalClass *ic)
: Object(ic)
VariantObject::Data::Data(InternalClass *ic)
: Object::Data(ic)
{
}
VariantObject::VariantObject(ExecutionEngine *engine, const QVariant &value)
: Object(engine->variantClass)
VariantObject::Data::Data(ExecutionEngine *engine, const QVariant &value)
: Object::Data(engine->variantClass)
{
d()->data = value;
data = value;
if (isScarce())
engine->scarceResources.insert(d());
engine->scarceResources.insert(this);
}
QVariant VariantObject::toVariant(const QV4::ValueRef v)
@ -86,18 +86,16 @@ QVariant VariantObject::toVariant(const QV4::ValueRef v)
return QVariant();
}
bool VariantObject::isScarce() const
bool VariantObject::Data::isScarce() const
{
QVariant::Type t = d()->data.type();
QVariant::Type t = data.type();
return t == QVariant::Pixmap || t == QVariant::Image;
}
void VariantObject::destroy(Managed *that)
{
VariantObject *v = static_cast<VariantObject *>(that);
if (v->isScarce())
v->d()->node.remove();
v->~VariantObject();
v->d()->~Data();
}
bool VariantObject::isEqualTo(Managed *m, Managed *other)
@ -116,7 +114,7 @@ bool VariantObject::isEqualTo(Managed *m, Managed *other)
void VariantObject::addVmePropertyReference()
{
if (isScarce() && ++d()->vmePropertyReferenceCount == 1) {
if (d()->isScarce() && ++d()->vmePropertyReferenceCount == 1) {
// remove from the ep->scarceResources list
// since it is now no longer eligible to be
// released automatically by the engine.
@ -126,7 +124,7 @@ void VariantObject::addVmePropertyReference()
void VariantObject::removeVmePropertyReference()
{
if (isScarce() && --d()->vmePropertyReferenceCount == 0) {
if (d()->isScarce() && --d()->vmePropertyReferenceCount == 0) {
// and add to the ep->scarceResources list
// since it is now eligible to be released
// automatically by the engine.
@ -135,11 +133,6 @@ void VariantObject::removeVmePropertyReference()
}
VariantPrototype::VariantPrototype(InternalClass *ic)
: VariantObject(ic)
{
}
void VariantPrototype::init()
{
defineDefaultProperty(QStringLiteral("preserve"), method_preserve, 0);
@ -152,7 +145,7 @@ QV4::ReturnedValue VariantPrototype::method_preserve(CallContext *ctx)
{
Scope scope(ctx);
Scoped<VariantObject> o(scope, ctx->d()->callData->thisObject.as<QV4::VariantObject>());
if (o && o->isScarce())
if (o && o->d()->isScarce())
o->d()->node.remove();
return Encode::undefined();
}
@ -162,7 +155,7 @@ QV4::ReturnedValue VariantPrototype::method_destroy(CallContext *ctx)
Scope scope(ctx);
Scoped<VariantObject> o(scope, ctx->d()->callData->thisObject.as<QV4::VariantObject>());
if (o) {
if (o->isScarce())
if (o->d()->isScarce())
o->d()->node.remove();
o->d()->data = QVariant();
}

View File

@ -68,6 +68,13 @@ struct VariantObject : Object
{
struct Data : Object::Data, public ExecutionEngine::ScarceResourceData
{
Data(InternalClass *ic);
Data(ExecutionEngine *engine, const QVariant &value);
~Data() {
if (isScarce())
node.remove();
}
bool isScarce() const;
int vmePropertyReferenceCount;
};
struct __Data : public ExecutionEngine::ScarceResourceData
@ -77,14 +84,10 @@ struct VariantObject : Object
V4_OBJECT
VariantObject(InternalClass *ic);
VariantObject(ExecutionEngine *engine, const QVariant &value);
static QVariant toVariant(const ValueRef v);
void addVmePropertyReference();
void removeVmePropertyReference();
bool isScarce() const;
static void destroy(Managed *that);
static bool isEqualTo(Managed *m, Managed *other);
@ -93,8 +96,6 @@ struct VariantObject : Object
struct VariantPrototype : VariantObject
{
public:
VariantPrototype(InternalClass *ic);
void init();
static ReturnedValue method_preserve(CallContext *ctx);