qmljsinterpreter.cpp 46.6 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
Roberto Raggi committed
46
47
#ifndef NO_DECLARATIVE_BACKEND

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

92
93
QmlObjectValue::QmlObjectValue(const QMetaObject *metaObject, const QString &qmlTypeName, int majorVersion, int minorVersion, Engine *engine)
    : ObjectValue(engine), _metaObject(metaObject), _qmlTypeName(qmlTypeName), _majorVersion(majorVersion), _minorVersion(minorVersion)
94
95
96
{
    setClassName(qmlTypeName); // ### TODO: we probably need to do more than just this...
}
Roberto Raggi's avatar
Roberto Raggi committed
97

98
QmlObjectValue::~QmlObjectValue() {}
Roberto Raggi's avatar
Roberto Raggi committed
99

100
101
102
103
const Value *QmlObjectValue::lookupMember(const QString &name) const
{
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
        QMetaProperty prop = _metaObject->property(index);
Roberto Raggi's avatar
Roberto Raggi committed
104

105
106
107
        if (name == QString::fromUtf8(prop.name()))
            return propertyValue(prop);
    }
Roberto Raggi's avatar
Roberto Raggi committed
108

109
110
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
        QMetaMethod method = _metaObject->method(index);
111

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

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

118
        const QString methodName = signature.left(indexOfParen);
119

120
121
        if (methodName != name) {
            continue;
122

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

126
127
        } else if (method.methodType() == QMetaMethod::Signal && method.access() != QMetaMethod::Private) {
            return new MetaFunction(method, engine());
128
        }
Roberto Raggi's avatar
Roberto Raggi committed
129
130
    }

131
132
    return ObjectValue::lookupMember(name);
}
Roberto Raggi's avatar
Roberto Raggi committed
133

134
135
136
137
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
        QMetaProperty prop = _metaObject->property(index);
138

139
140
        processor->processProperty(prop.name(), propertyValue(prop));
    }
141

142
143
    for (int index = 0; index < _metaObject->methodCount(); ++index) {
        QMetaMethod method = _metaObject->method(index);
144

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

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

151
        const QString methodName = signature.left(indexOfParen);
152

153
154
        if (method.methodType() == QMetaMethod::Slot && method.access() == QMetaMethod::Public) {
            processor->processSlot(methodName, engine()->undefinedValue());
155

156
157
158
        } else if (method.methodType() == QMetaMethod::Signal && method.access() != QMetaMethod::Private) {
            // process the signal
            processor->processSignal(methodName, engine()->undefinedValue());
159

160
161
162
163
            QString slotName;
            slotName += QLatin1String("on");
            slotName += methodName.at(0).toUpper();
            slotName += methodName.midRef(1);
164

165
166
167
            // process the generated slot
            processor->processGeneratedSlot(slotName, engine()->undefinedValue());
        }
Roberto Raggi's avatar
Roberto Raggi committed
168
169
    }

170
171
172
173
174
175
176
    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
177

178
179
180
181
182
183
184
185
186
187
188
189
        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
190
191
192
193
194
195
196
            QString typeName = QString::fromUtf8(prop.typeName());

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

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

197
            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
198
199
200
201
                return objectValue;
        }
    }

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    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
265
266
#endif

267
268
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
269
270
271
272
273
274
275
276
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

Roberto Raggi's avatar
Roberto Raggi committed
277
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
278
279
280
281
282
283
284
};

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

Roberto Raggi's avatar
Roberto Raggi committed
285
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
286
287
288
289
290
291
292
};

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

Roberto Raggi's avatar
Roberto Raggi committed
293
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
294
295
296
297
298
299
300
};

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

Roberto Raggi's avatar
Roberto Raggi committed
301
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
302
303
304
305
306
307
308
};

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

Roberto Raggi's avatar
Roberto Raggi committed
309
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
310
311
312
313
314
315
316
};

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

Roberto Raggi's avatar
Roberto Raggi committed
317
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
318
319
320
321
322
323
324
};

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

Roberto Raggi's avatar
Roberto Raggi committed
325
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
326
327
328
329
330
331
332
};

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

Roberto Raggi's avatar
Roberto Raggi committed
333
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
};

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
376
const Value *ObjectCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
377
{
Roberto Raggi's avatar
Roberto Raggi committed
378
379
380
381
382
383
384
385
    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
386
387
}

Roberto Raggi's avatar
Roberto Raggi committed
388
const Value *FunctionCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
389
{
Roberto Raggi's avatar
Roberto Raggi committed
390
391
392
393
394
395
396
397
    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
398
399
}

Roberto Raggi's avatar
Roberto Raggi committed
400
const Value *ArrayCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
401
{
Roberto Raggi's avatar
Roberto Raggi committed
402
403
404
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
405

Roberto Raggi's avatar
Roberto Raggi committed
406
407
408
409
    thisObject->setClassName("Array");
    thisObject->setPrototype(engine()->arrayPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
410
411
}

Roberto Raggi's avatar
Roberto Raggi committed
412
const Value *StringCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
413
{
Roberto Raggi's avatar
Roberto Raggi committed
414
415
    if (activation->calledAsFunction())
        return engine()->convertToString(activation->thisObject());
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
416

Roberto Raggi's avatar
Roberto Raggi committed
417
418
419
420
421
    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
422
423
}

Roberto Raggi's avatar
Roberto Raggi committed
424
const Value *BooleanCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
425
{
Roberto Raggi's avatar
Roberto Raggi committed
426
427
428
429
430
431
432
    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
433
434
}

Roberto Raggi's avatar
Roberto Raggi committed
435
const Value *NumberCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
436
{
Roberto Raggi's avatar
Roberto Raggi committed
437
438
439
440
441
442
443
    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
444
445
}

Roberto Raggi's avatar
Roberto Raggi committed
446
const Value *DateCtor::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
    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
455
456
}

