qmljsinterpreter.cpp 48.9 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
33

#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
34
35
#include <QtCore/QDebug>

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

44
45
using namespace QmlJS::Interpreter;

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
46
47
namespace {

Roberto Raggi's avatar
Roberto Raggi committed
48
49
#ifndef NO_DECLARATIVE_BACKEND

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class QmlAttachedKeys: public ObjectValue
{
    static const char **builtinPropertyNames()
    {
        static const char *properties[] = {
            "enabledChanged",
            "pressed",
            "released",
            "digit0Pressed",
            "digit1Pressed",
            "digit2Pressed",
            "digit3Pressed",
            "digit4Pressed",
            "digit5Pressed",
            "digit6Pressed",
            "digit7Pressed",
            "digit8Pressed",
            "digit9Pressed",
            "leftPressed",
            "rightPressed",
            "upPressed",
            "downPressed",
            "asteriskPressed",
            "numberSignPressed",
            "escapePressed",
            "returnPressed",
            "enterPressed",
            "deletePressed",
            "spacePressed",
            "backPressed",
            "cancelPressed",
            "selectPressed",
            "yesPressed",
            "noPressed",
            "context1Pressed",
            "context2Pressed",
            "context3Pressed",
            "context4Pressed",
            "callPressed",
            "hangupPressed",
            "flipPressed",
            "menuPressed",
            "volumeUpPressed",
            "volumeDownPressed",
            0
        };

        return properties;
    }

    QSet<QString> _generatedSlots;

public:
    QmlAttachedKeys(Engine *engine)
        : ObjectValue(engine)
    {
        for (const char **it = builtinPropertyNames(); *it; ++it) {
            QString signalName = QLatin1String(*it);
            QString generatedSlot = QLatin1String("on");
            generatedSlot += signalName.at(0).toUpper();
            generatedSlot += signalName.midRef(1);
            _generatedSlots.insert(generatedSlot);
        }
    }

    virtual void processMembers(MemberProcessor *processor) const
    {
        foreach (const QString &generatedSlot, _generatedSlots)
            processor->processSlot(generatedSlot, engine()->undefinedValue());

        ObjectValue::processMembers(processor);
    }
};

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class MetaFunction: public FunctionValue
{
    QMetaMethod _method;

public:
    MetaFunction(const QMetaMethod &method, Engine *engine)
        : FunctionValue(engine), _method(method)
    {
        engine->registerObject(this);
    }

    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
169
170
171
172
class QmlObjectValue: public ObjectValue
{
public:
    QmlObjectValue(const QMetaObject *metaObject, Engine *engine)
173
174
175
        : ObjectValue(engine), _metaObject(metaObject)
    {
    }
Roberto Raggi's avatar
Roberto Raggi committed
176
177
178
179
180
181
182
183
184
185
186
187

    virtual ~QmlObjectValue() {}

    virtual const Value *lookupMember(const QString &name) const
    {
        for (int index = 0; index < _metaObject->propertyCount(); ++index) {
            QMetaProperty prop = _metaObject->property(index);

            if (name == QString::fromUtf8(prop.name()))
                return propertyValue(prop);
        }

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        for (int index = 0; index < _metaObject->methodCount(); ++index) {
            QMetaMethod method = _metaObject->method(index);

            const QString signature = QString::fromUtf8(method.signature());

            const int indexOfParen = signature.indexOf(QLatin1Char('('));
            if (indexOfParen == -1)
                continue; // skip it, invalid signature.

            const QString methodName = signature.left(indexOfParen);

            if (methodName != name) {
                continue;

            } else if (method.methodType() == QMetaMethod::Slot && method.access() == QMetaMethod::Public) {
                return new MetaFunction(method, engine());

            } else if (method.methodType() == QMetaMethod::Signal && method.access() != QMetaMethod::Private) {
                return new MetaFunction(method, engine());
            }
        }

Roberto Raggi's avatar
Roberto Raggi committed
210
211
212
213
214
215
216
217
        return ObjectValue::lookupMember(name);
    }

    virtual void processMembers(MemberProcessor *processor) const
    {
        for (int index = 0; index < _metaObject->propertyCount(); ++index) {
            QMetaProperty prop = _metaObject->property(index);

218
            processor->processProperty(prop.name(), propertyValue(prop));
Roberto Raggi's avatar
Roberto Raggi committed
219
        }
220
221

        for (int index = 0; index < _metaObject->methodCount(); ++index) {
222
            QMetaMethod method = _metaObject->method(index);
223

224
            const QString signature = QString::fromUtf8(method.signature());
225

226
            const int indexOfParen = signature.indexOf(QLatin1Char('('));
227
228
229
            if (indexOfParen == -1)
                continue; // skip it, invalid signature.

230
            const QString methodName = signature.left(indexOfParen);
231

232
            if (method.methodType() == QMetaMethod::Slot && method.access() == QMetaMethod::Public) {
233
234
                processor->processSlot(methodName, engine()->undefinedValue());

235
            } else if (method.methodType() == QMetaMethod::Signal && method.access() != QMetaMethod::Private) {
236
                // process the signal
237
                processor->processSignal(methodName, engine()->undefinedValue());
238
239
240
241
242
243
244

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

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

249
        ObjectValue::processMembers(processor);
Roberto Raggi's avatar
Roberto Raggi committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    }

    const Value *propertyValue(const QMetaProperty &prop) const {
        const Value *value = engine()->undefinedValue();

        if (QmlMetaType::isObject(prop.userType())) {
            QString typeName = QString::fromUtf8(prop.typeName());

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

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

            if (const ObjectValue *objectValue = engine()->newQmlObject(typeName))
                return objectValue;
        }

        switch (prop.type()) {
        case QMetaType::QByteArray:
        case QMetaType::QString:
270
        case QMetaType::QUrl:
Roberto Raggi's avatar
Roberto Raggi committed
271
272
273
274
275
276
277
278
279
280
281
282
283
            value = engine()->stringValue();
            break;

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

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

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
        case QMetaType::QFont: {
            // ### cache
            ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
            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;

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
        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;

Roberto Raggi's avatar
Roberto Raggi committed
321
322
323
324
325
326
327
328
329
330
331
332
333
        default:
            break;
        } // end of switch

        return value;
    }

private:
    const QMetaObject *_metaObject;
};

#endif

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
334
335
336
337
338
339
340
341
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

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

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

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

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

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

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

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

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

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
390
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
391
392
393
394
395
396
397
};

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

Roberto Raggi's avatar
Roberto Raggi committed
398
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
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
434
435
436
437
438
439
440
};

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
441
const Value *ObjectCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
442
{
Roberto Raggi's avatar
Roberto Raggi committed
443
444
445
446
447
448
449
450
    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
451
452
}

Roberto Raggi's avatar
Roberto Raggi committed
453
const Value *FunctionCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
454
{
Roberto Raggi's avatar
Roberto Raggi committed
455
456
457
458
459
460
461
462
    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
463
464
}

Roberto Raggi's avatar
Roberto Raggi committed
465
const Value *ArrayCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
466
{
Roberto Raggi's avatar
Roberto Raggi committed
467
468
469
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
470

Roberto Raggi's avatar
Roberto Raggi committed
471
472
473
474
    thisObject->setClassName("Array");
    thisObject->setPrototype(engine()->arrayPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
475
476
}

Roberto Raggi's avatar
Roberto Raggi committed
477
const Value *StringCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
478
{
Roberto Raggi's avatar
Roberto Raggi committed
479
480
    if (activation->calledAsFunction())
        return engine()->convertToString(activation->thisObject());
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
481

Roberto Raggi's avatar
Roberto Raggi committed
482
483
484
485
486
    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
487
488
}

Roberto Raggi's avatar
Roberto Raggi committed
489
const Value *BooleanCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
490
{
Roberto Raggi's avatar
Roberto Raggi committed
491
492
493
494
495
496
497
    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
498
499
}

Roberto Raggi's avatar
Roberto Raggi committed
500
const Value *NumberCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
501
{
Roberto Raggi's avatar
Roberto Raggi committed
502
503
504
505
506
507
508
    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
509
510
}

Roberto Raggi's avatar
Roberto Raggi committed
511
const Value *DateCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
512
{
Roberto Raggi's avatar
Roberto Raggi committed
513
514
515
516
517
518
519
    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
520
521
}

Roberto Raggi's avatar
Roberto Raggi committed
522
const Value *RegExpCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
523
{
Roberto Raggi's avatar
Roberto Raggi committed
524
525
526
527
528
529
530
531
532
533
534
535
    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
536
537
538
539
}

} // end of anonymous namespace

540
541
542
////////////////////////////////////////////////////////////////////////////////
// ValueVisitor
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
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
571
572
573
574
575
576
577
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 *)
{
}
578
579
580
581
582


////////////////////////////////////////////////////////////////////////////////
// Value
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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;
}
625
626
627
628

////////////////////////////////////////////////////////////////////////////////
// Environment
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
629
630
631
632
633
634
635
636
637
638
639
640
Environment::Environment()
{
}

Environment::~Environment()
{
}

const Environment *Environment::parent() const
{
    return 0;
}
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661

const Value *Environment::lookup(const QString &name) const
{
    if (const Value *member = lookupMember(name))
        return member;

    else if (const Environment *p = parent())
        return p->lookup(name);

    else
        return 0;
}

const Value *Environment::lookupMember(const QString &name) const {
    Q_UNUSED(name);
    return 0;
}

////////////////////////////////////////////////////////////////////////////////
// Values
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
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);
}
681

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
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);
}

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
MemberProcessor::MemberProcessor()
{
}

