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

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

42
43
#include <languageutils/fakemetaobject.h>

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

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

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

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

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

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

        return true;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

        return FunctionValue::argumentName(index);
    }

    virtual bool isVariadic() const
    {
        return false;
    }

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

Roberto Raggi's avatar
Roberto Raggi committed
154
155
} // end of anonymous namespace

156
QmlObjectValue::QmlObjectValue(FakeMetaObject::ConstPtr metaObject, const QString &className,
157
                               const QString &packageName, const ComponentVersion version, Engine *engine)
158
    : ObjectValue(engine),
159
      _attachedType(0),
160
      _metaObject(metaObject),
161
162
      _packageName(packageName),
      _componentVersion(version)
163
{
164
    setClassName(className);
165
}
Roberto Raggi's avatar
Roberto Raggi committed
166

167
168
QmlObjectValue::~QmlObjectValue()
{}
Roberto Raggi's avatar
Roberto Raggi committed
169

170
const Value *QmlObjectValue::findOrCreateSignature(int index, const FakeMetaMethod &method, QString *methodName) const
Roberto Raggi's avatar
Roberto Raggi committed
171
{
172
    *methodName = method.methodName();
Roberto Raggi's avatar
Roberto Raggi committed
173
174
175
176
    const Value *value = _metaSignature.value(index);
    if (! value) {
        value = new MetaFunction(method, engine());
        _metaSignature.insert(index, value);
Roberto Raggi's avatar
Roberto Raggi committed
177
    }
Roberto Raggi's avatar
Roberto Raggi committed
178
    return value;
179
}
Roberto Raggi's avatar
Roberto Raggi committed
180

181
182
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
183
    // process the meta enums
184
185
    for (int index = _metaObject->enumeratorOffset(); index < _metaObject->enumeratorCount(); ++index) {
        FakeMetaEnum e = _metaObject->enumerator(index);
186
187

        for (int i = 0; i < e.keyCount(); ++i) {
188
            processor->processEnumerator(e.key(i), engine()->numberValue());
189
190
191
        }
    }

192
193
    // all explicitly defined signal names
    QSet<QString> explicitSignals;
194

Roberto Raggi's avatar
Roberto Raggi committed
195
196
    // process the meta methods
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
197
        FakeMetaMethod method = _metaObject->method(index);
Roberto Raggi's avatar
Roberto Raggi committed
198
199
        QString methodName;
        const Value *signature = findOrCreateSignature(index, method, &methodName);
200

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

204
        } else if (method.methodType() == FakeMetaMethod::Signal && method.access() != FakeMetaMethod::Private) {
205
            // process the signal
Roberto Raggi's avatar
Roberto Raggi committed
206
            processor->processSignal(methodName, signature);
207
            explicitSignals.insert(methodName);
208

209
            QString slotName = QLatin1String("on");
210
211
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
212

213
            // process the generated slot
Roberto Raggi's avatar
Roberto Raggi committed
214
            processor->processGeneratedSlot(slotName, signature);
215
        }
Roberto Raggi's avatar
Roberto Raggi committed
216
217
    }

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
        FakeMetaProperty prop = _metaObject->property(index);

        const QString propertyName = prop.name();
        processor->processProperty(propertyName, propertyValue(prop));

        // every property always has a onXyzChanged slot, even if the NOTIFY
        // signal has a different name
        QString signalName = propertyName;
        signalName += QLatin1String("Changed");
        if (!explicitSignals.contains(signalName)) {
            QString slotName = QLatin1String("on");
            slotName += signalName.at(0).toUpper();
            slotName += signalName.midRef(1);

            // process the generated slot
            processor->processGeneratedSlot(slotName, engine()->undefinedValue());
        }
    }

239
240
241
    if (_attachedType)
        _attachedType->processMembers(processor);

242
243
244
    ObjectValue::processMembers(processor);
}

