qmljsinterpreter.cpp 61.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
#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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
ScopeChain::ScopeChain()
    : globalScope(0)
    , qmlTypes(0)
{
}

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()
{
748
    _all.clear();
749

750
    _all += globalScope;
751
752

    foreach (QmlComponentChain *parent, qmlComponentScope.instantiatingComponents)
753
        parent->add(&_all);
754
755

    if (qmlComponentScope.rootObject)
756
757
758
        _all += qmlComponentScope.rootObject;
    _all += qmlScopeObjects;
    _all += qmlComponentScope.functionScopes;
759
    if (qmlComponentScope.ids)
760
        _all += qmlComponentScope.ids;
761
    if (qmlTypes)
762
763
764
765
766
767
768
        _all += qmlTypes;
    _all += jsScopes;
}

QList<const ObjectValue *> ScopeChain::all() const
{
    return _all;
769
770
771
}


Roberto Raggi's avatar
Roberto Raggi committed
772
Context::Context(Engine *engine)
773
    : _engine(engine),
Christian Kamm's avatar
Christian Kamm committed
774
775
776
      _lookupMode(JSLookup),
      _qmlScopeObjectIndex(-1),
      _qmlScopeObjectSet(false)
Roberto Raggi's avatar
Roberto Raggi committed
777
778
779
780
781
782
783
{
}

Context::~Context()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
784
void Context::build(const QList<QmlJS::AST::Node *> &astPath, QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot)
785
786
{
    Link link(this, doc, snapshot);
787
    link.scopeChainAt(doc, astPath);
788
789
}

Roberto Raggi's avatar
Roberto Raggi committed
790
791
792
793
794
Engine *Context::engine() const
{
    return _engine;
}

795
796
797
798
799
800
const ScopeChain &Context::scopeChain() const
{
    return _scopeChain;
}

ScopeChain &Context::scopeChain()
801
802
803
804
805
806
807
808
809
810
811
812
813
814
{
    return _scopeChain;
}

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

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

Tobias Hunger's avatar
Tobias Hunger committed
815
const ObjectValue *Context::typeEnvironment(const QmlJS::Document *doc) const
816
{
817
    return _typeEnvironments.value(doc, 0);
818
819
}

Tobias Hunger's avatar
Tobias Hunger committed
820
void Context::setTypeEnvironment(const QmlJS::Document *doc, const ObjectValue *typeEnvironment)
821
{
822
    _typeEnvironments[doc] = typeEnvironment;
823
824
825
}

const Value *Context::lookup(const QString &name)
826
{
827
    QList<const ObjectValue *> scopes = _scopeChain.all();
828
829
    for (int index = scopes.size() - 1; index != -1; --index) {
        const ObjectValue *scope = scopes.at(index);
830

831
        if (const Value *member = scope->lookupMember(name, this)) {
832
833
834
835
836
837
838
839
840
            if (_lookupMode == JSLookup || ! dynamic_cast<const ASTVariableReference *>(member)) {
                return member;
            }
        }
    }

    return _engine->undefinedValue();
}

Tobias Hunger's avatar
Tobias Hunger committed
841
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId *qmlTypeName)
842
{
843
    const ObjectValue *objectValue = typeEnvironment(doc);
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

    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
859
860
861
862
863
864
865
866
867
868
869
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);
}

870
871
Reference::Reference(Engine *engine)
    : _engine(engine)
Roberto Raggi's avatar
Roberto Raggi committed
872
{
873
    _engine->registerValue(this);
Roberto Raggi's avatar
Roberto Raggi committed
874
875
876
877
878
879
}

Reference::~Reference()
{
}

880
881
882
883
884
Engine *Reference::engine() const
{
    return _engine;
}

Roberto Raggi's avatar
Roberto Raggi committed
885
886
887
888
889
890
891
892
893
894
const Reference *Reference::asReference() const
{
    return this;
}

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

895
896
897
898
const Value *Reference::value(Context *) const
{
    return _engine->undefinedValue();
}
Roberto Raggi's avatar
Roberto Raggi committed
899

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
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
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;
}

961
962
963
964
965
966
967
968
969
970
971
972
973
MemberProcessor::MemberProcessor()
{
}

MemberProcessor::~MemberProcessor()
{
}

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

974
975
976
977
978
bool MemberProcessor::processEnumerator(const QString &, const Value *)
{
    return true;
}

979
980
981
982
983
984
985
986
987
988
bool MemberProcessor::processSignal(const QString &, const Value *)
{
    return true;
}

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

