summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qmlexpression.cpp
blob: 6f32ef489ae94df95a132792fc79d297cb2f1b37 (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (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 Technology Preview License Agreement accompanying
** this package.
**
** 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.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmlexpression.h"
#include "qmlexpression_p.h"

#include "qmlengine_p.h"
#include "qmlcontext_p.h"
#include "qmlrewrite_p.h"
#include "qmlcompiler_p.h"

#include <QtCore/qdebug.h>
#include <QtScript/qscriptprogram.h>

#include <private/qscriptdeclarativeclass_p.h>

Q_DECLARE_METATYPE(QList<QObject *>);

QT_BEGIN_NAMESPACE

bool QmlDelayedError::addError(QmlEnginePrivate *e)
{
    if (!e || prevError) return false;

    if (e->inProgressCreations == 0) return false; // Not in construction

    prevError = &e->erroredBindings;
    nextError = e->erroredBindings;
    e->erroredBindings = this;
    if (nextError) nextError->prevError = &nextError;

    return true;
}

QmlExpressionData::QmlExpressionData()
: q(0), dataRef(0), expressionFunctionValid(false), expressionRewritten(false), me(0), 
  trackChange(true), isShared(false), line(-1), guardList(0), guardListLength(0)
{
}

QmlExpressionData::~QmlExpressionData()
{
    if (guardList) { delete [] guardList; guardList = 0; }
    if (dataRef) dataRef->release();
}

QmlExpressionPrivate::QmlExpressionPrivate()
: data(new QmlExpressionData)
{
    data->q = this;
}

QmlExpressionPrivate::QmlExpressionPrivate(QmlExpressionData *d)
: data(d)
{
    data->q = this;
}

QmlExpressionPrivate::~QmlExpressionPrivate()
{
    if (data) { data->q = 0; data->release(); data = 0; }
}

void QmlExpressionPrivate::init(QmlContext *ctxt, const QString &expr, 
                                QObject *me)
{
    data->expression = expr;

    data->QmlAbstractExpression::setContext(ctxt);
    data->me = me;
}

void QmlExpressionPrivate::init(QmlContext *ctxt, void *expr, QmlRefCount *rc, 
                                QObject *me, const QString &url, int lineNumber)
{
    data->url = url;
    data->line = lineNumber;

    if (data->dataRef) data->dataRef->release();
    data->dataRef = rc;
    if (data->dataRef) data->dataRef->addref();

    quint32 *exprData = (quint32 *)expr;
    QmlCompiledData *dd = (QmlCompiledData *)rc;

    data->expressionRewritten = true;
    data->expression = QString::fromRawData((QChar *)(exprData + 2), exprData[1]);

    int progIdx = *(exprData);
    bool isShared = progIdx & 0x80000000;
    progIdx &= 0x7FFFFFFF;

    QmlEngine *engine = ctxt->engine();
    QmlEnginePrivate *ep = QmlEnginePrivate::get(engine);
    QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(engine);

    if (isShared) {

        if (!dd->cachedClosures.at(progIdx)) {
            QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(scriptEngine);
            scriptContext->pushScope(ep->contextClass->newSharedContext());
            dd->cachedClosures[progIdx] = new QScriptValue(scriptEngine->evaluate(data->expression, data->url, data->line));
            scriptEngine->popContext();
        }

        data->expressionFunction = *dd->cachedClosures.at(progIdx);
        data->isShared = true;
        data->expressionFunctionValid = true;

    } else {

#if !defined(Q_OS_SYMBIAN) //XXX Why doesn't this work?
        if (!dd->cachedPrograms.at(progIdx)) {
            dd->cachedPrograms[progIdx] =
                new QScriptProgram(data->expression, data->url, data->line);
        }

        data->expressionFunction = evalInObjectScope(ctxt, me, *dd->cachedPrograms.at(progIdx));
#else
        data->expressionFunction = evalInObjectScope(ctxt, me, data->expression);
#endif

        data->expressionFunctionValid = true;
    }

    data->QmlAbstractExpression::setContext(ctxt);
    data->me = me;
}

QScriptValue QmlExpressionPrivate::evalInObjectScope(QmlContext *context, QObject *object, 
                                                     const QString &program)
{
    QmlEnginePrivate *ep = QmlEnginePrivate::get(context->engine());
    QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(&ep->scriptEngine);
    scriptContext->pushScope(ep->contextClass->newContext(context, object));
    QScriptValue rv = ep->scriptEngine.evaluate(program);
    ep->scriptEngine.popContext();
    return rv;
}

QScriptValue QmlExpressionPrivate::evalInObjectScope(QmlContext *context, QObject *object, 
                                                     const QScriptProgram &program)
{
    QmlEnginePrivate *ep = QmlEnginePrivate::get(context->engine());
    QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(&ep->scriptEngine);
    scriptContext->pushScope(ep->contextClass->newContext(context, object));
    QScriptValue rv = ep->scriptEngine.evaluate(program);
    ep->scriptEngine.popContext();
    return rv;
}

/*!
    \class QmlExpression
    \brief The QmlExpression class evaluates JavaScript in a QML context.
*/

/*!
    Create an invalid QmlExpression.

    As the expression will not have an associated QmlContext, this will be a
    null expression object and its value will always be an invalid QVariant.
 */
QmlExpression::QmlExpression()
: QObject(*new QmlExpressionPrivate, 0)
{
}

/*!  \internal */
QmlExpression::QmlExpression(QmlContext *ctxt, void *expr,
                             QmlRefCount *rc, QObject *me, 
                             const QString &url, int lineNumber,
                             QmlExpressionPrivate &dd)
: QObject(dd, 0)
{
    Q_D(QmlExpression);
    d->init(ctxt, expr, rc, me, url, lineNumber);
}

/*!
    Create a QmlExpression object.

    The \a expression JavaScript will be executed in the \a ctxt QmlContext.
    If specified, the \a scope object's properties will also be in scope during
    the expression's execution.
*/
QmlExpression::QmlExpression(QmlContext *ctxt, const QString &expression,
                             QObject *scope)
: QObject(*new QmlExpressionPrivate, 0)
{
    Q_D(QmlExpression);
    d->init(ctxt, expression, scope);
}

/*!  \internal */
QmlExpression::QmlExpression(QmlContext *ctxt, const QString &expression,
                             QObject *scope, QmlExpressionPrivate &dd)
: QObject(dd, 0)
{
    Q_D(QmlExpression);
    d->init(ctxt, expression, scope);
}

/*!
    Destroy the QmlExpression instance.
*/
QmlExpression::~QmlExpression()
{
}

/*!
    Returns the QmlEngine this expression is associated with, or 0 if there
    is no association or the QmlEngine has been destroyed.
*/
QmlEngine *QmlExpression::engine() const
{
    Q_D(const QmlExpression);
    return d->data->context()?d->data->context()->engine():0;
}

/*!
    Returns the QmlContext this expression is associated with, or 0 if there
    is no association or the QmlContext has been destroyed.
*/
QmlContext *QmlExpression::context() const
{
    Q_D(const QmlExpression);
    return d->data->context();
}

/*!
    Returns the expression string.
*/
QString QmlExpression::expression() const
{
    Q_D(const QmlExpression);
    return d->data->expression;
}

/*!
    Clear the expression.
*/
void QmlExpression::clearExpression()
{
    setExpression(QString());
}

/*!
    Set the expression to \a expression.
*/
void QmlExpression::setExpression(const QString &expression)
{
    Q_D(QmlExpression);

    d->clearGuards();

    d->data->expression = expression;
    d->data->expressionFunctionValid = false;
    d->data->expressionRewritten = false;
    d->data->expressionFunction = QScriptValue();
}

void QmlExpressionPrivate::exceptionToError(QScriptEngine *scriptEngine, 
                                            QmlError &error)
{
    if (scriptEngine->hasUncaughtException() && 
        scriptEngine->uncaughtException().isError()) {

        QString fileName;
        int lineNumber = scriptEngine->uncaughtExceptionLineNumber();

        QScriptValue exception = scriptEngine->uncaughtException();
        QLatin1String fileNameProp("fileName");

        if (!exception.property(fileNameProp).toString().isEmpty()){
            fileName = exception.property(fileNameProp).toString();
        } else {
            fileName = QLatin1String("<Unknown File>");
        }

        error.setUrl(QUrl(fileName));
        error.setLine(lineNumber);
        error.setColumn(-1);
        error.setDescription(exception.toString());
    } else {
        error = QmlError();
    }
}

QVariant QmlExpressionPrivate::evalQtScript(QObject *secondaryScope, bool *isUndefined)
{
#ifdef Q_ENABLE_PERFORMANCE_LOG
    QmlPerfTimer<QmlPerf::BindValueQt> perfqt;
#endif

    QmlExpressionData *data = this->data;
    QmlContextPrivate *ctxtPriv = data->context()->d_func();
    QmlEngine *engine = data->context()->engine();
    QmlEnginePrivate *ep = QmlEnginePrivate::get(engine);

    if (secondaryScope)
       ctxtPriv->defaultObjects.append(secondaryScope);

    QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(engine);

    if (!data->expressionFunctionValid) {

        QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(scriptEngine);
        scriptContext->pushScope(ep->contextClass->newContext(data->context(), data->me));

        if (data->expressionRewritten) {
            data->expressionFunction = scriptEngine->evaluate(data->expression, 
                                                              data->url, data->line);
        } else {
            QmlRewrite::RewriteBinding rewriteBinding;

            bool ok = true;
            const QString code = rewriteBinding(data->expression, &ok);
            if (!ok) {
                scriptEngine->popContext();
                return QVariant();
            }
            data->expressionFunction = scriptEngine->evaluate(code, data->url, data->line);
        }

        scriptEngine->popContext();
        data->expressionFunctionValid = true;
    }

    QmlContext *oldSharedContext = 0;
    QObject *oldSharedScope = 0;
    if (data->isShared) {
        oldSharedContext = ep->sharedContext;
        oldSharedScope = ep->sharedScope;
        ep->sharedContext = data->context();
        ep->sharedScope = data->me;
    }

    QScriptValue svalue = data->expressionFunction.call();

    if (data->isShared) {
        ep->sharedContext = oldSharedContext;
        ep->sharedScope = oldSharedScope;
    }

    if (isUndefined)
        *isUndefined = svalue.isUndefined() || scriptEngine->hasUncaughtException();

    // Handle exception
    if (scriptEngine->hasUncaughtException()) {
       exceptionToError(scriptEngine, data->error);
       scriptEngine->clearExceptions();
       return QVariant();
    } else {
        data->error = QmlError();
    }

    if (secondaryScope) {
        QObject *last = ctxtPriv->defaultObjects.takeLast();
        Q_ASSERT(last == secondaryScope);
        Q_UNUSED(last);
    }

    QVariant rv;

    if (svalue.isArray()) {
        int length = svalue.property(QLatin1String("length")).toInt32();
        if (length && svalue.property(0).isObject()) {
            QList<QObject *> list;
            for (int ii = 0; ii < length; ++ii) {
                QScriptValue arrayItem = svalue.property(ii);
                QObject *d = arrayItem.toQObject();
                list << d;
            }
            rv = QVariant::fromValue(list);
        }
    } else if (svalue.isObject() &&
               ep->objectClass->scriptClass(svalue) == ep->objectClass) {
        QObject *o = svalue.toQObject();
        int type = QMetaType::QObjectStar;
        // If the object is null, we extract the predicted type.  While this isn't
        // 100% reliable, in many cases it gives us better error messages if we
        // assign this null-object to an incompatible property
        if (!o) type = ep->objectClass->objectType(svalue);

        return QVariant(type, &o);
    }

    if (rv.isNull())
        rv = svalue.toVariant();

    return rv;
}

QVariant QmlExpressionPrivate::value(QObject *secondaryScope, bool *isUndefined)
{
    Q_Q(QmlExpression);

    QVariant rv;
    if (!q->engine()) {
        qWarning("QmlExpression: Attempted to evaluate an expression in an invalid context");
        return rv;
    }

    if (data->expression.isEmpty())
        return rv;

#ifdef Q_ENABLE_PERFORMANCE_LOG
    QmlPerfTimer<QmlPerf::BindValue> perf;
#endif

    QmlEnginePrivate *ep = QmlEnginePrivate::get(q->engine());

    QmlExpression *lastCurrentExpression = ep->currentExpression;
    bool lastCaptureProperties = ep->captureProperties;
    QPODVector<QmlEnginePrivate::CapturedProperty> lastCapturedProperties;
    ep->capturedProperties.copyAndClear(lastCapturedProperties);

    ep->currentExpression = q;
    ep->captureProperties = data->trackChange;

    // This object might be deleted during the eval
    QmlExpressionData *localData = data;
    localData->addref();

    rv = evalQtScript(secondaryScope, isUndefined);

    ep->currentExpression = lastCurrentExpression;
    ep->captureProperties = lastCaptureProperties;

    // Check if we were deleted
    if (localData->q) {
        if ((!data->trackChange || !ep->capturedProperties.count()) && data->guardList) {
            clearGuards();
        } else if(data->trackChange) {
            updateGuards(ep->capturedProperties);
        }
    }

    localData->release();

    lastCapturedProperties.copyAndClear(ep->capturedProperties);

    return rv;
}

/*!
    Returns the value of the expression, or an invalid QVariant if the
    expression is invalid or has an error.

    \a isUndefined is set to true if the expression resulted in an
    undefined value.

    \sa hasError(), error()
*/
QVariant QmlExpression::value(bool *isUndefined)
{
    Q_D(QmlExpression);
    return d->value(0, isUndefined);
}

/*!
    Returns true if the expression results in a constant value.
    QmlExpression::value() must have been invoked at least once before the
    return from this method is valid.
 */
bool QmlExpression::isConstant() const
{
    Q_D(const QmlExpression);
    return !d->data->guardList;
}

/*!
    Returns true if the changes are tracked in the expression's value.
*/
bool QmlExpression::trackChange() const
{
    Q_D(const QmlExpression);
    return d->data->trackChange;
}

/*!
    Set whether changes are tracked in the expression's value to \a trackChange.

    If true, the QmlExpression will monitor properties involved in the
    expression's evaluation, and call QmlExpression::valueChanged() if they have
    changed.  This allows an application to ensure that any value associated
    with the result of the expression remains up to date.

    If false, the QmlExpression will not montitor properties involved in the
    expression's evaluation, and QmlExpression::valueChanged() will never be
    called.  This is more efficient if an application wants a "one off"
    evaluation of the expression.

    By default, trackChange is true.
*/
void QmlExpression::setTrackChange(bool trackChange)
{
    Q_D(QmlExpression);
    d->data->trackChange = trackChange;
}

/*!
    Returns the source file URL for this expression.  The source location must
    have been previously set by calling setSourceLocation().
*/
QString QmlExpression::sourceFile() const
{
    Q_D(const QmlExpression);
    return d->data->url;
}

/*!
    Returns the source file line number for this expression.  The source location 
    must have been previously set by calling setSourceLocation().
*/
int QmlExpression::lineNumber() const
{
    Q_D(const QmlExpression);
    return d->data->line;
}

/*!
    Set the location of this expression to \a line of \a url. This information
    is used by the script engine.
*/
void QmlExpression::setSourceLocation(const QString &url, int line)
{
    Q_D(QmlExpression);
    d->data->url = url;
    d->data->line = line;
}

/*!
    Returns the expression's scope object, if provided, otherwise 0.

    In addition to data provided by the expression's QmlContext, the scope
    object's properties are also in scope during the expression's evaluation.
*/
QObject *QmlExpression::scopeObject() const
{
    Q_D(const QmlExpression);
    return d->data->me;
}

/*!
    Returns true if the last call to value() resulted in an error, 
    otherwise false.
    
    \sa error(), clearError()
*/
bool QmlExpression::hasError() const
{
    Q_D(const QmlExpression);
    return d->data->error.isValid();
}

/*!
    Clear any expression errors.  Calls to hasError() following this will
    return false.

    \sa hasError(), error()
*/
void QmlExpression::clearError()
{
    Q_D(QmlExpression);
    d->data->error = QmlError();
}

/*!
    Return any error from the last call to value().  If there was no error,
    this returns an invalid QmlError instance.

    \sa hasError(), clearError()
*/

QmlError QmlExpression::error() const
{
    Q_D(const QmlExpression);
    return d->data->error;
}

/*! \internal */
void QmlExpression::__q_notify()
{
    emitValueChanged();
}

void QmlExpressionPrivate::clearGuards()
{
    Q_Q(QmlExpression);

    static int notifyIdx = -1;
    if (notifyIdx == -1) 
        notifyIdx = 
            QmlExpression::staticMetaObject.indexOfMethod("__q_notify()");

    for (int ii = 0; ii < data->guardListLength; ++ii) {
        if (data->guardList[ii].data()) {
#if (QT_VERSION >= QT_VERSION_CHECK(4, 6, 2))
            QMetaObject::disconnectOne(data->guardList[ii].data(), 
                                       data->guardList[ii].notifyIndex, 
                                       q, notifyIdx);
#else
            // QTBUG-6781
            QMetaObject::disconnect(data->guardList[ii].data(), 
                                    data->guardList[ii].notifyIndex, 
                                    q, notifyIdx);
#endif
        }
    }

    delete [] data->guardList; data->guardList = 0; 
    data->guardListLength = 0;
}

void QmlExpressionPrivate::updateGuards(const QPODVector<QmlEnginePrivate::CapturedProperty> &properties)
{
    //clearGuards();
    Q_Q(QmlExpression);

    static int notifyIdx = -1;
    if (notifyIdx == -1) 
        notifyIdx = 
            QmlExpression::staticMetaObject.indexOfMethod("__q_notify()");

    QmlExpressionData::SignalGuard *newGuardList = 0;
    
    if (properties.count() != data->guardListLength)
        newGuardList = new QmlExpressionData::SignalGuard[properties.count()];

    bool outputWarningHeader = false;
    int hit = 0;
    for (int ii = 0; ii < properties.count(); ++ii) {
        const QmlEnginePrivate::CapturedProperty &property = properties.at(ii);

        bool needGuard = true;
        if (ii >= data->guardListLength) {
            // New guard
        } else if(data->guardList[ii].data() == property.object && 
                  data->guardList[ii].notifyIndex == property.notifyIndex) {
            // Cache hit
            if (!data->guardList[ii].isDuplicate || 
                (data->guardList[ii].isDuplicate && hit == ii)) {
                needGuard = false;
                ++hit;
            }
        } else if(data->guardList[ii].data() && !data->guardList[ii].isDuplicate) {
            // Cache miss
#if (QT_VERSION >= QT_VERSION_CHECK(4, 6, 2))
            QMetaObject::disconnectOne(data->guardList[ii].data(), 
                                       data->guardList[ii].notifyIndex, 
                                       q, notifyIdx);
#else
            // QTBUG-6781
            QMetaObject::disconnect(data->guardList[ii].data(), 
                                    data->guardList[ii].notifyIndex, 
                                    q, notifyIdx);
#endif
        } 
        /* else {
            // Cache miss, but nothing to do
        } */

        if (needGuard) {
            if (!newGuardList) {
                newGuardList = new QmlExpressionData::SignalGuard[properties.count()];
                for (int jj = 0; jj < ii; ++jj)
                    newGuardList[jj] = data->guardList[jj];
            }

            if (property.notifyIndex != -1) {
                bool existing = false;
                for (int jj = 0; !existing && jj < ii; ++jj) 
                    existing = newGuardList[jj].data() == property.object &&
                        newGuardList[jj].notifyIndex == property.notifyIndex;

                newGuardList[ii] = property.object;
                newGuardList[ii].notifyIndex = property.notifyIndex;
                if (existing)
                    newGuardList[ii].isDuplicate = true;
                else
                    QMetaObject::connect(property.object, property.notifyIndex,
                                         q, notifyIdx);
            } else {
                if (!outputWarningHeader) {
                    outputWarningHeader = true;
                    qWarning() << "QmlExpression: Expression" << q->expression()
                               << "depends on non-NOTIFYable properties:";
                }

                const QMetaObject *metaObj = property.object->metaObject();
                QMetaProperty metaProp = metaObj->property(property.coreIndex);

                qWarning().nospace() << "    " << metaObj->className()
                                     << "::" << metaProp.name();
            }
        } else if (newGuardList) {
            newGuardList[ii] = data->guardList[ii];
        }
    }

    for (int ii = properties.count(); ii < data->guardListLength; ++ii) {
        if (data->guardList[ii].data() && !data->guardList[ii].isDuplicate) {
#if (QT_VERSION >= QT_VERSION_CHECK(4, 6, 2))
            QMetaObject::disconnectOne(data->guardList[ii].data(), 
                                       data->guardList[ii].notifyIndex, 
                                       q, notifyIdx);
#else
            // QTBUG-6781
            QMetaObject::disconnect(data->guardList[ii].data(), 
                                    data->guardList[ii].notifyIndex, 
                                    q, notifyIdx);
#endif
        }
    }

    if (newGuardList) {
        if (data->guardList) delete [] data->guardList;
        data->guardList = newGuardList;
        data->guardListLength = properties.count();
    }
}

/*!
    \fn void QmlExpression::valueChanged()

    Emitted each time the expression value changes from the last time it was
    evaluated.  The expression must have been evaluated at least once (by
    calling QmlExpression::value()) before this signal will be emitted.
*/

/*!
    Subclasses can capture the emission of the valueChanged() signal by overriding
    this function. They can choose whether to then call valueChanged().
*/
void QmlExpression::emitValueChanged()
{
    emit valueChanged();
}

QmlAbstractExpression::QmlAbstractExpression()
: m_context(0), m_prevExpression(0), m_nextExpression(0)
{
}

QmlAbstractExpression::~QmlAbstractExpression()
{
    if (m_prevExpression) {
        *m_prevExpression = m_nextExpression;
        if (m_nextExpression) 
            m_nextExpression->m_prevExpression = m_prevExpression;
    }
}

QmlContext *QmlAbstractExpression::context() const
{
    return m_context;
}

void QmlAbstractExpression::setContext(QmlContext *context)
{
    if (m_prevExpression) {
        *m_prevExpression = m_nextExpression;
        if (m_nextExpression) 
            m_nextExpression->m_prevExpression = m_prevExpression;
        m_prevExpression = 0;
        m_nextExpression = 0;
    }

    m_context = context;

    if (m_context) {
        QmlContextPrivate *cp = 
            static_cast<QmlContextPrivate *>(QObjectPrivate::get(m_context));
        m_nextExpression = cp->expressions;
        if (m_nextExpression) 
            m_nextExpression->m_prevExpression = &m_nextExpression;
        m_prevExpression = &cp->expressions;
        cp->expressions = this;
    }
}

void QmlAbstractExpression::refresh()
{
}

bool QmlAbstractExpression::isValid() const
{
    return m_context != 0;
}

QT_END_NAMESPACE