qmljsinterpreter.cpp 82.7 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 "parser/qmljsast_p.h"
34
35
36
37

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

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

Roberto Raggi's avatar
Roberto Raggi committed
47
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
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

101
102
103
104
namespace QmlJS {
namespace Interpreter {

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

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

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

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

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

    int keyCount() const
Erik Verbruggen's avatar
Erik Verbruggen committed
124
    { return m_keys.size(); }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
};

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

    enum {
        Private,
        Protected,
        Public
    };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    QString defaultPropertyName() const
    { return m_defaultPropertyName; }

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

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

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

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

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

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
338
                _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "The file is not module file."));
339
340
341
342
343
344
345
346
347
348
349
        }

        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
350
        _xml.raiseError(QCoreApplication::translate("QmlJS::Interpreter::QmlXmlReader", "Unexpected element <%1> in <%2>").arg(child.toString(), parent));
351
352
353
354
355
356
357
358
    }

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

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

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

386
        QString name, defaultPropertyName;
387
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
        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;
                    }
                }
420
421
            } else if (attr.name() == QLatin1String("defaultProperty")) {
                defaultPropertyName = attr.value().toString();
422
423
424
425
426
427
428
429
430
431
432
433
434
435
            } 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);
436
437
        if (! defaultPropertyName.isEmpty())
            metaObject->setDefaultPropertyName(defaultPropertyName);
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

        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) {
456
        int dotIdx = name.lastIndexOf(QLatin1Char('.'));
457
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
        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);
        }
546
547

        metaObject->addEnum(metaEnum);
548
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
    }

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

        metaObject->addMethod(method);
616
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
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

839
840
    return value;
}
841

842
843
844
845
846
847
848
849
850
QString QmlObjectValue::packageName() const
{ return _metaObject->packageName(); }

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

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

851
852
853
QString QmlObjectValue::defaultPropertyName() const
{ return _metaObject->defaultPropertyName(); }

854
855
856
857
858
859
860
861
862
863
864
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();
}

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

874
875
876
877
878
bool QmlObjectValue::isEnum(const QString &typeName) const
{
    return _metaObject->enumeratorIndex(typeName) != -1;
}

879
880
881
882
883
884
885
886
887
888
889
890
891
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;
}

892
bool QmlObjectValue::isDerivedFrom(const FakeMetaObject *base) const
893
{
894
    for (const FakeMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
895
896
897
898
899
900
        if (iter == base)
            return true;
    }
    return false;
}

901
902
namespace {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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