989
990
991
992
993
bool MemberProcessor::processGeneratedSlot(const QString &, const Value *)
{
    return true;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
994
ObjectValue::ObjectValue(Engine *engine)
995
    : _engine(engine),
996
      _prototype(0)
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
997
{
998
    engine->registerValue(this);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
}

ObjectValue::~ObjectValue()
{
}

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

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

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

1020
const ObjectValue *ObjectValue::prototype(Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1021
{
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    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
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
}

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

1057
const Value *ObjectValue::property(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1058
{
1059
    return lookupMember(name, context);
1060
1061
}

1062
bool ObjectValue::checkPrototype(const ObjectValue *, QSet<const ObjectValue *> *) const
1063
{
1064
#if 0
1065
1066
1067
1068
1069
1070
1071
    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
1072
        if (prototype() && ! prototype()->checkPrototype(proto, processed))
1073
1074
1075
1076
            return false;

        return true;
    }
1077
#endif
1078
1079
1080
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1081
1082
1083
1084
1085
1086
1087
void ObjectValue::processMembers(MemberProcessor *processor) const
{
    QHashIterator<QString, const Value *> it(_members);

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

1088
        if (! processor->processProperty(it.key(), it.value()))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1089
1090
1091
1092
            break;
    }
}

1093
const Value *ObjectValue::lookupMember(const QString &name, Context *context) const
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1094
{
Roberto Raggi's avatar
Roberto Raggi committed
1095
    if (const Value *m = _members.value(name))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1096
        return m;
Roberto Raggi's avatar
Roberto Raggi committed
1097
1098
1099
1100
1101
1102
    else {
        LookupMember slowLookup(name);
        processMembers(&slowLookup);
        if (slowLookup.value())
            return slowLookup.value();
    }
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1103

1104
1105
1106
    const ObjectValue *prototypeObject = prototype(context);
    if (prototypeObject) {
        if (const Value *m = prototypeObject->lookupMember(name, context))
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1107
1108
1109
1110
1111
1112
            return m;
    }

    return 0;
}

1113
Activation::Activation(Context *parentContext)
1114
    : _thisObject(0),
1115
1116
      _calledAsFunction(true),
      _parentContext(parentContext)
Roberto Raggi's avatar
Roberto Raggi committed
1117
1118
1119
1120
1121
1122
1123
{
}

Activation::~Activation()
{
}

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
Context *Activation::parentContext() const
{
    return _parentContext;
}

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

Roberto Raggi's avatar
Roberto Raggi committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
FunctionValue::FunctionValue(Engine *engine)
    : ObjectValue(engine)
{
}

FunctionValue::~FunctionValue()
{
}

const Value *FunctionValue::construct(const ValueList &actuals) const
{
1186
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1187
1188
1189
1190
    activation.setCalledAsConstructor(true);
    activation.setThisObject(engine()->newObject());
    activation.setArguments(actuals);
    return invoke(&activation);
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1191
1192
1193
1194
}

const Value *FunctionValue::call(const ValueList &actuals) const
{
1195
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1196
1197
1198
1199
    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
1200
1201
1202
1203
}

const Value *FunctionValue::call(const ObjectValue *thisObject, const ValueList &actuals) const
{
1204
    Activation activation;
Roberto Raggi's avatar
Roberto Raggi committed
1205
1206
1207
1208
    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
1209
1210
}

1211
1212
1213
1214
1215
const Value *FunctionValue::returnValue() const
{
    return engine()->undefinedValue();
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1216
1217
1218
1219
1220
int FunctionValue::argumentCount() const
{
    return 0;
}

1221
1222
1223
1224
1225
1226
1227
const Value *FunctionValue::argument(int) const
{
    return engine()->undefinedValue();
}

QString FunctionValue::argumentName(int index) const
{
Roberto Raggi's avatar
Roberto Raggi committed
1228
    return QString::fromLatin1("arg%1").arg(index + 1);
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
}

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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
const FunctionValue *FunctionValue::asFunctionValue() const
{
    return this;
}

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

Function::Function(Engine *engine)
    : FunctionValue(engine), _returnValue(0)
1253
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1254
    setClassName("Function");
1255
1256
}

Roberto Raggi's avatar
Roberto Raggi committed
1257
1258
1259
1260
Function::~Function()
{
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1261
void Function::addArgument(const Value *argument)
1262
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1263
    _arguments.push_back(argument);
1264
1265
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1266
const Value *Function::returnValue() const
1267
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1268
    return _returnValue;
1269
1270
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1271
1272
1273
1274
void Function::setReturnValue(const Value *returnValue)
{
    _returnValue = returnValue;
}
1275

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1276
1277
1278
1279
int Function::argumentCount() const
{
    return _arguments.size();
}
1280

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1281
1282
1283
1284
const Value *Function::argument(int index) const
{
    return _arguments.at(index);
}
1285

1286
const Value *Function::property(const QString &name, Context *context) const
1287
1288
1289
1290
{
    if (name == "length")
        return engine()->numberValue();

1291
    return FunctionValue::property(name, context);
1292
1293
}

Roberto Raggi's avatar
Roberto Raggi committed
1294
const Value *Function::invoke(const Activation *activation) const
1295
{
Roberto Raggi's avatar
Roberto Raggi committed
1296
    return activation->thisObject(); // ### FIXME it should return undefined
1297
1298
1299
1300
1301
}

////////////////////////////////////////////////////////////////////////////////
// typing environment
////////////////////////////////////////////////////////////////////////////////
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1302
1303
1304
1305
ConvertToNumber::ConvertToNumber(Engine *engine)
    : _engine(engine), _result(0)
{
}
1306

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1307
1308
const Value *ConvertToNumber::operator()(const Value *value)
{
1309
1310
1311
1312
1313
1314
1315
1316
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1317
1318
const Value *ConvertToNumber::switchResult(const Value *value)
{
1319
1320
1321
1322
1323
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
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)
{
1351
    if (const FunctionValue *valueOfMember = value_cast<const FunctionValue *>(object->lookupMember("valueOf", 0))) {
1352
1353
1354
1355
1356
1357
        _result = value_cast<const NumberValue *>(valueOfMember->call(object)); // ### invoke convert-to-number?
    }
}

void ConvertToNumber::visit(const FunctionValue *object)
{
1358
    if (const FunctionValue *valueOfMember = value_cast<const FunctionValue *>(object->lookupMember("valueOf", 0))) {
1359
1360
1361
1362
        _result = value_cast<const NumberValue *>(valueOfMember->call(object)); // ### invoke convert-to-number?
    }
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1363
1364
1365
1366
ConvertToString::ConvertToString(Engine *engine)
    : _engine(engine), _result(0)
{
}
1367

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1368
1369
const Value *ConvertToString::operator()(const Value *value)
{
1370
1371
1372
1373
1374
1375
1376
1377
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1378
1379
const Value *ConvertToString::switchResult(const Value *value)
{
1380
1381
1382
1383
1384
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
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)
{
1412
    if (const FunctionValue *toStringMember = value_cast<const FunctionValue *>(object->lookupMember("toString", 0))) {
1413
1414
1415
1416
1417
1418
        _result = value_cast<const StringValue *>(toStringMember->call(object)); // ### invoke convert-to-string?
    }
}

void ConvertToString::visit(const FunctionValue *object)
{
1419
    if (const FunctionValue *toStringMember = value_cast<const FunctionValue *>(object->lookupMember("toString", 0))) {
1420
1421
1422
1423
        _result = value_cast<const StringValue *>(toStringMember->call(object)); // ### invoke convert-to-string?
    }
}

1424
1425
1426
1427
ConvertToObject::ConvertToObject(Engine *engine)
    : _engine(engine), _result(0)
{
}
1428

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1429
1430
const Value *ConvertToObject::operator()(const Value *value)
{
1431
1432
1433
1434
1435
1436
1437
1438
    const Value *previousValue = switchResult(0);

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

    return switchResult(previousValue);
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1439
1440
const Value *ConvertToObject::switchResult(const Value *value)
{
1441
1442
1443
1444
1445
    const Value *previousResult = _result;
    _result = value;
    return previousResult;
}

1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
void ConvertToObject::visit(const NullValue *value)
{
    _result = value;
}

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

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1456
void ConvertToObject::visit(const NumberValue *value)
1457
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1458
1459
1460
    ValueList actuals;
    actuals.append(value);
    _result = _engine->numberCtor()->construct(actuals);
1461
1462
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1463
void ConvertToObject::visit(const BooleanValue *value)
1464
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1465
1466
1467
    ValueList actuals;
    actuals.append(value);
    _result = _engine->booleanCtor()->construct(actuals);
1468
1469
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1470
void ConvertToObject::visit(const StringValue *value)
1471
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1472
1473
1474
    ValueList actuals;
    actuals.append(value);
    _result = _engine->stringCtor()->construct(actuals);
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
}

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

void ConvertToObject::visit(const FunctionValue *object)
{
    _result = object;
}
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496

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

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

    return _result;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1497
void TypeId::visit(const NullValue *)
1498
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1499
1500
    _result = QLatin1String("null");
}
1501

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
1502
void TypeId::visit(const UndefinedValue *)
1503
{
Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed