qmljsinterpreter.cpp 97.6 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
29
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
30
31
32
33
**
**************************************************************************/

#include "qmljsinterpreter.h"
34
#include "qmljsevaluate.h"
35
#include "qmljslink.h"
36
#include "qmljsbind.h"
37
#include "qmljsscopebuilder.h"
38
#include "qmljstypedescriptionreader.h"
39
#include "qmljsscopeastpath.h"
40
#include "parser/qmljsast_p.h"
41

42
43
#include <languageutils/fakemetaobject.h>

44
#include <QtCore/QFile>
45
#include <QtCore/QDir>
46
47
#include <QtCore/QString>
#include <QtCore/QStringList>
48
49
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
50
#include <QtCore/QXmlStreamReader>
51
#include <QtCore/QProcess>
52
53
#include <QtCore/QDebug>

54
using namespace LanguageUtils;
55
using namespace QmlJS::Interpreter;
56
using namespace QmlJS::AST;
57

Roberto Raggi's avatar
Roberto Raggi committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace {

class LookupMember: public MemberProcessor
{
    QString _name;
    const Value *_value;

    bool process(const QString &name, const Value *value)
    {
        if (_value)
            return false;

        if (name == _name) {
            _value = value;
            return false;
        }

        return true;
    }

public:
    LookupMember(const QString &name)
        : _name(name), _value(0) {}

    const Value *value() const { return _value; }

    virtual bool processProperty(const QString &name, const Value *value)
    {
        return process(name, value);
    }

    virtual bool processEnumerator(const QString &name, const Value *value)
    {
        return process(name, value);
    }

    virtual bool processSignal(const QString &name, const Value *value)
    {
        return process(name, value);
    }

    virtual bool processSlot(const QString &name, const Value *value)
    {
        return process(name, value);
    }

    virtual bool processGeneratedSlot(const QString &name, const Value *value)
    {
        return process(name, value);
    }
};

110
111
class MetaFunction: public FunctionValue
{
112
    FakeMetaMethod _method;
113
114

public:
115
    MetaFunction(const FakeMetaMethod &method, Engine *engine)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        : FunctionValue(engine), _method(method)
    {
    }

    virtual const Value *returnValue() const
    {
        return engine()->undefinedValue();
    }

    virtual int argumentCount() const
    {
        return _method.parameterNames().size();
    }

    virtual const Value *argument(int) const
    {
        return engine()->undefinedValue();
    }

    virtual QString argumentName(int index) const
    {
        if (index < _method.parameterNames().size())
            return _method.parameterNames().at(index);

        return FunctionValue::argumentName(index);
    }

    virtual bool isVariadic() const
    {
        return false;
    }

    virtual const Value *invoke(const Activation *) const
    {
        return engine()->undefinedValue();
    }
};

154
155
class QmlXmlReader
{
156
157
    Q_DECLARE_TR_FUNCTIONS(QmlJS::Interpreter::QmlXmlReader)

158
159
160
public:
    QmlXmlReader(QIODevice *dev)
        : _xml(dev)
Erik Verbruggen's avatar
Erik Verbruggen committed
161
        , _objects(0)
162
163
    {}

164
165
166
167
    QmlXmlReader(const QByteArray &data)
        : _xml(data)
    {}

168
    bool operator()(QMap<QString, FakeMetaObject::Ptr> *objects) {
169
170
171
172
173
174
175
        Q_ASSERT(objects);
        _objects = objects;

        if (_xml.readNextStartElement()) {
            if (_xml.name() == "module")
                readModule();
            else
176
                _xml.raiseError(tr("The file is not module file."));
177
178
179
180
181
182
183
184
185
186
187
        }

        return !_xml.error();
    }

    QString errorMessage() const {
        return _xml.errorString();
    }

private:
    void unexpectedElement(const QStringRef &child, const QString &parent) {
188
        _xml.raiseError(tr("Unexpected element <%1> in <%2>").arg(child.toString(), parent));
189
190
191
192
193
194
195
196
    }

    void ignoreAttr(const QXmlStreamAttribute &attr) {
        qDebug() << "** ignoring attribute" << attr.name().toString()
                 << "in tag" << _xml.name();
    }

    void invalidAttr(const QString &value, const QString &attrName, const QString &tag) {
197
        _xml.raiseError(tr("invalid value '%1' for attribute %2 in <%3>").arg(value, attrName, tag));
198
199
200
    }

    void noValidAttr(const QString &attrName, const QString &tag) {
201
        _xml.raiseError(tr("<%1> has no valid %2 attribute").arg(tag, attrName));
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    }

    void readModule()
    {
        Q_ASSERT(_xml.isStartElement() && _xml.name() == QLatin1String("module"));

        foreach (const QXmlStreamAttribute &attr, _xml.attributes())
            ignoreAttr(attr);

        while (_xml.readNextStartElement()) {
            if (_xml.name() == QLatin1String("type"))
                readType();
            else
                unexpectedElement(_xml.name(), QLatin1String("module"));
        }
    }

    void readType()
    {
        const QLatin1String tag("type");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

224
        bool doInsert = true;
225
        QString name, defaultPropertyName;
226
        ComponentVersion version;
227
        QString extends;
228
        QString id;
229
230
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
231
232
                id = attr.value().toString();
                if (id.isEmpty()) {
233
234
235
                    invalidAttr(name, QLatin1String("name"), tag);
                    return;
                }
236
237
            } else if (attr.name() == QLatin1String("defaultProperty")) {
                defaultPropertyName = attr.value().toString();
238
239
240
            } else if (attr.name() == QLatin1String("extends")) {
                if (! attr.value().isEmpty())
                    extends = attr.value().toString();
241
242
243
244
245

                if (extends == name) {
                    invalidAttr(extends, QLatin1String("extends"), tag);
                    doInsert = false;
                }
246
247
248
249
250
            } else {
                ignoreAttr(attr);
            }
        }

251
        FakeMetaObject::Ptr metaObject = FakeMetaObject::Ptr(new FakeMetaObject);
252
253
        if (! extends.isEmpty())
            metaObject->setSuperclassName(extends);
254
255
        if (! defaultPropertyName.isEmpty())
            metaObject->setDefaultPropertyName(defaultPropertyName);
256
257
258
259
260
261
262
263
264
265

        while (_xml.readNextStartElement()) {
            if (_xml.name() == QLatin1String("property"))
                readProperty(metaObject);
            else if (_xml.name() == QLatin1String("enum"))
                readEnum(metaObject);
            else if (_xml.name() == QLatin1String("signal"))
                readSignal(metaObject);
            else if (_xml.name() == QLatin1String("method"))
                readMethod(metaObject);
266
267
            else if (_xml.name() == QLatin1String("exports"))
                readExports(metaObject);
268
269
270
271
            else
                unexpectedElement(_xml.name(), tag);
        }

272
        metaObject->addExport(id, QString(), ComponentVersion());
273

274
        if (doInsert)
275
            _objects->insert(id, metaObject);
276
277
278
    }

    bool split(const QString &name, QString *packageName, QString *className) {
279
        int dotIdx = name.lastIndexOf(QLatin1Char('.'));
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
        if (dotIdx != -1) {
            if (packageName)
                *packageName = name.left(dotIdx);
            if (className)
                *className = name.mid(dotIdx + 1);
            return true;
        } else {
            if (packageName)
                packageName->clear();
            if (className)
                *className = name;
            return false;
        }
    }

