qmljsinterpreter.cpp 83 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "qmljsinterpreter.h"
31
#include "qmljsevaluate.h"
32
#include "qmljslink.h"
33
#include "qmljsscopebuilder.h"
34
#include "parser/qmljsast_p.h"
35
36
37
38

#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QStringList>
39
40
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
41
#include <QtCore/QXmlStreamReader>
Friedemann Kleint's avatar
Friedemann Kleint committed
42
#include <QtCore/QCoreApplication>
43
44
45
#include <QtCore/QDebug>

using namespace QmlJS::Interpreter;
46
using namespace QmlJS::AST;
47

Roberto Raggi's avatar
Roberto Raggi committed
48
49
50
51
52
53
54
55
56
57
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
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);
    }
};

} // end of anonymous namespace

102
103
104
105
namespace QmlJS {
namespace Interpreter {

class FakeMetaEnum {
Erik Verbruggen's avatar
Erik Verbruggen committed
106
107
108
    QString m_name;
    QStringList m_keys;
    QList<int> m_values;
109
110
111

public:
    FakeMetaEnum(const QString &name)
Erik Verbruggen's avatar
Erik Verbruggen committed
112
        : m_name(name)
113
114
    {}

115
116
117
    QString name() const
    { return m_name; }

118
    void addKey(const QString &key, int value)
Erik Verbruggen's avatar
Erik Verbruggen committed
119
    { m_keys.append(key); m_values.append(value); }
120
121

    QString key(int index) const
Erik Verbruggen's avatar
Erik Verbruggen committed
122
    { return m_keys.at(index); }
123
124

    int keyCount() const
Erik Verbruggen's avatar
Erik Verbruggen committed
125
    { return m_keys.size(); }
Christian Kamm's avatar
Christian Kamm committed
126
127
128

    QStringList keys() const
    { return m_keys; }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
};

class FakeMetaMethod {
public:
    enum {
        Signal,
        Slot,
        Method
    };

    enum {
        Private,
        Protected,
        Public
    };

public:
    FakeMetaMethod(const QString &name, const QString &returnType = QString())
147
        : m_name(name), m_returnType(returnType), m_methodTy(Method), m_methodAccess(Public)
148
149
150
    {}

    QString methodName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
151
    { return m_name; }
152
153

    QStringList parameterNames() const
Erik Verbruggen's avatar
Erik Verbruggen committed
154
    { return m_paramNames; }
155
156

    QStringList parameterTypes() const
Erik Verbruggen's avatar
Erik Verbruggen committed
157
    { return m_paramTypes; }
158
159

    void addParameter(const QString &name, const QString &type)
Erik Verbruggen's avatar
Erik Verbruggen committed
160
    { m_paramNames.append(name); m_paramTypes.append(type); }
161
162

    int methodType() const
Erik Verbruggen's avatar
Erik Verbruggen committed
163
    { return m_methodTy; }
164
    void setMethodType(int methodType)
Erik Verbruggen's avatar
Erik Verbruggen committed
165
    { m_methodTy = methodType; }
166
167

    int access() const
Erik Verbruggen's avatar
Erik Verbruggen committed
168
    { return m_methodAccess; }
169
170

private:
Erik Verbruggen's avatar
Erik Verbruggen committed
171
172
173
174
175
176
    QString m_name;
    QString m_returnType;
    QStringList m_paramNames;
    QStringList m_paramTypes;
    int m_methodTy;
    int m_methodAccess;
177
178
179
};

class FakeMetaProperty {
Erik Verbruggen's avatar
Erik Verbruggen committed
180
181
182
    QString m_propertyName;
    QString m_type;
    bool m_isList;
183
184
185

public:
    FakeMetaProperty(const QString &name, const QString &type, bool isList)
Erik Verbruggen's avatar
Erik Verbruggen committed
186
        : m_propertyName(name), m_type(type), m_isList(isList)
187
188
189
    {}