Roberto Raggi's avatar
Roberto Raggi committed
457
const Value *RegExpCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
458
{
Roberto Raggi's avatar
Roberto Raggi committed
459
460
461
462
463
464
465
466
467
468
469
470
    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
471
472
473
474
}

} // end of anonymous namespace

475
476
477
////////////////////////////////////////////////////////////////////////////////
// ValueVisitor
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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 *)
{
}
513
514
515
516
517


////////////////////////////////////////////////////////////////////////////////
// Value
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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;
}
560
561
562
563

////////////////////////////////////////////////////////////////////////////////
// Environment
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
564
565
566
567
568
569
570
571
572
573
574
575
Environment::Environment()
{
}

Environment::~Environment()
{
}

const Environment *Environment::parent() const
{
    return 0;
}
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
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);
}
616

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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);
}

647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
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;
}

670
671
672
673
674
bool MemberProcessor::processGeneratedSlot(const QString &, const Value *)
{
    return true;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
675
ObjectValue::ObjectValue(Engine *engine)
676
677
678
    : _engine(engine),
      _prototype(0),
      _scope(0)
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
679
{
680
    engine->registerObject(this);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
681
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
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
}

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
{
739
740
741
742
743
744
745
    return lookupMember(name);
}

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

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
746
    if (! prototype || checkPrototype(prototype, &processed))
747
748
749
750
751
        _prototype = prototype;
    else
        qWarning() << "**** invalid prototype:";
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
752
bool ObjectValue::checkPrototype(const ObjectValue *proto, QSet<const ObjectValue *> *processed) const
753
754
755
756
757
758
759
760
{
    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
761
        if (prototype() && ! prototype()->checkPrototype(proto, processed))
762
763
764
765
766
767
768
769
            return false;

        return true;
    }

    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
770
771
772
773
774
775
776
void ObjectValue::processMembers(MemberProcessor *processor) const
{
    QHashIterator<QString, const Value *> it(_members);

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

777
        if (! processor->processProperty(it.key(), it.value()))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
778
779
780
781
782
783
784
785
786
787
788
            break;
    }
}

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

const Value *ObjectValue::lookupMember(const QString &name) const
{
Roberto Raggi's avatar
Roberto Raggi committed
789
    if (const Value *m = _members.value(name))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
790
791
792
793
794
795
796
797
798
799
        return m;

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

    return 0;
}

800
801
Activation::Activation()
    : _thisObject(0),
Roberto Raggi's avatar
Roberto Raggi committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
      _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
850
851
852
853
854
855
856
857
858
859
860
FunctionValue::FunctionValue(Engine *engine)
    : ObjectValue(engine)
{
}

FunctionValue::~FunctionValue()
{
}

const Value *FunctionValue::construct(const ValueList &actuals) const
{
861
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
862
863
864
865
    activation.setCalledAsConstructor(true);
    activation.setThisObject(engine()->newObject());
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
866
867
868
869
}

const Value *FunctionValue::call(const ValueList &actuals) const
{
870
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
871
872
873
874
    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
875
876
877
878
}

const Value *FunctionValue::call(const ObjectValue *thisObject, const ValueList &actuals) const
{
879
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
880
881
882
883
    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
884
885
}

886
887
888
889
890
const Value *FunctionValue::returnValue() const
{
    return engine()->undefinedValue();
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
891
892
893
894
895
int FunctionValue::argumentCount() const
{
    return 0;
}

896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
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
916
917
918
919
920
921
922
923
924
925
926
927
const FunctionValue *FunctionValue::asFunctionValue() const
{
    return this;
}

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

Function::Function(Engine *engine)
    : FunctionValue(engine), _returnValue(0)
928
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
929
    setClassName("Function");
930
931
}

Roberto Raggi's avatar
Roberto Raggi committed
932
933
934
935
Function::~Function()
{
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
936
void Function::addArgument(const Value *argument)
937
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
938
    _arguments.push_back(argument);
939
940
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
941
const Value *Function::returnValue() const
942
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
943
    return _returnValue;
944
945
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
946
947
948
949
void Function::setReturnValue(const Value *returnValue)
{
    _returnValue = returnValue;
}
950

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
951
952
953
954
int Function::argumentCount() const
{
    return _arguments.size();
}
955

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
956
957
958
959
const Value *Function::argument(int index) const
{
    return _arguments.at(index);
}
960
961
962
963
964
965
966
967
968

const Value *Function::property(const QString &name) const
{
    if (name == "length")
        return engine()->numberValue();

    return FunctionValue::property(name);
}

Roberto Raggi's avatar
Roberto Raggi committed
969
const Value *Function::invoke(const Activation *activation) const
970
{
Roberto Raggi's avatar
Roberto Raggi committed
971
    return activation->thisObject(); // ### FIXME it should return undefined
972
973
974
975
976
}

////////////////////////////////////////////////////////////////////////////////
// typing environment
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
977
978
979
980
ConvertToNumber::ConvertToNumber(Engine *engine)
    : _engine(engine), _result(0)
{
}
981

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
982
983
const Value *ConvertToNumber::operator()(const Value *value)
{
984
985
986
987
988
989
990
991
    const Value *previousValue = switchResult(0);

    if (value)
        value->accept(this);

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
992
993
const Value *ConvertToNumber::switchResult(const Value *value)
{
994
995
996
997
998
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

999
1000
void ConvertToNumber::visit(const NullValue *)
{