diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-08 06:55:57 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-08 07:23:00 (GMT) |
commit | 3baf285917e2ea3183866768807f2495010602ab (patch) | |
tree | 46b1b25adc1b7e7b988f97fc039c6c048b29d3cd | |
parent | 7d82f3875cd26c1cb0ad46ae0a9fddeb87e049d6 (diff) | |
download | Qt-3baf285917e2ea3183866768807f2495010602ab.zip Qt-3baf285917e2ea3183866768807f2495010602ab.tar.gz Qt-3baf285917e2ea3183866768807f2495010602ab.tar.bz2 |
Improve static assignment to QVariant's in the int and double case
8 files changed, 110 insertions, 7 deletions
diff --git a/src/declarative/qml/qdeclarativecompiler.cpp b/src/declarative/qml/qdeclarativecompiler.cpp index e34cd66..d2b2024 100644 --- a/src/declarative/qml/qdeclarativecompiler.cpp +++ b/src/declarative/qml/qdeclarativecompiler.cpp @@ -342,9 +342,22 @@ void QDeclarativeCompiler::genLiteralAssignment(const QMetaProperty &prop, switch(type) { case -1: { - instr.type = QDeclarativeInstruction::StoreVariant; - instr.storeString.propertyIndex = prop.propertyIndex(); - instr.storeString.value = output->indexForString(string); + if (v->value.isNumber()) { + double n = v->value.asNumber(); + if (double(int(n)) == n) { + instr.type = QDeclarativeInstruction::StoreVariantInteger; + instr.storeInteger.propertyIndex = prop.propertyIndex(); + instr.storeInteger.value = int(n); + } else { + instr.type = QDeclarativeInstruction::StoreVariantDouble; + instr.storeDouble.propertyIndex = prop.propertyIndex(); + instr.storeDouble.value = n; + } + } else { + instr.type = QDeclarativeInstruction::StoreVariant; + instr.storeString.propertyIndex = prop.propertyIndex(); + instr.storeString.value = output->indexForString(string); + } } break; case QVariant::String: diff --git a/src/declarative/qml/qdeclarativeinstruction.cpp b/src/declarative/qml/qdeclarativeinstruction.cpp index 1f8b8af..d88d06a 100644 --- a/src/declarative/qml/qdeclarativeinstruction.cpp +++ b/src/declarative/qml/qdeclarativeinstruction.cpp @@ -130,6 +130,12 @@ void QDeclarativeCompiledData::dump(QDeclarativeInstruction *instr, int idx) case QDeclarativeInstruction::StoreVariant: qWarning().nospace() << idx << "\t\t" << line << "\t" << "STORE_VARIANT\t\t" << instr->storeString.propertyIndex << "\t" << instr->storeString.value << "\t\t" << primitives.at(instr->storeString.value); break; + case QDeclarativeInstruction::StoreVariantInteger: + qWarning().nospace() << idx << "\t\t" << line << "\t" << "STORE_VARIANT_INTEGER\t\t" << instr->storeInteger.propertyIndex << "\t" << instr->storeInteger.value; + break; + case QDeclarativeInstruction::StoreVariantDouble: + qWarning().nospace() << idx << "\t\t" << line << "\t" << "STORE_VARIANT_DOUBLE\t\t" << instr->storeDouble.propertyIndex << "\t" << instr->storeDouble.value; + break; case QDeclarativeInstruction::StoreObject: qWarning().nospace() << idx << "\t\t" << line << "\t" << "STORE_OBJECT\t\t" << instr->storeObject.propertyIndex; break; diff --git a/src/declarative/qml/qdeclarativeinstruction_p.h b/src/declarative/qml/qdeclarativeinstruction_p.h index 1f3c964..e8287c0 100644 --- a/src/declarative/qml/qdeclarativeinstruction_p.h +++ b/src/declarative/qml/qdeclarativeinstruction_p.h @@ -114,6 +114,8 @@ public: StoreRectF, /* storeRect */ StoreVector3D, /* storeVector3D */ StoreVariant, /* storeString */ + StoreVariantInteger, /* storeInteger */ + StoreVariantDouble, /* storeDouble */ StoreObject, /* storeObject */ StoreVariantObject, /* storeObject */ StoreInterface, /* storeObject */ diff --git a/src/declarative/qml/qdeclarativestringconverters.cpp b/src/declarative/qml/qdeclarativestringconverters.cpp index 5c88b9a..c0f8338 100644 --- a/src/declarative/qml/qdeclarativestringconverters.cpp +++ b/src/declarative/qml/qdeclarativestringconverters.cpp @@ -82,10 +82,6 @@ QVariant QDeclarativeStringConverters::variantFromString(const QString &s) { if (s.isEmpty()) return QVariant(s); - if (s.startsWith(QLatin1Char('\'')) && s.endsWith(QLatin1Char('\''))) { - QString data = s.mid(1, s.length() - 2); - return QVariant(data); - } bool ok = false; QRectF r = rectFFromString(s, &ok); if (ok) return QVariant(r); diff --git a/src/declarative/qml/qdeclarativevme.cpp b/src/declarative/qml/qdeclarativevme.cpp index 2d1a549..0addfabd 100644 --- a/src/declarative/qml/qdeclarativevme.cpp +++ b/src/declarative/qml/qdeclarativevme.cpp @@ -345,6 +345,26 @@ QObject *QDeclarativeVME::run(QDeclarativeVMEStack<QObject *> &stack, } break; + case QDeclarativeInstruction::StoreVariantInteger: + { + QObject *target = stack.top(); + QVariant v(instr.storeInteger.value); + void *a[] = { &v, 0, &status, &flags }; + QMetaObject::metacall(target, QMetaObject::WriteProperty, + instr.storeString.propertyIndex, a); + } + break; + + case QDeclarativeInstruction::StoreVariantDouble: + { + QObject *target = stack.top(); + QVariant v(instr.storeDouble.value); + void *a[] = { &v, 0, &status, &flags }; + QMetaObject::metacall(target, QMetaObject::WriteProperty, + instr.storeString.propertyIndex, a); + } + break; + case QDeclarativeInstruction::StoreString: { QObject *target = stack.top(); diff --git a/tests/auto/declarative/qdeclarativeinstruction/tst_qdeclarativeinstruction.cpp b/tests/auto/declarative/qdeclarativeinstruction/tst_qdeclarativeinstruction.cpp index a1aceb7..f4df130 100644 --- a/tests/auto/declarative/qdeclarativeinstruction/tst_qdeclarativeinstruction.cpp +++ b/tests/auto/declarative/qdeclarativeinstruction/tst_qdeclarativeinstruction.cpp @@ -520,6 +520,24 @@ void tst_qdeclarativeinstruction::dump() data->bytecode << i; } + { + QDeclarativeInstruction i; + i.line = 51; + i.type = QDeclarativeInstruction::StoreVariantInteger; + i.storeInteger.value = 11; + i.storeInteger.propertyIndex = 32; + data->bytecode << i; + } + + { + QDeclarativeInstruction i; + i.line = 52; + i.type = QDeclarativeInstruction::StoreVariantDouble; + i.storeDouble.value = 33.7; + i.storeDouble.propertyIndex = 19; + data->bytecode << i; + } + QStringList expect; expect << "Index\tLine\tOperation\t\tData1\tData2\tData3\tComments" @@ -575,6 +593,8 @@ void tst_qdeclarativeinstruction::dump() << "47\t\tNA\tDEFER\t\t\t7" << "48\t\t48\tSTORE_IMPORTED_SCRIPT\t2" << "49\t\t50\tXXX UNKOWN INSTRUCTION\t1234" + << "50\t\t51\tSTORE_VARIANT_INTEGER\t\t32\t11" + << "51\t\t52\tSTORE_VARIANT_DOUBLE\t\t19\t33.7" << "-------------------------------------------------------------------------------"; messages = QStringList(); diff --git a/tests/auto/declarative/qdeclarativelanguage/data/assignLiteralToVariant.qml b/tests/auto/declarative/qdeclarativelanguage/data/assignLiteralToVariant.qml new file mode 100644 index 0000000..a1d33ef --- /dev/null +++ b/tests/auto/declarative/qdeclarativelanguage/data/assignLiteralToVariant.qml @@ -0,0 +1,14 @@ +import Qt 4.6 + +QtObject { + property var test1: 1 + property var test2: 1.7 + property var test3: "Hello world!" + property var test4: "#FF008800" + property var test5: "10,10,10x10" + property var test6: "10,10" + property var test7: "10x10" + property var test8: "100,100,100" + property var test9: String("#FF008800") +} + diff --git a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp index bf10a01..bfb56ba 100644 --- a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp +++ b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp @@ -87,6 +87,7 @@ private slots: void assignBasicTypes(); void assignTypeExtremes(); void assignCompositeToType(); + void assignLiteralToVariant(); void customParserTypes(); void rootAsQmlComponent(); void inlineQmlComponents(); @@ -479,6 +480,37 @@ void tst_qdeclarativelanguage::assignCompositeToType() QVERIFY(object != 0); } +// Test that literals are stored correctly in variant properties +void tst_qdeclarativelanguage::assignLiteralToVariant() +{ + QDeclarativeComponent component(&engine, TEST_FILE("assignLiteralToVariant.qml")); + VERIFY_ERRORS(0); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test1").userType(), (int)QVariant::Int); + QCOMPARE(object->property("test2").userType(), (int)QMetaType::Double); + QCOMPARE(object->property("test3").userType(), (int)QVariant::String); + QCOMPARE(object->property("test4").userType(), (int)QVariant::Color); + QCOMPARE(object->property("test5").userType(), (int)QVariant::RectF); + QCOMPARE(object->property("test6").userType(), (int)QVariant::PointF); + QCOMPARE(object->property("test7").userType(), (int)QVariant::SizeF); + QCOMPARE(object->property("test8").userType(), (int)QVariant::Vector3D); + QCOMPARE(object->property("test9").userType(), (int)QVariant::String); + + QVERIFY(object->property("test1") == QVariant(1)); + QVERIFY(object->property("test2") == QVariant((double)1.7)); + QVERIFY(object->property("test3") == QVariant(QString(QLatin1String("Hello world!")))); + QVERIFY(object->property("test4") == QVariant(QColor::fromRgb(0xFF008800))); + QVERIFY(object->property("test5") == QVariant(QRectF(10, 10, 10, 10))); + QVERIFY(object->property("test6") == QVariant(QPointF(10, 10))); + QVERIFY(object->property("test7") == QVariant(QSizeF(10, 10))); + QVERIFY(object->property("test8") == QVariant(QVector3D(100, 100, 100))); + QVERIFY(object->property("test9") == QVariant(QString(QLatin1String("#FF008800")))); + + delete object; +} + // Tests that custom parser types can be instantiated void tst_qdeclarativelanguage::customParserTypes() { |