    QString name() const
Erik Verbruggen's avatar
Erik Verbruggen committed
190
    { return m_propertyName; }
191
192

    QString typeName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
193
    { return m_type; }
194
195
196

    bool isList() const
    { return m_isList; }
197
198
199
200
201
202
};

class FakeMetaObject {
    FakeMetaObject(FakeMetaObject&);
    FakeMetaObject &operator=(const FakeMetaObject&);

Erik Verbruggen's avatar
Erik Verbruggen committed
203
204
205
206
207
208
209
    QString m_name;
    QString m_package;
    int m_major;
    int m_minor;
    const FakeMetaObject *m_super;
    QString m_superName;
    QList<FakeMetaEnum> m_enums;
210
    QHash<QString, int> m_enumNameToIndex;
Erik Verbruggen's avatar
Erik Verbruggen committed
211
    QList<FakeMetaProperty> m_props;
212
    QHash<QString, int> m_propNameToIdx;
Erik Verbruggen's avatar
Erik Verbruggen committed
213
    QList<FakeMetaMethod> m_methods;
214
    QString m_defaultPropertyName;
215
216

public:
217
    FakeMetaObject(const QString &name, const QString &package, int majorVersion, int minorVersion)
Erik Verbruggen's avatar
Erik Verbruggen committed
218
        : m_name(name), m_package(package), m_major(majorVersion), m_minor(minorVersion), m_super(0)
219
220
221
    {}

    void setSuperclassName(const QString &superclass)
Erik Verbruggen's avatar
Erik Verbruggen committed
222
    { m_superName = superclass; }
223
    QString superclassName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
224
    { return m_superName; }
225
226

    void setSuperclass(FakeMetaObject *superClass)
Erik Verbruggen's avatar
Erik Verbruggen committed
227
    { m_super = superClass; }
228
    const FakeMetaObject *superClass() const
Erik Verbruggen's avatar
Erik Verbruggen committed
229
    { return m_super; }
230
    QString className() const
Erik Verbruggen's avatar
Erik Verbruggen committed
231
    { return m_name; }
232
    QString packageName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
233
    { return m_package; }
234
235

    void addEnum(const FakeMetaEnum &fakeEnum)
236
    { m_enumNameToIndex.insert(fakeEnum.name(), m_enums.size()); m_enums.append(fakeEnum); }
237
    int enumeratorCount() const
Erik Verbruggen's avatar
Erik Verbruggen committed
238
    { return m_enums.size(); }
239
240
241
    int enumeratorOffset() const
    { return 0; }
    FakeMetaEnum enumerator(int index) const
Erik Verbruggen's avatar
Erik Verbruggen committed
242
    { return m_enums.at(index); }
243
244
    int enumeratorIndex(const QString &name) const
    { return m_enumNameToIndex.value(name, -1); }
245
246

    void addProperty(const FakeMetaProperty &property)
247
    { m_propNameToIdx.insert(property.name(), m_props.size()); m_props.append(property); }
248
    int propertyCount() const
Erik Verbruggen's avatar
Erik Verbruggen committed
249
    { return m_props.size(); }
250
251
252
    int propertyOffset() const
    { return 0; }
    FakeMetaProperty property(int index) const
Erik Verbruggen's avatar
Erik Verbruggen committed
253
    { return m_props.at(index); }
254
255
    int propertyIndex(const QString &name) const
    { return m_propNameToIdx.value(name, -1); }
256
257

    void addMethod(const FakeMetaMethod &method)
Erik Verbruggen's avatar
Erik Verbruggen committed
258
    { m_methods.append(method); }
259
    int methodCount() const
Erik Verbruggen's avatar
Erik Verbruggen committed
260
    { return m_methods.size(); }
261
262
263
    int methodOffset() const
    { return 0; }
    FakeMetaMethod method(int index) const
Erik Verbruggen's avatar
Erik Verbruggen committed
264
    { return m_methods.at(index); }
265
266

