Skip to content

Commit

Permalink
Support null -> QObject and QObject -> bool conversions in v4.
Browse files Browse the repository at this point in the history
Change-Id: I66602bf52986a388f9b6fe2bcd4630b862138906
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
  • Loading branch information
Michael Brasser authored and Qt by Nokia committed Mar 20, 2012
1 parent 47e0817 commit 89c57d2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/qml/qml/v4/qv4bindings.cpp
Expand Up @@ -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];
Expand Down
6 changes: 6 additions & 0 deletions src/qml/qml/v4/qv4compiler.cpp
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/qml/qml/v4/qv4instruction.cpp
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/qml/qml/v4/qv4instruction_p.h
Expand Up @@ -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) \
Expand Down
16 changes: 16 additions & 0 deletions tests/auto/qml/v4/data/objectToBool.qml
@@ -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
}

1 change: 1 addition & 0 deletions tests/auto/qml/v4/tst_v4.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 89c57d2

Please sign in to comment.