From 9333ea8649838d7e0400b0e94c8cbd4fa5d216b0 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 9 Mar 2018 15:24:49 +0100 Subject: Ensure we read context properties before the global object Also global variables declared in a .pragma library script should not be saved in the global object, as the script has it's on context where those variables live. [ChangeLog][QtQml] Properties of the JS global object will now be looked up after local properties in the QML object. This can lead to runtime incompatibilities if your qml file is named the same as a property of the global object (e.g. Date.qml). Task-number: QTBUG-51581 Change-Id: I108aea4c76d088ca8c2124700f91e8eac3fc19f3 Reviewed-by: Lars Knoll --- .../auto/qml/qqmlecmascript/data/include_pragma_shadow.js | 7 +++++++ .../qml/qqmlecmascript/data/include_pragma_shadow.qml | 15 +++++++++++++++ tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.js create mode 100644 tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.qml (limited to 'tests/auto/qml/qqmlecmascript') diff --git a/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.js b/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.js new file mode 100644 index 0000000000..500f04bec7 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.js @@ -0,0 +1,7 @@ +.pragma library +var Shadowed = 2; +var global = (function(){return this})() + +// set Shadowed on the global object as well. This should be different from +// the variable above, as the library has it's on context +global.Shadowed = 1; diff --git a/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.qml b/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.qml new file mode 100644 index 0000000000..7cac09d342 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/include_pragma_shadow.qml @@ -0,0 +1,15 @@ +import QtQuick 2.0 +import "include_pragma_shadow.js" as Shadowed +import "include_pragma_shadow.js" as Other + +Item { + property bool result + + Component.onCompleted: { + result = false; + var global = (function(){return this})() + if (Shadowed.Shadowed === 2 && Other.Shadowed === 2 && global.Shadowed === 1) + result = true; + } +} + diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 0fa4c03dcb..5959ecc19f 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -6367,6 +6367,14 @@ void tst_qqmlecmascript::include() delete o; } + // Including file with ".pragma library", shadowing a global var + { + QQmlComponent component(&engine, testFileUrl("include_pragma_shadow.qml")); + QScopedPointer o(component.create()); + QVERIFY(!o.isNull()); + QCOMPARE(o->property("result").toBool(), true); + } + // Remote - error { TestHTTPServer server; -- cgit v1.2.3