qmljsinterpreter.cpp 62 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
#include "qmljsevaluate.h"
32
#include "qmljslink.h"
33
#include "parser/qmljsast_p.h"
34
35
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
36
37
#include <QtCore/QDebug>

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

47
using namespace QmlJS::Interpreter;
48
using namespace QmlJS::AST;
49

Roberto Raggi's avatar
Roberto Raggi committed
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
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
104
105
#ifndef NO_DECLARATIVE_BACKEND

Roberto Raggi's avatar
Roberto Raggi committed
106
107
namespace {

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
150
151
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
152
153
} // end of anonymous namespace

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

165
QmlObjectValue::~QmlObjectValue() {}
Roberto Raggi's avatar
Roberto Raggi committed
166

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
180
181
182
183
184
    *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
185
    }
Roberto Raggi's avatar
Roberto Raggi committed
186
    return value;
187
}
Roberto Raggi's avatar
Roberto Raggi committed
188

189
190
void QmlObjectValue::processMembers(MemberProcessor *processor) const
{
Roberto Raggi's avatar
Roberto Raggi committed
191
    // process the meta enums
192
193
194
195
196
197
198
199
    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
200
201
202
    // process the meta properties
    for (int index = 0; index < _metaObject->propertyCount(); ++index) {
        QMetaProperty prop = _metaObject->property(index);
203

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

Roberto Raggi's avatar
Roberto Raggi committed
207
208
209
210
211
    // 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);
212

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

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

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

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

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

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

238
239
240
241
242
243
244
245
246
247
248
249
        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
250
251
252
253
254
255
256
            QString typeName = QString::fromUtf8(prop.typeName());

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

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

257
            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
258
259
260
261
                return objectValue;
        }
    }

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
    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:
300
301
    case QMetaType::QPointF:
    case QMetaType::QVector2D: {
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
        // ### 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;

320
321
322
323
324
325
326
327
328
    case QMetaType::QVector3D: {
        // ### cache
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setProperty("x", engine()->numberValue());
        object->setProperty("y", engine()->numberValue());
        object->setProperty("z", engine()->numberValue());
        value = object;
    } break;

329
330
331
332
    default:
        break;
    } // end of switch

333
334
335
336
337
338
339
340
341
342
343
    const QString typeName = prop.typeName();
    if (typeName == QLatin1String("QmlGraphicsAnchorLine")) {
        ObjectValue *object = engine()->newObject(/*prototype =*/ 0);
        object->setClassName(QLatin1String("AnchorLine"));
        value = object;
    }
    if (value->asStringValue() && prop.name() == QLatin1String("easing")
            && isDerivedFrom(&QmlPropertyAnimation::staticMetaObject)) {
        value = engine()->easingCurveNameValue();
    }

344
345
    return value;
}
346
347
348
349
350
351
352
353
354
355

bool QmlObjectValue::isDerivedFrom(const QMetaObject *base) const
{
    for (const QMetaObject *iter = _metaObject; iter; iter = iter->superClass()) {
        if (iter == base)
            return true;
    }
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
356
357
#endif

358
359
namespace {

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
360
361
362
363
364
365
366
367
////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////
class ObjectCtor: public Function
{
public:
    ObjectCtor(Engine *engine);

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

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

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

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

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

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

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

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

Roberto Raggi's avatar
Roberto Raggi committed
400
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
401
402
403
404
405
406
407
};

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

Roberto Raggi's avatar
Roberto Raggi committed
408
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
409
410
411
412
413
414
415
};

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

Roberto Raggi's avatar
Roberto Raggi committed
416
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
417
418
419
420
421
422
423
};

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

Roberto Raggi's avatar
Roberto Raggi committed
424
    virtual const Value *invoke(const Activation *activation) const;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
};

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
467
const Value *ObjectCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
468
{
Roberto Raggi's avatar
Roberto Raggi committed
469
470
471
472
473
474
475
476
    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
477
478
}

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