295
    void readProperty(FakeMetaObject::Ptr metaObject)
296
297
298
299
300
301
    {
        const QLatin1String tag("property");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name, type;
        bool isList = false;
302
303
        bool isWritable = false;
        bool isPointer = false;
304
305
306
307
308
309
310
311
312
313
314
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else if (attr.name() == QLatin1String("type")) {
                type = attr.value().toString();
            } else if (attr.name() == QLatin1String("isList")) {
                if (attr.value() == QLatin1String("true")) {
                    isList = true;
                } else if (attr.value() == QLatin1String("false")) {
                    isList = false;
                } else {
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
                    invalidAttr(attr.value().toString(), QLatin1String("isList"), tag);
                    return;
                }
            } else if (attr.name() == QLatin1String("isWritable")) {
                if (attr.value() == QLatin1String("true")) {
                    isWritable = true;
                } else if (attr.value() == QLatin1String("false")) {
                    isWritable = false;
                } else {
                    invalidAttr(attr.value().toString(), QLatin1String("isWritable"), tag);
                    return;
                }
            } else if (attr.name() == QLatin1String("isPointer")) {
                if (attr.value() == QLatin1String("true")) {
                    isPointer = true;
                } else if (attr.value() == QLatin1String("false")) {
                    isPointer = false;
                } else {
                    invalidAttr(attr.value().toString(), QLatin1String("isPointer"), tag);
334
335
336
337
338
339
340
341
342
343
344
345
                    return;
                }
            } else {
                ignoreAttr(attr);
            }
        }

        if (name.isEmpty())
            noValidAttr(QLatin1String("name"), tag);
        else if (type.isEmpty())
            noValidAttr(QLatin1String("type"), tag);
        else
