--_inLoop;
}
+bool RewriteBinding::visit(AST::CaseBlock *ast)
+{
+ // Process the initial sequence of the case clauses.
+ for (AST::CaseClauses *it = ast->clauses; it; it = it->next) {
+ // Return the value of the last statement in the block, if this is the last `case clause'
+ // of the switch statement.
+ bool returnTheValueOfLastStatement = (it->next == 0) && (ast->defaultClause == 0) && (ast->moreClauses == 0);
+
+ if (AST::CaseClause *clause = it->clause) {
+ accept(clause->expression);
+ rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
+ }
+ }
+
+ // Process the default case clause
+ if (ast->defaultClause) {
+ // Return the value of the last statement in the block, if this is the last `case clause'
+ // of the switch statement.
+ bool rewriteTheLastStatement = (ast->moreClauses == 0);
+
+ rewriteCaseStatements(ast->defaultClause->statements, rewriteTheLastStatement);
+ }
+
+ // Process trailing `case clauses'
+ for (AST::CaseClauses *it = ast->moreClauses; it; it = it->next) {
+ // Return the value of the last statement in the block, if this is the last `case clause'
+ // of the switch statement.
+ bool returnTheValueOfLastStatement = (it->next == 0);
+
+ if (AST::CaseClause *clause = it->clause) {
+ accept(clause->expression);
+ rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
+ }
+ }
+
+ return false;
+}
+
+void RewriteBinding::rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement)
+{
+ for (AST::StatementList *it = statements; it; it = it->next) {
+ if (it->next && AST::cast<AST::BreakStatement *>(it->next->statement) != 0) {
+ // The value of the first statement followed by a `break'.
+ accept(it->statement);
+ break;
+ } else if (!it->next) {
+ if (rewriteTheLastStatement)
+ accept(it->statement);
+ else if (AST::Block *block = AST::cast<AST::Block *>(it->statement))
+ rewriteCaseStatements(block->statements, rewriteTheLastStatement);
+ }
+ }
+}
+
QString RewriteSignalHandler::operator()(const QString &code, const QString &name)
{
return QStringLiteral("(function ") + name + QStringLiteral("() { ") + code + QStringLiteral(" })");
void automaticSemicolon();
void unaryExpression();
+ void switchStatement();
private:
static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter);
}
}
+
+void tst_qdeclarativeecmascript::switchStatement()
+{
+ {
+ QDeclarativeComponent component(&engine, TEST_FILE("switchStatement.1.qml"));
+ MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
+ QVERIFY(object != 0);
+
+ // `object->value()' is the number of executed statements
+
+ object->setStringProperty("A");
+ QCOMPARE(object->value(), 5);
+
+ object->setStringProperty("S");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("D");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("F");
+ QCOMPARE(object->value(), 4);
+
+ object->setStringProperty("something else");
+ QCOMPARE(object->value(), 1);
+ }
+
+ {
+ QDeclarativeComponent component(&engine, TEST_FILE("switchStatement.2.qml"));
+ MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
+ QVERIFY(object != 0);
+
+ // `object->value()' is the number of executed statements
+
+ object->setStringProperty("A");
+ QCOMPARE(object->value(), 5);
+
+ object->setStringProperty("S");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("D");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("F");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("something else");
+ QCOMPARE(object->value(), 4);
+ }
+
+ {
+ QDeclarativeComponent component(&engine, TEST_FILE("switchStatement.3.qml"));
+ MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
+ QVERIFY(object != 0);
+
+ // `object->value()' is the number of executed statements
+
+ object->setStringProperty("A");
+ QCOMPARE(object->value(), 5);
+
+ object->setStringProperty("S");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("D");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("F");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("something else");
+ QCOMPARE(object->value(), 6);
+ }
+
+ {
+ QDeclarativeComponent component(&engine, TEST_FILE("switchStatement.4.qml"));
+ MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
+ QVERIFY(object != 0);
+
+ // `object->value()' is the number of executed statements
+
+ object->setStringProperty("A");
+ QCOMPARE(object->value(), 5);
+
+ object->setStringProperty("S");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("D");
+ QCOMPARE(object->value(), 3);
+
+ object->setStringProperty("F");
+ QCOMPARE(object->value(), 3);
+
+ QString warning = component.url().toString() + ":4: Unable to assign [undefined] to int";
+ QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
+
+ object->setStringProperty("something else");
+ }
+
+ {
+ QDeclarativeComponent component(&engine, TEST_FILE("switchStatement.5.qml"));
+ MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
+ QVERIFY(object != 0);
+
+ // `object->value()' is the number of executed statements
+
+ object->setStringProperty("A");
+ QCOMPARE(object->value(), 1);
+
+ object->setStringProperty("S");
+ QCOMPARE(object->value(), 1);
+
+ object->setStringProperty("D");
+ QCOMPARE(object->value(), 1);
+
+ object->setStringProperty("F");
+ QCOMPARE(object->value(), 1);
+
+ object->setStringProperty("something else");
+ QCOMPARE(object->value(), 1);
+ }
+}
+
QTEST_MAIN(tst_qdeclarativeecmascript)
#include "tst_qdeclarativeecmascript.moc"