/
qv4bindings.cpp
1721 lines (1482 loc) · 58.2 KB
/
qv4bindings.cpp
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
// #define REGISTER_CLEANUP_DEBUG
#include "qv4bindings_p.h"
#include "qv4program_p.h"
#include "qv4compiler_p.h"
#include "qv4compiler_p_p.h"
#include <private/qqmlaccessors_p.h>
#include <private/qqmlprofilerservice_p.h>
#include <private/qqmlmetatype_p.h>
#include <private/qqmltrace_p.h>
#include <private/qqmlstringconverters_p.h>
#include <QtQml/qqmlinfo.h>
#include <QtCore/qnumeric.h>
#include <QtCore/qmath.h>
#include <math.h> // ::fmod
QT_BEGIN_NAMESPACE
using namespace QQmlJS;
namespace {
struct Register {
typedef QQmlRegisterType Type;
void setUndefined() { dataType = UndefinedType; }
void setNull() { dataType = NullType; }
void setNaN() { setqreal(qSNaN()); }
bool isUndefined() const { return dataType == UndefinedType; }
void setQObject(QObject *o) { qobjectValue = o; dataType = QObjectStarType; }
QObject *getQObject() const { return qobjectValue; }
void setqreal(qreal v) { qrealValue = v; dataType = QRealType; }
qreal getqreal() const { return qrealValue; }
qreal &getqrealref() { return qrealValue; }
void setint(int v) { intValue = v; dataType = IntType; }
int getint() const { return intValue; }
int &getintref() { return intValue; }
void setbool(bool v) { boolValue = v; dataType = BoolType; }
bool getbool() const { return boolValue; }
bool &getboolref() { return boolValue; }
QVariant *getvariantptr() { return (QVariant *)typeDataPtr(); }
QString *getstringptr() { return (QString *)typeDataPtr(); }
QUrl *geturlptr() { return (QUrl *)typeDataPtr(); }
QColor *getcolorptr() { return (QColor *)typeDataPtr(); }
const QVariant *getvariantptr() const { return (QVariant *)typeDataPtr(); }
const QString *getstringptr() const { return (QString *)typeDataPtr(); }
const QUrl *geturlptr() const { return (QUrl *)typeDataPtr(); }
const QColor *getcolorptr() const { return (QColor *)typeDataPtr(); }
void *typeDataPtr() { return (void *)&data; }
void *typeMemory() { return (void *)data; }
const void *typeDataPtr() const { return (void *)&data; }
const void *typeMemory() const { return (void *)data; }
Type gettype() const { return dataType; }
void settype(Type t) { dataType = t; }
Type dataType; // Type of data
union {
QObject *qobjectValue;
qreal qrealValue;
int intValue;
bool boolValue;
void *data[sizeof(QVariant)];
qint64 q_for_alignment_1;
double q_for_alignment_2;
};
inline void cleanup();
inline void cleanupString();
inline void cleanupUrl();
inline void cleanupColor();
inline void cleanupVariant();
inline void copy(const Register &other);
inline void init(Type type);
#ifdef REGISTER_CLEANUP_DEBUG
Register() {
type = 0;
}
~Register() {
if (dataType >= FirstCleanupType)
qWarning("Register leaked of type %d", dataType);
}
#endif
};
void Register::cleanup()
{
if (dataType >= FirstCleanupType) {
if (dataType == QStringType) {
getstringptr()->~QString();
} else if (dataType == QUrlType) {
geturlptr()->~QUrl();
} else if (dataType == QColorType) {
getcolorptr()->~QColor();
} else if (dataType == QVariantType) {
getvariantptr()->~QVariant();
}
}
setUndefined();
}
void Register::cleanupString()
{
getstringptr()->~QString();
setUndefined();
}
void Register::cleanupUrl()
{
geturlptr()->~QUrl();
setUndefined();
}
void Register::cleanupColor()
{
getcolorptr()->~QColor();
setUndefined();
}
void Register::cleanupVariant()
{
getvariantptr()->~QVariant();
setUndefined();
}
void Register::copy(const Register &other)
{
*this = other;
if (other.dataType >= FirstCleanupType) {
if (other.dataType == QStringType)
new (getstringptr()) QString(*other.getstringptr());
else if (other.dataType == QUrlType)
new (geturlptr()) QUrl(*other.geturlptr());
else if (other.dataType == QColorType)
new (getcolorptr()) QColor(*other.getcolorptr());
else if (other.dataType == QVariantType)
new (getvariantptr()) QVariant(*other.getvariantptr());
}
}
void Register::init(Type type)
{
dataType = type;
if (dataType >= FirstCleanupType) {
if (dataType == QStringType)
new (getstringptr()) QString();
else if (dataType == QUrlType)
new (geturlptr()) QUrl();
else if (dataType == QColorType)
new (getcolorptr()) QColor();
else if (dataType == QVariantType)
new (getvariantptr()) QVariant();
}
}
} // end of anonymous namespace
QV4Bindings::QV4Bindings(const char *programData,
QQmlContextData *context,
QQmlRefCount *ref)
: subscriptions(0), program(0), dataRef(0), bindings(0)
{
program = (QV4Program *)programData;
dataRef = ref;
if (dataRef) dataRef->addref();
if (program) {
subscriptions = new Subscription[program->subscriptions];
bindings = new Binding[program->bindings];
QQmlAbstractExpression::setContext(context);
}
}
QV4Bindings::~QV4Bindings()
{
delete [] bindings;
delete [] subscriptions; subscriptions = 0;
if (dataRef) dataRef->release();
}
QQmlAbstractBinding *QV4Bindings::configBinding(int index, QObject *target,
QObject *scope, int property,
int line, int column)
{
Binding *rv = bindings + index;
rv->index = index;
rv->property = property;
rv->target = target;
rv->scope = scope;
rv->line = line;
rv->column = column;
rv->parent = this;
addref(); // This is decremented in Binding::destroy()
return rv;
}
void QV4Bindings::Binding::setEnabled(bool e, QQmlPropertyPrivate::WriteFlags flags)
{
if (enabled != e) {
enabled = e;
if (e) update(flags);
}
}
void QV4Bindings::Binding::update(QQmlPropertyPrivate::WriteFlags flags)
{
parent->run(this, flags);
}
void QV4Bindings::Binding::destroy()
{
enabled = false;
removeFromObject();
clear();
removeError();
parent->release();
}
int QV4Bindings::Binding::propertyIndex() const
{
//mask out the type information set for value types
return property & 0xFF00FFFF;
}
QObject *QV4Bindings::Binding::object() const
{
return target;
}
void QV4Bindings::Subscription::subscriptionCallback(QQmlNotifierEndpoint *e)
{
Subscription *s = static_cast<Subscription *>(e);
s->bindings->subscriptionNotify(s->method);
}
void QV4Bindings::subscriptionNotify(int id)
{
QV4Program::BindingReferenceList *list = program->signalTable(id);
for (quint32 ii = 0; ii < list->count; ++ii) {
QV4Program::BindingReference *bindingRef = list->bindings + ii;
Binding *binding = bindings + bindingRef->binding;
if (binding->executedBlocks & bindingRef->blockMask) {
run(binding, QQmlPropertyPrivate::DontRemoveBinding);
}
}
}
void QV4Bindings::run(Binding *binding, QQmlPropertyPrivate::WriteFlags flags)
{
if (!binding->enabled)
return;
QQmlContextData *context = QQmlAbstractExpression::context();
if (!context || !context->isValid())
return;
QQmlTrace trace("V4 Binding Update");
trace.addDetail("URL", context->url);
trace.addDetail("Line", binding->line);
trace.addDetail("Column", binding->column);
QQmlBindingProfiler prof(context->urlString, binding->line, binding->column);
if (binding->updating) {
QString name;
if (binding->property & 0xFFFF0000) {
QQmlEnginePrivate *ep = QQmlEnginePrivate::get(context->engine);
QQmlValueType *vt = ep->valueTypes[(binding->property >> 16) & 0xFF];
Q_ASSERT(vt);
name = QLatin1String(binding->target->metaObject()->property(binding->property & 0xFFFF).name());
name.append(QLatin1String("."));
name.append(QLatin1String(vt->metaObject()->property(binding->property >> 24).name()));
} else {
name = QLatin1String(binding->target->metaObject()->property(binding->property).name());
}
qmlInfo(binding->target) << tr("Binding loop detected for property \"%1\"").arg(name);
return;
}
binding->updating = true;
if (binding->property & 0xFFFF0000) {
QQmlEnginePrivate *ep = QQmlEnginePrivate::get(context->engine);
QQmlValueType *vt = ep->valueTypes[(binding->property >> 16) & 0xFF];
Q_ASSERT(vt);
vt->read(binding->target, binding->property & 0xFFFF);
QObject *target = vt;
run(binding->index, binding->executedBlocks, context, binding, binding->scope, target, flags);
vt->write(binding->target, binding->property & 0xFFFF, flags);
} else {
run(binding->index, binding->executedBlocks, context, binding, binding->scope, binding->target, flags);
}
binding->updating = false;
}
void QV4Bindings::unsubscribe(int subIndex)
{
Subscription *sub = (subscriptions + subIndex);
sub->disconnect();
}
void QV4Bindings::subscribeId(QQmlContextData *p, int idIndex, int subIndex)
{
unsubscribe(subIndex);
if (p->idValues[idIndex]) {
Subscription *sub = (subscriptions + subIndex);
sub->bindings = this;
sub->method = subIndex;
sub->connect(&p->idValues[idIndex].bindings);
}
}
void QV4Bindings::subscribe(QObject *o, int notifyIndex, int subIndex)
{
Subscription *sub = (subscriptions + subIndex);
if (sub->isConnected(o, notifyIndex))
return;
sub->bindings = this;
sub->method = subIndex;
if (o)
sub->connect(o, notifyIndex);
else
sub->disconnect();
}
// Conversion functions - these MUST match the QtScript expression path
inline static qreal toReal(Register *reg, int type, bool *ok = 0)
{
if (ok) *ok = true;
if (type == QMetaType::QReal) {
return reg->getqreal();
} else if (type == qMetaTypeId<QVariant>()) {
return reg->getvariantptr()->toReal();
} else {
if (ok) *ok = false;
return 0;
}
}
inline static QString toString(Register *reg, int type, bool *ok = 0)
{
if (ok) *ok = true;
if (type == QMetaType::QReal) {
return QString::number(reg->getqreal());
} else if (type == QMetaType::Int) {
return QString::number(reg->getint());
} else if (type == qMetaTypeId<QVariant>()) {
return reg->getvariantptr()->toString();
} else if (type == QMetaType::QString) {
return *reg->getstringptr();
} else {
if (ok) *ok = false;
return QString();
}
}
inline static bool toBool(Register *reg, int type, bool *ok = 0)
{
if (ok) *ok = true;
if (type == QMetaType::Bool) {
return reg->getbool();
} else if (type == qMetaTypeId<QVariant>()) {
return reg->getvariantptr()->toBool();
} else {
if (ok) *ok = false;
return false;
}
}
inline static QUrl toUrl(Register *reg, int type, QQmlContextData *context, bool *ok = 0)
{
if (ok) *ok = true;
QUrl base;
if (type == qMetaTypeId<QVariant>()) {
QVariant *var = reg->getvariantptr();
int vt = var->type();
if (vt == QVariant::Url) {
base = var->toUrl();
} else if (vt == QVariant::ByteArray) {
// Preserve any valid percent-encoded octets supplied by the source
base.setEncodedUrl(var->toByteArray(), QUrl::TolerantMode);
} else if (vt == QVariant::String) {
base.setEncodedUrl(var->toString().toUtf8(), QUrl::TolerantMode);
} else {
if (ok) *ok = false;
return QUrl();
}
} else if (type == QMetaType::QString) {
base.setEncodedUrl(reg->getstringptr()->toUtf8(), QUrl::TolerantMode);
} else {
if (ok) *ok = false;
return QUrl();
}
if (!base.isEmpty() && base.isRelative())
return context->url.resolved(base);
else
return base;
}
static bool testCompareVariants(const QVariant &qtscriptRaw, const QVariant &v4)
{
QVariant qtscript = qtscriptRaw;
if (qtscript.userType() == v4.userType()) {
} else if (qtscript.canConvert((QVariant::Type)v4.userType())) {
qtscript.convert((QVariant::Type)v4.userType());
} else if (qtscript.userType() == QVariant::Invalid && v4.userType() == QMetaType::QObjectStar) {
qtscript = qVariantFromValue<QObject *>(0);
} else {
return false;
}
int type = qtscript.userType();
if (type == QQmlMetaType::QQuickAnchorLineMetaTypeId()) {
return QQmlMetaType::QQuickAnchorLineCompare(qtscript.constData(), v4.constData());
} else if (type == QMetaType::Double) {
double la = qvariant_cast<double>(qtscript);
double lr = qvariant_cast<double>(v4);
return la == lr || (qIsNaN(la) && qIsNaN(lr));
} else if (type == QMetaType::Float) {
float la = qvariant_cast<float>(qtscript);
float lr = qvariant_cast<float>(v4);
return la == lr || (qIsNaN(la) && qIsNaN(lr));
} else {
return qtscript == v4;
}
}
QByteArray testResultToString(const QVariant &result, bool undefined)
{
if (undefined) {
return "undefined";
} else {
QString rv;
QDebug d(&rv);
d << result;
return rv.toUtf8();
}
}
static void testBindingResult(const QString &binding, int line, int column,
QQmlContextData *context, QObject *scope,
const Register &result, int resultType)
{
QQmlExpression expression(context->asQQmlContext(), scope, binding);
bool isUndefined = false;
QVariant value = expression.evaluate(&isUndefined);
bool iserror = false;
QByteArray qtscriptResult;
QByteArray v4Result;
if (expression.hasError()) {
iserror = true;
qtscriptResult = "exception";
} else {
qtscriptResult = testResultToString(value, isUndefined);
}
if (isUndefined && result.isUndefined()) {
return;
} else if(isUndefined != result.isUndefined()) {
iserror = true;
}
QVariant v4value;
if (!result.isUndefined()) {
switch (resultType) {
case QMetaType::QString:
v4value = *result.getstringptr();
break;
case QMetaType::QUrl:
v4value = *result.geturlptr();
break;
case QMetaType::QObjectStar:
v4value = qVariantFromValue<QObject *>(result.getQObject());
break;
case QMetaType::Bool:
v4value = result.getbool();
break;
case QMetaType::Int:
v4value = result.getint();
break;
case QMetaType::QReal:
v4value = result.getqreal();
break;
default:
if (resultType == QQmlMetaType::QQuickAnchorLineMetaTypeId()) {
v4value = QVariant(QQmlMetaType::QQuickAnchorLineMetaTypeId(), result.typeDataPtr());
} else {
iserror = true;
v4Result = "Unknown V4 type";
}
}
}
if (v4Result.isEmpty())
v4Result = testResultToString(v4value, result.isUndefined());
if (!testCompareVariants(value, v4value))
iserror = true;
if (iserror) {
qWarning().nospace() << "QV4: Optimization error @" << context->url.toString().toUtf8().constData() << ":" << line << ":" << column;
qWarning().nospace() << " Binding: " << binding;
qWarning().nospace() << " QtScript: " << qtscriptResult.constData();
qWarning().nospace() << " V4: " << v4Result.constData();
}
}
static void testBindingException(const QString &binding, int line, int column,
QQmlContextData *context, QObject *scope)
{
QQmlExpression expression(context->asQQmlContext(), scope, binding);
bool isUndefined = false;
QVariant value = expression.evaluate(&isUndefined);
if (!expression.hasError()) {
QByteArray qtscriptResult = testResultToString(value, isUndefined);
qWarning().nospace() << "QV4: Optimization error @" << context->url.toString().toUtf8().constData() << ":" << line << ":" << column;
qWarning().nospace() << " Binding: " << binding;
qWarning().nospace() << " QtScript: " << qtscriptResult.constData();
qWarning().nospace() << " V4: exception";
}
}
static void throwException(int id, QQmlDelayedError *error,
QV4Program *program, QQmlContextData *context,
const QString &description = QString())
{
error->error.setUrl(context->url);
if (description.isEmpty())
error->error.setDescription(QLatin1String("TypeError: Result of expression is not an object"));
else
error->error.setDescription(description);
if (id != 0xFF) {
quint64 e = *((quint64 *)(program->data() + program->exceptionDataOffset) + id);
error->error.setLine((e >> 32) & 0xFFFFFFFF);
error->error.setColumn(e & 0xFFFFFFFF);
} else {
error->error.setLine(-1);
error->error.setColumn(-1);
}
if (!context->engine || !error->addError(QQmlEnginePrivate::get(context->engine)))
QQmlEnginePrivate::warning(context->engine, error->error);
}
const qreal QV4Bindings::D32 = 4294967296.0;
qint32 QV4Bindings::toInt32(qreal n)
{
if (qIsNaN(n) || qIsInf(n) || (n == 0))
return 0;
double sign = (n < 0) ? -1.0 : 1.0;
qreal abs_n = fabs(n);
n = ::fmod(sign * ::floor(abs_n), D32);
const double D31 = D32 / 2.0;
if (sign == -1 && n < -D31)
n += D32;
else if (sign != -1 && n >= D31)
n -= D32;
return qint32 (n);
}
inline quint32 QV4Bindings::toUint32(qreal n)
{
if (qIsNaN(n) || qIsInf(n) || (n == 0))
return 0;
double sign = (n < 0) ? -1.0 : 1.0;
qreal abs_n = fabs(n);
n = ::fmod(sign * ::floor(abs_n), D32);
if (n < 0)
n += D32;
return quint32 (n);
}
#define THROW_EXCEPTION_STR(id, str) { \
if (testBinding) testBindingException(*testBindingSource, bindingLine, bindingColumn, context, scope); \
throwException((id), error, program, context, (str)); \
goto exceptionExit; \
}
#define THROW_EXCEPTION(id) THROW_EXCEPTION_STR(id, QString())
#define MARK_REGISTER(reg) cleanupRegisterMask |= (1 << (reg))
#define MARK_CLEAN_REGISTER(reg) cleanupRegisterMask &= ~(1 << (reg))
#define STRING_REGISTER(reg) { \
registers[(reg)].settype(QStringType); \
MARK_REGISTER(reg); \
}
#define URL_REGISTER(reg) { \
registers[(reg)].settype(QUrlType); \
MARK_REGISTER(reg); \
}
#define COLOR_REGISTER(reg) { \
registers[(reg)].settype(QColorType); \
MARK_REGISTER(reg); \
}
#define VARIANT_REGISTER(reg) { \
registers[(reg)].settype(QVariantType); \
MARK_REGISTER(reg); \
}
#ifdef QML_THREADED_INTERPRETER
void **QV4Bindings::getDecodeInstrTable()
{
static void **decode_instr;
if (!decode_instr) {
QV4Bindings *dummy = new QV4Bindings(0, 0, 0);
quint32 executedBlocks = 0;
dummy->run(0, executedBlocks, 0, 0, 0, 0,
QQmlPropertyPrivate::BypassInterceptor,
&decode_instr);
dummy->release();
}
return decode_instr;
}
#endif
void QV4Bindings::run(int instrIndex, quint32 &executedBlocks,
QQmlContextData *context, QQmlDelayedError *error,
QObject *scope, QObject *output,
QQmlPropertyPrivate::WriteFlags storeFlags
#ifdef QML_THREADED_INTERPRETER
,void ***table
#endif
)
{
#ifdef QML_THREADED_INTERPRETER
if (table) {
static void *decode_instr[] = {
FOR_EACH_V4_INSTR(QML_V4_INSTR_ADDR)
};
*table = decode_instr;
return;
}
#endif
error->removeError();
Register registers[32];
quint32 cleanupRegisterMask = 0;
executedBlocks = 0;
const char *code = program->instructions();
code += instrIndex * QML_V4_INSTR_SIZE(Jump, jump);
const V4Instr *instr = reinterpret_cast<const V4Instr *>(code);
const char *data = program->data();
QString *testBindingSource = 0;
bool testBinding = false;
int bindingLine = 0;
int bindingColumn = 0;
#ifdef QML_THREADED_INTERPRETER
goto *instr->common.code;
#else
for (;;) {
switch (instr->common.type) {
#endif
QML_V4_BEGIN_INSTR(Noop, common)
QML_V4_END_INSTR(Noop, common)
QML_V4_BEGIN_INSTR(BindingId, id)
bindingLine = instr->id.line;
bindingColumn = instr->id.column;
QML_V4_END_INSTR(BindingId, id)
QML_V4_BEGIN_INSTR(SubscribeId, subscribeop)
subscribeId(context, instr->subscribeop.index, instr->subscribeop.offset);
QML_V4_END_INSTR(SubscribeId, subscribeop)
QML_V4_BEGIN_INSTR(Subscribe, subscribeop)
{
QObject *o = 0;
const Register &object = registers[instr->subscribeop.reg];
if (!object.isUndefined()) o = object.getQObject();
subscribe(o, instr->subscribeop.index, instr->subscribeop.offset);
}
QML_V4_END_INSTR(Subscribe, subscribeop)
QML_V4_BEGIN_INSTR(FetchAndSubscribe, fetchAndSubscribe)
{
Register ® = registers[instr->fetchAndSubscribe.reg];
if (reg.isUndefined())
THROW_EXCEPTION(instr->fetchAndSubscribe.exceptionId);
QObject *object = reg.getQObject();
if (!object) {
reg.setUndefined();
} else {
int subIdx = instr->fetchAndSubscribe.subscription;
Subscription *sub = 0;
if (subIdx != -1) {
sub = (subscriptions + subIdx);
sub->bindings = this;
sub->method = subIdx;
}
reg.init((Register::Type)instr->fetchAndSubscribe.valueType);
if (instr->fetchAndSubscribe.valueType >= FirstCleanupType)
MARK_REGISTER(instr->fetchAndSubscribe.reg);
QQmlAccessors *accessors = instr->fetchAndSubscribe.property.accessors;
accessors->read(object, instr->fetchAndSubscribe.property.accessorData,
reg.typeDataPtr());
if (accessors->notifier) {
QQmlNotifier *notifier = 0;
accessors->notifier(object, instr->fetchAndSubscribe.property.accessorData, ¬ifier);
if (notifier) sub->connect(notifier);
} else if (instr->fetchAndSubscribe.property.notifyIndex != -1) {
sub->connect(object, instr->fetchAndSubscribe.property.notifyIndex);
}
}
}
QML_V4_END_INSTR(FetchAndSubscribe, fetchAndSubscribe)
QML_V4_BEGIN_INSTR(LoadId, load)
registers[instr->load.reg].setQObject(context->idValues[instr->load.index].data());
QML_V4_END_INSTR(LoadId, load)
QML_V4_BEGIN_INSTR(LoadScope, load)
registers[instr->load.reg].setQObject(scope);
QML_V4_END_INSTR(LoadScope, load)
QML_V4_BEGIN_INSTR(LoadRoot, load)
registers[instr->load.reg].setQObject(context->contextObject);
QML_V4_END_INSTR(LoadRoot, load)
QML_V4_BEGIN_INSTR(LoadModuleObject, load)
{
Register ® = registers[instr->load.reg];
const QString *name = reg.getstringptr();
QQmlTypeNameCache::Result r = context->imports->query(*name);
reg.cleanupString();
if (r.isValid() && r.importNamespace) {
QQmlMetaType::ModuleApiInstance *moduleApi = context->imports->moduleApi(r.importNamespace);
if (moduleApi) {
if (moduleApi->qobjectCallback) {
moduleApi->qobjectApi = moduleApi->qobjectCallback(context->engine, context->engine);
moduleApi->qobjectCallback = 0;
moduleApi->scriptCallback = 0;
}
if (moduleApi->qobjectApi)
reg.setQObject(moduleApi->qobjectApi);
}
}
}
QML_V4_END_INSTR(LoadModuleObject, load)
QML_V4_BEGIN_INSTR(LoadAttached, attached)
{
const Register &input = registers[instr->attached.reg];
Register &output = registers[instr->attached.output];
if (input.isUndefined())
THROW_EXCEPTION(instr->attached.exceptionId);
QObject *object = registers[instr->attached.reg].getQObject();
if (!object) {
output.setUndefined();
} else {
QObject *attached = qmlAttachedPropertiesObjectById(instr->attached.id, input.getQObject(), true);
Q_ASSERT(attached);
output.setQObject(attached);
}
}
QML_V4_END_INSTR(LoadAttached, attached)
QML_V4_BEGIN_INSTR(UnaryNot, unaryop)
{
registers[instr->unaryop.output].setbool(!registers[instr->unaryop.src].getbool());
}
QML_V4_END_INSTR(UnaryNot, unaryop)
QML_V4_BEGIN_INSTR(UnaryMinusReal, unaryop)
{
registers[instr->unaryop.output].setqreal(-registers[instr->unaryop.src].getqreal());
}
QML_V4_END_INSTR(UnaryMinusReal, unaryop)
QML_V4_BEGIN_INSTR(UnaryMinusInt, unaryop)
{
registers[instr->unaryop.output].setint(-registers[instr->unaryop.src].getint());
}
QML_V4_END_INSTR(UnaryMinusInt, unaryop)
QML_V4_BEGIN_INSTR(UnaryPlusReal, unaryop)
{
registers[instr->unaryop.output].setqreal(+registers[instr->unaryop.src].getqreal());
}
QML_V4_END_INSTR(UnaryPlusReal, unaryop)
QML_V4_BEGIN_INSTR(UnaryPlusInt, unaryop)
{
registers[instr->unaryop.output].setint(+registers[instr->unaryop.src].getint());
}
QML_V4_END_INSTR(UnaryPlusInt, unaryop)
QML_V4_BEGIN_INSTR(ConvertBoolToInt, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setint(src.getbool());
}
QML_V4_END_INSTR(ConvertBoolToInt, unaryop)
QML_V4_BEGIN_INSTR(ConvertBoolToReal, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setqreal(src.getbool());
}
QML_V4_END_INSTR(ConvertBoolToReal, unaryop)
QML_V4_BEGIN_INSTR(ConvertBoolToString, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) {
output.setUndefined();
} else {
new (output.getstringptr()) QString(QLatin1String(src.getbool() ? "true" : "false"));
STRING_REGISTER(instr->unaryop.output);
}
}
QML_V4_END_INSTR(ConvertBoolToString, unaryop)
QML_V4_BEGIN_INSTR(ConvertIntToBool, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setbool(src.getint());
}
QML_V4_END_INSTR(ConvertIntToBool, unaryop)
QML_V4_BEGIN_INSTR(ConvertIntToReal, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setqreal(qreal(src.getint()));
}
QML_V4_END_INSTR(ConvertIntToReal, unaryop)
QML_V4_BEGIN_INSTR(ConvertIntToString, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) {
output.setUndefined();
} else {
new (output.getstringptr()) QString(QString::number(src.getint()));
STRING_REGISTER(instr->unaryop.output);
}
}
QML_V4_END_INSTR(ConvertIntToString, unaryop)
QML_V4_BEGIN_INSTR(ConvertRealToBool, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setbool(src.getqreal() != 0);
}
QML_V4_END_INSTR(ConvertRealToBool, unaryop)
QML_V4_BEGIN_INSTR(ConvertRealToInt, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
if (src.isUndefined()) output.setUndefined();
else output.setint(toInt32(src.getqreal()));
}
QML_V4_END_INSTR(ConvertRealToInt, unaryop)
QML_V4_BEGIN_INSTR(ConvertRealToString, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
// ### NaN
if (src.isUndefined()) {
output.setUndefined();
} else {
new (output.getstringptr()) QString(QString::number(src.getqreal()));
STRING_REGISTER(instr->unaryop.output);
}
}
QML_V4_END_INSTR(ConvertRealToString, unaryop)
QML_V4_BEGIN_INSTR(ConvertStringToBool, unaryop)
{
const Register &src = registers[instr->unaryop.src];
Register &output = registers[instr->unaryop.output];
// ### NaN
if (src.isUndefined()) {
output.setUndefined();
} else {
// Delegate the conversion. This is pretty fast and it doesn't require a QScriptEngine.
// Ideally we should just call the methods in the QScript namespace directly.
QJSValue tmp(*src.getstringptr());
if (instr->unaryop.src == instr->unaryop.output) {