Support variable length instructions in QML bytecode
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeinstruction / tst_qdeclarativeinstruction.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <private/qdeclarativecompiler_p.h>
44
45 #ifdef Q_OS_SYMBIAN
46 // In Symbian OS test data is located in applications private dir
47 #define SRCDIR "."
48 #endif
49
50 class tst_qdeclarativeinstruction : public QObject
51 {
52     Q_OBJECT
53 public:
54     tst_qdeclarativeinstruction() {}
55
56 private slots:
57     void dump();
58 };
59
60 static QStringList messages;
61 static void msgHandler(QtMsgType, const char *msg)
62 {
63     messages << QLatin1String(msg);
64 }
65
66 void tst_qdeclarativeinstruction::dump()
67 {
68     QDeclarativeCompiledData *data = new QDeclarativeCompiledData(0);
69     {
70         QDeclarativeInstruction i;
71         i.setType(QDeclarativeInstruction::Init);
72         i.init.bindingsSize = 0;
73         i.init.parserStatusSize = 3;
74         i.init.contextCache = -1;
75         i.init.compiledBinding = -1;
76         data->addInstruction(i);
77     }
78
79     {
80         QDeclarativeCompiledData::TypeReference ref;
81         ref.className = "Test";
82         data->types << ref;
83
84         QDeclarativeInstruction i;
85         i.setType(QDeclarativeInstruction::CreateObject);
86         i.create.type = 0;
87         i.create.data = -1;
88         i.create.bindingBits = -1;
89         i.create.column = 10;
90         data->addInstruction(i);
91     }
92
93     {
94         data->primitives << "testId";
95
96         QDeclarativeInstruction i;
97         i.setType(QDeclarativeInstruction::SetId);
98         i.setId.value = data->primitives.count() - 1;
99         i.setId.index = 0;
100         data->addInstruction(i);
101     }
102
103     {
104         QDeclarativeInstruction i;
105         i.setType(QDeclarativeInstruction::SetDefault);
106         data->addInstruction(i);
107     }
108
109     {
110         QDeclarativeInstruction i;
111         i.setType(QDeclarativeInstruction::CreateComponent);
112         i.createComponent.count = 3;
113         i.createComponent.column = 4;
114         i.createComponent.endLine = 14;
115         i.createComponent.metaObject = 0;
116
117         data->addInstruction(i);
118     }
119
120     {
121         QDeclarativeInstruction i;
122         i.setType(QDeclarativeInstruction::StoreMetaObject);
123         i.storeMeta.data = 3;
124         i.storeMeta.aliasData = 6;
125         i.storeMeta.propertyCache = 7;
126
127         data->addInstruction(i);
128     }
129
130     {
131         QDeclarativeInstruction i;
132         i.setType(QDeclarativeInstruction::StoreFloat);
133         i.storeFloat.propertyIndex = 3;
134         i.storeFloat.value = 11.3;
135         data->addInstruction(i);
136     }
137
138     {
139         QDeclarativeInstruction i;
140         i.setType(QDeclarativeInstruction::StoreDouble);
141         i.storeDouble.propertyIndex = 4;
142         i.storeDouble.value = 14.8;
143         data->addInstruction(i);
144     }
145
146     {
147         QDeclarativeInstruction i;
148         i.setType(QDeclarativeInstruction::StoreInteger);
149         i.storeInteger.propertyIndex = 5;
150         i.storeInteger.value = 9;
151         data->addInstruction(i);
152     }
153
154     {
155         QDeclarativeInstruction i;
156         i.setType(QDeclarativeInstruction::StoreBool);
157         i.storeBool.propertyIndex = 6;
158         i.storeBool.value = true;
159
160         data->addInstruction(i);
161     }
162
163     {
164         data->primitives << "Test String";
165         QDeclarativeInstruction i;
166         i.setType(QDeclarativeInstruction::StoreString);
167         i.storeString.propertyIndex = 7;
168         i.storeString.value = data->primitives.count() - 1;
169         data->addInstruction(i);
170     }
171
172     {
173         data->urls << QUrl("http://www.nokia.com");
174         QDeclarativeInstruction i;
175         i.setType(QDeclarativeInstruction::StoreUrl);
176         i.storeUrl.propertyIndex = 8;
177         i.storeUrl.value = data->urls.count() - 1;
178         data->addInstruction(i);
179     }
180
181     {
182         QDeclarativeInstruction i;
183         i.setType(QDeclarativeInstruction::StoreColor);
184         i.storeColor.propertyIndex = 9;
185         i.storeColor.value = 0xFF00FF00;
186         data->addInstruction(i);
187     }
188
189     {
190         QDeclarativeInstruction i;
191         i.setType(QDeclarativeInstruction::StoreDate);
192         i.storeDate.propertyIndex = 10;
193         i.storeDate.value = 9;
194         data->addInstruction(i);
195     }
196
197     {
198         QDeclarativeInstruction i;
199         i.setType(QDeclarativeInstruction::StoreTime);
200         i.storeTime.propertyIndex = 11;
201         i.storeTime.valueIndex = 33;
202         data->addInstruction(i);
203     }
204
205     {
206         QDeclarativeInstruction i;
207         i.setType(QDeclarativeInstruction::StoreDateTime);
208         i.storeDateTime.propertyIndex = 12;
209         i.storeDateTime.valueIndex = 44;
210         data->addInstruction(i);
211     }
212
213     {
214         QDeclarativeInstruction i;
215         i.setType(QDeclarativeInstruction::StorePoint);
216         i.storeRealPair.propertyIndex = 13;
217         i.storeRealPair.valueIndex = 3;
218         data->addInstruction(i);
219     }
220
221     {
222         QDeclarativeInstruction i;
223         i.setType(QDeclarativeInstruction::StorePointF);
224         i.storeRealPair.propertyIndex = 14;
225         i.storeRealPair.valueIndex = 9;
226         data->addInstruction(i);
227     }
228
229     {
230         QDeclarativeInstruction i;
231         i.setType(QDeclarativeInstruction::StoreSize);
232         i.storeRealPair.propertyIndex = 15;
233         i.storeRealPair.valueIndex = 8;
234         data->addInstruction(i);
235     }
236
237     {
238         QDeclarativeInstruction i;
239         i.setType(QDeclarativeInstruction::StoreSizeF);
240         i.storeRealPair.propertyIndex = 16;
241         i.storeRealPair.valueIndex = 99;
242         data->addInstruction(i);
243     }
244
245     {
246         QDeclarativeInstruction i;
247         i.setType(QDeclarativeInstruction::StoreRect);
248         i.storeRect.propertyIndex = 17;
249         i.storeRect.valueIndex = 2;
250         data->addInstruction(i);
251     }
252
253     {
254         QDeclarativeInstruction i;
255         i.setType(QDeclarativeInstruction::StoreRectF);
256         i.storeRect.propertyIndex = 18;
257         i.storeRect.valueIndex = 19;
258         data->addInstruction(i);
259     }
260
261     {
262         QDeclarativeInstruction i;
263         i.setType(QDeclarativeInstruction::StoreVector3D);
264         i.storeVector3D.propertyIndex = 19;
265         i.storeVector3D.valueIndex = 9;
266         data->addInstruction(i);
267     }
268
269     {
270         data->primitives << "color(1, 1, 1, 1)";
271         QDeclarativeInstruction i;
272         i.setType(QDeclarativeInstruction::StoreVariant);
273         i.storeString.propertyIndex = 20;
274         i.storeString.value = data->primitives.count() - 1;
275
276         data->addInstruction(i);
277     }
278
279     {
280         QDeclarativeInstruction i;
281         i.setType(QDeclarativeInstruction::StoreObject);
282         i.storeObject.propertyIndex = 21;
283         data->addInstruction(i);
284     }
285
286     {
287         QDeclarativeInstruction i;
288         i.setType(QDeclarativeInstruction::StoreVariantObject);
289         i.storeObject.propertyIndex = 22;
290         data->addInstruction(i);
291     }
292
293     {
294         QDeclarativeInstruction i;
295         i.setType(QDeclarativeInstruction::StoreInterface);
296         i.storeObject.propertyIndex = 23;
297         data->addInstruction(i);
298     }
299
300     {
301         data->primitives << "console.log(1921)";
302
303         QDeclarativeInstruction i;
304         i.setType(QDeclarativeInstruction::StoreSignal);
305         i.storeSignal.signalIndex = 2;
306         i.storeSignal.value = data->primitives.count() - 1;
307         data->addInstruction(i);
308     }
309
310     {
311         QDeclarativeInstruction i;
312         i.setType(QDeclarativeInstruction::StoreScriptString);
313         i.storeScriptString.propertyIndex = 24;
314         i.storeScriptString.value = 3;
315         i.storeScriptString.scope = 1;
316         data->addInstruction(i);
317     }
318
319     {
320         data->datas << "mySignal";
321
322         QDeclarativeInstruction i;
323         i.setType(QDeclarativeInstruction::AssignSignalObject);
324         i.assignSignalObject.signal = 0;
325         data->addInstruction(i);
326     }
327
328     {
329         QDeclarativeInstruction i;
330         i.setType(QDeclarativeInstruction::AssignCustomType);
331         i.assignCustomType.propertyIndex = 25;
332         i.assignCustomType.valueIndex = 4;
333         data->addInstruction(i);
334     }
335
336     {
337         QDeclarativeInstruction i;
338         i.setType(QDeclarativeInstruction::StoreBinding);
339         i.assignBinding.property = 26;
340         i.assignBinding.value = 3;
341         i.assignBinding.context = 2;
342         i.assignBinding.owner = 0;
343         data->addInstruction(i);
344     }
345
346     {
347         QDeclarativeInstruction i;
348         i.setType(QDeclarativeInstruction::StoreCompiledBinding);
349         i.assignBinding.property = 27;
350         i.assignBinding.value = 2;
351         i.assignBinding.context = 4;
352         i.assignBinding.owner = 0;
353         data->addInstruction(i);
354     }
355
356     {
357         QDeclarativeInstruction i;
358         i.setType(QDeclarativeInstruction::StoreValueSource);
359         i.assignValueSource.property = 29;
360         i.assignValueSource.owner = 1;
361         i.assignValueSource.castValue = 4;
362         data->addInstruction(i);
363     }
364
365     {
366         QDeclarativeInstruction i;
367         i.setType(QDeclarativeInstruction::StoreValueInterceptor);
368         i.assignValueInterceptor.property = 30;
369         i.assignValueInterceptor.owner = 2;
370         i.assignValueInterceptor.castValue = -4;
371         data->addInstruction(i);
372     }
373
374     {
375         QDeclarativeInstruction i;
376         i.setType(QDeclarativeInstruction::BeginObject);
377         i.begin.castValue = 4;
378         data->addInstruction(i);
379     }
380
381     {
382         QDeclarativeInstruction i;
383         i.setType(QDeclarativeInstruction::StoreObjectQList);
384         data->addInstruction(i);
385     }
386
387     {
388         QDeclarativeInstruction i;
389         i.setType(QDeclarativeInstruction::AssignObjectList);
390         data->addInstruction(i);
391     }
392
393     {
394         QDeclarativeInstruction i;
395         i.setType(QDeclarativeInstruction::FetchAttached);
396         i.fetchAttached.id = 23;
397         data->addInstruction(i);
398     }
399
400     {
401         QDeclarativeInstruction i;
402         i.setType(QDeclarativeInstruction::FetchQList);
403         i.fetch.property = 32;
404         data->addInstruction(i);
405     }
406
407     {
408         QDeclarativeInstruction i;
409         i.setType(QDeclarativeInstruction::FetchObject);
410         i.fetch.property = 33;
411         data->addInstruction(i);
412     }
413
414     {
415         QDeclarativeInstruction i;
416         i.setType(QDeclarativeInstruction::FetchValueType);
417         i.fetchValue.property = 34;
418         i.fetchValue.type = 6;
419         i.fetchValue.bindingSkipList = 7;
420         data->addInstruction(i);
421     }
422
423     {
424         QDeclarativeInstruction i;
425         i.setType(QDeclarativeInstruction::PopFetchedObject);
426         data->addInstruction(i);
427     }
428
429     {
430         QDeclarativeInstruction i;
431         i.setType(QDeclarativeInstruction::PopQList);
432         data->addInstruction(i);
433     }
434
435     {
436         QDeclarativeInstruction i;
437         i.setType(QDeclarativeInstruction::PopValueType);
438         i.fetchValue.property = 35;
439         i.fetchValue.type = 8;
440         data->addInstruction(i);
441     }
442
443     {
444         QDeclarativeInstruction i;
445         i.setType(QDeclarativeInstruction::Defer);
446         i.defer.deferCount = 7;
447         data->addInstruction(i);
448     }
449
450     {
451         QDeclarativeInstruction i;
452         i.setType(QDeclarativeInstruction::Defer);
453         i.defer.deferCount = 7;
454         data->addInstruction(i);
455     }
456
457     {
458         QDeclarativeInstruction i;
459         i.setType(QDeclarativeInstruction::StoreImportedScript);
460         i.storeScript.value = 2;
461         data->addInstruction(i);
462     }
463
464     {
465         QDeclarativeInstruction i;
466         i.setType(QDeclarativeInstruction::StoreVariantInteger);
467         i.storeInteger.value = 11;
468         i.storeInteger.propertyIndex = 32;
469         data->addInstruction(i);
470     }
471
472     {
473         QDeclarativeInstruction i;
474         i.setType(QDeclarativeInstruction::StoreVariantDouble);
475         i.storeDouble.value = 33.7;
476         i.storeDouble.propertyIndex = 19;
477         data->addInstruction(i);
478     }
479
480     {
481         QDeclarativeInstruction i;
482         i.setType(QDeclarativeInstruction::Done);
483         data->addInstruction(i);
484     }
485
486     QStringList expect;
487     expect 
488         << "Index\tOperation\t\tData1\tData2\tData3\tComments"
489         << "-------------------------------------------------------------------------------"
490         << "0\t\tINIT\t\t\t0\t3\t-1\t-1"
491         << "1\t\tCREATE\t\t\t0\t-1\t\t\"Test\""
492         << "2\t\tSETID\t\t\t0\t\t\t\"testId\""
493         << "3\t\tSET_DEFAULT"
494         << "4\t\tCREATE_COMPONENT\t3"
495         << "5\t\tSTORE_META\t\t3"
496         << "6\t\tSTORE_FLOAT\t\t3\t11.3"
497         << "7\t\tSTORE_DOUBLE\t\t4\t14.8"
498         << "8\t\tSTORE_INTEGER\t\t5\t9"
499         << "9\t\tSTORE_BOOL\t\t6\ttrue"
500         << "10\t\tSTORE_STRING\t\t7\t1\t\t\"Test String\""
501         << "11\t\tSTORE_URL\t\t8\t0\t\tQUrl(\"http://www.nokia.com\") "
502         << "12\t\tSTORE_COLOR\t\t9\t\t\t\"ff00ff00\""
503         << "13\t\tSTORE_DATE\t\t10\t9"
504         << "14\t\tSTORE_TIME\t\t11\t33"
505         << "15\t\tSTORE_DATETIME\t\t12\t44"
506         << "16\t\tSTORE_POINT\t\t13\t3"
507         << "17\t\tSTORE_POINTF\t\t14\t9"
508         << "18\t\tSTORE_SIZE\t\t15\t8"
509         << "19\t\tSTORE_SIZEF\t\t16\t99"
510         << "20\t\tSTORE_RECT\t\t17\t2"
511         << "21\t\tSTORE_RECTF\t\t18\t19"
512         << "22\t\tSTORE_VECTOR3D\t\t19\t9"
513         << "23\t\tSTORE_VARIANT\t\t20\t2\t\t\"color(1, 1, 1, 1)\""
514         << "24\t\tSTORE_OBJECT\t\t21"
515         << "25\t\tSTORE_VARIANT_OBJECT\t22"
516         << "26\t\tSTORE_INTERFACE\t\t23"
517         << "27\t\tSTORE_SIGNAL\t\t2\t3\t\t\"console.log(1921)\""
518         << "28\t\tSTORE_SCRIPT_STRING\t24\t3\t1"
519         << "29\t\tASSIGN_SIGNAL_OBJECT\t0\t\t\t\"mySignal\""
520         << "30\t\tASSIGN_CUSTOMTYPE\t25\t4"
521         << "31\t\tSTORE_BINDING\t26\t3\t2"
522         << "32\t\tSTORE_COMPILED_BINDING\t27\t2\t4"
523         << "33\t\tSTORE_VALUE_SOURCE\t29\t4"
524         << "34\t\tSTORE_VALUE_INTERCEPTOR\t30\t-4"
525         << "35\t\tBEGIN\t\t\t4"
526         << "36\t\tSTORE_OBJECT_QLIST"
527         << "37\t\tASSIGN_OBJECT_LIST"
528         << "38\t\tFETCH_ATTACHED\t\t23"
529         << "39\t\tFETCH_QLIST\t\t32"
530         << "40\t\tFETCH\t\t\t33"
531         << "41\t\tFETCH_VALUE\t\t34\t6\t7"
532         << "42\t\tPOP"
533         << "43\t\tPOP_QLIST"
534         << "44\t\tPOP_VALUE\t\t35\t8"
535         << "45\t\tDEFER\t\t\t7"
536         << "46\t\tDEFER\t\t\t7"
537         << "47\t\tSTORE_IMPORTED_SCRIPT\t2"
538         << "48\t\tSTORE_VARIANT_INTEGER\t\t32\t11"
539         << "49\t\tSTORE_VARIANT_DOUBLE\t\t19\t33.7"
540         << "50\t\tDONE"
541         << "-------------------------------------------------------------------------------";
542
543     messages = QStringList();
544     QtMsgHandler old = qInstallMsgHandler(msgHandler);
545     data->dumpInstructions();
546     qInstallMsgHandler(old);
547
548     QCOMPARE(messages.count(), expect.count());
549     for (int ii = 0; ii < messages.count(); ++ii) {
550         QCOMPARE(messages.at(ii), expect.at(ii));
551     }
552
553     data->release();
554 }
555
556 QTEST_MAIN(tst_qdeclarativeinstruction)
557
558 #include "tst_qdeclarativeinstruction.moc"