summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qmlbasicscript.cpp
blob: 40ffffe313d522757585ebc5969338d751ae4816 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmlbasicscript_p.h"
#include <QColor>
#include <QDebug>
#include <private/qmlengine_p.h>
#include <private/qmlcontext_p.h>
#include <QStack>
#include <private/qfxperf_p.h>
#include <private/qmlrefcount_p.h>
#include <private/qmljsast_p.h>
#include <private/qmljsengine_p.h>

QT_BEGIN_NAMESPACE

using namespace QmlJS;

struct ScriptInstruction {
    enum {
        Load,       // fetch
        Fetch,       // fetch

        LoadIdObject,    // fetch
        FetchConstant,   // constant
        FetchD0Constant, // constant
        FetchD1Constant, // constant

        Equals,      // NA

        Int,         // integer
        Bool,        // boolean
    } type;

    union {
        struct {
            int idx;
        } fetch;
        struct {
            int value;
        } integer;
        struct {
            bool value;
        } boolean;
        struct {
            short idx;
            short notify;
            int type;
        } constant;
    };
};

DEFINE_BOOL_CONFIG_OPTION(scriptWarnings, QML_SCRIPT_WARNINGS);

class QmlBasicScriptPrivate
{
public:
    enum Flags { OwnData = 0x00000001 };
    
    int size;
    int stateSize;
    int instructionCount;
    int exprLen;

    ScriptInstruction *instructions() const { return (ScriptInstruction *)((char *)this + sizeof(QmlBasicScriptPrivate)); }

    const char *expr() const
    {
        return (const char *)(instructions() + instructionCount);
    }

    const char *data() const 
    {
        return (const char *)(instructions() + instructionCount) + exprLen + 1;
    }

    static unsigned int alignRound(int s)
    {
        if (s % 4) 
            s += 4 - (s % 4);
        return s;
    }
};

QDebug operator<<(QDebug lhs, const QmlBasicScriptNodeCache &rhs)
{
    switch(rhs.type) {
    case QmlBasicScriptNodeCache::Invalid:
        lhs << "Invalid";
        break;
    case QmlBasicScriptNodeCache::Core:
        lhs << "Core" << rhs.object << rhs.core;
        break;
    case QmlBasicScriptNodeCache::Attached: 
        lhs << "Attached" << rhs.object << rhs.attached;
        break;
    case QmlBasicScriptNodeCache::SignalProperty: 
        lhs << "SignalProperty" << rhs.object << rhs.core;
        break;
    case QmlBasicScriptNodeCache::Variant: 
        lhs << "Variant" << rhs.context;
        break;
    }

    return lhs;
}

void QmlBasicScriptNodeCache::clear()
{
    object = 0;
    metaObject = 0;
    type = Invalid;
}

static QVariant toObjectOrVariant(const QVariant &v)
{
    switch(v.userType()) {
        case QVariant::String:
        case QVariant::UInt:
        case QVariant::Int:
        case QMetaType::Float:
        case QVariant::Double:
        case QVariant::Color:
        case QVariant::Bool:
        default:
        {
            if (v.type() == QVariant::UserType) {
                QObject *o = QmlMetaType::toQObject(v);
                if (o)
                    return qVariantFromValue(o);
                else
                    return v;
            }
            return v;
        }
    }
}