346
            createProperty(metaObject, name, type, isList, isWritable, isPointer);
347
348
349
350
351
352

        while (_xml.readNextStartElement()) {
            unexpectedElement(_xml.name(), tag);
        }
    }

353
    void createProperty(FakeMetaObject::Ptr metaObject, const QString &name,
354
                        const QString &type, bool isList, bool isWritable, bool isPointer) {
355
356
        Q_ASSERT(metaObject);

357
        metaObject->addProperty(FakeMetaProperty(name, type, isList, isWritable, isPointer));
358
359
    }

360
    void readEnum(FakeMetaObject::Ptr metaObject)
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    {
        Q_ASSERT(metaObject);

        QLatin1String tag("enum");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else {
                ignoreAttr(attr);
            }
        }

        if (name.isEmpty()) {
            noValidAttr(QLatin1String("name"), tag);
            return;
        }

        FakeMetaEnum metaEnum(name);

        while (_xml.readNextStartElement()) {
            if (_xml.name() == QLatin1String("enumerator"))
                readEnumerator(&metaEnum);
            else
                unexpectedElement(_xml.name(), tag);
        }
389
390

        metaObject->addEnum(metaEnum);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    }

    void readEnumerator(FakeMetaEnum *metaEnum)
    {
        Q_ASSERT(metaEnum);

        QLatin1String tag("enumerator");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name;
        int value = 0;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else if (attr.name() == QLatin1String("value")) {
                const QString valueStr = attr.value().toString();
                bool ok = false;
                value = valueStr.toInt(&ok);
                if (!ok) {
                    invalidAttr(valueStr, QLatin1String("value"), tag);
                }
            } else {
                ignoreAttr(attr);
            }
        }

        if (name.isEmpty())
            noValidAttr(QLatin1String("name"), tag);
        else
            metaEnum->addKey(name, value);

        while (_xml.readNextStartElement()) {
            unexpectedElement(_xml.name(), tag);
        }
    }

427
    void readSignal(FakeMetaObject::Ptr metaObject)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    {
        Q_ASSERT(metaObject);
        QLatin1String tag("signal");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else {
                ignoreAttr(attr);
            }
        }

        if (name.isEmpty()) {
            noValidAttr(QLatin1String("name"), tag);
            return;
        }

        FakeMetaMethod method(name);
        method.setMethodType(FakeMetaMethod::Signal);

        while (_xml.readNextStartElement()) {
            if (_xml.name() == QLatin1String("param")) {
                readParam(&method);
            } else {
                unexpectedElement(_xml.name(), tag);
            }
        }
457
458

        metaObject->addMethod(method);
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    }

    void readParam(FakeMetaMethod *method)
    {
        Q_ASSERT(method);
        QLatin1String tag("param");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name, type;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else if (attr.name() == QLatin1String("type")) {
                type = attr.value().toString();
473
            } else if (attr.name() == QLatin1String("isPointer")) {
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
            } else {
                ignoreAttr(attr);
            }
        }

        // note: name attribute is optional
        if (type.isEmpty())
            noValidAttr(QLatin1String("type"), tag);

        method->addParameter(name, type);

        while (_xml.readNextStartElement()) {
            unexpectedElement(_xml.name(), tag);
        }
    }

