From 8274f78ffd1a7f1b8295e7a4dd653dd107f22acb Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 15 Feb 2018 11:51:25 +0100 Subject: Correctly set this object when calling scope/context functions When a function is called that is in a QML scope or a QML context, set the 'this' object to the QML scope. Note: this patch is 5.9 specific. 5.11 has a similair issue, but the implementation is quite different, so that needs a separate fix. Task-number: QTBUG-59357 Change-Id: Ia78e012d413c40a094e957f4020502cd055ac286 Reviewed-by: Simon Hausmann (cherry picked from commit b2420780df98cb3c98553da18a5b1bc5b64e9e83) --- src/qml/jsruntime/qv4runtime.cpp | 5 +++++ tests/auto/qml/qqmllanguage/data/thisInQmlScope.qml | 10 ++++++++++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/auto/qml/qqmllanguage/data/thisInQmlScope.qml diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index 0a05c50432..5cd0443b71 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -956,6 +956,9 @@ ReturnedValue Runtime::callQmlScopeObjectProperty(ExecutionEngine *engine, int p QString error = QStringLiteral("Property '%1' of scope object is not a function").arg(propertyIndex); return engine->throwTypeError(error); } + + QObject *scopeObj = static_cast(callData->thisObject).d()->qml->scopeObject; + callData->thisObject = QObjectWrapper::wrap(engine, scopeObj); return o->call(callData); } @@ -968,6 +971,8 @@ ReturnedValue Runtime::callQmlContextObjectProperty(ExecutionEngine *engine, int return engine->throwTypeError(error); } + QObject *contextObject = static_cast(callData->thisObject).d()->qml->context->contextObject; + callData->thisObject = QObjectWrapper::wrap(engine, contextObject); return o->call(callData); } diff --git a/tests/auto/qml/qqmllanguage/data/thisInQmlScope.qml b/tests/auto/qml/qqmllanguage/data/thisInQmlScope.qml new file mode 100644 index 0000000000..e3c99e70e1 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/thisInQmlScope.qml @@ -0,0 +1,10 @@ +import QtQml 2.2 +QtObject { + property int x: 42 + property int y: 0 + function g(){ + y = this.x; + } + property var f: g + Component.onCompleted: f() +} diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 5f6185ad01..fc54e069aa 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -258,6 +258,8 @@ private slots: void valueTypeGroupPropertiesInBehavior(); + void thisInQmlScope(); + private: QQmlEngine engine; QStringList defaultImportPathList; @@ -4171,6 +4173,19 @@ void tst_qqmllanguage::valueTypeGroupPropertiesInBehavior() QCOMPARE(animation->property("easing").value().type(), QEasingCurve::InOutQuad); } +void tst_qqmllanguage::thisInQmlScope() +{ + QQmlEngine engine; + + QQmlComponent component(&engine, testFileUrl("thisInQmlScope.qml")); + QTRY_VERIFY(component.isReady()); + VERIFY_ERRORS(0); + QScopedPointer o(component.create()); + QVERIFY(!o.isNull()); + QCOMPARE(o->property("x"), QVariant(42)); + QCOMPARE(o->property("y"), QVariant(42)); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" -- cgit v1.2.3