static QVariant fetch_value(QObject *o, int idx, int type)
{
    if (!o)
        return QVariant();

    switch(type) {
        case QVariant::String:
            {
                QString val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QVariant::UInt:
            {
                uint val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QVariant::Int:
            {
                int val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QMetaType::Float:
            {
                float val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QVariant::Double:
            {
                double val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QVariant::Color:
            {
                QColor val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        case QVariant::Bool:
            {
                bool val;
                void *args[] = { &val, 0 };
                QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                return QVariant(val);
            }
            break;
        default:  
            {
                if (QmlMetaType::isObject(type)) {
                    // NOTE: This assumes a cast to QObject does not alter the 
                    // object pointer
                    QObject *val = 0;
                    void *args[] = { &val, 0 };
                    QMetaObject::metacall(o, QMetaObject::ReadProperty, idx, args);
                    return QVariant::fromValue(val);
                } else {
                    QVariant var = o->metaObject()->property(idx).read(o);
                    if (QmlMetaType::isObject(var.userType())) {
                        QObject *obj = 0;
                        obj = *(QObject **)var.data();
                        var = QVariant::fromValue(obj);
                    }
                    return var;
                }
            }
            break;
    };
}

QVariant QmlBasicScriptNodeCache::value(const char *) const
{
    //QFxPerfTimer<QFxPerf::BasicScriptValue> pt;
    switch(type) {
    case Invalid:
        break;
    case Core:
        return fetch_value(object, core, coreType);
        break;
    case Attached:
        return qVariantFromValue(static_cast<QObject *>(attached));
        break;
    case SignalProperty:
        break;
    case Variant:
        return context->propertyValues[contextIndex];
    };
    return QVariant();
}

struct QmlBasicScriptCompiler
{
    QmlBasicScriptCompiler()
    : script(0), stateSize(0) {}

    QmlBasicScript *script;
    int stateSize;

    QmlParser::Object *context;
    QmlParser::Object *component;
    QHash<QString, QmlParser::Object *> ids;

    bool compile(QmlJS::AST::Node *);

    bool compileExpression(QmlJS::AST::Node *);

    bool tryConstant(QmlJS::AST::Node *);
    bool parseConstant(QmlJS::AST::Node *);
    bool tryName(QmlJS::AST::Node *);
    bool parseName(QmlJS::AST::Node *, QmlParser::Object ** = 0);
    bool tryBinaryExpression(QmlJS::AST::Node *);
    bool compileBinaryExpression(QmlJS::AST::Node *);

    QByteArray data;
    QList<ScriptInstruction> bytecode;
};

/*!
    \internal
    \class QmlBasicScript
    \brief The QmlBasicScript class provides a fast implementation of a limited subset of QmlJS bindings.

    QmlBasicScript instances are used to accelerate binding.  Instead of using
    the slower, fully fledged QmlJS engine, many simple bindings can be
    evaluated using the QmlBasicScript engine.

    To see if the QmlBasicScript engine can handle a binding, call compile()
    and check the return value, or isValid() afterwards.  

    To evaluate the binding, the QmlBasicScript instance needs some memory in
    which to cache state.  This may be allocated by calling newScriptState()
    and destroyed by calling deleteScriptState().  The state data is then passed
    to the run() method when evaluating the binding.

    To further accelerate binding, QmlBasicScript can return a precompiled 
    version of itself that can be saved for future use.  Call compileData() to
    get an opaque pointer to the compiled state, and compileDataSize() for the
    size of this data in bytes.  This data can be saved and passed to future
    instances of the QmlBasicScript constructor.  The initial copy of compile
    data is owned by the QmlBindScript instance on which compile() was called.
*/

/*!
    Create a new QmlBasicScript instance.
*/
QmlBasicScript::QmlBasicScript()
: flags(0), d(0), rc(0)
{
}

/*!
    Create a new QmlBasicScript instance from saved \a data.

    \a data \b must be data previously acquired from calling compileData() on a
    previously created QmlBasicScript instance.  Any other data will almost
    certainly cause the QmlBasicScript engine to crash.

    \a data must continue to be valid throughout the QmlBasicScript instance 
    life.  It does not assume ownership of the memory.

    If \a owner is set, it is referenced on creation and dereferenced on 
    destruction of this instance.
*/
QmlBasicScript::QmlBasicScript(const char *data, QmlRefCount *owner)
: flags(0), d((QmlBasicScriptPrivate *)data), rc(owner)
{
    if (rc) rc->addref();
}

/*!
    Return the text of the script expression.
 */
QByteArray QmlBasicScript::expression() const
{
    if (!d)
        return QByteArray();
    else
        return QByteArray(d->expr());
}

/*!
    Destroy the script instance.
*/
QmlBasicScript::~QmlBasicScript()
{
    if (flags & QmlBasicScriptPrivate::OwnData)
        free(d);
    if (rc) rc->release();
    d = 0;
    rc = 0;
}

/*!
    Clear this script.  The object will then be in its initial state, as though
    it were freshly constructed with default constructor.
*/
void QmlBasicScript::clear()
{
    if (flags & QmlBasicScriptPrivate::OwnData)
        free(d);
    if (rc) rc->release();
    d = 0;
    rc = 0;
    flags = 0;
}

/*!
    Return the script state memory for this script instance.  This memory should
    only be destroyed by calling deleteScriptState().
 */
void *QmlBasicScript::newScriptState()
{
    if (!d) {
        return 0;
    } else  {
        void *rv = ::malloc(d->stateSize * sizeof(QmlBasicScriptNodeCache));
        ::memset(rv, 0, d->stateSize * sizeof(QmlBasicScriptNodeCache));
        return rv;
    }
}

/*!
    Delete the \a data previously allocated by newScriptState().
 */
void QmlBasicScript::deleteScriptState(void *data)
{
    if (!data) return;
    Q_ASSERT(d);
    clearCache(data);
    free(data);
}

/*!
    Dump the script instructions to stderr for debugging.
 */
void QmlBasicScript::dump()
{
    if (!d)
        return;

    qWarning() << d->instructionCount << "instructions:";
    const char *data = d->data();
    for (int ii = 0; ii < d->instructionCount; ++ii) {
        const ScriptInstruction &instr = d->instructions()[ii];

        switch(instr.type) {
        case ScriptInstruction::Load:
            qWarning().nospace() << "LOAD\t\t" << instr.fetch.idx << "\t\t" 
                                 << QByteArray(data + instr.fetch.idx);
            break;
        case ScriptInstruction::Fetch:
            qWarning().nospace() << "FETCH\t\t" << instr.fetch.idx << "\t\t" 
                                 << QByteArray(data + instr.fetch.idx);
            break;
        case ScriptInstruction::Equals:
            qWarning().nospace() << "EQUALS";
            break;
        case ScriptInstruction::Int:
            qWarning().nospace() << "INT\t\t" << instr.integer.value;
            break;
        case ScriptInstruction::Bool:
            qWarning().nospace() << "BOOL\t\t" << instr.boolean.value;
            break;
        default:
            qWarning().nospace() << "UNKNOWN";
            break;
        }
    }
}

/*!
    Return true if this is a valid script binding, otherwise returns false.
 */
bool QmlBasicScript::isValid() const
{
    return d != 0;
}

bool QmlBasicScript::compile(const Expression &expression)
{
    if (!expression.expression.asAST()) return false;

    QByteArray expr = expression.expression.asScript().toLatin1();
    const char *src = expr.constData();

    QmlBasicScriptCompiler bsc;
    bsc.script = this;
    bsc.context = expression.context;
    bsc.component = expression.component;
    bsc.ids = expression.ids;

    if (d) {
        if (flags & QmlBasicScriptPrivate::OwnData)
            free(d);
        d = 0;
        flags = 0;
    }

    if (bsc.compile(expression.expression.asAST())) {
        int len = ::strlen(src);
        flags = QmlBasicScriptPrivate::OwnData;
        int size = sizeof(QmlBasicScriptPrivate) + 
                   bsc.bytecode.count() * sizeof(ScriptInstruction) + 
                   QmlBasicScriptPrivate::alignRound(bsc.data.count() + len + 1);
        d = (QmlBasicScriptPrivate *) malloc(size);
        d->size = size;
        d->stateSize = bsc.stateSize;
        d->instructionCount = bsc.bytecode.count();
        d->exprLen = len;
        ::memcpy((char *)d->expr(), src, len + 1);
        for (int ii = 0; ii < d->instructionCount; ++ii) 
            d->instructions()[ii] = bsc.bytecode.at(ii);
        ::memcpy((char *)d->data(), bsc.data.constData(), bsc.data.count());
    } 

    return d != 0;
}

bool QmlBasicScriptCompiler::compile(QmlJS::AST::Node *node)
{
    return compileExpression(node);
}

bool QmlBasicScriptCompiler::tryConstant(QmlJS::AST::Node *node)
{
    if (node->kind == AST::Node::Kind_TrueLiteral || 
        node->kind == AST::Node::Kind_FalseLiteral)
        return true;

    if (node->kind == AST::Node::Kind_NumericLiteral) {
        AST::NumericLiteral *lit = static_cast<AST::NumericLiteral *>(node);

        return lit->suffix == AST::NumericLiteral::noSuffix &&
               double(int(lit->value)) == lit->value;
    }

    return false;
}

bool QmlBasicScriptCompiler::parseConstant(QmlJS::AST::Node *node)
{
    ScriptInstruction instr;

    if (node->kind == AST::Node::Kind_NumericLiteral) {
        AST::NumericLiteral *lit = static_cast<AST::NumericLiteral *>(node);
        instr.type = ScriptInstruction::Int;
        instr.integer.value = int(lit->value);
    } else {
        instr.type = ScriptInstruction::Bool;
        instr.boolean.value = node->kind == AST::Node::Kind_TrueLiteral;
    }

    bytecode.append(instr);

    return true;
}

bool QmlBasicScriptCompiler::tryName(QmlJS::AST::Node *node)
{
    return node->kind == AST::Node::Kind_IdentifierExpression ||
           node->kind == AST::Node::Kind_FieldMemberExpression;
}

bool QmlBasicScriptCompiler::parseName(AST::Node *node, 
                                       QmlParser::Object **type)
{
    bool load = false;
    QmlParser::Object *loadedType = 0;
    QString name;
    if (node->kind == AST::Node::Kind_IdentifierExpression) {
        name = static_cast<AST::IdentifierExpression *>(node)->name->asString();
        load = true;
    } else if (node->kind == AST::Node::Kind_FieldMemberExpression) {
        AST::FieldMemberExpression *expr = static_cast<AST::FieldMemberExpression *>(node);

        if (!parseName(expr->base, &loadedType))
            return false;

        name = expr->name->asString();
    } else {
        return false;
    }

    ScriptInstruction instr;
    if (load) {

        if (ids.contains(name)) {
            instr.type = ScriptInstruction::LoadIdObject;
            instr.fetch.idx = ids.value(name)->idIndex;

            if (type)
                *type = ids.value(name);

        } else {
            int d0Idx = context->metaObject()->indexOfProperty(name.toUtf8().constData());
            int d1Idx = -1;
            if (d0Idx == -1)
                d1Idx = component->metaObject()->indexOfProperty(name.toUtf8().constData());
            if (d0Idx != -1) {

                instr.type = ScriptInstruction::FetchD0Constant;
                instr.constant.idx = d0Idx;
                QMetaProperty prop = context->metaObject()->property(d0Idx);
                if (prop.isConstant())
                    instr.constant.notify = 0;
                else
                    instr.constant.notify = prop.notifySignalIndex(); 
                instr.constant.type = prop.userType();

            } else if (d1Idx != -1) {

                instr.type = ScriptInstruction::FetchD1Constant;
                instr.constant.idx = d1Idx;
                QMetaProperty prop = component->metaObject()->property(d1Idx);
                if (prop.isConstant())
                    instr.constant.notify = 0;
                else
                    instr.constant.notify = prop.notifySignalIndex(); 
                instr.constant.type = prop.userType();

            }  else {

                int nref = data.count();
                data.append(name.toUtf8());
                data.append('\0');
                instr.type = ScriptInstruction::Load;
                instr.fetch.idx = nref;
                ++stateSize;

            }
        }

    } else {

        int idx = -1;
        if (loadedType)
            idx = loadedType->metaObject()->indexOfProperty(name.toUtf8().constData());
        if (idx != -1) {
            instr.type = ScriptInstruction::FetchConstant;
            instr.constant.idx = idx;
            QMetaProperty prop = loadedType->metaObject()->property(idx);
            if (prop.isConstant())
                instr.constant.notify = 0;
            else
                instr.constant.notify = prop.notifySignalIndex(); 
            instr.constant.type = prop.userType();
        } else {
            int nref = data.count();
            data.append(name.toUtf8());
            data.append('\0');
            instr.type = ScriptInstruction::Fetch;
            instr.fetch.idx = nref;
            ++stateSize;
        }

    }

    bytecode.append(instr);
    return true;
}

bool QmlBasicScriptCompiler::compileExpression(QmlJS::AST::Node *node)
{
    if (tryBinaryExpression(node))
        return compileBinaryExpression(node);
    else if (tryConstant(node))
        return parseConstant(node);
    else if (tryName(node))
        return parseName(node);
    else
        return false;
}

bool QmlBasicScriptCompiler::tryBinaryExpression(AST::Node *node)
{
    if (node->kind == AST::Node::Kind_BinaryExpression) {
        AST::BinaryExpression *expr = 
            static_cast<AST::BinaryExpression *>(node);

        if (expr->op == QSOperator::Equal)
            return true;
    }
    return false;
}

bool QmlBasicScriptCompiler::compileBinaryExpression(AST::Node *node)
{
    if (node->kind == AST::Node::Kind_BinaryExpression) {
        AST::BinaryExpression *expr = 
            static_cast<AST::BinaryExpression *>(node);

        if (!compileExpression(expr->left)) return false;
        if (!compileExpression(expr->right)) return false;

        ScriptInstruction instr;
        switch (expr->op) {
        case QSOperator::Equal:
            instr.type = ScriptInstruction::Equals;
            break;
        default:
            return false;
        }

        bytecode.append(instr);
        return true;
    } 
    return false;
}

/*!
    \internal
*/
void QmlBasicScript::clearCache(void *voidCache)
{
    QmlBasicScriptNodeCache *dataCache =
        reinterpret_cast<QmlBasicScriptNodeCache *>(voidCache);

    for (int ii = 0; ii < d->stateSize; ++ii) {
        if (!dataCache[ii].isCore() && !dataCache[ii].isVariant() && 
            dataCache[ii].object) {
            QMetaObject::removeGuard(&dataCache[ii].object);
            dataCache[ii].object = 0;
        }
        dataCache[ii].clear();
    }
}

void QmlBasicScript::guard(QmlBasicScriptNodeCache &n)
{
    if (n.object) {
        if (n.isVariant()) {
        } else if (n.isCore()) {
            n.metaObject = 
                n.object->metaObject();
        } else {
            QMetaObject::addGuard(&n.object);
        }
    }
}

bool QmlBasicScript::valid(QmlBasicScriptNodeCache &n, QObject *obj)
{
    return n.object == obj && 
                       (!n.isCore() || obj->metaObject() == n.metaObject); 
}


/*!
    \enum QmlBasicScript::CacheState
    \value NoChange The query has not change.  Any previous monitoring is still
    valid.
    \value Incremental The query has been incrementally changed.  Any previous 
    monitoring is still valid, but needs to have the fresh properties added to
    it.
    \value Reset The entire query has been reset from the beginning.  Any previous
    monitoring is now invalid.
*/

/*!
    Run the script in \a context and return the result.  \a voidCache should 
    contain state memory previously acquired from newScript. 
 */
QVariant QmlBasicScript::run(QmlContext *context, void *voidCache, CacheState *cached)
{
    if (!isValid())
        return QVariant();
    
    QmlContextPrivate *contextPrivate = context->d_func();
    QmlEnginePrivate *enginePrivate = context->engine()->d_func();

    QmlBasicScriptNodeCache *dataCache =
        reinterpret_cast<QmlBasicScriptNodeCache *>(voidCache);
    int dataCacheItem; 

    QStack<QVariant> stack;
    
    bool resetting = false;
    bool hasReset = false;

    const char *data = d->data();

    if (dataCache[0].type == QmlBasicScriptNodeCache::Invalid) {
        resetting = true;
        hasReset = true;
    }

    CacheState state = NoChange;

    dataCacheItem = 0;
    for (int idx = 0; idx < d->instructionCount; ++idx) {
        const ScriptInstruction &instr = d->instructions()[idx];

        switch(instr.type) {
            case ScriptInstruction::LoadIdObject: 
            {
                stack.push(contextPrivate->propertyValues.at(instr.fetch.idx));
                enginePrivate->capturedProperties <<
                    QmlEnginePrivate::CapturedProperty(context, -1, contextPrivate->notifyIndex + instr.fetch.idx); 
                state = Reset;
            }
                break;

            case ScriptInstruction::FetchD0Constant: 
            {
                QObject *obj = contextPrivate->defaultObjects.at(0);

                stack.push(fetch_value(obj, instr.constant.idx, instr.constant.type));
                if (obj && instr.constant.notify != 0)
                    enginePrivate->capturedProperties <<
                        QmlEnginePrivate::CapturedProperty(obj, instr.constant.idx, instr.constant.notify); 
                state = Reset;
            }
                break;

            case ScriptInstruction::FetchD1Constant: 
            {
                QObject *obj = contextPrivate->defaultObjects.at(1);

                stack.push(fetch_value(obj, instr.constant.idx, instr.constant.type));
                if (obj && instr.constant.notify != 0)
                    enginePrivate->capturedProperties <<
                        QmlEnginePrivate::CapturedProperty(obj, instr.constant.idx, instr.constant.notify); 
                state = Reset;
            }
                break;

            case ScriptInstruction::FetchConstant: 
            {
                QVariant o = stack.pop();
                QObject *obj = qvariant_cast<QObject *>(o);

                stack.push(fetch_value(obj, instr.constant.idx, instr.constant.type));
                if (obj && instr.constant.notify != 0)
                    enginePrivate->capturedProperties <<
                        QmlEnginePrivate::CapturedProperty(obj, instr.constant.idx, instr.constant.notify); 
                state = Reset;
            }
                break;

            case ScriptInstruction::Load: // either an object or a property
            case ScriptInstruction::Fetch: // can only be a property
            {
                const char *id = data + instr.fetch.idx;
                QmlBasicScriptNodeCache &n = dataCache[dataCacheItem];

                if (instr.type == ScriptInstruction::Load) {

                    if (n.type == QmlBasicScriptNodeCache::Invalid || state == Reset) {
                        context->engine()->d_func()->loadCache(n, QLatin1String(id), static_cast<QmlContextPrivate*>(context->d_ptr));
                        if (state != Reset)
                            state = Incremental;
                    }

                    if(!n.isValid())
                        qWarning("ReferenceError: %s is not defined", id);

                } else { // instr.type == ScriptInstruction::Fetch

                    QVariant o = stack.pop();
                    QObject *obj = qvariant_cast<QObject *>(o);
                    if (!obj) {
                        if (n.type == QmlBasicScriptNodeCache::Invalid) {
                            if (scriptWarnings())
                                qWarning() << "QmlBasicScript: Unable to convert" << o;
                            *cached = state;
                            return QVariant();
                        } else {
                            clearCache(dataCache);
                            *cached = Reset;
                            CacheState dummy;
                            return run(context, voidCache, &dummy);
                        }
                    } else if (n.type == QmlBasicScriptNodeCache::Invalid || state == Reset) {
                        context->engine()->d_func()->fetchCache(n, QLatin1String(id), obj);
                        guard(n);
                        if (state != Reset)
                            state = Incremental;
                    } else if (!valid(n, obj)) {
                        clearCache(dataCache);
                        *cached = Reset;
                        CacheState dummy;
                        return run(context, voidCache, &dummy);
                    }

                }

                QVariant var = n.value(id);
                stack.push(var);
                ++dataCacheItem;
            }
                break;
            case ScriptInstruction::Int:
                stack.push(QVariant(instr.integer.value));
                break;
            case ScriptInstruction::Bool:
                stack.push(QVariant(instr.boolean.value));
                break;
            case ScriptInstruction::Equals:
                {
                    QVariant rhs = stack.pop();
                    QVariant lhs = stack.pop();

                    stack.push(rhs == lhs);
                }
                break;
            default:
                break;
        }
    }

    *cached = state;

    if (stack.isEmpty())
        return QVariant();
    else
        return stack.top();
}

/*!
    Return a pointer to the script's compile data, or null if there is no data.
 */
const char *QmlBasicScript::compileData() const
{
    return (const char *)d;
}

/*!
    Return the size of the script's compile data, or zero if there is no data.
    The size will always be a multiple of 4.
 */
unsigned int QmlBasicScript::compileDataSize() const
{
    if (d)
        return d->size;
    else
        return 0;
}

bool QmlBasicScript::isSingleLoad() const
{
    if (!d)
        return false;

    return d->instructionCount == 1 &&
           d->instructions()[0].type == ScriptInstruction::Load;
}

QByteArray QmlBasicScript::singleLoadTarget() const
{
    if (!isSingleLoad())
        return QByteArray();

    // We know there is one instruction and it is a load
    return QByteArray(d->data() + d->instructions()[0].fetch.idx);
}


QT_END_NAMESPACE