qmljsinterpreter.cpp 83.2 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(); }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
};

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

    enum {
        Private,
        Protected,
        Public
    };

public:
    FakeMetaMethod(const QString &name, const QString &returnType = QString())
144
        : m_name(name), m_returnType(returnType), m_methodTy(Method), m_methodAccess(Public)
145
146
147
    {}

    QString methodName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
148
    { return m_name; }
149
150

    QStringList parameterNames() const
Erik Verbruggen's avatar
Erik Verbruggen committed
151
    { return m_paramNames; }
152
153

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

    void addParameter(const QString &name, const QString &type)
Erik Verbruggen's avatar
Erik Verbruggen committed
157
    { m_paramNames.append(name); m_paramTypes.append(type); }
158
159

    int methodType() const
Erik Verbruggen's avatar
Erik Verbruggen committed
160
    { return m_methodTy; }
161
    void setMethodType(int methodType)
Erik Verbruggen's avatar
Erik Verbruggen committed
162
    { m_methodTy = methodType; }
163
164

    int access() const
Erik Verbruggen's avatar
Erik Verbruggen committed
165
    { return m_methodAccess; }
166
167

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

class FakeMetaProperty {
Erik Verbruggen's avatar
Erik Verbruggen committed
177
178
179
    QString m_propertyName;
    QString m_type;
    bool m_isList;
180
181
182

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

    QString name() const
Erik Verbruggen's avatar
Erik Verbruggen committed
187
    { return m_propertyName; }
188
189

    QString typeName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
190
    { return m_type; }
191
192
193

    bool isList() const
    { return m_isList; }
194
195
196
197
198
199
};

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

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

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

    void setSuperclassName(const QString &superclass)
Erik Verbruggen's avatar
Erik Verbruggen committed
219
    { m_superName = superclass; }
220
    QString superclassName() const
Erik Verbruggen's avatar
Erik Verbruggen committed
221
    { return m_superName; }
222
223

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

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

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

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

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

    QString defaultPropertyName() const
    { return m_defaultPropertyName; }

    void setDefaultPropertyName(const QString defaultPropertyName)
    { m_defaultPropertyName = defaultPropertyName; }
273
274
275
276
};

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

Roberto Raggi's avatar
Roberto Raggi committed
278
279
namespace {

280
281
class MetaFunction: public FunctionValue
{
282
    FakeMetaMethod _method;
283
284

public:
285
    MetaFunction(const FakeMetaMethod &method, Engine *engine)
286
287
288
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
        : 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();
    }
};

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
class QmlXmlReader
{
public:
    QmlXmlReader(QIODevice *dev)
        : _xml(dev)
    {}

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

        if (_xml.readNextStartElement()) {
            if (_xml.name() == "module")
                readModule();
            else
Friedemann Kleint's avatar
Friedemann Kleint committed
339
                _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "The file is not module file."));
340
341
342
343
344
345
346
347
348
349
350
        }

        return !_xml.error();
    }

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

private:
    void unexpectedElement(const QStringRef &child, const QString &parent) {
Friedemann Kleint's avatar
Friedemann Kleint committed
351
        _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "Unexpected element <%1> in <%2>").arg(child.toString(), parent));
352
353
354
355
356
357
358
359
    }

    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) {
Friedemann Kleint's avatar
Friedemann Kleint committed
360
        _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "invalid value '%1' for attribute %2 in <%3>").arg(value, attrName, tag));
361
362
363
    }

    void noValidAttr(const QString &attrName, const QString &tag) {
Friedemann Kleint's avatar
Friedemann Kleint committed
364
        _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "<%1> has no valid %2 attribute").arg(tag, attrName));
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    }

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

387
        QString name, defaultPropertyName;
388
389
390
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
        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;
                    }
                }
421
422
            } else if (attr.name() == QLatin1String("defaultProperty")) {
                defaultPropertyName = attr.value().toString();
423
424
425
426
427
428
429
430
431
432
433
434
435
436
            } else if (attr.name() == QLatin1String("extends")) {
                if (! attr.value().isEmpty())
                    extends = attr.value().toString();
            } else {
                ignoreAttr(attr);
            }
        }

        QString className, packageName;
        split(name, &packageName, &className);
        FakeMetaObject *metaObject = new FakeMetaObject(className, packageName,
                                                        major, minor);
        if (! extends.isEmpty())
            metaObject->setSuperclassName(extends);
