qmljsinterpreter.cpp 52.7 KB
Newer Older
1
2
3
4
5
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
32
#include "qmljscheck.h"
#include "parser/qmljsast_p.h"
33
34
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
35
36
#include <QtCore/QDebug>

Roberto Raggi's avatar
Roberto Raggi committed
37
38
39
40
#ifndef NO_DECLARATIVE_BACKEND
#  include <QtDeclarative/QmlType>
#  include <QtDeclarative/QmlMetaType>
#  include <QtDeclarative/private/qmlgraphicsanchors_p.h> // ### remove me
41
42
#  include <QtDeclarative/private/qmlgraphicsrectangle_p.h> // ### remove me
#  include <QtDeclarative/private/qmlvaluetype_p.h> // ### remove me
Roberto Raggi's avatar
Roberto Raggi committed
43
44
#endif

45
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

Roberto Raggi's avatar
Roberto Raggi committed
102
103
#ifndef NO_DECLARATIVE_BACKEND

Roberto Raggi's avatar
Roberto Raggi committed
104
105
namespace {

106
107
108
109
110
111
112
113
114
115
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
class MetaFunction: public FunctionValue
{
    QMetaMethod _method;

public:
    MetaFunction(const QMetaMethod &method, Engine *engine)
        : 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
150
151
} // end of anonymous namespace

152
153
154
155
156
157
158
QmlObjectValue::QmlObjectValue(const QMetaObject *metaObject, const QString &qmlTypeName,
                               int majorVersion, int minorVersion, Engine *engine)
    : ObjectValue(engine),
      _metaObject(metaObject),
      _qmlTypeName(qmlTypeName),
      _majorVersion(majorVersion),
      _minorVersion(minorVersion)
159
160
161
{
    setClassName(qmlTypeName); // ### TODO: we probably need to do more than just this...
}
Roberto Raggi's avatar
Roberto Raggi committed
162

163
QmlObjectValue::~QmlObjectValue() {}
Roberto Raggi's avatar
Roberto Raggi committed
164

165
const Value *QmlObjectValue::lookupMember(const QString &name, Context *context) const
166
{
167
    return ObjectValue::lookupMember(name, context);
Roberto Raggi's avatar
Roberto Raggi committed
168
}
169

Roberto Raggi's avatar
Roberto Raggi committed
170
171
172
const Value *QmlObjectValue::findOrCreateSignature(int index, const QMetaMethod &method, QString *methodName) const
{
    const QString signature = QString::fromUtf8(method.signature());
173

Roberto Raggi's avatar
Roberto Raggi committed
174
175
176
    const int indexOfParen = signature.indexOf(QLatin1Char('('));
    if (indexOfParen == -1)
        return engine()->undefinedValue(); // skip it, invalid signature.
177

Roberto Raggi's avatar
Roberto Raggi committed
178
179
180
181
182
    *methodName = signature.left(indexOfParen);
    const Value *value = _metaSignature.value(index);
    if (! value) {
        value = new MetaFunction(method, engine());
        _metaSignature.insert(index, value);
Roberto Raggi's avatar
Roberto Raggi committed
183
    }
Roberto Raggi's avatar
Roberto Raggi committed
184
    return value;
185
}
Roberto Raggi's avatar
Roberto Raggi committed
186

187
188
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
189
    // process the meta enums
190
191
192
193
194
195
196
197
    for (int index = _metaObject->enumeratorOffset(); index < _metaObject->propertyCount(); ++index) {
        QMetaEnum e = _metaObject->enumerator(index);

        for (int i = 0; i < e.keyCount(); ++i) {
            processor->processEnumerator(QString::fromUtf8(e.key(i)), engine()->numberValue());
        }
    }

Roberto Raggi's avatar
Roberto Raggi committed
198
199
200
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
        QMetaProperty prop = _metaObject->property(index);
201

Roberto Raggi's avatar
Roberto Raggi committed
202
203
        processor->processProperty(prop.name(), propertyValue(prop));
    }