Roberto Raggi's avatar
Roberto Raggi committed
491
const Value *ArrayCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
492
{
Roberto Raggi's avatar
Roberto Raggi committed
493
494
495
    ObjectValue *thisObject = activation->thisObject();
    if (activation->calledAsFunction())
        thisObject = engine()->newObject();
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
496

Roberto Raggi's avatar
Roberto Raggi committed
497
498
499
500
    thisObject->setClassName("Array");
    thisObject->setPrototype(engine()->arrayPrototype());
    thisObject->setProperty("length", engine()->numberValue());
    return thisObject;
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
501
502
}

Roberto Raggi's avatar
Roberto Raggi committed
503
const Value *StringCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
504
{
Roberto Raggi's avatar
Roberto Raggi committed
505
506
    if (activation->calledAsFunction())
        return engine()->convertToString(activation->thisObject());
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
507

Roberto Raggi's avatar
Roberto Raggi committed
508
509
510
511
512
    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
513
514
}

Roberto Raggi's avatar
Roberto Raggi committed
515
const Value *BooleanCtor::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
    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
524
525
}

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

Roberto Raggi's avatar
Roberto Raggi committed
537
const Value *DateCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
538
{
Roberto Raggi's avatar
Roberto Raggi committed
539
540
541
542
543
544
545
    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
546
547
}

Roberto Raggi's avatar
Roberto Raggi committed
548
const Value *RegExpCtor::invoke(const Activation *activation) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
549
{
Roberto Raggi's avatar
Roberto Raggi committed
550
551
552
553
554
555
556
557
558
559
560
561
    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
562
563
564
565
}

} // end of anonymous namespace

566
567
568
////////////////////////////////////////////////////////////////////////////////
// ValueVisitor
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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 *)
{
}
604

Roberto Raggi's avatar
Roberto Raggi committed
605
606
607
void ValueVisitor::visit(const Reference *)
{
}
608

609
610
611
612
void ValueVisitor::visit(const EasingCurveNameValue *)
{
}

613
614
615
////////////////////////////////////////////////////////////////////////////////
// Value
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
616
617
618
619
620
621
622
623
Value::Value()
{
}

Value::~Value()
{
}

624
625
626
627
628
bool Value::getSourceLocation(QString *, int *, int *) const
{
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
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;
}
663

Roberto Raggi's avatar
Roberto Raggi committed
664
665
666
667
668
const Reference *Value::asReference() const
{
    return 0;
}

669
670
671
672
673
const EasingCurveNameValue *Value::asEasingCurveNameValue() const
{
    return 0;
}

674
675
676
////////////////////////////////////////////////////////////////////////////////
// Values
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
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);
}
696

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

728
729
730
731
732
733
ScopeChain::ScopeChain()
    : globalScope(0)
    , qmlTypes(0)
{
}

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
ScopeChain::QmlComponentChain::QmlComponentChain()
    : rootObject(0), ids(0)
{
}

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

void ScopeChain::QmlComponentChain::clear()
{
    qDeleteAll(instantiatingComponents);
    instantiatingComponents.clear();
    rootObject = 0;
    functionScopes.clear();
    ids = 0;
}

753
754
755
756
757
758
759
760
761
762
763
764
765
766
void ScopeChain::QmlComponentChain::add(QList<const ObjectValue *> *list) const
{
    foreach (QmlComponentChain *parent, instantiatingComponents)
        parent->add(list);

    if (rootObject)
        list->append(rootObject);
    list->append(functionScopes);
    if (ids)
        list->append(ids);
}

void ScopeChain::update()
{
767
    _all.clear();
768

769
    _all += globalScope;
770
771

    foreach (QmlComponentChain *parent, qmlComponentScope.instantiatingComponents)
772
        parent->add(&_all);
773
774

    if (qmlComponentScope.rootObject)
775
776
777
        _all += qmlComponentScope.rootObject;
    _all += qmlScopeObjects;
    _all += qmlComponentScope.functionScopes;
778
    if (qmlComponentScope.ids)
779
        _all += qmlComponentScope.ids;
780
    if (qmlTypes)
781
782
783
784
785
786
787
        _all += qmlTypes;
    _all += jsScopes;
}