    int majorVersion() const
Erik Verbruggen's avatar
Erik Verbruggen committed
267
    { return m_major; }
268
    int minorVersion() const
Erik Verbruggen's avatar
Erik Verbruggen committed
269
    { return m_minor; }
270
271
272
273
274
275

    QString defaultPropertyName() const
    { return m_defaultPropertyName; }

    void setDefaultPropertyName(const QString defaultPropertyName)
    { m_defaultPropertyName = defaultPropertyName; }
276
277
278
279
};

} // end of Interpreter namespace
} // end of QmlJS namespace
Roberto Raggi's avatar
Roberto Raggi committed
280

Roberto Raggi's avatar
Roberto Raggi committed
281
282
namespace {

283
284
class MetaFunction: public FunctionValue
{
285
    FakeMetaMethod _method;
286
287

public:
288
    MetaFunction(const FakeMetaMethod &method, Engine *engine)
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        : 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();
    }
};

327
328
class QmlXmlReader
{
329
330
    Q_DECLARE_TR_FUNCTIONS(QmlJS::Interpreter::QmlXmlReader)

331
332
333
public:
    QmlXmlReader(QIODevice *dev)
        : _xml(dev)
Erik Verbruggen's avatar
Erik Verbruggen committed
334
        , _objects(0)
335
336
337
338
339
340
341
342
343
344
    {}

    bool operator()(QMap<QString, FakeMetaObject *> *objects) {
        Q_ASSERT(objects);
        _objects = objects;

        if (_xml.readNextStartElement()) {
            if (_xml.name() == "module")
                readModule();
            else
345
                _xml.raiseError(tr("The file is not module file."));
346
347
348
349
350
351
352
353
354
355
356
        }

        return !_xml.error();
    }

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

private:
    void unexpectedElement(const QStringRef &child, const QString &parent) {
357
        _xml.raiseError(tr("Unexpected element <%1> in <%2>").arg(child.toString(), parent));
358
359
360
361
362
363
364
365
    }

    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) {
366
        _xml.raiseError(tr("invalid value '%1' for attribute %2 in <%3>").arg(value, attrName, tag));
367
368
369
    }

    void noValidAttr(const QString &attrName, const QString &tag) {
370
        _xml.raiseError(tr("<%1> has no valid %2 attribute").arg(tag, attrName));
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    }

    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);

393
        bool doInsert = true;
394
        QString name, defaultPropertyName;
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
427
        int major = -1, minor = -1;
        QString extends;
        foreach (const QXmlStreamAttribute &attr, _xml.attributes()) {
            if (attr.name() == QLatin1String("name")) {
                name = attr.value().toString();
                if (name.isEmpty()) {
                    invalidAttr(name, QLatin1String("name"), tag);
                    return;
                }
            } else if (attr.name() == QLatin1String("version")) {
                QString version = attr.value().toString();
                int dotIdx = version.indexOf('.');
                if (dotIdx == -1) {
                    bool ok = false;
                    major = version.toInt(&ok);
                    if (!ok) {
                        invalidAttr(version, QLatin1String("version"), tag);
                        return;
                    }
                    minor = -1;
                } else {
                    bool ok = false;
                    major = version.left(dotIdx).toInt(&ok);
                    if (!ok) {
                        invalidAttr(version, QLatin1String("version"), tag);
                        return;
                    }
                    minor = version.mid(dotIdx + 1).toInt(&ok);
                    if (!ok) {
                        invalidAttr(version, QLatin1String("version"), tag);
                        return;
                    }
                }
428
429
            } else if (attr.name() == QLatin1String("defaultProperty")) {
                defaultPropertyName = attr.value().toString();
430
431
432
            } else if (attr.name() == QLatin1String("extends")) {
                if (! attr.value().isEmpty())
                    extends = attr.value().toString();
433
434
435
436
437

                if (extends == name) {
                    invalidAttr(extends, QLatin1String("extends"), tag);
                    doInsert = false;
                }
438
439
440
441
442
443
444
445
446
447
448
            } else {
                ignoreAttr(attr);
            }
        }

        QString className, packageName;
        split(name, &packageName, &className);
        FakeMetaObject *metaObject = new FakeMetaObject(className, packageName,
                                                        major, minor);
        if (! extends.isEmpty())
            metaObject->setSuperclassName(extends);