437
438
        if (! defaultPropertyName.isEmpty())
            metaObject->setDefaultPropertyName(defaultPropertyName);
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

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

        _objects->insert(name, metaObject);
    }

    bool split(const QString &name, QString *packageName, QString *className) {
457
        int dotIdx = name.lastIndexOf(QLatin1Char('.'));
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
        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);
        }
547
548

        metaObject->addEnum(metaEnum);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
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
    }

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

        metaObject->addMethod(method);
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
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
    }

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

        metaObject->addMethod(method);
681
682
683
684
685
686
687
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
688
689
} // end of anonymous namespace

690
691
692
const int QmlObjectValue::NoVersion = -1;

QmlObjectValue::QmlObjectValue(const FakeMetaObject *metaObject, Engine *engine)
693
    : ObjectValue(engine),
694
      _metaObject(metaObject)
695
{
696
    setClassName(metaObject->className()); // ### TODO: we probably need to do more than just this...
697
}
Roberto Raggi's avatar
Roberto Raggi committed
698

699
700
QmlObjectValue::~QmlObjectValue()
{}
Roberto Raggi's avatar
Roberto Raggi committed
701

702
const Value *QmlObjectValue::findOrCreateSignature(int index, const FakeMetaMethod &method, QString *methodName) const
Roberto Raggi's avatar
Roberto Raggi committed
703
{
704
    *methodName = method.methodName();
Roberto Raggi's avatar
Roberto Raggi committed
705
706
707
708
    const Value *value = _metaSignature.value(index);
    if (! value) {
        value = new MetaFunction(method, engine());
        _metaSignature.insert(index, value);
Roberto Raggi's avatar
Roberto Raggi committed
709
    }
Roberto Raggi's avatar
Roberto Raggi committed
710
    return value;
711
}
Roberto Raggi's avatar
Roberto Raggi committed
712

713
714
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
715
    // process the meta enums
716
717
    for (int index = _metaObject->enumeratorOffset(); index < _metaObject->enumeratorCount(); ++index) {
        FakeMetaEnum e = _metaObject->enumerator(index);
718
719

        for (int i = 0; i < e.keyCount(); ++i) {
720
            processor->processEnumerator(e.key(i), engine()->numberValue());
721
722
723
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
724
725
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
726
        FakeMetaProperty prop = _metaObject->property(index);
727

Roberto Raggi's avatar
Roberto Raggi committed
728
729
        processor->processProperty(prop.name(), propertyValue(prop));
    }
730

Roberto Raggi's avatar
Roberto Raggi committed
731
732
    // process the meta methods
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
733
        FakeMetaMethod method = _metaObject->method(index);
Roberto Raggi's avatar
Roberto Raggi committed
734
735
        QString methodName;
        const Value *signature = findOrCreateSignature(index, method, &methodName);
736

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

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

744
745
746
747
            QString slotName;
            slotName += QLatin1String("on");
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
748

749
            // process the generated slot
Roberto Raggi's avatar
Roberto Raggi committed
750
            processor->processGeneratedSlot(slotName, signature);
751
        }
Roberto Raggi's avatar
Roberto Raggi committed
752
753
    }

754
755
756
    ObjectValue::processMembers(processor);
}