204

Roberto Raggi's avatar
Roberto Raggi committed
205
206
207
208
209
    // process the meta methods
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
        QMetaMethod method = _metaObject->method(index);
        QString methodName;
        const Value *signature = findOrCreateSignature(index, method, &methodName);
210

211
        if (method.methodType() == QMetaMethod::Slot && method.access() == QMetaMethod::Public) {
Roberto Raggi's avatar
Roberto Raggi committed
212
            processor->processSlot(methodName, signature);
213

214
215
        } else if (method.methodType() == QMetaMethod::Signal && method.access() != QMetaMethod::Private) {
            // process the signal
Roberto Raggi's avatar
Roberto Raggi committed
216
            processor->processSignal(methodName, signature);
217

218
219
220
221
            QString slotName;
            slotName += QLatin1String("on");
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
222

223
            // process the generated slot
Roberto Raggi's avatar
Roberto Raggi committed
224
            processor->processGeneratedSlot(slotName, signature);
225
        }
Roberto Raggi's avatar
Roberto Raggi committed
226
227
    }

228
229
230
231
232
233
234
    ObjectValue::processMembers(processor);
}

const Value *QmlObjectValue::propertyValue(const QMetaProperty &prop) const
{
    if (QmlMetaType::isObject(prop.userType())) {
        QmlType *qmlPropertyType = QmlMetaType::qmlType(QmlMetaType::metaObjectForType(prop.userType()));
Roberto Raggi's avatar
Roberto Raggi committed
235

236
237
238
239
240
241
242
243
244
245
246
247
        if (qmlPropertyType && !qmlPropertyType->qmlTypeName().isEmpty()) {
            QString typeName = qmlPropertyType->qmlTypeName();
            int slashIdx = typeName.lastIndexOf(QLatin1Char('/'));
            QString package;
            if (slashIdx != -1) {
                package = typeName.left(slashIdx);
                typeName = typeName.mid(slashIdx + 1);
            }

            if (const ObjectValue *objectValue = engine()->newQmlObject(typeName, package, qmlPropertyType->majorVersion(), qmlPropertyType->minorVersion()))
                return objectValue;
        } else {
Roberto Raggi's avatar
Roberto Raggi committed
248
249
250
251
252
253
254
            QString typeName = QString::fromUtf8(prop.typeName());

            if (typeName.endsWith(QLatin1Char('*')))
                typeName.truncate(typeName.length() - 1);

            typeName.replace(QLatin1Char('.'), QLatin1Char('/'));

255
            if (const ObjectValue *objectValue = engine()->newQmlObject(typeName, "", -1, -1))  // ### we should extend this to lookup the property types in the QmlType object, instead of the QMetaProperty.
Roberto Raggi's avatar
Roberto Raggi committed
256
257
258
259
                return objectValue;
        }
    }

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
    const Value *value = engine()->undefinedValue();

    switch (prop.type()) {
    case QMetaType::QByteArray:
    case QMetaType::QString:
    case QMetaType::QUrl:
        value = engine()->stringValue();
        break;

    case QMetaType::Bool:
        value = engine()->booleanValue();
        break;

    case QMetaType::Int:
    case QMetaType::Float:
    case QMetaType::Double:
        value = engine()->numberValue();
        break;

    case QMetaType::QFont: {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        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;
    } break;

    case QMetaType::QPoint:
    case QMetaType::QPointF: {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        value = object;
    } break;

    case QMetaType::QRect:
    case QMetaType::QRectF: {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("width", engine()->numberValue());
        object->setProperty("height", engine()->numberValue());
        value = object;
    } break;

    default:
        break;
    } // end of switch

    return value;
}
Roberto Raggi's avatar
Roberto Raggi committed
323
324
#endif

325
326
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
327
328
329
330
331
332
333
334
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
335
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
336
337
338
339
340
341
342
};

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

Roberto Raggi's avatar
Roberto Raggi committed
343
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
344
345
346
347
348
349
350
};

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