245
const Value *QmlObjectValue::propertyValue(const FakeMetaProperty &prop) const
246
{
247
    QString typeName = prop.typeName();
Roberto Raggi's avatar
Roberto Raggi committed
248

249
    // ### Verify type resolving.
250
    QmlObjectValue *objectValue = engine()->cppQmlTypes().typeByCppName(typeName);
251
    if (objectValue) {
252
253
254
        FakeMetaObject::Export exp = objectValue->metaObject()->exportInPackage(packageName());
        if (exp.isValid())
            objectValue = engine()->cppQmlTypes().typeByQualifiedName(exp.packageNameVersion);
255
        return objectValue;
256
    }
Roberto Raggi's avatar
Roberto Raggi committed
257

258
    const Value *value = engine()->undefinedValue();
259
260
    if (typeName == QLatin1String("QByteArray")
            || typeName == QLatin1String("string")
Christian Kamm's avatar
Christian Kamm committed
261
            || typeName == QLatin1String("QString")) {
262
        value = engine()->stringValue();
Christian Kamm's avatar
Christian Kamm committed
263
264
    } else if (typeName == QLatin1String("QUrl")) {
        value = engine()->urlValue();
265
    } else if (typeName == QLatin1String("bool")) {
266
        value = engine()->booleanValue();
267
    } else if (typeName == QLatin1String("int")
268
               || typeName == QLatin1String("long")) {
Christian Kamm's avatar
Christian Kamm committed
269
        value = engine()->intValue();
270
271
272
    }  else if (typeName == QLatin1String("float")
                || typeName == QLatin1String("double")
                || typeName == QLatin1String("qreal")) {
Christian Kamm's avatar
Christian Kamm committed
273
274
        // ### Review: more types here?
        value = engine()->realValue();
275
    } else if (typeName == QLatin1String("QFont")) {
276
        value = engine()->qmlFontObject();
277
278
279
    } else if (typeName == QLatin1String("QPoint")
            || typeName == QLatin1String("QPointF")
            || typeName == QLatin1String("QVector2D")) {
280
        value = engine()->qmlPointObject();
281
282
    } else if (typeName == QLatin1String("QSize")
            || typeName == QLatin1String("QSizeF")) {
283
        value = engine()->qmlSizeObject();
284
285
    } else if (typeName == QLatin1String("QRect")
            || typeName == QLatin1String("QRectF")) {
286
        value = engine()->qmlRectObject();
287
    } else if (typeName == QLatin1String("QVector3D")) {
288
        value = engine()->qmlVector3DObject();
289
    } else if (typeName == QLatin1String("QColor")) {
290
        value = engine()->colorValue();
291
    } else if (typeName == QLatin1String("QDeclarativeAnchorLine")) {
292
        value = engine()->anchorLineValue();
293
294
    }

Christian Kamm's avatar
Christian Kamm committed
295
    // might be an enum
296
297
298
299
300
301
302
303
304
305
    const QmlObjectValue *base = this;
    const QStringList components = typeName.split(QLatin1String("::"));
    if (components.size() == 2) {
        base = engine()->cppQmlTypes().typeByCppName(components.first());
        typeName = components.last();
    }
    if (base) {
        const FakeMetaEnum &metaEnum = base->getEnum(typeName);
        if (metaEnum.isValid())
            value = new QmlEnumValue(metaEnum, engine());
Christian Kamm's avatar
Christian Kamm committed
306
307
    }

308
309
    return value;
}
310

311
const QmlObjectValue *QmlObjectValue::prototype() const
312
{
313
314
    Q_ASSERT(!_prototype || dynamic_cast<const QmlObjectValue *>(_prototype));
    return static_cast<const QmlObjectValue *>(_prototype);
315
316
}

317
318
319
320
321
322
323
324
325
326
const QmlObjectValue *QmlObjectValue::attachedType() const
{
    return _attachedType;
}

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

327
FakeMetaObject::ConstPtr QmlObjectValue::metaObject() const
328
{
329
    return _metaObject;
330
331
}

332
333
334
QString QmlObjectValue::packageName() const
{ return _packageName; }

335
ComponentVersion QmlObjectValue::version() const
336
{ return _componentVersion; }
337

338
339
340
QString QmlObjectValue::defaultPropertyName() const
{ return _metaObject->defaultPropertyName(); }