449
450
        if (! defaultPropertyName.isEmpty())
            metaObject->setDefaultPropertyName(defaultPropertyName);
451
452
453
454
455
456
457
458
459
460
461
462
463
464

        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);
            else
                unexpectedElement(_xml.name(), tag);
        }

465
466
467
468
        if (doInsert)
            _objects->insert(name, metaObject);
        else
            delete metaObject;
469
470
471
    }

    bool split(const QString &name, QString *packageName, QString *className) {
472
        int dotIdx = name.lastIndexOf(QLatin1Char('.'));
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
        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;
        }
    }

    void readProperty(FakeMetaObject *metaObject)
    {
        const QLatin1String tag("property");
        Q_ASSERT(_xml.isStartElement() && _xml.name() == tag);

        QString name, type;
        bool isList = false;
        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 {
                    invalidAttr(attr.value().toString(), QLatin1String("idList"), tag);
                    return;
                }
            } else {
                ignoreAttr(attr);
            }
        }

        if (name.isEmpty())
            noValidAttr(QLatin1String("name"), tag);
        else if (type.isEmpty())
            noValidAttr(QLatin1String("type"), tag);
        else
            createProperty(metaObject, name, type, isList);

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

    void createProperty(FakeMetaObject *metaObject, const QString &name,
                        const QString &type, bool isList) {
        Q_ASSERT(metaObject);

        metaObject->addProperty(FakeMetaProperty(name, type, isList));
    }

    void readEnum(FakeMetaObject *metaObject)
    {
        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);
        }
562
563

        metaObject->addEnum(metaEnum);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    }

    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);
        }
    }

    void readSignal(FakeMetaObject *metaObject)
    {
        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);
            }
        }
630
631

        metaObject->addMethod(method);
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    }

    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();
            } 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);
        }
    }

    void readMethod(FakeMetaObject *metaObject)
    {
        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);

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

        metaObject->addMethod(method);
696
697
698
699
700
701
702
    }

private:
    QXmlStreamReader _xml;
    QMap<QString, FakeMetaObject *> *_objects;
};

Roberto Raggi's avatar
Roberto Raggi committed
703
704
} // end of anonymous namespace

705
706
707
const int QmlObjectValue::NoVersion = -1;

QmlObjectValue::QmlObjectValue(const FakeMetaObject *metaObject, Engine *engine)
708
    : ObjectValue(engine),
709
      _metaObject(metaObject)
710
{
711
    setClassName(metaObject->className()); // ### TODO: we probably need to do more than just this...
712
}
Roberto Raggi's avatar
Roberto Raggi committed
713

714
715
QmlObjectValue::~QmlObjectValue()
{}
Roberto Raggi's avatar
Roberto Raggi committed
716

717
const Value *QmlObjectValue::findOrCreateSignature(int index, const FakeMetaMethod &method, QString *methodName) const
Roberto Raggi's avatar
Roberto Raggi committed
718
{
719
    *methodName = method.methodName();
Roberto Raggi's avatar
Roberto Raggi committed
720
721
722
723
    const Value *value = _metaSignature.value(index);
    if (! value) {
        value = new MetaFunction(method, engine());
        _metaSignature.insert(index, value);
Roberto Raggi's avatar
Roberto Raggi committed
724
    }
Roberto Raggi's avatar
Roberto Raggi committed
725
    return value;
726
}
Roberto Raggi's avatar
Roberto Raggi committed
727

