1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlcompiler_p.h"
43 #include "qqmlengine.h"
44 #include "qqmlcomponent.h"
45 #include "qqmlcomponent_p.h"
46 #include "qqmlcontext.h"
47 #include "qqmlcontext_p.h"
48 #ifdef QML_THREADED_VME_INTERPRETER
49 #include "qqmlvme_p.h"
52 #include <QtCore/qdebug.h>
54 #include <private/qobject_p.h>
58 int QQmlCompiledData::indexForString(const QString &data)
60 int idx = primitives.indexOf(data);
62 idx = primitives.count();
68 int QQmlCompiledData::indexForByteArray(const QByteArray &data)
70 int idx = datas.indexOf(data);
78 int QQmlCompiledData::indexForUrl(const QUrl &data)
80 int idx = urls.indexOf(data);
88 QQmlCompiledData::QQmlCompiledData(QQmlEngine *engine)
89 : engine(engine), importCache(0), metaTypeId(-1), listMetaTypeId(-1), isRegisteredWithEngine(false),
94 bytecode.reserve(1024);
97 void QQmlCompiledData::destroy()
99 if (engine && hasEngine())
100 QQmlEnginePrivate::deleteInEngineThread(engine, this);
105 QQmlCompiledData::~QQmlCompiledData()
107 if (isRegisteredWithEngine)
108 QQmlEnginePrivate::get(engine)->unregisterCompositeType(this);
112 for (int ii = 0; ii < types.count(); ++ii) {
113 if (types.at(ii).component)
114 types.at(ii).component->release();
115 if (types.at(ii).typePropertyCache)
116 types.at(ii).typePropertyCache->release();
119 for (int ii = 0; ii < propertyCaches.count(); ++ii)
120 propertyCaches.at(ii)->release();
122 for (int ii = 0; ii < contextCaches.count(); ++ii)
123 contextCaches.at(ii)->release();
125 for (int ii = 0; ii < scripts.count(); ++ii)
126 scripts.at(ii)->release();
129 importCache->release();
131 if (rootPropertyCache)
132 rootPropertyCache->release();
135 void QQmlCompiledData::clear()
137 for (int ii = 0; ii < programs.count(); ++ii)
138 qPersistentDispose(programs[ii].bindings);
142 Returns the property cache, if one alread exists. The cache is not referenced.
144 QQmlPropertyCache *QQmlCompiledData::TypeReference::propertyCache() const
147 return typePropertyCache;
149 return component->rootPropertyCache;
153 Returns the property cache, creating one if it doesn't already exist. The cache is not referenced.
155 QQmlPropertyCache *QQmlCompiledData::TypeReference::createPropertyCache(QQmlEngine *engine)
157 if (typePropertyCache) {
158 return typePropertyCache;
160 typePropertyCache = QQmlEnginePrivate::get(engine)->cache(type->metaObject());
161 typePropertyCache->addref();
162 return typePropertyCache;
164 return component->rootPropertyCache;
169 void QQmlCompiledData::dumpInstructions()
173 qWarning().nospace() << "Index\tOperation\t\tData1\tData2\tData3\tComments";
174 qWarning().nospace() << "-------------------------------------------------------------------------------";
176 const char *instructionStream = bytecode.constData();
177 const char *endInstructionStream = bytecode.constData() + bytecode.size();
179 int instructionCount = 0;
180 while (instructionStream < endInstructionStream) {
181 QQmlInstruction *instr = (QQmlInstruction *)instructionStream;
182 dump(instr, instructionCount);
183 instructionStream += QQmlInstruction::size(instructionType(instr));
187 qWarning().nospace() << "-------------------------------------------------------------------------------";
190 int QQmlCompiledData::addInstructionHelper(QQmlInstruction::Type type, QQmlInstruction &instr)
192 #ifdef QML_THREADED_VME_INTERPRETER
193 instr.common.code = QQmlVME::instructionJumpTable()[static_cast<int>(type)];
195 instr.common.instructionType = type;
197 int ptrOffset = bytecode.size();
198 int size = QQmlInstruction::size(type);
199 if (bytecode.capacity() <= bytecode.size() + size)
200 bytecode.reserve(bytecode.size() + size + 512);
201 bytecode.append(reinterpret_cast<const char *>(&instr), size);
205 int QQmlCompiledData::nextInstructionIndex()
207 return bytecode.size();
210 QQmlInstruction *QQmlCompiledData::instruction(int index)
212 return (QQmlInstruction *)(bytecode.constData() + index);
215 QQmlInstruction::Type QQmlCompiledData::instructionType(const QQmlInstruction *instr)
217 #ifdef QML_THREADED_VME_INTERPRETER
218 void **jumpTable = QQmlVME::instructionJumpTable();
219 void *code = instr->common.code;
221 # define QML_CHECK_INSTR_CODE(I, FMT) \
222 if (jumpTable[static_cast<int>(QQmlInstruction::I)] == code) \
223 return QQmlInstruction::I;
225 FOR_EACH_QML_INSTR(QML_CHECK_INSTR_CODE)
226 Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid instruction address");
227 return static_cast<QQmlInstruction::Type>(0);
228 # undef QML_CHECK_INSTR_CODE
230 return static_cast<QQmlInstruction::Type>(instr->common.instructionType);
234 void QQmlCompiledData::initialize(QQmlEngine *engine)
236 Q_ASSERT(!hasEngine());
237 QQmlCleanup::addToEngine(engine);