Rex Dieter 62ead32
From 7dcda224fe73cb51a29e8946afd641a989d7209a Mon Sep 17 00:00:00 2001
Rex Dieter 62ead32
From: Simon Hausmann <simon.hausmann@qt.io>
Rex Dieter 62ead32
Date: Wed, 25 May 2016 16:22:44 +0200
Rex Dieter 62ead32
Subject: [PATCH 16/40] Fix crash with SignalTransition
Rex Dieter 62ead32
Rex Dieter 62ead32
Don't crash when using SignalTransition with a signal object instead of the
Rex Dieter 62ead32
slot used to emit the signal. A signal object is just as good.
Rex Dieter 62ead32
Rex Dieter 62ead32
Task-number: QTBUG-53596
Rex Dieter 62ead32
Change-Id: I8a419d16ec0c257c9a798a83ee5bad338794cdd2
Rex Dieter 62ead32
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Rex Dieter 62ead32
---
Rex Dieter 62ead32
 src/imports/statemachine/signaltransition.cpp      | 26 ++++++--
Rex Dieter 62ead32
 src/qml/jsruntime/qv4qobjectwrapper_p.h            |  2 +-
Rex Dieter 62ead32
 .../qmltest/statemachine/tst_signaltransition.qml  | 76 ++++++++++++++++++++++
Rex Dieter 62ead32
 3 files changed, 96 insertions(+), 8 deletions(-)
Rex Dieter 62ead32
 create mode 100644 tests/auto/qmltest/statemachine/tst_signaltransition.qml
Rex Dieter 62ead32
Rex Dieter 62ead32
diff --git a/src/imports/statemachine/signaltransition.cpp b/src/imports/statemachine/signaltransition.cpp
Rex Dieter 62ead32
index 33ee11c..4f6c769 100644
Rex Dieter 62ead32
--- a/src/imports/statemachine/signaltransition.cpp
Rex Dieter 62ead32
+++ b/src/imports/statemachine/signaltransition.cpp
Rex Dieter 62ead32
@@ -105,15 +105,27 @@ void SignalTransition::setSignal(const QJSValue &signal)
Rex Dieter 62ead32
     QV4::ExecutionEngine *jsEngine = QV8Engine::getV4(QQmlEngine::contextForObject(this)->engine());
Rex Dieter 62ead32
     QV4::Scope scope(jsEngine);
Rex Dieter 62ead32
 
Rex Dieter 62ead32
-    QV4::Scoped<QV4::QObjectMethod> qobjectSignal(scope, QJSValuePrivate::convertedToValue(jsEngine, m_signal));
Rex Dieter 62ead32
-    Q_ASSERT(qobjectSignal);
Rex Dieter 62ead32
-
Rex Dieter 62ead32
-    QObject *sender = qobjectSignal->object();
Rex Dieter 62ead32
-    Q_ASSERT(sender);
Rex Dieter 62ead32
-    QMetaMethod metaMethod = sender->metaObject()->method(qobjectSignal->methodIndex());
Rex Dieter 62ead32
+    QObject *sender;
Rex Dieter 62ead32
+    QMetaMethod signalMethod;
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+    QV4::ScopedValue value(scope, QJSValuePrivate::convertedToValue(jsEngine, m_signal));
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+    // Did we get the "slot" that can be used to invoke the signal?
Rex Dieter 62ead32
+    if (QV4::QObjectMethod *signalSlot = value->as<QV4::QObjectMethod>()) {
Rex Dieter 62ead32
+        sender = signalSlot->object();
Rex Dieter 62ead32
+        Q_ASSERT(sender);
Rex Dieter 62ead32
+        signalMethod = sender->metaObject()->method(signalSlot->methodIndex());
Rex Dieter 62ead32
+    } else if (QV4::QmlSignalHandler *signalObject = value->as<QV4::QmlSignalHandler>()) { // or did we get the signal object (the one with the connect()/disconnect() functions) ?
Rex Dieter 62ead32
+        sender = signalObject->object();
Rex Dieter 62ead32
+        Q_ASSERT(sender);
Rex Dieter 62ead32
+        signalMethod = sender->metaObject()->method(signalObject->signalIndex());
Rex Dieter 62ead32
+    } else {
Rex Dieter 62ead32
+        qmlInfo(this) << tr("Specified signal does not exist.");
Rex Dieter 62ead32
+        return;
Rex Dieter 62ead32
+    }
Rex Dieter 62ead32
 
Rex Dieter 62ead32
     QSignalTransition::setSenderObject(sender);
Rex Dieter 62ead32
-    QSignalTransition::setSignal(metaMethod.methodSignature());
Rex Dieter 62ead32
+    QSignalTransition::setSignal(signalMethod.methodSignature());
Rex Dieter 62ead32
 
Rex Dieter 62ead32
     connectTriggered();
Rex Dieter 62ead32
 }
Rex Dieter 62ead32
diff --git a/src/qml/jsruntime/qv4qobjectwrapper_p.h b/src/qml/jsruntime/qv4qobjectwrapper_p.h
Rex Dieter 62ead32
index 1126013..0fc39b2 100644
Rex Dieter 62ead32
--- a/src/qml/jsruntime/qv4qobjectwrapper_p.h
Rex Dieter 62ead32
+++ b/src/qml/jsruntime/qv4qobjectwrapper_p.h
Rex Dieter 62ead32
@@ -166,7 +166,7 @@ struct Q_QML_EXPORT QObjectMethod : public QV4::FunctionObject
Rex Dieter 62ead32
     static void markObjects(Heap::Base *that, QV4::ExecutionEngine *e);
