From 4909773f8162de49830d65e886747c11fff72934 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 20 Mar 2018 12:46:58 +0100 Subject: Fix calling Qt.binding() on bound functions Calling Qt.binding() on a bound function object is a valid use case and used to work until Qt 5.8. The problem was that we optimized the code in QQmlBinding and QQmlJavascriptExpression to directly work on a QV4::Function, so this wouldn't work anymore. To fix this make sure recursive calls to Function.bind() are unrolled (so that the BoundFunction's target is never a bound function itself), then add the bound function as an optional member to the QQmlBinding and use it's bound arguments if present. Task-number: QTBUG-61927 Change-Id: I472214ddd82fc2a1212efd9b769861fc43d2ddaf Reviewed-by: Simon Hausmann --- .../qqmlecmascript/data/bindingBoundFunctions.qml | 34 ++++++++++++++++++++++ .../auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 12 ++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/auto/qml/qqmlecmascript/data/bindingBoundFunctions.qml (limited to 'tests/auto/qml/qqmlecmascript') diff --git a/tests/auto/qml/qqmlecmascript/data/bindingBoundFunctions.qml b/tests/auto/qml/qqmlecmascript/data/bindingBoundFunctions.qml new file mode 100644 index 0000000000..8dbd2fd3d9 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/bindingBoundFunctions.qml @@ -0,0 +1,34 @@ +import QtQuick 2.6 + +QtObject { + property bool success: false + property var num: 100 + property var simple: 0 + property var complex: 0 + + + Component.onCompleted: { + function s(x) { + return x + } + function c(x) { + return x + num + } + + var bound = s.bind(undefined, 100) + simple = Qt.binding(bound) + if (simple != 100) + return; + var bound = c.bind(undefined, 100) + complex = Qt.binding(bound); + + if (complex != 200) + return; + num = 0; + if (complex != 100) + return; + + print("success!!!"); + success = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index c0cf123243..f40a9758f7 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -289,6 +289,7 @@ private slots: void withStatement(); void tryStatement(); void replaceBinding(); + void bindingBoundFunctions(); void deleteRootObjectInCreation(); void onDestruction(); void onDestructionViaGC(); @@ -7247,6 +7248,17 @@ void tst_qqmlecmascript::replaceBinding() delete obj; } +void tst_qqmlecmascript::bindingBoundFunctions() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("bindingBoundFunctions.qml")); + QObject *obj = c.create(); + QVERIFY(obj != nullptr); + + QVERIFY(obj->property("success").toBool()); + delete obj; +} + void tst_qqmlecmascript::deleteRootObjectInCreation() { { -- cgit v1.2.3