Support null -> QObject and QObject -> bool conversions in v4.
authorMichael Brasser <michael.brasser@nokia.com>
Tue, 13 Mar 2012 06:24:49 +0000 (16:24 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 20 Mar 2012 04:37:59 +0000 (05:37 +0100)
Change-Id: I66602bf52986a388f9b6fe2bcd4630b862138906
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/qml/qml/v4/qv4bindings.cpp
src/qml/qml/v4/qv4compiler.cpp
src/qml/qml/v4/qv4instruction.cpp
src/qml/qml/v4/qv4instruction_p.h
tests/auto/qml/v4/data/objectToBool.qml [new file with mode: 0644]
tests/auto/qml/v4/tst_v4.cpp

index 5d367f2..5bace30 100644 (file)
@@ -1160,6 +1160,25 @@ void QV4Bindings::run(int instrIndex, quint32 &executedBlocks,
     }
     QML_V4_END_INSTR(ConvertColorToString, unaryop)
 
+    QML_V4_BEGIN_INSTR(ConvertObjectToBool, unaryop)
+    {
+        const Register &src = registers[instr->unaryop.src];
+        Register &output = registers[instr->unaryop.output];
+        // ### NaN
+        if (src.isUndefined())
+            output.setUndefined();
+        else
+            output.setbool(src.getQObject() != 0);
+    }
+    QML_V4_END_INSTR(ConvertObjectToBool, unaryop)
+
+    QML_V4_BEGIN_INSTR(ConvertNullToObject, unaryop)
+    {
+        Register &output = registers[instr->unaryop.output];
+        output.setQObject(0);
+    }
+    QML_V4_END_INSTR(ConvertNullToObject, unaryop)
+
     QML_V4_BEGIN_INSTR(ResolveUrl, unaryop)
     {
         const Register &src = registers[instr->unaryop.src];
index 09b0f38..467083a 100644 (file)
@@ -958,6 +958,7 @@ void QV4CompilerPrivate::visitMove(IR::Move *s)
             case IR::StringType: opcode = V4Instr::ConvertStringToBool; break;
             case IR::UrlType: opcode = V4Instr::ConvertUrlToBool; break;
             case IR::ColorType: opcode = V4Instr::ConvertColorToBool; break;
+            case IR::ObjectType: opcode = V4Instr::ConvertObjectToBool; break;
             default: break;
             } // switch
         } else if (targetTy == IR::IntType) {
@@ -1010,6 +1011,11 @@ void QV4CompilerPrivate::visitMove(IR::Move *s)
             case IR::StringType: opcode = V4Instr::ConvertStringToColor; break;
             default: break;
             } // switch
+        } else if (targetTy == IR::ObjectType) {
+            switch (sourceTy) {
+            case IR::NullType: opcode = V4Instr::ConvertNullToObject; break;
+            default: break;
+            } // switch
         }
         if (opcode != V4Instr::Noop) {
             V4Instr conv;
index 1ed8bd2..cb6ff40 100644 (file)
@@ -189,6 +189,12 @@ void Bytecode::dump(const V4Instr *i, int address) const
     case V4Instr::ConvertColorToString:
         INSTR_DUMP << "\t" << "ConvertColorToString" << "\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
+    case V4Instr::ConvertObjectToBool:
+        INSTR_DUMP << "\t" << "ConvertObjectToBool" << "\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
+        break;
+    case V4Instr::ConvertNullToObject:
+        INSTR_DUMP << "\t" << "ConvertNullToObject" << "\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
+        break;
     case V4Instr::ResolveUrl:
         INSTR_DUMP << "\t" << "ResolveUrl" << "\t\t" << "Input_Reg(" << i->unaryop.src << ") -> Output_Reg(" << i->unaryop.output << ")";
         break;
index d6c790e..239cb36 100644 (file)
@@ -98,6 +98,8 @@ QT_BEGIN_NAMESPACE
     F(ConvertUrlToString, unaryop) \
     F(ConvertColorToBool, unaryop) \
     F(ConvertColorToString, unaryop) \
+    F(ConvertObjectToBool, unaryop) \
+    F(ConvertNullToObject, unaryop) \
     F(ResolveUrl, unaryop) \
     F(MathSinReal, unaryop) \
     F(MathCosReal, unaryop) \
diff --git a/tests/auto/qml/v4/data/objectToBool.qml b/tests/auto/qml/v4/data/objectToBool.qml
new file mode 100644 (file)
index 0000000..8c8a67b
--- /dev/null
@@ -0,0 +1,16 @@
+import QtQuick 2.0
+
+QtObject {
+    property QtObject prop1: null
+    property QtObject prop2: QtObject {}
+
+    property bool test1: prop1 ? true : false
+    property bool test2: prop2 ? true : false
+
+    property bool test3: prop1 == false
+    property bool test4: prop1 === false
+
+    property bool test5: prop2 == false
+    property bool test6: prop2 === false
+}
+
index 1c89617..8c811f2 100644 (file)
@@ -133,6 +133,7 @@ void tst_v4::qtscript_data()
     QTest::newRow("double bool jump") << "doubleBoolJump.qml";
     QTest::newRow("unary minus") << "unaryMinus.qml";
     QTest::newRow("null qobject") << "nullQObject.qml";
+    QTest::newRow("qobject -> bool") << "objectToBool.qml";
 }
 
 void tst_v4::unnecessaryReeval()