QList<const ObjectValue *> ScopeChain::all() const
{
    return _all;
788
789
790
}


Roberto Raggi's avatar
Roberto Raggi committed
791
Context::Context(Engine *engine)
792
    : _engine(engine),
Christian Kamm's avatar
Christian Kamm committed
793
794
795
      _lookupMode(JSLookup),
      _qmlScopeObjectIndex(-1),
      _qmlScopeObjectSet(false)
Roberto Raggi's avatar
Roberto Raggi committed
796
797
798
799
800
801
802
{
}

Context::~Context()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
803
void Context::build(const QList<QmlJS::AST::Node *> &astPath, QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot)
804
805
{
    Link link(this, doc, snapshot);
806
    link.scopeChainAt(doc, astPath);
807
808
}

Roberto Raggi's avatar
Roberto Raggi committed
809
810
811
812
813
Engine *Context::engine() const
{
    return _engine;
}

814
815
816
817
818
819
const ScopeChain &Context::scopeChain() const
{
    return _scopeChain;
}

ScopeChain &Context::scopeChain()
820
821
822
823
824
825
826
827
828
829
830
831
832
833
{
    return _scopeChain;
}

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

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

Tobias Hunger's avatar
Tobias Hunger committed
834
const ObjectValue *Context::typeEnvironment(const QmlJS::Document *doc) const
835
{
836
    return _typeEnvironments.value(doc, 0);
837
838
}

Tobias Hunger's avatar
Tobias Hunger committed
839
void Context::setTypeEnvironment(const QmlJS::Document *doc, const ObjectValue *typeEnvironment)
840
{
841
    _typeEnvironments[doc] = typeEnvironment;
842
843
844
}

const Value *Context::lookup(const QString &name)
845
{
846
    QList<const ObjectValue *> scopes = _scopeChain.all();
847
848
    for (int index = scopes.size() - 1; index != -1; --index) {
        const ObjectValue *scope = scopes.at(index);
849

850
        if (const Value *member = scope->lookupMember(name, this)) {
851
852
853
854
855
856
857
858
859
            if (_lookupMode == JSLookup || ! dynamic_cast<const ASTVariableReference *>(member)) {
                return member;
            }
        }
    }

    return _engine->undefinedValue();
}