490
    void readMethod(FakeMetaObject::Ptr metaObject)
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
    {
        Q_ASSERT(metaObject);
        QLatin1String tag("method");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name, type;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
            } else if (attr.name() == QLatin1String("type")) {
                type = attr.value().toString();
            } else {
                ignoreAttr(attr);
            }
        }

        // note: type attribute is optional, in which case it's a void method.
        if (name.isEmpty()) {
            noValidAttr(QLatin1String("name"), tag);
            return;
        }

        FakeMetaMethod method(name, type);
514
        method.setMethodType(FakeMetaMethod::Slot);
515
516
517
518
519
520
521
522

        while (_xml.readNextStartElement()) {
            if (_xml.name() == QLatin1String("param")) {
                readParam(&method);
            } else {
                unexpectedElement(_xml.name(), tag);
            }
        }
523
524

        metaObject->addMethod(method);
525
526
    }

527
    void readExports(FakeMetaObject::Ptr metaObject)
528
529
530
531
532
533
534
535
536
537
    {
        Q_ASSERT(metaObject);
        QLatin1String tag("exports");
        QLatin1String childTag("export");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        while (_xml.readNextStartElement()) {
            if (_xml.name() == childTag) {
                QString type;
                QString package;
538
                ComponentVersion version;
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
                foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
                    if (attr.name() == QLatin1String("module")) {
                        package = attr.value().toString();
                    } else if (attr.name() == QLatin1String("type")) {
                        type = attr.value().toString();
                    } else if (attr.name() == QLatin1String("version")) {
                        QString versionStr = attr.value().toString();
                        int dotIdx = versionStr.indexOf('.');
                        if (dotIdx == -1) {
                            bool ok = false;
                            const int major = versionStr.toInt(&ok);
                            if (!ok) {
                                invalidAttr(versionStr, QLatin1String("version"), childTag);
                                continue;
                            }
554
                            version = ComponentVersion(major, ComponentVersion::NoVersion);
555
556
557
558
559
560
561
562
563
564
565
566
                        } else {
                            bool ok = false;
                            const int major = versionStr.left(dotIdx).toInt(&ok);
                            if (!ok) {
                                invalidAttr(versionStr, QLatin1String("version"), childTag);
                                continue;
                            }
                            const int minor = versionStr.mid(dotIdx + 1).toInt(&ok);
                            if (!ok) {
                                invalidAttr(versionStr, QLatin1String("version"), childTag);
                                continue;
                            }
567
                            version = ComponentVersion(major, minor);
568
569
570
571
572
573
574
575
576
577
578
579
580
                        }
                    } else {
                        ignoreAttr(attr);
                    }
                }
                metaObject->addExport(type, package, version);
            } else {
                unexpectedElement(_xml.name(), childTag);
            }
            _xml.skipCurrentElement(); // the <export> tag should be empty anyhow
        }
    }

581
582
private:
    QXmlStreamReader _xml;
583
    QMap<QString, FakeMetaObject::Ptr> *_objects;
584
585
};

Roberto Raggi's avatar
Roberto Raggi committed
586
587
} // end of anonymous namespace

588
QmlObjectValue::QmlObjectValue(FakeMetaObject::ConstPtr metaObject, const QString &className,
589
                               const QString &packageName, const ComponentVersion version, Engine *engine)
590
    : ObjectValue(engine),
591
      _attachedType(0),
592
      _metaObject(metaObject),
593
594
      _packageName(packageName),
      _componentVersion(version)
595
{
596
    setClassName(className);
597
}
Roberto Raggi's avatar
Roberto Raggi committed
598

599
600
QmlObjectValue::~QmlObjectValue()
{}
Roberto Raggi's avatar
Roberto Raggi committed
601

