Fix Qt.include with cached compilation units and resources
authorSimon Hausmann <simon.hausmann@digia.com>
Mon, 4 Aug 2014 13:27:24 +0000 (15:27 +0200)
committerSimon Hausmann <simon.hausmann@digia.com>
Wed, 6 Aug 2014 07:56:49 +0000 (09:56 +0200)
Similar to the worker scripts we also need to do a lookup for cached
scripts here. Added also a test to ensure that Qt.include works correctly
from Qt resources.

Change-Id: Idb67af3da4b0cc91edbd3d2746d074fd68ed8bf0
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/jsruntime/qv4include.cpp
tests/auto/qml/qqmlecmascript/qqmlecmascript.pro
tests/auto/qml/qqmlecmascript/qqmlecmascript.qrc [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp

index d5bae0e..2661ecc 100644 (file)
@@ -210,20 +210,28 @@ QV4::ReturnedValue QV4Include::method_include(QV4::CallContext *ctx)
         result = i->result();
 
     } else {
+        QScopedPointer<QV4::Script> script;
 
-        QFile f(localFile);
+        if (const QQmlPrivate::CachedQmlUnit *cachedUnit = QQmlMetaType::findCachedCompilationUnit(url)) {
+            QV4::CompiledData::CompilationUnit *jsUnit = cachedUnit->createCompilationUnit();
+            script.reset(new QV4::Script(v4, qmlcontextobject, jsUnit));
+        } else {
+            QFile f(localFile);
 
-        if (f.open(QIODevice::ReadOnly)) {
-            QByteArray data = f.readAll();
-            QString code = QString::fromUtf8(data);
-            QmlIR::Document::removeScriptPragmas(code);
+            if (f.open(QIODevice::ReadOnly)) {
+                QByteArray data = f.readAll();
+                QString code = QString::fromUtf8(data);
+                QmlIR::Document::removeScriptPragmas(code);
 
-            QV4::Script script(v4, qmlcontextobject, code, url.toString());
+                script.reset(new QV4::Script(v4, qmlcontextobject, code, url.toString()));
+            }
+        }
 
+        if (!script.isNull()) {
             QV4::ExecutionContext *ctx = v4->currentContext();
-            script.parse();
+            script->parse();
             if (!v4->hasException)
-                script.run();
+                script->run();
             if (v4->hasException) {
                 QV4::ScopedValue ex(scope, ctx->catchException());
                 result = resultValue(v4, Exception);
index 6193ee7..6f3f765 100644 (file)
@@ -9,6 +9,8 @@ HEADERS += testtypes.h \
            ../../shared/testhttpserver.h
 INCLUDEPATH += ../../shared
 
+RESOURCES += qqmlecmascript.qrc
+
 include (../../shared/util.pri)
 
 # QMAKE_CXXFLAGS = -fprofile-arcs -ftest-coverage
diff --git a/tests/auto/qml/qqmlecmascript/qqmlecmascript.qrc b/tests/auto/qml/qqmlecmascript/qqmlecmascript.qrc
new file mode 100644 (file)
index 0000000..e0e29ad
--- /dev/null
@@ -0,0 +1,8 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource>
+<file>data/include.qml</file>
+<file>data/include.js</file>
+<file>data/js/include2.js</file>
+<file>data/js/include3.js</file>
+</qresource>
+</RCC>
index a9486a8..34413b2 100644 (file)
@@ -6016,6 +6016,23 @@ void tst_qqmlecmascript::include()
 
     delete o;
     }
+
+    // include from resources
+    {
+    QQmlComponent component(&engine, QUrl("qrc:///data/include.qml"));
+    QObject *o = component.create();
+    QVERIFY(o != 0);
+
+    QCOMPARE(o->property("test0").toInt(), 99);
+    QCOMPARE(o->property("test1").toBool(), true);
+    QCOMPARE(o->property("test2").toBool(), true);
+    QCOMPARE(o->property("test2_1").toBool(), true);
+    QCOMPARE(o->property("test3").toBool(), true);
+    QCOMPARE(o->property("test3_1").toBool(), true);
+
+    delete o;
+    }
+
 }
 
 void tst_qqmlecmascript::includeRemoteSuccess()