Tobias Hunger's avatar
Tobias Hunger committed
860
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId *qmlTypeName)
861
{
862
    const ObjectValue *objectValue = typeEnvironment(doc);
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877

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

889
890
Reference::Reference(Engine *engine)
    : _engine(engine)
Roberto Raggi's avatar
Roberto Raggi committed
891
{
892
    _engine->registerValue(this);
Roberto Raggi's avatar
Roberto Raggi committed
893
894
895
896
897
898
}

Reference::~Reference()
{
}

899
900
901
902
903
Engine *Reference::engine() const
{
    return _engine;
}

Roberto Raggi's avatar
Roberto Raggi committed
904
905
906
907
908
909
910
911
912
913
const Reference *Reference::asReference() const
{
    return this;
}

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

914
915
916
917
const Value *Reference::value(Context *) const
{
    return _engine->undefinedValue();
}
Roberto Raggi's avatar
Roberto Raggi committed
918

919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
void EasingCurveNameValue::accept(ValueVisitor *visitor) const
{
    visitor->visit(this);
}

QSet<QString> EasingCurveNameValue::_curveNames;
QSet<QString> EasingCurveNameValue::curveNames()
{
    if (_curveNames.isEmpty()) {
        _curveNames = QSet<QString>()
                      << "easeLinear"
                      << "easeInQuad"
                      << "easeOutQuad"
                      << "easeInOutQuad"
                      << "easeOutInQuad"
                      << "easeInCubic"
                      << "easeOutCubic"
                      << "easeInOutCubic"
                      << "easeOutInCubic"
                      << "easeInQuart"
                      << "easeOutQuart"
                      << "easeInOutQuart"
                      << "easeOutInQuart"
                      << "easeInQuint"
                      << "easeOutQuint"
                      << "easeInOutQuint"
                      << "easeOutInQuint"
                      << "easeInSine"
                      << "easeOutSine"
                      << "easeInOutSine"
                      << "easeOutInSine"
                      << "easeInExpo"
                      << "easeOutExpo"
                      << "easeInOutExpo"
                      << "easeOutInExpo"
                      << "easeInCirc"
                      << "easeOutCirc"
                      << "easeInOutCirc"
                      << "easeOutInCirc"
                      << "easeInElastic"
                      << "easeOutElastic"
                      << "easeInOutElastic"
                      << "easeOutInElastic"
                      << "easeInBack"
                      << "easeOutBack"
                      << "easeInOutBack"
                      << "easeOutInBack"
                      << "easeInBounce"
                      << "easeOutBounce"
                      << "easeInOutBounce"
                      << "easeOutInBounce";
    }

    return _curveNames;
}

const EasingCurveNameValue *EasingCurveNameValue::asEasingCurveNameValue() const
{
    return this;
}

980
981
982
983
984
985
986
987
988
989
990
991
992
MemberProcessor::MemberProcessor()
{
}

MemberProcessor::~MemberProcessor()
{
}

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

993
994
995
996
997
bool MemberProcessor::processEnumerator(const QString &, const Value *)
{
    return true;
}

998
999
1000
1001
1002
1003
1004
1005
1006
1007
bool MemberProcessor::processSignal(const QString &, const Value *)
{
    return true;
}

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

1008
1009
1010
1011
1012
bool MemberProcessor::processGeneratedSlot(const QString &, const Value *)
{
    return true;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1013
ObjectValue::ObjectValue(Engine *engine)
1014
    : _engine(engine),
1015
      _prototype(0)
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1016
{
1017
    engine->registerValue(this);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
}

ObjectValue::~ObjectValue()
{
}

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

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

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

1039
const ObjectValue *ObjectValue::prototype(Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1040
{
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
    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
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
}

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

1076
const Value *ObjectValue::property(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1077
{
1078
    return lookupMember(name, context);
1079
1080
}

1081
bool ObjectValue::checkPrototype(const ObjectValue *, QSet<const ObjectValue *> *) const
1082
{
1083
#if 0
1084
1085
1086
1087
1088
1089
1090
    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
1091
        if (prototype() && ! prototype()->checkPrototype(proto, processed))
1092
1093
1094
1095
            return false;

        return true;
    }
1096
#endif
1097
1098
1099
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1100
1101
1102
1103
1104
1105
1106
void ObjectValue::processMembers(MemberProcessor *processor) const
{
    QHashIterator<QString, const Value *> it(_members);

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

1107
        if (! processor->processProperty(it.key(), it.value()))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1108
1109
1110
1111
            break;
    }
}

1112
const Value *ObjectValue::lookupMember(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1113
{
Roberto Raggi's avatar
Roberto Raggi committed
1114
    if (const Value *m = _members.value(name))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1115
        return m;
Roberto Raggi's avatar
Roberto Raggi committed
1116
1117
1118
1119
1120
1121
    else {
        LookupMember slowLookup(name);
        processMembers(&slowLookup);
        if (slowLookup.value())
            return slowLookup.value();
    }
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1122

1123
1124
1125
    const ObjectValue *prototypeObject = prototype(context);
    if (prototypeObject) {
        if (const Value *m = prototypeObject->lookupMember(name, context))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1126
1127
1128
1129
1130
1131
            return m;
    }

    return 0;
}

1132
Activation::Activation(Context *parentContext)
1133
    : _thisObject(0),
1134
1135
      _calledAsFunction(true),
      _parentContext(parentContext)
Roberto Raggi's avatar
Roberto Raggi committed
1136
1137
1138
1139
1140
1141
1142
{
}

Activation::~Activation()
{
}

1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
Context *Activation::parentContext() const
{
    return _parentContext;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
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
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
FunctionValue::FunctionValue(Engine *engine)
    : ObjectValue(engine)
{
}

FunctionValue::~FunctionValue()
{
}

const Value *FunctionValue::construct(const ValueList &actuals) const
{
1205
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1206
1207
1208
1209
    activation.setCalledAsConstructor(true);
    activation.setThisObject(engine()->newObject());
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1210
1211
1212
1213
}

const Value *FunctionValue::call(const ValueList &actuals) const
{
1214
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1215
1216
1217
1218
    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
1219
1220
1221
1222
}

const Value *FunctionValue::call(const ObjectValue *thisObject, const ValueList &actuals) const
{
1223
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1224
1225
1226
1227
    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
1228
1229
}

1230
1231
1232
1233
1234
const Value *FunctionValue::returnValue() const
{
    return engine()->undefinedValue();
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1235
1236
1237
1238
1239
int FunctionValue::argumentCount() const
{
    return 0;
}

1240
1241
1242
1243
1244
1245
1246
const Value *FunctionValue::argument(int) const
{
    return engine()->undefinedValue();
}

QString FunctionValue::argumentName(int index) const
{
Roberto Raggi's avatar
Roberto Raggi committed
1247
    return QString::fromLatin1("arg%1").arg(index + 1);
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
}

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
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
const FunctionValue *FunctionValue::asFunctionValue() const
{
    return this;
}

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

Function::Function(Engine *engine)
    : FunctionValue(engine), _returnValue(0)
1272
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1273
    setClassName("Function");
1274
1275
}

Roberto Raggi's avatar
Roberto Raggi committed
1276
1277
1278
1279
Function::~Function()
{
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1280
void Function::addArgument(const Value *argument)
1281
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1282
    _arguments.push_back(argument);
1283
1284
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1285
const Value *Function::returnValue() const
1286
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1287
    return _returnValue;
1288
1289
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1290
1291
1292
1293
void Function::setReturnValue(const Value *returnValue)
{
    _returnValue = returnValue;
}
1294

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1295
1296
1297
1298
int Function::argumentCount() const
{
    return _arguments.size();
}
1299

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1300
1301
1302
1303
const Value *Function::argument(int index) const
{
    return _arguments.at(index);
}
1304

1305
const Value *Function::property(const QString &name, Context *context) const
1306
1307
1308
1309
{
    if (name == "length")
        return engine()->numberValue();

1310
    return FunctionValue::property(name, context);
1311
1312
}

Roberto Raggi's avatar
Roberto Raggi committed
1313
const Value *Function::invoke(const Activation *activation) const
1314
{
Roberto Raggi's avatar
Roberto Raggi committed
1315
    return activation->thisObject(); // ### FIXME it should return undefined
1316
1317
1318
1319
1320
}

////////////////////////////////////////////////////////////////////////////////
// typing environment
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1321
1322
1323
1324
ConvertToNumber::ConvertToNumber(Engine *engine)
    : _engine(engine), _result(0)
{
}
1325

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1326
1327
const Value *ConvertToNumber::operator()(const Value *value)
{
1328
1329
1330
1331
1332
1333
1334
1335
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1336
1337
const Value *ConvertToNumber::switchResult(const Value *value)
{
1338
1339
1340
1341
1342
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
void ConvertToNumber::visit(const NullValue *)
{
    _result = _engine->numberValue();
}

void ConvertToNumber::visit(const UndefinedValue *)
{
    _result = _engine->numberValue();
}

void ConvertToNumber::visit(const NumberValue *value)
{
    _result = value;
}

void ConvertToNumber::visit(const BooleanValue *)
{
    _result = _engine->numberValue();
}

void ConvertToNumber::visit(const StringValue *)
{
    _result = _engine->numberValue();
}

void ConvertToNumber::visit(const ObjectValue *object)
{
1370
    if (const FunctionValue *valueOfMember = value_cast<const FunctionValue *>(object->lookupMember("valueOf", 0))) {
1371
1372
1373
1374
1375
1376
        _result = value_cast<const NumberValue *>(valueOfMember->call(object)); // ### invoke convert-to-number?
    }
}

void ConvertToNumber::visit(const FunctionValue *object)
{
1377
    if (const FunctionValue *valueOfMember = value_cast<const FunctionValue *>(object->lookupMember("valueOf", 0))) {
1378
1379
1380
1381
        _result = value_cast<const NumberValue *>(valueOfMember->call(object)); // ### invoke convert-to-number?
    }
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1382
1383
1384
1385
ConvertToString::ConvertToString(Engine *engine)
    : _engine(engine), _result(0)
{
}
1386

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1387
1388
const Value *ConvertToString::operator()(const Value *value)
{
1389
1390
1391
1392
1393
1394
1395
1396
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1397
1398
const Value *ConvertToString::switchResult(const Value *value)
{
1399
1400
1401
1402
1403
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
void ConvertToString::visit(const NullValue *)
{
    _result = _engine->stringValue();
}

void ConvertToString::visit(const UndefinedValue *)
{
    _result = _engine->stringValue();
}

void ConvertToString::visit(const NumberValue *)
{
    _result = _engine->stringValue();
}

void ConvertToString::visit(const BooleanValue *)
{
    _result = _engine->stringValue();
}

void ConvertToString::visit(const StringValue *value)
{
    _result = value;
}

void ConvertToString::visit(const ObjectValue *object)
{
1431
    if (const FunctionValue *toStringMember = value_cast<const FunctionValue *>(object->lookupMember("toString", 0))) {
1432
1433
1434
1435
1436
1437
        _result = value_cast<const StringValue *>(toStringMember->call(object)); // ### invoke convert-to-string?
    }
}

void ConvertToString::visit(const FunctionValue *object)
{
1438
    if (const FunctionValue *toStringMember = value_cast<const FunctionValue *>(object->lookupMember("toString", 0))) {
1439
1440
1441
1442
        _result = value_cast<const StringValue *>(toStringMember->call(object)); // ### invoke convert-to-string?
    }
}

1443
1444
1445
1446
ConvertToObject::ConvertToObject(Engine *engine)
    : _engine(engine), _result(0)
{
}
1447

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1448
1449
const Value *ConvertToObject::operator()(const Value *value)
{
1450
1451
1452
1453
1454
1455
1456
1457
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1458
1459
const Value *ConvertToObject::switchResult(const Value *value)
{
1460
1461
1462
1463
1464
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
void ConvertToObject::visit(const NullValue *value)
{
    _result = value;
}

void ConvertToObject::visit(const UndefinedValue *)
{
    _result = _engine->nullValue();
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1475
void ConvertToObject::visit(const NumberValue *value)
1476
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1477
1478
1479
    ValueList actuals;
    actuals.append(value);
    _result = _engine->numberCtor()->construct(actuals);
1480
1481
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1482
void ConvertToObject::visit(const BooleanValue *value)
1483
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1484
1485
1486
    ValueList actuals;
    actuals.append(value);
    _result = _engine->booleanCtor()->construct(actuals);
1487
1488
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1489
void ConvertToObject::visit(const StringValue *value)
1490
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1491
1492
1493
    ValueList actuals;
    actuals.append(value);
    _result = _engine->stringCtor()->construct(actuals);
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
}

void ConvertToObject::visit(const ObjectValue *object)
{
    _result = object;
}

void ConvertToObject::visit(const FunctionValue *object)
{
    _result = object;
}
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515

QString TypeId::operator()(const Value *value)
{
    _result = QLatin1String("unknown");

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

    return _result;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1516
void TypeId::visit(const NullValue *)