728
729
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
730
    // process the meta enums
731
732
    for (int index = _metaObject->enumeratorOffset(); index < _metaObject->enumeratorCount(); ++index) {
        FakeMetaEnum e = _metaObject->enumerator(index);
733
734

        for (int i = 0; i < e.keyCount(); ++i) {
735
            processor->processEnumerator(e.key(i), engine()->numberValue());
736
737
738
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
739
740
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
741
        FakeMetaProperty prop = _metaObject->property(index);
742

Roberto Raggi's avatar
Roberto Raggi committed
743
744
        processor->processProperty(prop.name(), propertyValue(prop));
    }
745

Roberto Raggi's avatar
Roberto Raggi committed
746
747
    // process the meta methods
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
748
        FakeMetaMethod method = _metaObject->method(index);
Roberto Raggi's avatar
Roberto Raggi committed
749
750
        QString methodName;
        const Value *signature = findOrCreateSignature(index, method, &methodName);
751

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

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

759
760
761
762
            QString slotName;
            slotName += QLatin1String("on");
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
763

764
            // process the generated slot
Roberto Raggi's avatar
Roberto Raggi committed
765
            processor->processGeneratedSlot(slotName, signature);
766
        }
Roberto Raggi's avatar
Roberto Raggi committed
767
768
    }

769
770
771
    ObjectValue::processMembers(processor);
}

772
const Value *QmlObjectValue::propertyValue(const FakeMetaProperty &prop) const
773
{
774
    const QString typeName = prop.typeName();
Roberto Raggi's avatar
Roberto Raggi committed
775

776
777
778
779
    // ### Verify type resolving.
    QmlObjectValue *objectValue = engine()->metaTypeSystem().staticTypeForImport(typeName);
    if (objectValue)
        return objectValue;
Roberto Raggi's avatar
Roberto Raggi committed
780

781
    const Value *value = engine()->undefinedValue();
782
783
784
785
    if (typeName == QLatin1String("QByteArray")
            || typeName == QLatin1String("string")
            || typeName == QLatin1String("QString")
            || typeName == QLatin1String("QUrl")) {
786
        value = engine()->stringValue();
787
    } else if (typeName == QLatin1String("bool")) {
788
        value = engine()->booleanValue();
789
790
791
792
793
794
795
    } else if (typeName == QLatin1String("int")
            || typeName == QLatin1String("float")
            || typeName == QLatin1String("double")
            || typeName == QLatin1String("qreal")
            || typeName == QLatin1String("long")
            // ### Review: more types here?
            ) {
796
        value = engine()->numberValue();
797
    } else if (typeName == QLatin1String("QFont")) {
798
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
799
        object->setClassName(QLatin1String("Font"));
800
801
        object->setProperty("family", engine()->stringValue());
        object->setProperty("weight", engine()->undefinedValue()); // ### make me an object
Yann Bodson's avatar
Yann Bodson committed
802
        object->setProperty("capitalization", engine()->undefinedValue()); // ### make me an object
803
804
805
806
807
808
809
810
811
812
        object->setProperty("bold", engine()->booleanValue());
        object->setProperty("italic", engine()->booleanValue());
        object->setProperty("underline", engine()->booleanValue());
        object->setProperty("overline", engine()->booleanValue());
        object->setProperty("strikeout", engine()->booleanValue());
        object->setProperty("pointSize", engine()->numberValue());
        object->setProperty("pixelSize", engine()->numberValue());
        object->setProperty("letterSpacing", engine()->numberValue());
        object->setProperty("wordSpacing", engine()->numberValue());
        value = object;
813
814
815
    } else if (typeName == QLatin1String("QPoint")
            || typeName == QLatin1String("QPointF")
            || typeName == QLatin1String("QVector2D")) {
816
817
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
818
        object->setClassName(QLatin1String("Point"));
819
820
821
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        value = object;
822
823
824
825
826
827
828
829
    } else if (typeName == QLatin1String("QSize")
            || typeName == QLatin1String("QSizeF")) {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setClassName(QLatin1String("Size"));
        object->setProperty("width", engine()->numberValue());
        object->setProperty("height", engine()->numberValue());
        value = object;
830
831
    } else if (typeName == QLatin1String("QRect")
            || typeName == QLatin1String("QRectF")) {
832
833
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
834
        object->setClassName("Rect");
835
836
837
838
839
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("width", engine()->numberValue());
        object->setProperty("height", engine()->numberValue());
        value = object;
840
    } else if (typeName == QLatin1String("QVector3D")) {
841
842
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
843
        object->setClassName(QLatin1String("Vector3D"));
844
845
846
847
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("z", engine()->numberValue());
        value = object;
848
    } else if (typeName == QLatin1String("QColor")) {
849
        value = engine()->colorValue();
850
    } else if (typeName == QLatin1String("QDeclarativeAnchorLine")) {
851
        value = engine()->anchorLineValue();
852
853
    }

Christian Kamm's avatar
Christian Kamm committed
854
855
856
857
858
859
860
    // might be an enum
    int enumIndex = _metaObject->enumeratorIndex(prop.typeName());
    if (enumIndex != -1) {
        const FakeMetaEnum &metaEnum = _metaObject->enumerator(enumIndex);
        value = new QmlEnumValue(metaEnum, engine());
    }

861
862
    return value;
}
863