602
const Value *QmlObjectValue::findOrCreateSignature(int index, const FakeMetaMethod &method, QString *methodName) const
Roberto Raggi's avatar
Roberto Raggi committed
603
{
604
    *methodName = method.methodName();
Roberto Raggi's avatar
Roberto Raggi committed
605
606
607
608
    const Value *value = _metaSignature.value(index);
    if (! value) {
        value = new MetaFunction(method, engine());
        _metaSignature.insert(index, value);
Roberto Raggi's avatar
Roberto Raggi committed
609
    }
Roberto Raggi's avatar
Roberto Raggi committed
610
    return value;
611
}
Roberto Raggi's avatar
Roberto Raggi committed
612

613
614
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
615
    // process the meta enums
616
617
    for (int index = _metaObject->enumeratorOffset(); index < _metaObject->enumeratorCount(); ++index) {
        FakeMetaEnum e = _metaObject->enumerator(index);
618
619

        for (int i = 0; i < e.keyCount(); ++i) {
620
            processor->processEnumerator(e.key(i), engine()->numberValue());
621
622
623
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
624
625
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
626
        FakeMetaProperty prop = _metaObject->property(index);
627

Roberto Raggi's avatar
Roberto Raggi committed
628
629
        processor->processProperty(prop.name(), propertyValue(prop));
    }
630

Roberto Raggi's avatar
Roberto Raggi committed
631
632
    // process the meta methods
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
633
        FakeMetaMethod method = _metaObject->method(index);
Roberto Raggi's avatar
Roberto Raggi committed
634
635
        QString methodName;
        const Value *signature = findOrCreateSignature(index, method, &methodName);
636

637
        if (method.methodType() == FakeMetaMethod::Slot && method.access() == FakeMetaMethod::Public) {
Roberto Raggi's avatar
Roberto Raggi committed
638
            processor->processSlot(methodName, signature);
639

640
        } else if (method.methodType() == FakeMetaMethod::Signal && method.access() != FakeMetaMethod::Private) {
641
            // process the signal
Roberto Raggi's avatar
Roberto Raggi committed
642
            processor->processSignal(methodName, signature);
643

644
645
646
647
            QString slotName;
            slotName += QLatin1String("on");
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
648

649
            // process the generated slot
Roberto Raggi's avatar
Roberto Raggi committed
650
            processor->processGeneratedSlot(slotName, signature);
651
        }
Roberto Raggi's avatar
Roberto Raggi committed
652
653
    }

654
655
656
    if (_attachedType)
        _attachedType->processMembers(processor);

657
658
659
    ObjectValue::processMembers(processor);
}

