Commit ab642bc8 authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS: Add refcounting to FakeMetaObjects.

Previously they were leaked when a qmldump or the C++ exported QML
type list updated.

Just deleting the previous FakeMetaObjects is not an option, as they
might still be used in a QmlObjectValue owned by an Engine.

Reviewed-by: Erik Verbruggen
parent 62d66fcd
......@@ -147,7 +147,7 @@ public:
virtual void findMacroUsages(const CPlusPlus::Macro &macro) = 0;
virtual QList<LanguageUtils::FakeMetaObject *> exportedQmlObjects() const = 0;
virtual QList<LanguageUtils::FakeMetaObject::ConstPtr> exportedQmlObjects() const = 0;
Q_SIGNALS:
void documentUpdated(CPlusPlus::Document::Ptr doc);
......
......@@ -128,9 +128,9 @@ void FakeMetaObject::setSuperclassName(const QString &superclass)
QString FakeMetaObject::superclassName() const
{ return m_superName; }
void FakeMetaObject::setSuperclass(FakeMetaObject *superClass)
void FakeMetaObject::setSuperclass(ConstPtr superClass)
{ m_super = superClass; }
const FakeMetaObject *FakeMetaObject::superClass() const
FakeMetaObject::ConstPtr FakeMetaObject::superClass() const
{ return m_super; }
void FakeMetaObject::addEnum(const FakeMetaEnum &fakeEnum)
......
......@@ -41,6 +41,7 @@
#include <QtCore/QStringList>
#include <QtCore/QList>
#include <QtCore/QHash>
#include <QtCore/QSharedPointer>
namespace LanguageUtils {
......@@ -118,6 +119,9 @@ class LANGUAGEUTILS_EXPORT FakeMetaObject {
Q_DISABLE_COPY(FakeMetaObject);
public:
typedef QSharedPointer<FakeMetaObject> Ptr;
typedef QSharedPointer<const FakeMetaObject> ConstPtr;
class Export {
public:
QString package;
......@@ -128,7 +132,7 @@ public:
private:
QList<Export> m_exports;
const FakeMetaObject *m_super;
ConstPtr m_super;
QString m_superName;
QList<FakeMetaEnum> m_enums;
QHash<QString, int> m_enumNameToIndex;
......@@ -145,8 +149,8 @@ public:
void setSuperclassName(const QString &superclass);
QString superclassName() const;
void setSuperclass(FakeMetaObject *superClass);
const FakeMetaObject *superClass() const;
void setSuperclass(ConstPtr superClass);
ConstPtr superClass() const;
void addEnum(const FakeMetaEnum &fakeEnum);
int enumeratorCount() const;
......
......@@ -39,16 +39,14 @@
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
#include <languageutils/fakemetaobject.h>
#include "parser/qmldirparser_p.h"
#include "parser/qmljsengine_p.h"
#include "qmljs_global.h"
QT_QML_BEGIN_NAMESPACE
namespace LanguageUtils {
class FakeMetaObject;
}
namespace QmlJS {
class Bind;
......@@ -134,7 +132,7 @@ private:
bool _valid;
QList<QmlDirParser::Component> _components;
QList<QmlDirParser::Plugin> _plugins;
typedef QList<const LanguageUtils::FakeMetaObject *> FakeMetaObjectList;
typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
FakeMetaObjectList _metaObjects;
DumpStatus _dumpStatus;
......
......@@ -164,7 +164,7 @@ public:
: _xml(data)
{}
bool operator()(QMap<QString, FakeMetaObject *> *objects) {
bool operator()(QMap<QString, FakeMetaObject::Ptr> *objects) {
Q_ASSERT(objects);
_objects = objects;
......@@ -247,7 +247,7 @@ private:
}
}
FakeMetaObject *metaObject = new FakeMetaObject;
FakeMetaObject::Ptr metaObject = FakeMetaObject::Ptr(new FakeMetaObject);
if (! extends.isEmpty())
metaObject->setSuperclassName(extends);
if (! defaultPropertyName.isEmpty())
......@@ -270,11 +270,8 @@ private:
metaObject->addExport(id, QString(), ComponentVersion());
if (doInsert) {
if (doInsert)
_objects->insert(id, metaObject);
} else {
delete metaObject;
}
}
bool split(const QString &name, QString *packageName, QString *className) {
......@@ -294,7 +291,7 @@ private:
}
}
void readProperty(FakeMetaObject *metaObject)
void readProperty(FakeMetaObject::Ptr metaObject)
{
const QLatin1String tag("property");
Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);
......@@ -352,14 +349,14 @@ private:
}
}
void createProperty(FakeMetaObject *metaObject, const QString &name,
void createProperty(FakeMetaObject::Ptr metaObject, const QString &name,
const QString &type, bool isList, bool isWritable, bool isPointer) {
Q_ASSERT(metaObject);
metaObject->addProperty(FakeMetaProperty(name, type, isList, isWritable, isPointer));
}
void readEnum(FakeMetaObject *metaObject)
void readEnum(FakeMetaObject::Ptr metaObject)
{
Q_ASSERT(metaObject);
......@@ -426,7 +423,7 @@ private:
}
}
void readSignal(FakeMetaObject *metaObject)
void readSignal(FakeMetaObject::Ptr metaObject)
{
Q_ASSERT(metaObject);
QLatin1String tag("signal");
......@@ -489,7 +486,7 @@ private:
}
}
void readMethod(FakeMetaObject *metaObject)
void readMethod(FakeMetaObject::Ptr metaObject)
{
Q_ASSERT(metaObject);
QLatin1String tag("method");
......@@ -526,7 +523,7 @@ private:
metaObject->addMethod(method);
}
void readExports(FakeMetaObject *metaObject)
void readExports(FakeMetaObject::Ptr metaObject)
{
Q_ASSERT(metaObject);
QLatin1String tag("exports");
......@@ -582,12 +579,12 @@ private:
private:
QXmlStreamReader _xml;
QMap<QString, FakeMetaObject *> *_objects;
QMap<QString, FakeMetaObject::Ptr> *_objects;
};
} // end of anonymous namespace
QmlObjectValue::QmlObjectValue(const FakeMetaObject *metaObject, const QString &className,
QmlObjectValue::QmlObjectValue(FakeMetaObject::ConstPtr metaObject, const QString &className,
const QString &packageName, const ComponentVersion version, Engine *engine)
: ObjectValue(engine),
_metaObject(metaObject),
......@@ -734,7 +731,7 @@ QString QmlObjectValue::defaultPropertyName() const
QString QmlObjectValue::propertyType(const QString &propertyName) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
int propIdx = iter->propertyIndex(propertyName);
if (propIdx != -1) {
return iter->property(propIdx).typeName();
......@@ -745,7 +742,7 @@ QString QmlObjectValue::propertyType(const QString &propertyName) const
bool QmlObjectValue::isListProperty(const QString &propertyName) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
int propIdx = iter->propertyIndex(propertyName);
if (propIdx != -1) {
return iter->property(propIdx).isList();
......@@ -761,7 +758,7 @@ bool QmlObjectValue::isEnum(const QString &typeName) const
bool QmlObjectValue::isWritable(const QString &propertyName) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
int propIdx = iter->propertyIndex(propertyName);
if (propIdx != -1) {
return iter->property(propIdx).isWritable();
......@@ -772,7 +769,7 @@ bool QmlObjectValue::isWritable(const QString &propertyName) const
bool QmlObjectValue::isPointer(const QString &propertyName) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
int propIdx = iter->propertyIndex(propertyName);
if (propIdx != -1) {
return iter->property(propIdx).isPointer();
......@@ -791,7 +788,7 @@ bool QmlObjectValue::hasLocalProperty(const QString &typeName) const
bool QmlObjectValue::hasProperty(const QString &propertyName) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
int propIdx = iter->propertyIndex(propertyName);
if (propIdx != -1) {
return true;
......@@ -831,11 +828,11 @@ bool QmlObjectValue::hasChildInPackage() const
QHashIterator<QString, QmlObjectValue *> it(engine()->cppQmlTypes().types());
while (it.hasNext()) {
it.next();
const FakeMetaObject *other = it.value()->_metaObject;
FakeMetaObject::ConstPtr other = it.value()->_metaObject;
// if it has only the default no-package export, it is not really exported
if (other->exports().size() <= 1)
continue;
for (const FakeMetaObject *iter = other; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = other; iter; iter = iter->superClass()) {
if (iter == _metaObject) // this object is a parent of other
return true;
}
......@@ -843,9 +840,9 @@ bool QmlObjectValue::hasChildInPackage() const
return false;
}
bool QmlObjectValue::isDerivedFrom(const FakeMetaObject *base) const
bool QmlObjectValue::isDerivedFrom(FakeMetaObject::ConstPtr base) const
{
for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
for (FakeMetaObject::ConstPtr iter = _metaObject; iter; iter = iter->superClass()) {
if (iter == base)
return true;
}
......@@ -1946,12 +1943,12 @@ const Value *Function::invoke(const Activation *activation) const
// typing environment
////////////////////////////////////////////////////////////////////////////////
QList<const FakeMetaObject *> CppQmlTypesLoader::builtinObjects;
QList<const FakeMetaObject *> CppQmlTypesLoader::cppObjects;
QList<FakeMetaObject::ConstPtr> CppQmlTypesLoader::builtinObjects;
QList<FakeMetaObject::ConstPtr> CppQmlTypesLoader::cppObjects;
QStringList CppQmlTypesLoader::load(const QFileInfoList &xmlFiles)
{
QMap<QString, FakeMetaObject *> newObjects;
QMap<QString, FakeMetaObject::Ptr> newObjects;
QStringList errorMsgs;
foreach (const QFileInfo &xmlFile, xmlFiles) {
......@@ -1973,7 +1970,7 @@ QStringList CppQmlTypesLoader::load(const QFileInfoList &xmlFiles)
// we need to go from QList<T *> of newObjects.values() to QList<const T *>
// and there seems to be no better way
QMapIterator<QString, FakeMetaObject *> it(newObjects);
QMapIterator<QString, FakeMetaObject::Ptr> it(newObjects);
while (it.hasNext()) {
it.next();
builtinObjects.append(it.value());
......@@ -1983,7 +1980,7 @@ QStringList CppQmlTypesLoader::load(const QFileInfoList &xmlFiles)
return errorMsgs;
}
QString CppQmlTypesLoader::parseQmlTypeXml(const QByteArray &xml, QMap<QString, FakeMetaObject *> *newObjects)
QString CppQmlTypesLoader::parseQmlTypeXml(const QByteArray &xml, QMap<QString, FakeMetaObject::Ptr> *newObjects)
{
QmlXmlReader reader(xml);
if (!reader(newObjects)) {
......@@ -1995,16 +1992,16 @@ QString CppQmlTypesLoader::parseQmlTypeXml(const QByteArray &xml, QMap<QString,
return QString();
}
void CppQmlTypesLoader::setSuperClasses(QMap<QString, FakeMetaObject *> *newObjects)
void CppQmlTypesLoader::setSuperClasses(QMap<QString, FakeMetaObject::Ptr> *newObjects)
{
QMapIterator<QString, FakeMetaObject *> it(*newObjects);
QMapIterator<QString, FakeMetaObject::Ptr> it(*newObjects);
while (it.hasNext()) {
it.next();
FakeMetaObject *obj = it.value();
FakeMetaObject::Ptr obj = it.value();
const QString superName = obj->superclassName();
if (! superName.isEmpty()) {
FakeMetaObject *superClass = newObjects->value(superName);
FakeMetaObject::Ptr superClass = newObjects->value(superName);
if (superClass)
obj->setSuperclass(superClass);
else
......@@ -2013,10 +2010,10 @@ void CppQmlTypesLoader::setSuperClasses(QMap<QString, FakeMetaObject *> *newObje
}
}
void CppQmlTypes::load(Engine *engine, const QList<const FakeMetaObject *> &objects)
void CppQmlTypes::load(Engine *engine, const QList<FakeMetaObject::ConstPtr> &objects)
{
// load
foreach (const FakeMetaObject *metaObject, objects) {
foreach (FakeMetaObject::ConstPtr metaObject, objects) {
for (int i = 0; i < metaObject->exports().size(); ++i) {
const FakeMetaObject::Export &exp = metaObject->exports().at(i);
// make sure we're not loading duplicate objects
......@@ -2031,7 +2028,7 @@ void CppQmlTypes::load(Engine *engine, const QList<const FakeMetaObject *> &obje
}
// set prototypes
foreach (const FakeMetaObject *metaObject, objects) {
foreach (FakeMetaObject::ConstPtr metaObject, objects) {
foreach (const FakeMetaObject::Export &exp, metaObject->exports()) {
QmlObjectValue *objectValue = _typesByFullyQualifiedName.value(exp.packageNameVersion);
if (!objectValue || !metaObject->superClass())
......@@ -2043,7 +2040,7 @@ void CppQmlTypes::load(Engine *engine, const QList<const FakeMetaObject *> &obje
// needs to create Positioner (Qt) and Positioner (QtQuick)
bool created = true;
QmlObjectValue *v = objectValue;
const FakeMetaObject *fmo = metaObject;
FakeMetaObject::ConstPtr fmo = metaObject;
while (created && fmo->superClass()) {
QmlObjectValue *superValue = getOrCreate(exp.package, fmo->superclassName(),
fmo->superClass(), engine, &created);
......@@ -2136,7 +2133,7 @@ QmlObjectValue *CppQmlTypes::typeByQualifiedName(const QString &package, const Q
}
QmlObjectValue *CppQmlTypes::getOrCreate(const QString &package, const QString &cppName,
const FakeMetaObject *metaObject, Engine *engine, bool *created)
FakeMetaObject::ConstPtr metaObject, Engine *engine, bool *created)
{
QString typeName = cppName;
ComponentVersion version;
......
......@@ -35,6 +35,7 @@
#define QMLJS_INTERPRETER_H
#include <languageutils/componentversion.h>
#include <languageutils/fakemetaobject.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljs_global.h>
#include <qmljs/parser/qmljsastfwd_p.h>
......@@ -46,13 +47,6 @@
#include <QtCore/QSet>
#include <QtCore/QMutex>
namespace LanguageUtils {
class FakeMetaObject;
class FakeMetaMethod;
class FakeMetaProperty;
class FakeMetaEnum;
}
namespace QmlJS {
class NameId;
......@@ -452,7 +446,7 @@ private:
class QMLJS_EXPORT QmlObjectValue: public ObjectValue
{
public:
QmlObjectValue(const LanguageUtils::FakeMetaObject *metaObject, const QString &className,
QmlObjectValue(LanguageUtils::FakeMetaObject::ConstPtr metaObject, const QString &className,
const QString &packageName, const LanguageUtils::ComponentVersion version,
Engine *engine);
virtual ~QmlObjectValue();
......@@ -478,10 +472,10 @@ public:
protected:
const Value *findOrCreateSignature(int index, const LanguageUtils::FakeMetaMethod &method,
QString *methodName) const;
bool isDerivedFrom(const LanguageUtils::FakeMetaObject *base) const;
bool isDerivedFrom(LanguageUtils::FakeMetaObject::ConstPtr base) const;
private:
const LanguageUtils::FakeMetaObject *_metaObject;
LanguageUtils::FakeMetaObject::ConstPtr _metaObject;
const QString _packageName;
const LanguageUtils::ComponentVersion _componentVersion;
mutable QHash<int, const Value *> _metaSignature;
......@@ -592,20 +586,20 @@ class QMLJS_EXPORT CppQmlTypesLoader
public:
/** \return an empty list when successful, error messages otherwise. */
static QStringList load(const QFileInfoList &xmlFiles);
static QList<const LanguageUtils::FakeMetaObject *> builtinObjects;
static QList<const LanguageUtils::FakeMetaObject *> cppObjects;
static QList<LanguageUtils::FakeMetaObject::ConstPtr> builtinObjects;
static QList<LanguageUtils::FakeMetaObject::ConstPtr> cppObjects;
// parses the xml string and fills the newObjects map
static QString parseQmlTypeXml(const QByteArray &xml,
QMap<QString, LanguageUtils::FakeMetaObject *> *newObjects);
QMap<QString, LanguageUtils::FakeMetaObject::Ptr> *newObjects);
private:
static void setSuperClasses(QMap<QString, LanguageUtils::FakeMetaObject *> *newObjects);
static void setSuperClasses(QMap<QString, LanguageUtils::FakeMetaObject::Ptr> *newObjects);
};
class QMLJS_EXPORT CppQmlTypes
{
public:
void load(Interpreter::Engine *interpreter, const QList<const LanguageUtils::FakeMetaObject *> &objects);
void load(Interpreter::Engine *interpreter, const QList<LanguageUtils::FakeMetaObject::ConstPtr> &objects);
QList<Interpreter::QmlObjectValue *> typesForImport(const QString &prefix, LanguageUtils::ComponentVersion version) const;
Interpreter::QmlObjectValue *typeForImport(const QString &qualifiedName,
......@@ -623,7 +617,7 @@ public:
private:
QmlObjectValue *getOrCreate(const QString &package, const QString &cppName,
const LanguageUtils::FakeMetaObject *metaObject,
LanguageUtils::FakeMetaObject::ConstPtr metaObject,
Engine *engine, bool *created);
......
......@@ -1459,8 +1459,8 @@ static Class *lookupClass(const QString &expression, Scope *scope, TypeOfExpress
return 0;
}
static void populate(LanguageUtils::FakeMetaObject *fmo, Class *klass,
QHash<Class *, LanguageUtils::FakeMetaObject *> *classes,
static void populate(LanguageUtils::FakeMetaObject::Ptr fmo, Class *klass,
QHash<Class *, LanguageUtils::FakeMetaObject::Ptr> *classes,
TypeOfExpression &typeOf)
{
using namespace LanguageUtils;
......@@ -1539,27 +1539,27 @@ static void populate(LanguageUtils::FakeMetaObject *fmo, Class *klass,
if (!baseClass)
return;
FakeMetaObject *baseFmo = classes->value(baseClass);
FakeMetaObject::Ptr baseFmo = classes->value(baseClass);
if (!baseFmo) {
baseFmo = new FakeMetaObject;
baseFmo = FakeMetaObject::Ptr(new FakeMetaObject);
populate(baseFmo, baseClass, classes, typeOf);
}
fmo->setSuperclass(baseFmo);
}
}
QList<LanguageUtils::FakeMetaObject *> CppModelManager::exportedQmlObjects() const
QList<LanguageUtils::FakeMetaObject::ConstPtr> CppModelManager::exportedQmlObjects() const
{
using namespace LanguageUtils;
QList<FakeMetaObject *> exportedObjects;
QHash<Class *, FakeMetaObject *> classes;
QList<FakeMetaObject::ConstPtr> exportedObjects;
QHash<Class *, FakeMetaObject::Ptr> classes;
const Snapshot currentSnapshot = snapshot();
foreach (Document::Ptr doc, currentSnapshot) {
TypeOfExpression typeOf;
typeOf.init(doc, currentSnapshot);
foreach (const Document::ExportedQmlType &exportedType, doc->exportedQmlTypes()) {
FakeMetaObject *fmo = new FakeMetaObject;
FakeMetaObject::Ptr fmo(new FakeMetaObject);
fmo->addExport(exportedType.typeName, exportedType.packageName,
ComponentVersion(exportedType.majorVersion, exportedType.minorVersion));
exportedObjects += fmo;
......
......@@ -131,7 +131,7 @@ public:
virtual void findMacroUsages(const CPlusPlus::Macro &macro);
virtual QList<LanguageUtils::FakeMetaObject *> exportedQmlObjects() const;
virtual QList<LanguageUtils::FakeMetaObject::ConstPtr> exportedQmlObjects() const;
void setHeaderSuffixes(const QStringList &suffixes)
{ m_headerSuffixes = suffixes; }
......
......@@ -562,8 +562,5 @@ void ModelManager::updateCppQmlTypes()
if (!cppModelManager)
return;
QList<const LanguageUtils::FakeMetaObject *> constFMOs;
foreach (LanguageUtils::FakeMetaObject *fmo, cppModelManager->exportedQmlObjects())
constFMOs.append(fmo);
Interpreter::CppQmlTypesLoader::cppObjects = constFMOs;
Interpreter::CppQmlTypesLoader::cppObjects = cppModelManager->exportedQmlObjects();
}
......@@ -126,15 +126,15 @@ static QString qmldumpFailedMessage()
return PluginDumper::tr("Type dump of C++ plugin failed.\nCheck 'General Messages' output pane for details.");
}
static QList<const FakeMetaObject *> parseHelper(const QByteArray &xml, QString *error)
static QList<FakeMetaObject::ConstPtr> parseHelper(const QByteArray &xml, QString *error)
{
QList<const FakeMetaObject *> ret;
QMap<QString, FakeMetaObject *> newObjects;
QList<FakeMetaObject::ConstPtr> ret;
QMap<QString, FakeMetaObject::Ptr> newObjects;
*error = Interpreter::CppQmlTypesLoader::parseQmlTypeXml(xml, &newObjects);
if (error->isEmpty()) {
// convert from QList<T *> to QList<const T *>
QMapIterator<QString, FakeMetaObject *> it(newObjects);
QMapIterator<QString, FakeMetaObject::Ptr> it(newObjects);
while (it.hasNext()) {
it.next();
ret.append(it.value());
......@@ -162,7 +162,7 @@ void PluginDumper::qmlPluginTypeDumpDone(int exitCode)
const QByteArray output = process->readAllStandardOutput();
QString error;
QList<const FakeMetaObject *> objectsList = parseHelper(output, &error);
QList<FakeMetaObject::ConstPtr> objectsList = parseHelper(output, &error);
if (exitCode == 0 && !error.isEmpty()) {
libraryInfo.setDumpStatus(LibraryInfo::DumpError, tr("Type dump of C++ plugin failed. Parse error:\n'%1'").arg(error));
}
......@@ -229,7 +229,7 @@ void PluginDumper::dump(const Plugin &plugin)
libraryXmlFile.close();
QString error;
const QList<const FakeMetaObject *> objectsList = parseHelper(xml, &error);
const QList<FakeMetaObject::ConstPtr> objectsList = parseHelper(xml, &error);
if (error.isEmpty()) {
libraryInfo.setMetaObjects(objectsList);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment