Fix crash when QtQuick 2.0 wasn't imported
authorAaron Kennedy <aaron.kennedy@nokia.com>
Mon, 5 Dec 2011 14:10:59 +0000 (14:10 +0000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 5 Dec 2011 14:33:39 +0000 (15:33 +0100)
The QML compiler still tried to resolve the implicit "Component" element
within the QtQuick 2.0 namespace.

Task-number: QTBUG-23017
Change-Id: I62ae962f58787910a76f76c872daa08874b5df56
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/declarative/qml/qdeclarativecompiler.cpp
src/declarative/qml/qdeclarativecompiler_p.h
src/declarative/qml/qdeclarativecomponent_p.h
src/declarative/qml/qdeclarativeengine.cpp

index aa27d79..5500efa 100644 (file)
@@ -90,12 +90,14 @@ static QString id_string(QLatin1String("id"));
 static QString on_string(QLatin1String("on"));
 static QString Changed_string(QLatin1String("Changed"));
 static QString Component_string(QLatin1String("Component"));
+static QString Component_import_string(QLatin1String("QML/Component"));
 
 /*!
     Instantiate a new QDeclarativeCompiler.
 */
 QDeclarativeCompiler::QDeclarativeCompiler(QDeclarativePool *pool)
-: pool(pool), output(0), engine(0), unitRoot(0), unit(0), componentStats(0)
+: pool(pool), output(0), engine(0), unitRoot(0), unit(0), cachedComponentTypeRef(-1),
+  componentStats(0)
 {
     if (compilerStatDump()) 
         componentStats = pool->New<ComponentStats>();
@@ -829,6 +831,7 @@ bool QDeclarativeCompiler::compile(QDeclarativeEngine *engine,
     this->engine = 0;
     this->enginePrivate = 0;
     this->unit = 0;
+    this->cachedComponentTypeRef = -1;
     this->unitRoot = 0;
 
     return !isError();
@@ -1586,16 +1589,21 @@ bool QDeclarativeCompiler::buildSubObject(QDeclarativeScript::Object *obj, const
 
 int QDeclarativeCompiler::componentTypeRef()
 {
-    QDeclarativeType *t = QDeclarativeMetaType::qmlType(QLatin1String("QtQuick/Component"),2,0);
-    for (int ii = output->types.count() - 1; ii >= 0; --ii) {
-        if (output->types.at(ii).type == t)
-            return ii;
+    if (cachedComponentTypeRef == -1) {
+        QDeclarativeType *t = QDeclarativeMetaType::qmlType(Component_import_string,1,0);
+        for (int ii = output->types.count() - 1; ii >= 0; --ii) {
+            if (output->types.at(ii).type == t) {
+                cachedComponentTypeRef = ii;
+                return ii;
+            }
+        }
+        QDeclarativeCompiledData::TypeReference ref;
+        ref.className = Component_string;
+        ref.type = t;
+        output->types << ref;
+        cachedComponentTypeRef = output->types.count() - 1;
     }
-    QDeclarativeCompiledData::TypeReference ref;
-    ref.className = Component_string;
-    ref.type = t;
-    output->types << ref;
-    return output->types.count() - 1;
+    return cachedComponentTypeRef;
 }
 
 bool QDeclarativeCompiler::buildSignal(QDeclarativeScript::Property *prop, QDeclarativeScript::Object *obj,
index 9a67f32..77f5b86 100644 (file)
@@ -414,6 +414,7 @@ private:
     QDeclarativeEnginePrivate *enginePrivate;
     QDeclarativeScript::Object *unitRoot;
     QDeclarativeTypeData *unit;
+    int cachedComponentTypeRef;
 
     // Compiler component statistics.  Only collected if QML_COMPILER_STATS=1
     struct ComponentStat
index 49b3d03..1e6cbc8 100644 (file)
@@ -124,7 +124,7 @@ public:
     }
 };
 
-class QDeclarativeComponentAttached : public QObject
+class Q_AUTOTEST_EXPORT QDeclarativeComponentAttached : public QObject
 {
     Q_OBJECT
 public:
index ee42f0b..7d5bb59 100644 (file)
@@ -424,6 +424,8 @@ void QDeclarativeEnginePrivate::init()
         // is blocking on the thread that initialize the network access manager.
         QNetworkConfigurationManager man;
 
+        qmlRegisterType<QDeclarativeComponent>("QML", 1, 0, "Component");
+
         firstTime = false;
     }