660
const Value *QmlObjectValue::propertyValue(const FakeMetaProperty &prop) const
661
{
662
    const QString typeName = prop.typeName();
Roberto Raggi's avatar
Roberto Raggi committed
663

664
    // ### Verify type resolving.
665
    QmlObjectValue *objectValue = engine()->cppQmlTypes().typeByCppName(typeName);
666
    if (objectValue) {
667
668
669
        FakeMetaObject::Export exp = objectValue->metaObject()->exportInPackage(packageName());
        if (exp.isValid())
            objectValue = engine()->cppQmlTypes().typeByQualifiedName(exp.packageNameVersion);
670
        return objectValue;
671
    }
Roberto Raggi's avatar
Roberto Raggi committed
672

673
    const Value *value = engine()->undefinedValue();
674
675
    if (typeName == QLatin1String("QByteArray")
            || typeName == QLatin1String("string")
Christian Kamm's avatar
Christian Kamm committed
676
            || typeName == QLatin1String("QString")) {
677
        value = engine()->stringValue();
Christian Kamm's avatar
Christian Kamm committed
678
679
    } else if (typeName == QLatin1String("QUrl")) {
        value = engine()->urlValue();
680
    } else if (typeName == QLatin1String("bool")) {
681
        value = engine()->booleanValue();
682
    } else if (typeName == QLatin1String("int")
683
               || typeName == QLatin1String("long")) {
Christian Kamm's avatar
Christian Kamm committed
684
        value = engine()->intValue();
685
686
687
    }  else if (typeName == QLatin1String("float")
                || typeName == QLatin1String("double")
                || typeName == QLatin1String("qreal")) {
Christian Kamm's avatar
Christian Kamm committed
688
689
        // ### Review: more types here?
        value = engine()->realValue();
690
    } else if (typeName == QLatin1String("QFont")) {
691
        value = engine()->qmlFontObject();
692
693
694
    } else if (typeName == QLatin1String("QPoint")
            || typeName == QLatin1String("QPointF")
            || typeName == QLatin1String("QVector2D")) {
695
        value = engine()->qmlPointObject();
696
697
    } else if (typeName == QLatin1String("QSize")
            || typeName == QLatin1String("QSizeF")) {
698
        value = engine()->qmlSizeObject();
699
700
    } else if (typeName == QLatin1String("QRect")
            || typeName == QLatin1String("QRectF")) {
701
        value = engine()->qmlRectObject();
702
    } else if (typeName == QLatin1String("QVector3D")) {
703
        value = engine()->qmlVector3DObject();
704
    } else if (typeName == QLatin1String("QColor")) {
705
        value = engine()->colorValue();
706
    } else if (typeName == QLatin1String("QDeclarativeAnchorLine")) {
707
        value = engine()->anchorLineValue();
708
709
    }

Christian Kamm's avatar
Christian Kamm committed
710
711
712
713
714
715
716
    // might be an enum
    int enumIndex = _metaObject->enumeratorIndex(prop.typeName());
    if (enumIndex != -1) {
        const FakeMetaEnum &metaEnum = _metaObject->enumerator(enumIndex);
        value = new QmlEnumValue(metaEnum, engine());
    }

717
718
    return value;
}
719

720
const QmlObjectValue *QmlObjectValue::prototype() const
721
{
722
723
    Q_ASSERT(!_prototype || dynamic_cast<const QmlObjectValue *>(_prototype));
    return static_cast<const QmlObjectValue *>(_prototype);
724
725
}

726
727
728
729
730
731
732
733
734
735
const QmlObjectValue *QmlObjectValue::attachedType() const
{
    return _attachedType;
}

void QmlObjectValue::setAttachedType(QmlObjectValue *value)
{
    _attachedType = value;
}

736
FakeMetaObject::ConstPtr QmlObjectValue::metaObject() const
737
{
738
    return _metaObject;
739
740
}

741
742
743
QString QmlObjectValue::packageName() const
{ return _packageName; }

744
ComponentVersion QmlObjectValue::version() const
745
{ return _componentVersion; }
746

747
748
749
QString QmlObjectValue::defaultPropertyName() const
{ return _metaObject->defaultPropertyName(); }

750
751
QString QmlObjectValue::propertyType(const QString &propertyName) const
{
752
753
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
754
        int propIdx = iter->propertyIndex(propertyName);
755
        if (propIdx != -1) {
756
            return iter->property(propIdx).typeName();
757
758
759
760
761
        }
    }
    return QString();
}

762
bool QmlObjectValue::isListProperty(const QString &propertyName) const
763
{
764
765
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
766
767
768
769
770
771
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isList();
        }
    }
    return false;
772
773
}

774
775
776
777
778
bool QmlObjectValue::isEnum(const QString &typeName) const
{
    return _metaObject->enumeratorIndex(typeName) != -1;
}

779
780
bool QmlObjectValue::isWritable(const QString &propertyName) const
{
781
782
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
783
784
785
786
787
788
789
790
791
792
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isWritable();
        }
    }
    return false;
}

bool QmlObjectValue::isPointer(const QString &propertyName) const
{
793
794
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
795
796
797
798
799
800
801
802
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isPointer();
        }
    }
    return false;
}

803
804
805
806
807
808
809
810
811
812
bool QmlObjectValue::hasLocalProperty(const QString &typeName) const
{
    int idx = _metaObject->propertyIndex(typeName);
    if (idx == -1)
        return false;
    return true;
}

bool QmlObjectValue::hasProperty(const QString &propertyName) const
{
813
814
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
815
816
817
818
819
820
821
822
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return true;
        }
    }
    return false;
}