Rex Dieter 62ead32
 };
Rex Dieter 62ead32
 
Rex Dieter 62ead32
-struct QmlSignalHandler : public QV4::Object
Rex Dieter 62ead32
+struct Q_QML_EXPORT QmlSignalHandler : public QV4::Object
Rex Dieter 62ead32
 {
Rex Dieter 62ead32
     V4_OBJECT2(QmlSignalHandler, QV4::Object)
Rex Dieter 62ead32
     V4_PROTOTYPE(signalHandlerPrototype)
Rex Dieter 62ead32
diff --git a/tests/auto/qmltest/statemachine/tst_signaltransition.qml b/tests/auto/qmltest/statemachine/tst_signaltransition.qml
Rex Dieter 62ead32
new file mode 100644
Rex Dieter 62ead32
index 0000000..0e35207
Rex Dieter 62ead32
--- /dev/null
Rex Dieter 62ead32
+++ b/tests/auto/qmltest/statemachine/tst_signaltransition.qml
Rex Dieter 62ead32
@@ -0,0 +1,76 @@
Rex Dieter 62ead32
+/****************************************************************************
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** Copyright (C) 2014 Ford Motor Company
Rex Dieter 62ead32
+** Copyright (C) 2016 The Qt Company
Rex Dieter 62ead32
+** Contact: https://www.qt.io/licensing/
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** This file is part of the test suite module of the Qt Toolkit.
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** $QT_BEGIN_LICENSE:LGPL21$
Rex Dieter 62ead32
+** Commercial License Usage
Rex Dieter 62ead32
+** Licensees holding valid commercial Qt licenses may use this file in
Rex Dieter 62ead32
+** accordance with the commercial license agreement provided with the
Rex Dieter 62ead32
+** Software or, alternatively, in accordance with the terms contained in
Rex Dieter 62ead32
+** a written agreement between you and The Qt Company. For licensing terms
Rex Dieter 62ead32
+** and conditions see http://www.qt.io/terms-conditions. For further
Rex Dieter 62ead32
+** information use the contact form at http://www.qt.io/contact-us.
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** GNU Lesser General Public License Usage
Rex Dieter 62ead32
+** Alternatively, this file may be used under the terms of the GNU Lesser
Rex Dieter 62ead32
+** General Public License version 2.1 or version 3 as published by the Free
Rex Dieter 62ead32
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
Rex Dieter 62ead32
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
Rex Dieter 62ead32
+** following information to ensure the GNU Lesser General Public License
Rex Dieter 62ead32
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
Rex Dieter 62ead32
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** As a special exception, The Qt Company gives you certain additional
Rex Dieter 62ead32
+** rights. These rights are described in The Qt Company LGPL Exception
Rex Dieter 62ead32
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+** $QT_END_LICENSE$
Rex Dieter 62ead32
+**
Rex Dieter 62ead32
+****************************************************************************/
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+import QtTest 1.1
Rex Dieter 62ead32
+import QtQml.StateMachine 1.0
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+TestCase {
Rex Dieter 62ead32
+    id: testCase
Rex Dieter 62ead32
+    StateMachine {
Rex Dieter 62ead32
+        id: machine
Rex Dieter 62ead32
+        initialState: startState
Rex Dieter 62ead32
+        State {
Rex Dieter 62ead32
+            id: startState
Rex Dieter 62ead32
+            SignalTransition {
Rex Dieter 62ead32
+                id: signalTrans
Rex Dieter 62ead32
+                signal: testCase.onMysignal
Rex Dieter 62ead32
+                targetState: finalState
Rex Dieter 62ead32
+            }
Rex Dieter 62ead32
+        }
Rex Dieter 62ead32
+        FinalState {
Rex Dieter 62ead32
+            id: finalState
Rex Dieter 62ead32
+        }
Rex Dieter 62ead32
+    }
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+    SignalSpy {
Rex Dieter 62ead32
+        id: finalStateActive
Rex Dieter 62ead32
+        target: finalState
Rex Dieter 62ead32
+        signalName: "activeChanged"
Rex Dieter 62ead32
+    }
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+    signal mysignal()
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+    name: "testSignalTransition"
Rex Dieter 62ead32
+    function test_signalTransition()
Rex Dieter 62ead32
+    {
Rex Dieter 62ead32
+        // Start statemachine, should not have reached finalState yet.
Rex Dieter 62ead32
+        machine.start()
Rex Dieter 62ead32
+        tryCompare(finalStateActive, "count", 0)
Rex Dieter 62ead32
+        tryCompare(machine, "running", true)
Rex Dieter 62ead32
+
Rex Dieter 62ead32
+        testCase.mysignal()
Rex Dieter 62ead32
+        tryCompare(finalStateActive, "count", 1)
Rex Dieter 62ead32
+        tryCompare(machine, "running", false)
Rex Dieter 62ead32
+    }
Rex Dieter 62ead32
+}
Rex Dieter 62ead32
-- 
Rex Dieter 62ead32
1.9.3
Rex Dieter 62ead32