341
342
QString QmlObjectValue::propertyType(const QString &propertyName) const
{
343
344
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
345
        int propIdx = iter->propertyIndex(propertyName);
346
        if (propIdx != -1) {
347
            return iter->property(propIdx).typeName();
348
349
350
351
352
        }
    }
    return QString();
}

353
bool QmlObjectValue::isListProperty(const QString &propertyName) const
354
{
355
356
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
357
358
359
360
361
362
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isList();
        }
    }
    return false;
363
364
}

365
366
367
368
369
bool QmlObjectValue::isEnum(const QString &typeName) const
{
    return _metaObject->enumeratorIndex(typeName) != -1;
}

370
371
372
373
374
375
376
377
378
FakeMetaEnum QmlObjectValue::getEnum(const QString &typeName) const
{
    const int index = _metaObject->enumeratorIndex(typeName);
    if (index == -1)
        return FakeMetaEnum();

    return _metaObject->enumerator(index);
}

379
380
bool QmlObjectValue::isWritable(const QString &propertyName) const
{
381
382
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
383
384
385
386
387
388
389
390
391
392
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isWritable();
        }
    }
    return false;
}

bool QmlObjectValue::isPointer(const QString &propertyName) const
{
393
394
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
395
396
397
398
399
400
401
402
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return iter->property(propIdx).isPointer();
        }
    }
    return false;
}

403
404
405
406
407
408
409
410
411
412
bool QmlObjectValue::hasLocalProperty(const QString &typeName) const
{
    int idx = _metaObject->propertyIndex(typeName);
    if (idx == -1)
        return false;
    return true;
}

bool QmlObjectValue::hasProperty(const QString &propertyName) const
{
413
414
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
415
416
417
418
419
420
421
422
        int propIdx = iter->propertyIndex(propertyName);
        if (propIdx != -1) {
            return true;
        }
    }
    return false;
}

423
424
425
426
427
428
429
430
431
432
433
434
435
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;
}

436
437
438
439
440
441
442
443
444
QStringList QmlObjectValue::keysForEnum(const QString &enumName) const
{
    int idx = _metaObject->enumeratorIndex(enumName);
    if (idx == -1)
        return QStringList();
    const FakeMetaEnum &fme = _metaObject->enumerator(idx);
    return fme.keys();
}

445
446
447
448
449
450
// 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;
451
    QHashIterator<QString, QmlObjectValue *> it(engine()->cppQmlTypes().types());
452
453
    while (it.hasNext()) {
        it.next();
454
        FakeMetaObject::ConstPtr other = it.value()->_metaObject;
455
456
        // if it has only the default no-package export, it is not really exported
        if (other->exports().size() <= 1)
457
            continue;
458
459
        for (const QmlObjectValue *it = this; it; it = it->prototype()) {
            FakeMetaObject::ConstPtr iter = it->_metaObject;
460
461
462
463
464
465
466
            if (iter == _metaObject) // this object is a parent of other
                return true;
        }
    }
    return false;
}

467
bool QmlObjectValue::isDerivedFrom(FakeMetaObject::ConstPtr base) const
468
{
469
470
    for (const QmlObjectValue *it = this; it; it = it->prototype()) {
        FakeMetaObject::ConstPtr iter = it->_metaObject;
471
472
473
474
475
476
        if (iter == base)
            return true;
    }
    return false;
}

Christian Kamm's avatar
Christian Kamm committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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();
}

499
500
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
501
502
503
504
505
506
507
508
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
509
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
510
511
512
513
514
515
516
};

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

Roberto Raggi's avatar
Roberto Raggi committed
517
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
518
519
520
521
522
523
524
};

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

Roberto Raggi's avatar
Roberto Raggi committed
525
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
526
527
528
529
530
531
532
};

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

Roberto Raggi's avatar
Roberto Raggi committed
533
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
534
535
536
537
538
539
540
};

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

Roberto Raggi's avatar
Roberto Raggi committed
541
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
542
543
544
545
546
547
548
};

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

Roberto Raggi's avatar
Roberto Raggi committed
549
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
550
551
552
553
554
555
556
};

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

