From f7c3035fa1d965dceb36892122683a5ceb6cab89 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 28 Aug 2014 13:12:53 +0200 Subject: [PATCH 4/6] QML: parse .js files as JavaScript, not QML. When importing a JS library into a QML file with the "import" keyword, that JS file was parsed in QML mode, disallowing QML keywords like "as". Task-number: QTBUG-40143 Change-Id: Ie98adceb27544732c2e96657d41170db36bff288 Reviewed-by: Simon Hausmann --- src/qml/jsruntime/qv4script.cpp | 2 +- .../qml/qqmlecmascript/data/importScriptsWithoutQmlMode.js | 7 +++++++ .../qml/qqmlecmascript/data/importScriptsWithoutQmlMode.qml | 10 ++++++++++ tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 11 +++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.js create mode 100644 tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.qml diff --git a/src/qml/jsruntime/qv4script.cpp b/src/qml/jsruntime/qv4script.cpp index 36f61a1..4d6e069 100644 --- a/src/qml/jsruntime/qv4script.cpp +++ b/src/qml/jsruntime/qv4script.cpp @@ -349,7 +349,7 @@ QV4::CompiledData::CompilationUnit *Script::precompile(IR::Module *module, Compi QQmlJS::Engine ee; QQmlJS::Lexer lexer(&ee); - lexer.setCode(source, /*line*/1, /*qml mode*/true); + lexer.setCode(source, /*line*/1, /*qml mode*/false); QQmlJS::Parser parser(&ee); parser.parseProgram(); diff --git a/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.js b/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.js new file mode 100644 index 0000000..aabcc9f --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.js @@ -0,0 +1,7 @@ +.pragma library + +var as = undefined +function isLegal() { + var as = true; + return as; +} diff --git a/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.qml b/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.qml new file mode 100644 index 0000000..17d1219 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/importScriptsWithoutQmlMode.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 + +import "importScriptsWithoutQmlMode.js" as Export + +Rectangle { + id: root + property bool success: false + + Component.onCompleted: success = Export.isLegal() +} diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 34413b2..7e9adea 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -321,6 +321,7 @@ private slots: void lazyBindingEvaluation(); void varPropertyAccessOnObjectWithInvalidContext(); void importedScriptsAccessOnObjectWithInvalidContext(); + void importedScriptsWithoutQmlMode(); void contextObjectOnLazyBindings(); void garbageCollectionDuringCreation(); @@ -7609,6 +7610,16 @@ void tst_qqmlecmascript::importedScriptsAccessOnObjectWithInvalidContext() QTRY_VERIFY(obj->property("success") == true); } +void tst_qqmlecmascript::importedScriptsWithoutQmlMode() +{ + QQmlComponent component(&engine, testFileUrl("importScriptsWithoutQmlMode.qml")); + QScopedPointer obj(component.create()); + if (obj.isNull()) + qDebug() << component.errors().first().toString(); + QVERIFY(!obj.isNull()); + QTRY_VERIFY(obj->property("success") == true); +} + void tst_qqmlecmascript::contextObjectOnLazyBindings() { QQmlComponent component(&engine, testFileUrl("contextObjectOnLazyBindings.qml")); -- 2.1.0