Skip to content

Commit

Permalink
Correctly set this object when calling scope/context functions
Browse files Browse the repository at this point in the history
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 <simon.hausmann@qt.io>
  • Loading branch information
Erik Verbruggen committed Feb 15, 2018
1 parent d191278 commit b242078
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/qml/jsruntime/qv4runtime.cpp
Expand Up @@ -1064,6 +1064,8 @@ ReturnedValue Runtime::method_callQmlScopeObjectProperty(ExecutionEngine *engine
return engine->throwTypeError(error);
}

auto scopeObj = static_cast<const QmlContext &>(callData->thisObject).d()->qml->scopeObject;
callData->thisObject = QObjectWrapper::wrap(engine, scopeObj);
o->call(scope, callData);
return scope.result.asReturnedValue();
}
Expand All @@ -1077,6 +1079,8 @@ ReturnedValue Runtime::method_callQmlContextObjectProperty(ExecutionEngine *engi
return engine->throwTypeError(error);
}

auto scopeObj = static_cast<const QmlContext &>(callData->thisObject).d()->qml->context->contextData()->contextObject;
callData->thisObject = QObjectWrapper::wrap(engine, scopeObj);
o->call(scope, callData);
return scope.result.asReturnedValue();
}
Expand Down
10 changes: 10 additions & 0 deletions 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()
}
15 changes: 15 additions & 0 deletions tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
Expand Up @@ -273,6 +273,8 @@ private slots:

void lowercaseTypeNames();

void thisInQmlScope();

private:
QQmlEngine engine;
QStringList defaultImportPathList;
Expand Down Expand Up @@ -4604,6 +4606,19 @@ void tst_qqmllanguage::lowercaseTypeNames()
QCOMPARE(qmlRegisterSingletonType<QObject>("Test", 1, 0, "lowerCaseTypeName", nullptr), -1);
}

void tst_qqmllanguage::thisInQmlScope()
{
QQmlEngine engine;

QQmlComponent component(&engine, testFileUrl("thisInQmlScope.qml"));
QTRY_VERIFY(component.isReady());
VERIFY_ERRORS(0);
QScopedPointer<QObject> 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"

0 comments on commit b242078

Please sign in to comment.