Roberto Raggi's avatar
Roberto Raggi committed
557
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
558
559
560
561
562
563
564
};

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

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

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

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

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

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

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
608
const Value *ObjectCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
609
{
Roberto Raggi's avatar
Roberto Raggi committed
610
611
612
613
614
615
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("Object");
    thisObject->setPrototype(engine()->objectPrototype());
616
    thisObject->setMember("length", engine()->numberValue());
Roberto Raggi's avatar
Roberto Raggi committed
617
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
618
619
}

Roberto Raggi's avatar
Roberto Raggi committed
620
const Value *FunctionCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
621
{
Roberto Raggi's avatar
Roberto Raggi committed
622
623
624
625
626
627
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("Function");
    thisObject->setPrototype(engine()->functionPrototype());
628
    thisObject->setMember("length", engine()->numberValue());
Roberto Raggi's avatar
Roberto Raggi committed
629
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
630
631
}

Roberto Raggi's avatar
Roberto Raggi committed
632
const Value *ArrayCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
633
{
Roberto Raggi's avatar
Roberto Raggi committed
634
635
636
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
637

Roberto Raggi's avatar
Roberto Raggi committed
638
639
    thisObject->setClassName("Array");
    thisObject->setPrototype(engine()->arrayPrototype());
640
    thisObject->setMember("length", engine()->numberValue());
Roberto Raggi's avatar
Roberto Raggi committed
641
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
642
643
}

Roberto Raggi's avatar
Roberto Raggi committed
644
const Value *StringCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
645
{
Roberto Raggi's avatar
Roberto Raggi committed
646
647
    if (activation->calledAsFunction())
        return engine()->convertToString(activation->thisObject());
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
648

Roberto Raggi's avatar
Roberto Raggi committed
649
650
651
    ObjectValue *thisObject = activation->thisObject();
    thisObject->setClassName("String");
    thisObject->setPrototype(engine()->stringPrototype());
652
    thisObject->setMember("length", engine()->numberValue());
Roberto Raggi's avatar
Roberto Raggi committed
653
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
654
655
}

Roberto Raggi's avatar
Roberto Raggi committed
656
const Value *BooleanCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
657
{
Roberto Raggi's avatar
Roberto Raggi committed
658
659
660
661
662
663
664
    if (activation->calledAsFunction())
        return engine()->convertToBoolean(activation->thisObject());

    ObjectValue *thisObject = activation->thisObject();
    thisObject->setClassName("Boolean");
    thisObject->setPrototype(engine()->booleanPrototype());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
665
666
}

Roberto Raggi's avatar
Roberto Raggi committed
667
const Value *NumberCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
668
{
Roberto Raggi's avatar
Roberto Raggi committed
669
670
671
672
673
674
675
    if (activation->calledAsFunction())
        return engine()->convertToNumber(activation->thisObject());

    ObjectValue *thisObject = activation->thisObject();
    thisObject->setClassName("Number");
    thisObject->setPrototype(engine()->numberPrototype());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
676
677
}

Roberto Raggi's avatar
Roberto Raggi committed
678
const Value *DateCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
679
{
Roberto Raggi's avatar
Roberto Raggi committed
680
681
682
683
684
685
686
    if (activation->calledAsFunction())
        return engine()->stringValue();

    ObjectValue *thisObject = activation->thisObject();
    thisObject->setClassName("Date");
    thisObject->setPrototype(engine()->datePrototype());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
687
688
}

Roberto Raggi's avatar
Roberto Raggi committed
689
const Value *RegExpCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
690
{
Roberto Raggi's avatar
Roberto Raggi committed
691
692
693
694
695
696
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("RegExp");
    thisObject->setPrototype(engine()->regexpPrototype());
697
698
699
700
701
    thisObject->setMember("source", engine()->stringValue());
    thisObject->setMember("global", engine()->booleanValue());
    thisObject->setMember("ignoreCase", engine()->booleanValue());
    thisObject->setMember("multiline", engine()->booleanValue());
    thisObject->setMember("lastIndex", engine()->numberValue());
Roberto Raggi's avatar
Roberto Raggi committed
702
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
703
704
705
706
}

} // end of anonymous namespace

707
708
709
////////////////////////////////////////////////////////////////////////////////
// ValueVisitor
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
ValueVisitor::ValueVisitor()
{
}