823
824
825
826
827
828
829
830
831
832
833
834
835
bool QmlObjectValue::enumContainsKey(const QString &enumName, const QString &enumKeyName) const
{
    int idx = _metaObject->enumeratorIndex(enumName);
    if (idx == -1)
        return false;
    const FakeMetaEnum &fme = _metaObject->enumerator(idx);
    for (int i = 0; i < fme.keyCount(); ++i) {
        if (fme.key(i) == enumKeyName)
            return true;
    }
    return false;
}

836
837
838
839
840
841
842
843
844
QStringList QmlObjectValue::keysForEnum(const QString &enumName) const
{
    int idx = _metaObject->enumeratorIndex(enumName);
    if (idx == -1)
        return QStringList();
    const FakeMetaEnum &fme = _metaObject->enumerator(idx);
    return fme.keys();
}

845
846
847
848
849
850
// Returns true if this object is in a package or if there is an object that
// has this one in its prototype chain and is itself in a package.
bool QmlObjectValue::hasChildInPackage() const
{
    if (!packageName().isEmpty())
        return true;
851
    QHashIterator<QString, QmlObjectValue *> it(engine()->cppQmlTypes().types());
852
853
    while (it.hasNext()) {
        it.next();
854
        FakeMetaObject::ConstPtr other = it.value()->_metaObject;
855
856
        // if it has only the default no-package export, it is not really exported
        if (other->exports().size() <= 1)
857
            continue;
858
859
        for (const QmlObjectValue *it = this; it; it = it->prototype()) {
            FakeMetaObject::ConstPtr iter = it->_metaObject;
860
861
862
863
864
865
866
            if (iter == _metaObject) // this object is a parent of other
                return true;
        }
    }
    return false;
}

867
bool QmlObjectValue::isDerivedFrom(FakeMetaObject::ConstPtr base) const
868
{
869
870
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
871
872
873
874
875
876
        if (iter == base)
            return true;
    }
    return false;
}

Christian Kamm's avatar
Christian Kamm committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
QmlEnumValue::QmlEnumValue(const FakeMetaEnum &metaEnum, Engine *engine)
    : NumberValue(),
      _metaEnum(new FakeMetaEnum(metaEnum))
{
    engine->registerValue(this);
}

QmlEnumValue::~QmlEnumValue()
{
    delete _metaEnum;
}

QString QmlEnumValue::name() const
{
    return _metaEnum->name();
}

QStringList QmlEnumValue::keys() const
{
    return _metaEnum->keys();
}

899
900
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
901
902
903
904
905
906
907
908
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
909
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
910
911
912
913
914
915
916
};

class FunctionCtor: public Function
{
public:
    FunctionCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
917
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
918
919
920
921
922
923
924
};

class ArrayCtor: public Function
{
public:
    ArrayCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
925
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
926
927
928
929
930
931
932
};

class StringCtor: public Function
{
public:
    StringCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
933
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
934
935
936
937
938
939
940
};

class BooleanCtor: public Function
{
public:
    BooleanCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
941
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
942
943
944
945
946
947
948
};

class NumberCtor: public Function
{
public:
    NumberCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
949
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
950
951
952
953
954
955
956
};

class DateCtor: public Function
{
public:
    DateCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
957
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
958
959
960
961
962
963
964
};

class RegExpCtor: public Function
{
public:
    RegExpCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
965
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
};

ObjectCtor::ObjectCtor(Engine *engine)
    : Function(engine)
{
}

FunctionCtor::FunctionCtor(Engine *engine)
    : Function(engine)
{
}

ArrayCtor::ArrayCtor(Engine *engine)
    : Function(engine)
{
}

StringCtor::StringCtor(Engine *engine)
    : Function(engine)
{
}

BooleanCtor::BooleanCtor(Engine *engine)
    : Function(engine)
{
}

NumberCtor::NumberCtor(Engine *engine)
    : Function(engine)
{
}

DateCtor::DateCtor(Engine *engine)
    : Function(engine)
{
For faster browsing, not all history is shown. View entire blame