757
const Value *QmlObjectValue::propertyValue(const FakeMetaProperty &prop) const
758
{
759
    const QString typeName = prop.typeName();
Roberto Raggi's avatar
Roberto Raggi committed
760

761
762
763
764
    // ### Verify type resolving.
    QmlObjectValue *objectValue = engine()->metaTypeSystem().staticTypeForImport(typeName);
    if (objectValue)
        return objectValue;
Roberto Raggi's avatar
Roberto Raggi committed
765

766
    const Value *value = engine()->undefinedValue();
767
768
769
770
    if (typeName == QLatin1String("QByteArray")
            || typeName == QLatin1String("string")
            || typeName == QLatin1String("QString")
            || typeName == QLatin1String("QUrl")) {
771
        value = engine()->stringValue();
772
    } else if (typeName == QLatin1String("bool")) {
773
        value = engine()->booleanValue();
774
775
776
777
778
779
780
    } else if (typeName == QLatin1String("int")
            || typeName == QLatin1String("float")
            || typeName == QLatin1String("double")
            || typeName == QLatin1String("qreal")
            || typeName == QLatin1String("long")
            // ### Review: more types here?
            ) {
781
        value = engine()->numberValue();
782
    } else if (typeName == QLatin1String("QFont")) {
783
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
784
        object->setClassName(QLatin1String("Font"));
785
786
        object->setProperty("family", engine()->stringValue());
        object->setProperty("weight", engine()->undefinedValue()); // ### make me an object
Yann Bodson's avatar
Yann Bodson committed
787
        object->setProperty("capitalization", engine()->undefinedValue()); // ### make me an object
788
789
790
791
792
793
794
795
796
797
        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;
798
799
800
    } else if (typeName == QLatin1String("QPoint")
            || typeName == QLatin1String("QPointF")
            || typeName == QLatin1String("QVector2D")) {
801
802
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
803
        object->setClassName(QLatin1String("Point"));
804
805
806
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        value = object;
807
808
809
810
811
812
813
814
    } 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;
815
816
    } else if (typeName == QLatin1String("QRect")
            || typeName == QLatin1String("QRectF")) {
817
818
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
819
        object->setClassName("Rect");
820
821
822
823
824
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("width", engine()->numberValue());
        object->setProperty("height", engine()->numberValue());
        value = object;
825
    } else if (typeName == QLatin1String("QVector3D")) {
826
827
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
828
        object->setClassName(QLatin1String("Vector3D"));
829
830
831
832
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("z", engine()->numberValue());
        value = object;
833
    } else if (typeName == QLatin1String("QColor")) {
834
        value = engine()->colorValue();
835
    } else if (typeName == QLatin1String("QDeclarativeAnchorLine")) {
836
        value = engine()->anchorLineValue();
837
838
839
840
841
842
843
844
845
    } else if (typeName == QLatin1String("QEasingCurve")) {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setClassName(QLatin1String("EasingCurve"));
        object->setProperty("type", engine()->easingCurveNameValue());
        object->setProperty("period", engine()->numberValue());
        object->setProperty("amplitude", engine()->numberValue());
        object->setProperty("overshoot", engine()->numberValue());
        value = object;
846
847
    }

848
849
    return value;
}
850

851
852
853
854
855
856
857
858
859
QString QmlObjectValue::packageName() const
{ return _metaObject->packageName(); }

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

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

860
861
862
QString QmlObjectValue::defaultPropertyName() const
{ return _metaObject->defaultPropertyName(); }

863
864
865
866
867
868
869
870
871
872
873
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();
}

874
875
876
877
878
879
880
881
882
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();
}

883
884
885
886
887
bool QmlObjectValue::isEnum(const QString &typeName) const
{
    return _metaObject->enumeratorIndex(typeName) != -1;
}

888
889
890
891
892
893
894
895
896
897
898
899
900
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;
}

901
bool QmlObjectValue::isDerivedFrom(const FakeMetaObject *base) const
902
{
903
    for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
904
905
906
907
908
909
        if (iter == base)
            return true;
    }
    return false;
}

910
911
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
912
913
914
915
916
917
918
919
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
920
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
921
922
923
924
925
926
927
};

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

Roberto Raggi's avatar
Roberto Raggi committed
928
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
929
930
931
932
933
934
935
};

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

Roberto Raggi's avatar
Roberto Raggi committed
936
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
937
938
939
940
941
942
943
};

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

Roberto Raggi's avatar
Roberto Raggi committed
944
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
945
946
947
948
949
950
951
};

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

Roberto Raggi's avatar
Roberto Raggi committed
952
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
953
954
955
956
957
958
959
};

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

Roberto Raggi's avatar
Roberto Raggi committed
960
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
961
962
963
964
965
966
967
};

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

Roberto Raggi's avatar
Roberto Raggi committed
968
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
969
970
971
972
973
974
975
};

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

Roberto Raggi's avatar
Roberto Raggi committed
976
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
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)