864
865
866
867
868
869
870
871
872
QString QmlObjectValue::packageName() const
{ return _metaObject->packageName(); }

int QmlObjectValue::majorVersion() const
{ return _metaObject->majorVersion(); }

int QmlObjectValue::minorVersion() const
{ return _metaObject->minorVersion(); }

873
874
875
QString QmlObjectValue::defaultPropertyName() const
{ return _metaObject->defaultPropertyName(); }

876
877
878
879
880
881
882
883
884
885
886
QString QmlObjectValue::propertyType(const QString &propertyName) const
{
    for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
        int propIdx = _metaObject->propertyIndex(propertyName);
        if (propIdx != -1) {
            return _metaObject->property(propIdx).typeName();
        }
    }
    return QString();
}

887
888
889
890
891
892
893
894
895
bool QmlObjectValue::isListProperty(const QString &name) const
{
    int idx = _metaObject->propertyIndex(name);
    if (idx == -1)
        return false;
    FakeMetaProperty prop = _metaObject->property(idx);
    return prop.isList();
}

896
897
898
899
900
bool QmlObjectValue::isEnum(const QString &typeName) const
{
    return _metaObject->enumeratorIndex(typeName) != -1;
}

901
902
903
904
905
906
907
908
909
910
911
912
913
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;
}

914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
// 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;
    foreach (const FakeMetaObject *other, MetaTypeSystem::_metaObjects) {
        if (other->packageName().isEmpty())
            continue;
        for (const FakeMetaObject *iter = other; iter; iter = iter->superClass()) {
            if (iter == _metaObject) // this object is a parent of other
                return true;
        }
    }
    return false;
}

931
bool QmlObjectValue::isDerivedFrom(const FakeMetaObject *base) const
932
{
933
    for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
934
935
936
937
938
939
        if (iter == base)
            return true;
    }
    return false;
}

Christian Kamm's avatar
Christian Kamm committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
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();
}

962
963
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
964
965
966
967
968
969
970
971
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
972
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
973
974
975
976
977
978
979
};

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

Roberto Raggi's avatar
Roberto Raggi committed
980
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
981
982
983
984
985
986
987
};

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

Roberto Raggi's avatar
Roberto Raggi committed
988
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
989
990
991
992
993
994
995
};

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

Roberto Raggi's avatar
Roberto Raggi committed
996
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
997
998
999
1000
};

class BooleanCtor: public Function
{