Roberto Raggi's avatar
Roberto Raggi committed
351
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
352
353
354
355
356
357
358
};

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

Roberto Raggi's avatar
Roberto Raggi committed
359
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
360
361
362
363
364
365
366
};

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

Roberto Raggi's avatar
Roberto Raggi committed
367
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
368
369
370
371
372
373
374
};

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

Roberto Raggi's avatar
Roberto Raggi committed
375
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
376
377
378
379
380
381
382
};

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

Roberto Raggi's avatar
Roberto Raggi committed
383
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
384
385
386
387
388
389
390
};

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

Roberto Raggi's avatar
Roberto Raggi committed
391
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
};

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
434
const Value *ObjectCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
435
{
Roberto Raggi's avatar
Roberto Raggi committed
436
437
438
439
440
441
442
443
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("Object");
    thisObject->setPrototype(engine()->objectPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
444
445
}

Roberto Raggi's avatar
Roberto Raggi committed
446
const Value *FunctionCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
447
{
Roberto Raggi's avatar
Roberto Raggi committed
448
449
450
451
452
453
454
455
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("Function");
    thisObject->setPrototype(engine()->functionPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
456
457
}

Roberto Raggi's avatar
Roberto Raggi committed
458
const Value *ArrayCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
459
{
Roberto Raggi's avatar
Roberto Raggi committed
460
461
462
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
463

Roberto Raggi's avatar
Roberto Raggi committed
464
465
466
467
    thisObject->setClassName("Array");
    thisObject->setPrototype(engine()->arrayPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
468
469
}

Roberto Raggi's avatar
Roberto Raggi committed
470
const Value *StringCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
471
{
Roberto Raggi's avatar
Roberto Raggi committed
472
473
    if (activation->calledAsFunction())
        return engine()->convertToString(activation->thisObject());
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
474

Roberto Raggi's avatar
Roberto Raggi committed
475
476
477
478
479
    ObjectValue *thisObject = activation->thisObject();
    thisObject->setClassName("String");
    thisObject->setPrototype(engine()->stringPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
480
481
}

Roberto Raggi's avatar
Roberto Raggi committed
482
const Value *BooleanCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
483
{
Roberto Raggi's avatar
Roberto Raggi committed
484
485
486
487
488
489
490
    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
491
492
}

Roberto Raggi's avatar
Roberto Raggi committed
493
const Value *NumberCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
494
{
Roberto Raggi's avatar
Roberto Raggi committed
495
496
497
498
499
500
501
    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
502
503
}

Roberto Raggi's avatar
Roberto Raggi committed
504
const Value *DateCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
505
{
Roberto Raggi's avatar
Roberto Raggi committed
506
507
508
509
510
511
512
    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
513
514
}

Roberto Raggi's avatar
Roberto Raggi committed
515
const Value *RegExpCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
516
{
Roberto Raggi's avatar
Roberto Raggi committed
517
518
519
520
521
522
523
524
525
526
527
528
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();

    thisObject->setClassName("RegExp");
    thisObject->setPrototype(engine()->regexpPrototype());
    thisObject->setProperty("source", engine()->stringValue());
    thisObject->setProperty("global", engine()->booleanValue());
    thisObject->setProperty("ignoreCase", engine()->booleanValue());
    thisObject->setProperty("multiline", engine()->booleanValue());
    thisObject->setProperty("lastIndex", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
529
530
531
532
}

} // end of anonymous namespace

533
534
535
////////////////////////////////////////////////////////////////////////////////
// ValueVisitor
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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 *)
{
}
571

Roberto Raggi's avatar
Roberto Raggi committed
572
573
574
void ValueVisitor::visit(const Reference *)
{
}
575
576
577
578

////////////////////////////////////////////////////////////////////////////////
// Value
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
Value::Value()
{
}

Value::~Value()
{
}

const NullValue *Value::asNullValue() const
{
    return 0;
}

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

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

const BooleanValue *Value::asBooleanValue() const
{
    return 0;
}

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

const ObjectValue *Value::asObjectValue() const
{
    return 0;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
622
623
624
625
626
const Reference *Value::asReference() const
{
    return 0;
}

627
628
629
////////////////////////////////////////////////////////////////////////////////
// Values
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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);
}
649

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
const NumberValue *NumberValue::asNumberValue() const
{
    return this;
}

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

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

Roberto Raggi's avatar
Roberto Raggi committed
680
681

Context::Context(Engine *engine)
682
683
    : _engine(engine),
      _lookupMode(JSLookup)
Roberto Raggi's avatar
Roberto Raggi committed
684
685
686
687
688
689
690
691
692
693
694
695
{
}

Context::~Context()
{
}

Engine *Context::engine() const
{
    return _engine;
}

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
Context::ScopeChain Context::scopeChain() const
{
    return _scopeChain;
}

Context::LookupMode Context::lookupMode() const
{
    return _lookupMode;
}

void Context::setLookupMode(LookupMode lookupMode)
{
    _lookupMode = lookupMode;
}

711
712
713
714
715
716
717
718
719
720
const ObjectValue *Context::typeEnvironment() const
{
    return _typeEnvironment;
}

void Context::setTypeEnvironment(const ObjectValue *typeEnvironment)
{
    _typeEnvironment = typeEnvironment;
}

721
722
723
724
725
726
727
728
729
730
void Context::pushScope(const ObjectValue *object)
{
    _scopeChain.append(object);
}

void Context::popScope()
{
    _scopeChain.removeLast();
}

731
const Value *Context::lookup(const QString &name)
732
733
734
735
{
    for (int index = _scopeChain.size() - 1; index != -1; --index) {
        const ObjectValue *scope = _scopeChain.at(index);

736
        if (const Value *member = scope->lookupMember(name, this)) {
737
738
739
740
741
742
743
744
745
            if (_lookupMode == JSLookup || ! dynamic_cast<const ASTVariableReference *>(member)) {
                return member;
            }
        }
    }

    return _engine->undefinedValue();
}

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
const ObjectValue *Context::lookupType(AST::UiQualifiedId *qmlTypeName)
{
    const ObjectValue *objectValue = _typeEnvironment;

    for (UiQualifiedId *iter = qmlTypeName; objectValue && iter; iter = iter->next) {
        if (! iter->name)
            return 0;

        const Value *value = objectValue->property(iter->name->asString(), this);
        if (!value)
            return 0;

        objectValue = value->asObjectValue();
    }

    return objectValue;
}

Roberto Raggi's avatar
Roberto Raggi committed
764
765
766
767
768
769
770
771
772
773
774
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);
}