MemberProcessor::~MemberProcessor()
{
}

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

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

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

735
736
737
738
739
bool MemberProcessor::processGeneratedSlot(const QString &, const Value *)
{
    return true;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
ObjectValue::ObjectValue(Engine *engine)
    : _engine(engine), _prototype(0), _scope(0)
{
}

ObjectValue::~ObjectValue()
{
}

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

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

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

const ObjectValue *ObjectValue::prototype() const
{
    return _prototype;
}

const ObjectValue *ObjectValue::scope() const
{
    return _scope;
}

void ObjectValue::setScope(const ObjectValue *scope)
{
    _scope = scope;
}

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

const Value *ObjectValue::property(const QString &name) const
{
801
802
803
804
805
806
807
    return lookupMember(name);
}

void ObjectValue::setPrototype(const ObjectValue *prototype)
{
    QSet<const ObjectValue *> processed;

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
808
    if (! prototype || checkPrototype(prototype, &processed))
809
810
811
812
813
        _prototype = prototype;
    else
        qWarning() << "**** invalid prototype:";
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
814
bool ObjectValue::checkPrototype(const ObjectValue *proto, QSet<const ObjectValue *> *processed) const
815
816
817
818
819
820
821
822
{
    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
823
        if (prototype() && ! prototype()->checkPrototype(proto, processed))
824
825
826
827
828
829
830
831
            return false;

        return true;
    }

    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
832
833
834
835
836
837
838
void ObjectValue::processMembers(MemberProcessor *processor) const
{
    QHashIterator<QString, const Value *> it(_members);

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

839
        if (! processor->processProperty(it.key(), it.value()))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
840
841
842
843
844
845
846
847
848
849
850
            break;
    }
}

const Environment *ObjectValue::parent() const
{
    return _scope;
}

const Value *ObjectValue::lookupMember(const QString &name) const
{
Roberto Raggi's avatar
Roberto Raggi committed
851
    if (const Value *m = _members.value(name))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
852
853
854
855
856
857
858
859
860
861
        return m;

    if (_prototype) {
        if (const Value *m = _prototype->lookup(name))
            return m;
    }

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
Activation::Activation(Engine *engine)
    : ObjectValue(engine),
      _thisObject(0),
      _calledAsFunction(true)
{
}

Activation::~Activation()
{
}

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
{
    return _thisObject;
}

void Activation::setThisObject(ObjectValue *thisObject)
{
    _thisObject = thisObject;
}

ValueList Activation::arguments() const
{
    return _arguments;
}

void Activation::setArguments(const ValueList &arguments)
{
    _arguments = arguments;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
913
914
915
916
917
918
919
920
921
922
923
FunctionValue::FunctionValue(Engine *engine)
    : ObjectValue(engine)
{
}

FunctionValue::~FunctionValue()
{
}

const Value *FunctionValue::construct(const ValueList &actuals) const
{
Roberto Raggi's avatar
Roberto Raggi committed
924
925
926
927
928
    Activation activation(engine()); // ### FIXME: create on the heap
    activation.setCalledAsConstructor(true);
    activation.setThisObject(engine()->newObject());
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
929
930
931
932
}

const Value *FunctionValue::call(const ValueList &actuals) const
{
Roberto Raggi's avatar
Roberto Raggi committed
933
934
935
936
937
    Activation activation(engine()); // ### FIXME: create on the heap
    activation.setCalledAsFunction(true);
    activation.setThisObject(engine()->globalObject()); // ### FIXME: it should be `null'
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
938
939
940
941
}

const Value *FunctionValue::call(const ObjectValue *thisObject, const ValueList &actuals) const
{
Roberto Raggi's avatar
Roberto Raggi committed
942
943
944
945
946
    Activation activation(engine()); // ### FIXME: create on the heap
    activation.setCalledAsFunction(true);
    activation.setThisObject(const_cast<ObjectValue *>(thisObject)); // ### FIXME: remove the const_cast
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
947
948
}

949
950
951
952
953
const Value *FunctionValue::returnValue() const
{
    return engine()->undefinedValue();
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
954
955
956
957
958
int FunctionValue::argumentCount() const
{
    return 0;
}

959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
const Value *FunctionValue::argument(int) const
{
    return engine()->undefinedValue();
}

QString FunctionValue::argumentName(int index) const
{
    return QString::fromLatin1("arg%1").arg(index);
}

bool FunctionValue::isVariadic() const
{
    return true;
}

const Value *FunctionValue::invoke(const Activation *activation) const
{
    return activation->thisObject(); // ### FIXME: it should return undefined
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
979
980
981
982
983
984
985
986
987
988
989
990
const FunctionValue *FunctionValue::asFunctionValue() const
{
    return this;
}

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

Function::Function(Engine *engine)
    : FunctionValue(engine), _returnValue(0)
991
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
992
    setClassName("Function");
993
994
}

Roberto Raggi's avatar
Roberto Raggi committed
995
996
997
998
Function::~Function()
{
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
999
void Function::addArgument(const Value *argument)
1000
{