ValueVisitor::~ValueVisitor()
{
}

void ValueVisitor::visit(const NullValue *)
{
}

void ValueVisitor::visit(const UndefinedValue *)
{
}

void ValueVisitor::visit(const NumberValue *)
{
}

void ValueVisitor::visit(const BooleanValue *)
{
}

void ValueVisitor::visit(const StringValue *)
{
}

void ValueVisitor::visit(const ObjectValue *)
{
}

void ValueVisitor::visit(const FunctionValue *)
{
}
745

Roberto Raggi's avatar
Roberto Raggi committed
746
747
748
void ValueVisitor::visit(const Reference *)
{
}
749

750
751
752
753
void ValueVisitor::visit(const ColorValue *)
{
}

754
755
756
757
void ValueVisitor::visit(const AnchorLineValue *)
{
}

758
759
760
////////////////////////////////////////////////////////////////////////////////
// Value
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
761
762
763
764
765
766
767
768
Value::Value()
{
}

Value::~Value()
{
}

769
770
771
772
773
bool Value::getSourceLocation(QString *, int *, int *) const
{
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
const NullValue *Value::asNullValue() const
{
    return 0;
}

const UndefinedValue *Value::asUndefinedValue() const
{
    return 0;
}

const NumberValue *Value::asNumberValue() const
{
    return 0;
}

789
790
791
792
793
794
795
796
797
798
const IntValue *Value::asIntValue() const
{
    return 0;
}

const RealValue *Value::asRealValue() const
{
    return 0;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
799
800
801
802
803
804
805
806
807
808
const BooleanValue *Value::asBooleanValue() const
{
    return 0;
}

const StringValue *Value::asStringValue() const
{
    return 0;
}

Christian Kamm's avatar
Christian Kamm committed
809
810
811
812
813
const UrlValue *Value::asUrlValue() const
{
    return 0;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
814
815
816
817
818
819
820
821
822
const ObjectValue *Value::asObjectValue() const
{
    return 0;
}

const FunctionValue *Value::asFunctionValue() const
{
    return 0;
}
823

Roberto Raggi's avatar
Roberto Raggi committed
824
825
826
827
828
const Reference *Value::asReference() const
{
    return 0;
}

829
830
831
832
833
const ColorValue *Value::asColorValue() const
{
    return 0;
}

834
835
836
837
838
const AnchorLineValue *Value::asAnchorLineValue() const
{
    return 0;
}

839
840
841
////////////////////////////////////////////////////////////////////////////////
// Values
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
const NullValue *NullValue::asNullValue() const
{
    return this;
}

void NullValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

const UndefinedValue *UndefinedValue::asUndefinedValue() const
{
    return this;
}

void UndefinedValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}
861

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
862
863
864
865
866
const NumberValue *NumberValue::asNumberValue() const
{
    return this;
}

867
868
869
870
871
872
873
874
875
876
const RealValue *RealValue::asRealValue() const
{
    return this;
}

const IntValue *IntValue::asIntValue() const
{
    return this;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
void NumberValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

const BooleanValue *BooleanValue::asBooleanValue() const
{
    return this;
}

void BooleanValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

const StringValue *StringValue::asStringValue() const
{
    return this;
}

Christian Kamm's avatar
Christian Kamm committed
897
898
899
900
901
const UrlValue *UrlValue::asUrlValue() const
{
    return this;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
902
903
904
905
906
void StringValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

Roberto Raggi's avatar
Roberto Raggi committed
907

908
909
910
ScopeChain::ScopeChain()
    : globalScope(0)
    , qmlTypes(0)
911
    , jsImports(0)
912
913
914
{
}

915
916
917
918
919
920
921
922
923
924
925
926
927
ScopeChain::QmlComponentChain::QmlComponentChain()
{
}

ScopeChain::QmlComponentChain::~QmlComponentChain()
{
    qDeleteAll(instantiatingComponents);
}

void ScopeChain::QmlComponentChain::clear()
{
    qDeleteAll(instantiatingComponents);
    instantiatingComponents.clear();
928
    document = Document::Ptr(0);
929
930
}

931
void ScopeChain::QmlComponentChain::collect(QList<const ObjectValue *> *list) const
932
{
933
934
    foreach (const QmlComponentChain *parent, instantiatingComponents)
        parent->collect(list);
935

936
937
938
939
940
941
    if (!document)
        return;

    if (ObjectValue *root = document->bind()->rootObjectValue())
        list->append(root);
    if (ObjectValue *ids = document->bind()->idEnvironment())
942
943
944
945
946
        list->append(ids);
}

void ScopeChain::update()
{
947
    _all.clear();
948

949
    _all += globalScope;
950

951
952
    // the root scope in js files doesn't see instantiating components
    if (jsScopes.count() != 1 || !qmlScopeObjects.isEmpty()) {
953
        if (qmlComponentScope) {
954
955
            foreach (const QmlComponentChain *parent, qmlComponentScope->instantiatingComponents)
                parent->collect(&_all);
956
        }
957
    }
958

959
960
    ObjectValue *root = 0;
    ObjectValue *ids = 0;
961
962
963
    if (qmlComponentScope && qmlComponentScope->document) {
        root = qmlComponentScope->document->bind()->rootObjectValue();
        ids = qmlComponentScope->document->bind()->idEnvironment();
964
965
966
967
    }

    if (root && !qmlScopeObjects.contains(root))
        _all += root;
968
    _all += qmlScopeObjects;
969
970
    if (ids)
        _all += ids;
971
972
    if (qmlTypes)
        _all += qmlTypes;
973
974
    if (jsImports)
        _all += jsImports;
975
976
977
978
979
980
    _all += jsScopes;
}

QList<const ObjectValue *> ScopeChain::all() const
{
    return _all;
981
982
983
}


Pawel Polanski's avatar
Pawel Polanski committed
984
Context::Context(const QmlJS::Snapshot &snapshot)
985
986
    : _snapshot(snapshot),
      _engine(new Engine),
Christian Kamm's avatar
Christian Kamm committed
987
988
      _qmlScopeObjectIndex(-1),
      _qmlScopeObjectSet(false)
Roberto Raggi's avatar
Roberto Raggi committed
989
990
991
992
993
994
995
{
}

Context::~Context()
{
}

996
// the engine is only guaranteed to live as long as the context
Roberto Raggi's avatar
Roberto Raggi committed
997
998
Engine *Context::engine() const
{
999
    return _engine.data();
Roberto Raggi's avatar
Roberto Raggi committed
1000
1001
}

1002
1003
1004
1005
1006
QmlJS::Snapshot Context::snapshot() const
{
    return _snapshot;
}

1007
1008
1009
1010
1011
1012
const ScopeChain &Context::scopeChain() const
{
    return _scopeChain;
}

ScopeChain &Context::scopeChain()
1013
1014
1015
1016
{
    return _scopeChain;
}

1017
const Imports *Context::imports(const QmlJS::Document *doc) const
1018
{
1019
1020
    if (!doc)
        return 0;
1021
    return _imports.value(doc).data();
1022
1023
}

1024
void Context::setImports(const QmlJS::Document *doc, const Imports *imports)
1025
{
1026
1027
    if (!doc)
        return;
1028
    _imports[doc] = QSharedPointer<const Imports>(imports);
1029
1030
}

1031
const Value *Context::lookup(const QString &name, const ObjectValue **foundInScope) const
1032
{
1033
    QList<const ObjectValue *> scopes = _scopeChain.all();
1034
1035
    for (int index = scopes.size() - 1; index != -1; --index) {
        const ObjectValue *scope = scopes.at(index);
1036

1037
        if (const Value *member = scope->lookupMember(name, this)) {
1038
1039
            if (foundInScope)
                *foundInScope = scope;
1040
            return member;
1041
1042
1043
        }
    }

1044
1045
    if (foundInScope)
        *foundInScope = 0;
1046
1047
1048
    return _engine->undefinedValue();
}

1049
1050
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId *qmlTypeName,
                                       UiQualifiedId *qmlTypeNameEnd) const
1051
{
1052
1053
1054
1055
    const Imports *importsObj = imports(doc);
    if (!importsObj)
        return 0;
    const ObjectValue *objectValue = importsObj->typeScope();
1056
1057
    if (!objectValue)
        return 0;
1058

1059
1060
    for (UiQualifiedId *iter = qmlTypeName; objectValue && iter && iter != qmlTypeNameEnd;
         iter = iter->next) {
1061
1062
1063
        if (! iter->name)
            return 0;

1064
        const Value *value = objectValue->lookupMember(iter->name->asString(), this);
1065
1066
1067
1068
1069
1070
1071
1072
1073
        if (!value)
            return 0;

        objectValue = value->asObjectValue();
    }

    return objectValue;
}

1074
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, const QStringList &qmlTypeName) const
1075
{
1076
1077
1078
1079
1080
1081
    const Imports *importsObj = imports(doc);
    if (!importsObj)
        return 0;
    const ObjectValue *objectValue = importsObj->typeScope();
    if (!objectValue)
        return 0;
1082
1083

    foreach (const QString &name, qmlTypeName) {
1084
1085
1086
        if (!objectValue)
            return 0;

1087
        const Value *value = objectValue->lookupMember(name, this);
1088
1089
1090
1091
1092
1093
1094
1095
1096
        if (!value)
            return 0;

        objectValue = value->asObjectValue();
    }

    return objectValue;
}

1097
const Value *Context::lookupReference(const Value *value) const
1098
{
1099
1100
1101
1102
    const Reference *reference = value_cast<const Reference *>(value);
    if (!reference)
        return value;

1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
    if (_referenceStack.contains(reference))
        return 0;

    _referenceStack.append(reference);
    const Value *v = reference->value(this);
    _referenceStack.removeLast();

    return v;
}

Roberto Raggi's avatar
Roberto Raggi committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
const Value *Context::property(const ObjectValue *object, const QString &name) const
{
    const Properties properties = _properties.value(object);
    return properties.value(name, engine()->undefinedValue());
}

void Context::setProperty(const ObjectValue *object, const QString &name, const Value *value)
{
    _properties[object].insert(name, value);
}

1124
QString Context::defaultPropertyName(const ObjectValue *object) const
1125
{
1126
1127
1128
    PrototypeIterator iter(object, this);
    while (iter.hasNext()) {
        const ObjectValue *o = iter.next();
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
        if (const ASTObjectValue *astObjValue = dynamic_cast<const ASTObjectValue *>(o)) {
            QString defaultProperty = astObjValue->defaultPropertyName();
            if (!defaultProperty.isEmpty())
                return defaultProperty;
        } else if (const QmlObjectValue *qmlValue = dynamic_cast<const QmlObjectValue *>(o)) {
            return qmlValue->defaultPropertyName();
        }
    }
    return QString();
}

1140
1141
Reference::Reference(Engine *engine)
    : _engine(engine)
Roberto Raggi's avatar
Roberto Raggi committed
1142
{
1143
    _engine->registerValue(this);
Roberto Raggi's avatar
Roberto Raggi committed
1144
1145
1146
1147
1148
1149
}

Reference::~Reference()
{
}

1150
1151
1152
1153
1154
Engine *Reference::engine() const
{
    return _engine;
}

Roberto Raggi's avatar
Roberto Raggi committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
const Reference *Reference::asReference() const
{
    return this;
}

void Reference::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

1165
const Value *Reference::value(const Context *) const
1166
1167
1168
{
    return _engine->undefinedValue();
}
Roberto Raggi's avatar
Roberto Raggi committed
1169

1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
void ColorValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

const ColorValue *ColorValue::asColorValue() const
{
    return this;
}

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
void AnchorLineValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

const AnchorLineValue *AnchorLineValue::asAnchorLineValue() const
{
    return this;
}

1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
MemberProcessor::MemberProcessor()
{
}

MemberProcessor::~MemberProcessor()
{
}

bool MemberProcessor::processProperty(const QString &, const Value *)
{
    return true;
}

1203
1204
1205
1206
1207
bool MemberProcessor::processEnumerator(const QString &, const Value *)
{
    return true;
}

1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
bool MemberProcessor::processSignal(const QString &, const Value *)
{
    return true;
}

bool MemberProcessor::processSlot(const QString &,<