775
776
Reference::Reference(Engine *engine)
    : _engine(engine)
Roberto Raggi's avatar
Roberto Raggi committed
777
{
778
    _engine->registerValue(this);
Roberto Raggi's avatar
Roberto Raggi committed
779
780
781
782
783
784
}

Reference::~Reference()
{
}

785
786
787
788
789
Engine *Reference::engine() const
{
    return _engine;
}

Roberto Raggi's avatar
Roberto Raggi committed
790
791
792
793
794
795
796
797
798
799
const Reference *Reference::asReference() const
{
    return this;
}

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

800
801
802
803
const Value *Reference::value(Context *) const
{
    return _engine->undefinedValue();
}
Roberto Raggi's avatar
Roberto Raggi committed
804

805
806
807
808
809
810
811
812
813
814
815
816
817
MemberProcessor::MemberProcessor()
{
}

MemberProcessor::~MemberProcessor()
{
}

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

818
819
820
821
822
bool MemberProcessor::processEnumerator(const QString &, const Value *)
{
    return true;
}

823
824
825
826
827
828
829
830
831
832
bool MemberProcessor::processSignal(const QString &, const Value *)
{
    return true;
}

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

833
834
835
836
837
bool MemberProcessor::processGeneratedSlot(const QString &, const Value *)
{
    return true;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
838
ObjectValue::ObjectValue(Engine *engine)
839
    : _engine(engine),
840
      _prototype(0)
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
841
{
842
    engine->registerValue(this);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
}

ObjectValue::~ObjectValue()
{
}

Engine *ObjectValue::engine() const
{
    return _engine;
}

QString ObjectValue::className() const
{
    return _className;
}

void ObjectValue::setClassName(const QString &className)
{
    _className = className;
}

864
const ObjectValue *ObjectValue::prototype(Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
865
{
866
867
868
869
870
871
872
873
874
875
876
877
878
    const ObjectValue *prototypeObject = value_cast<const ObjectValue *>(_prototype);
    if (! prototypeObject) {
        if (const Reference *prototypeReference = value_cast<const Reference *>(_prototype)) {
            prototypeObject = value_cast<const ObjectValue *>(prototypeReference->value(context));
        }
    }
    return prototypeObject;
}

void ObjectValue::setPrototype(const Value *prototype)
{
    // ### FIXME: Check for cycles.
    _prototype = prototype;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
}

void ObjectValue::setProperty(const QString &name, const Value *value)
{
    _members[name] = value;
}

void ObjectValue::removeProperty(const QString &name)
{
    _members.remove(name);
}

const ObjectValue *ObjectValue::asObjectValue() const
{
    return this;
}

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

901
const Value *ObjectValue::property(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
902
{
903
    return lookupMember(name, context);
904
905
}

906
bool ObjectValue::checkPrototype(const ObjectValue *, QSet<const ObjectValue *> *) const
907
{
908
#if 0
909
910
911
912
913
914
915
    const int previousSize = processed->size();
    processed->insert(this);

    if (previousSize != processed->size()) {
        if (this == proto)
            return false;

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
916
        if (prototype() && ! prototype()->checkPrototype(proto, processed))
917
918
919
920
            return false;

        return true;
    }
921
#endif
922
923
924
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
925
926
927
928
929
930
931
void ObjectValue::processMembers(MemberProcessor *processor) const
{
    QHashIterator<QString, const Value *> it(_members);

    while (it.hasNext()) {
        it.next();

932
        if (! processor->processProperty(it.key(), it.value()))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
933
934
935
936
            break;
    }
}

937
const Value *ObjectValue::lookupMember(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
938
{
Roberto Raggi's avatar
Roberto Raggi committed
939
    if (const Value *m = _members.value(name))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
940
        return m;
Roberto Raggi's avatar
Roberto Raggi committed
941
942
943
944
945
946
    else {
        LookupMember slowLookup(name);
        processMembers(&slowLookup);
        if (slowLookup.value())
            return slowLookup.value();
    }
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
947

948
949
950
    const ObjectValue *prototypeObject = prototype(context);
    if (prototypeObject) {
        if (const Value *m = prototypeObject->lookupMember(name, context))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
951
952
953
954
955
956
            return m;
    }

    return 0;
}

957
Activation::Activation(Context *parentContext)
958
    : _thisObject(0),
959
960
      _calledAsFunction(true),
      _parentContext(parentContext)
Roberto Raggi's avatar
Roberto Raggi committed
961
962
963
964
965
966
967
{
}

Activation::~Activation()
{
}

968
969
970
971
972
973
974
975
976
977
978
Context *Activation::parentContext() const
{
    return _parentContext;
}

Context *Activation::context() const
{
    // ### FIXME: Real context for activations.
    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
bool Activation::calledAsConstructor() const
{
    return ! _calledAsFunction;
}

void Activation::setCalledAsConstructor(bool calledAsConstructor)
{
    _calledAsFunction = ! calledAsConstructor;
}

bool Activation::calledAsFunction() const
{
    return _calledAsFunction;
}

void Activation::setCalledAsFunction(bool calledAsFunction)
{
    _calledAsFunction = calledAsFunction;
}

ObjectValue *Activation::thisObject() const
{