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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite 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 http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtScript/qscriptcontext.h>
#include <QtScript/qscriptengine.h>
//TESTED_CLASS=
//TESTED_FILES=
Q_DECLARE_METATYPE(QScriptValueList)
class tst_QScriptContext : public QObject
{
Q_OBJECT
public:
tst_QScriptContext();
virtual ~tst_QScriptContext();
private slots:
void callee();
void arguments();
void thisObject();
void returnValue();
void throwError();
void throwValue();
void evaluateInFunction();
void pushAndPopContext();
void lineNumber();
void backtrace();
void scopeChain();
void pushAndPopScope();
void getSetActivationObject();
void inheritActivationAndThisObject();
void toString();
void calledAsConstructor();
void argumentsObjectInNative();
void jsActivationObject();
};
tst_QScriptContext::tst_QScriptContext()
{
}
tst_QScriptContext::~tst_QScriptContext()
{
}
static QScriptValue get_callee(QScriptContext *ctx, QScriptEngine *)
{
return ctx->callee();
}
static QScriptValue store_callee_and_return_primitive(QScriptContext *ctx, QScriptEngine *eng)
{
ctx->thisObject().setProperty("callee", ctx->callee());
return QScriptValue(eng, 123);
}
void tst_QScriptContext::callee()
{
QScriptEngine eng;
{
QScriptValue fun = eng.newFunction(get_callee);
fun.setProperty("foo", QScriptValue(&eng, "bar"));
eng.globalObject().setProperty("get_callee", fun);
QScriptValue result = eng.evaluate("get_callee()");
QCOMPARE(result.isFunction(), true);
QCOMPARE(result.property("foo").toString(), QString("bar"));
}
// callee when toPrimitive() is called internally
{
QScriptValue fun = eng.newFunction(store_callee_and_return_primitive);
QScriptValue obj = eng.newObject();
obj.setProperty("toString", fun);
QVERIFY(!obj.property("callee").isValid());
(void)obj.toString();
QVERIFY(obj.property("callee").isFunction());
QVERIFY(obj.property("callee").strictlyEquals(fun));
obj.setProperty("callee", QScriptValue());
QVERIFY(!obj.property("callee").isValid());
obj.setProperty("valueOf", fun);
(void)obj.toNumber();
QVERIFY(obj.property("callee").isFunction());
QVERIFY(obj.property("callee").strictlyEquals(fun));
}
}
static QScriptValue get_arguments(QScriptContext *ctx, QScriptEngine *eng)
{
QScriptValue array = eng->newArray();
for (int i = 0; i < ctx->argumentCount(); ++i)
array.setProperty(QString::number(i), ctx->argument(i));
return array;
}
static QScriptValue get_argumentsObject(QScriptContext *ctx, QScriptEngine *)
{
return ctx->argumentsObject();
}
void tst_QScriptContext::arguments()
{
QScriptEngine eng;
{
QScriptValue args = eng.currentContext()->argumentsObject();
QVERIFY(args.isObject());
QCOMPARE(args.property("length").toInt32(), 0);
}
{
QScriptValue fun = eng.newFunction(get_arguments);
eng.globalObject().setProperty("get_arguments", fun);
}
for (int x = 0; x < 2; ++x) {
QString prefix;
if (x == 0)
prefix = "";
else
prefix = "new ";
{
QScriptValue result = eng.evaluate(prefix+"get_arguments()");
QCOMPARE(result.isArray(), true);
QCOMPARE(result.property("length").toUInt32(), quint32(0));
}
{
QScriptValue result = eng.evaluate(prefix+"get_arguments(123)");
QCOMPARE(result.isArray(), true);
QCOMPARE(result.property("length").toUInt32(), quint32(1));
QCOMPARE(result.property("0").isNumber(), true);
QCOMPARE(result.property("0").toNumber(), 123.0);
}
{
QScriptValue result = eng.evaluate(prefix+"get_arguments(\"ciao\", null, true, undefined)");
QCOMPARE(result.isArray(), true);
QCOMPARE(result.property("length").toUInt32(), quint32(4));
QCOMPARE(result.property("0").isString(), true);
QCOMPARE(result.property("0").toString(), QString("ciao"));
QCOMPARE(result.property("1").isNull(), true);
QCOMPARE(result.property("2").isBoolean(), true);
QCOMPARE(result.property("2").toBoolean(), true);
QCOMPARE(result.property("3").isUndefined(), true);
}
{
QScriptValue fun = eng.newFunction(get_argumentsObject);
eng.globalObject().setProperty("get_argumentsObject", fun);
}
{
QScriptValue fun = eng.evaluate("get_argumentsObject");
QCOMPARE(fun.isFunction(), true);
QScriptValue result = eng.evaluate(prefix+"get_argumentsObject()");
QCOMPARE(result.isArray(), false);
QVERIFY(result.isObject());
QCOMPARE(result.property("length").toUInt32(), quint32(0));
QCOMPARE(result.propertyFlags("length"), QScriptValue::SkipInEnumeration);
QCOMPARE(result.property("callee").strictlyEquals(fun), true);
QCOMPARE(result.propertyFlags("callee"), QScriptValue::SkipInEnumeration);
QScriptValue replacedCallee(&eng, 123);
result.setProperty("callee", replacedCallee);
QVERIFY(result.property("callee").equals(replacedCallee));
QScriptValue replacedLength(&eng, 456);
result.setProperty("length", replacedLength);
QVERIFY(result.property("length").equals(replacedLength));
result.setProperty("callee", QScriptValue());
QVERIFY(!result.property("callee").isValid());
result.setProperty("length", QScriptValue());
QVERIFY(!result.property("length").isValid());
}
{
QScriptValue result = eng.evaluate(prefix+"get_argumentsObject(123)");
eng.evaluate("function nestedArg(x,y,z) { var w = get_argumentsObject('ABC' , x+y+z); return w; }");
QScriptValue result2 = eng.evaluate("nestedArg(1, 'a', 2)");
QCOMPARE(result.isArray(), false);
QVERIFY(result.isObject());
QCOMPARE(result.property("length").toUInt32(), quint32(1));
QCOMPARE(result.property("0").isNumber(), true);
QCOMPARE(result.property("0").toNumber(), 123.0);
QVERIFY(result2.isObject());
QCOMPARE(result2.property("length").toUInt32(), quint32(2));
QCOMPARE(result2.property("0").toString(), QString::fromLatin1("ABC"));
QCOMPARE(result2.property("1").toString(), QString::fromLatin1("1a2"));
}
{
QScriptValue result = eng.evaluate(prefix+"get_argumentsObject(\"ciao\", null, true, undefined)");
QCOMPARE(result.isArray(), false);
QCOMPARE(result.property("length").toUInt32(), quint32(4));
QCOMPARE(result.property("0").isString(), true);
QCOMPARE(result.property("0").toString(), QString("ciao"));
QCOMPARE(result.property("1").isNull(), true);
QCOMPARE(result.property("2").isBoolean(), true);
QCOMPARE(result.property("2").toBoolean(), true);
QCOMPARE(result.property("3").isUndefined(), true);
}
// arguments object returned from script
{
QScriptValue result = eng.evaluate("(function() { return arguments; })(123)");
QCOMPARE(result.isArray(), false);
QVERIFY(result.isObject());
QCOMPARE(result.property("length").toUInt32(), quint32(1));
QCOMPARE(result.property("0").isNumber(), true);
QCOMPARE(result.property("0").toNumber(), 123.0);
}
{
QScriptValue result = eng.evaluate("(function() { return arguments; })('ciao', null, true, undefined)");
QCOMPARE(result.isArray(), false);
QCOMPARE(result.property("length").toUInt32(), quint32(4));
QCOMPARE(result.property("0").isString(), true);
QCOMPARE(result.property("0").toString(), QString("ciao"));
QCOMPARE(result.property("1").isNull(), true);
QCOMPARE(result.property("2").isBoolean(), true);
QCOMPARE(result.property("2").toBoolean(), true);
QCOMPARE(result.property("3").isUndefined(), true);
}
}
}
static QScriptValue get_thisObject(QScriptContext *ctx, QScriptEngine *)
{
return ctx->thisObject();
}
void tst_QScriptContext::thisObject()
{
QScriptEngine eng;
QScriptValue fun = eng.newFunction(get_thisObject);
eng.globalObject().setProperty("get_thisObject", fun);
{
QScriptValue result = eng.evaluate("get_thisObject()");
QCOMPARE(result.isObject(), true);
QCOMPARE(result.toString(), QString("[object global]"));
}
{
QScriptValue result = eng.evaluate("get_thisObject.apply(new Number(123))");
QCOMPARE(result.isObject(), true);
QCOMPARE(result.toNumber(), 123.0);
}
{
QScriptValue obj = eng.newObject();
eng.currentContext()->setThisObject(obj);
QVERIFY(eng.currentContext()->thisObject().equals(obj));
eng.currentContext()->setThisObject(QScriptValue());
QVERIFY(eng.currentContext()->thisObject().equals(obj));
QScriptEngine eng2;
QScriptValue obj2 = eng2.newObject();
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::setThisObject() failed: cannot set an object created in a different engine");
eng.currentContext()->setThisObject(obj2);
}
}
void tst_QScriptContext::returnValue()
{
QSKIP("Internal function not implemented in JSC-based back-end", SkipAll);
QScriptEngine eng;
eng.evaluate("123");
QCOMPARE(eng.currentContext()->returnValue().toNumber(), 123.0);
eng.evaluate("\"ciao\"");
QCOMPARE(eng.currentContext()->returnValue().toString(), QString("ciao"));
}
static QScriptValue throw_Error(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::UnknownError, "foo");
}
static QScriptValue throw_TypeError(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::TypeError, "foo");
}
static QScriptValue throw_ReferenceError(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::ReferenceError, "foo");
}
static QScriptValue throw_SyntaxError(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::SyntaxError, "foo");
}
static QScriptValue throw_RangeError(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::RangeError, "foo");
}
static QScriptValue throw_URIError(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError(QScriptContext::URIError, "foo");
}
static QScriptValue throw_ErrorAndReturnUndefined(QScriptContext *ctx, QScriptEngine *eng)
{
ctx->throwError(QScriptContext::UnknownError, "foo");
return eng->undefinedValue();
}
void tst_QScriptContext::throwError()
{
QScriptEngine eng;
{
QScriptValue fun = eng.newFunction(throw_Error);
eng.globalObject().setProperty("throw_Error", fun);
QScriptValue result = eng.evaluate("throw_Error()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("Error: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_TypeError);
eng.globalObject().setProperty("throw_TypeError", fun);
QScriptValue result = eng.evaluate("throw_TypeError()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("TypeError: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_ReferenceError);
eng.globalObject().setProperty("throw_ReferenceError", fun);
QScriptValue result = eng.evaluate("throw_ReferenceError()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("ReferenceError: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_SyntaxError);
eng.globalObject().setProperty("throw_SyntaxError", fun);
QScriptValue result = eng.evaluate("throw_SyntaxError()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("SyntaxError: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_RangeError);
eng.globalObject().setProperty("throw_RangeError", fun);
QScriptValue result = eng.evaluate("throw_RangeError()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("RangeError: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_URIError);
eng.globalObject().setProperty("throw_URIError", fun);
QScriptValue result = eng.evaluate("throw_URIError()");
QCOMPARE(eng.hasUncaughtException(), true);
QCOMPARE(result.isError(), true);
QCOMPARE(result.toString(), QString("URIError: foo"));
}
{
QScriptValue fun = eng.newFunction(throw_ErrorAndReturnUndefined);
eng.globalObject().setProperty("throw_ErrorAndReturnUndefined", fun);
QScriptValue result = eng.evaluate("throw_ErrorAndReturnUndefined()");
QVERIFY(eng.hasUncaughtException());
QVERIFY(result.isError());
QCOMPARE(result.toString(), QString("Error: foo"));
}
}
static QScriptValue throw_value(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwValue(ctx->argument(0));
}
void tst_QScriptContext::throwValue()
{
QScriptEngine eng;
QScriptValue fun = eng.newFunction(throw_value);
eng.globalObject().setProperty("throw_value", fun);
{
QScriptValue result = eng.evaluate("throw_value(123)");
QCOMPARE(result.isError(), false);
QCOMPARE(result.toNumber(), 123.0);
QCOMPARE(eng.hasUncaughtException(), true);
}
}
static QScriptValue evaluate(QScriptContext *, QScriptEngine *eng)
{
return eng->evaluate("a = 123; a");
// return eng->evaluate("a");
}
void tst_QScriptContext::evaluateInFunction()
{
QScriptEngine eng;
QScriptValue fun = eng.newFunction(evaluate);
eng.globalObject().setProperty("evaluate", fun);
QScriptValue result = eng.evaluate("evaluate()");
QCOMPARE(result.isError(), false);
QCOMPARE(result.isNumber(), true);
QCOMPARE(result.toNumber(), 123.0);
QCOMPARE(eng.hasUncaughtException(), false);
QCOMPARE(eng.evaluate("a").toNumber(), 123.0);
}
void tst_QScriptContext::pushAndPopContext()
{
QScriptEngine eng;
QScriptContext *topLevel = eng.currentContext();
QCOMPARE(topLevel->engine(), &eng);
QScriptContext *ctx = eng.pushContext();
QVERIFY(ctx != 0);
QCOMPARE(ctx->parentContext(), topLevel);
QCOMPARE(eng.currentContext(), ctx);
QCOMPARE(ctx->engine(), &eng);
QCOMPARE(ctx->state(), QScriptContext::NormalState);
QCOMPARE(ctx->isCalledAsConstructor(), false);
QCOMPARE(ctx->argumentCount(), 0);
QCOMPARE(ctx->argument(0).isUndefined(), true);
QVERIFY(!ctx->argument(-1).isValid());
QCOMPARE(ctx->argumentsObject().isObject(), true);
QCOMPARE(ctx->activationObject().isObject(), true);
QCOMPARE(ctx->callee().isValid(), false);
QCOMPARE(ctx->thisObject().strictlyEquals(eng.globalObject()), true);
QScriptContext *ctx2 = eng.pushContext();
QCOMPARE(ctx2->parentContext(), ctx);
QCOMPARE(eng.currentContext(), ctx2);
eng.popContext();
QCOMPARE(eng.currentContext(), ctx);
eng.popContext();
QCOMPARE(eng.currentContext(), topLevel);
// popping the top-level context is not allowed
QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::popContext() doesn't match with pushContext()");
eng.popContext();
QCOMPARE(eng.currentContext(), topLevel);
{
QScriptContext *ctx3 = eng.pushContext();
ctx3->activationObject().setProperty("foo", QScriptValue(&eng, 123));
QVERIFY(eng.evaluate("foo").strictlyEquals(QScriptValue(&eng, 123)));
eng.evaluate("var bar = 'ciao'");
QVERIFY(ctx3->activationObject().property("bar", QScriptValue::ResolveLocal).strictlyEquals(QScriptValue(&eng, "ciao")));
eng.popContext();
}
{
QScriptContext *ctx4 = eng.pushContext();
QScriptValue obj = eng.newObject();
obj.setProperty("prop", QScriptValue(&eng, 456));
ctx4->setThisObject(obj);
QScriptValue ret = eng.evaluate("var tmp = this.prop; tmp + 1");
QCOMPARE(eng.currentContext(), ctx4);
QVERIFY(ret.strictlyEquals(QScriptValue(&eng, 457)));
eng.popContext();
}
// throwing an exception
{
QScriptContext *ctx5 = eng.pushContext();
QScriptValue ret = eng.evaluate("throw new Error('oops')");
QVERIFY(ret.isError());
QVERIFY(eng.hasUncaughtException());
QCOMPARE(eng.currentContext(), ctx5);
eng.popContext();
}
}
void tst_QScriptContext::lineNumber()
{
QScriptEngine eng;
QScriptValue result = eng.evaluate("try { eval(\"foo = 123;\\n this[is{a{syntax|error@#$%@#% \"); } catch (e) { e.lineNumber; }", "foo.qs", 123);
QVERIFY(!eng.hasUncaughtException());
QVERIFY(result.isNumber());
QCOMPARE(result.toInt32(), 2);
result = eng.evaluate("foo = 123;\n bar = 42\n0 = 0");
QVERIFY(eng.hasUncaughtException());
QCOMPARE(eng.uncaughtExceptionLineNumber(), 3);
QCOMPARE(result.property("lineNumber").toInt32(), 3);
}
static QScriptValue getBacktrace(QScriptContext *ctx, QScriptEngine *eng)
{
return qScriptValueFromValue(eng, ctx->backtrace());
}
void tst_QScriptContext::backtrace()
{
QScriptEngine eng;
eng.globalObject().setProperty("bt", eng.newFunction(getBacktrace));
QString fileName = "testfile";
QStringList expected;
expected << "<native>(123)@:-1"
<< "foo(hello,[object Object])@testfile:2"
<< "<global>()@testfile:4";
QScriptValue ret = eng.evaluate(
"function foo() {\n"
" return bt(123);\n"
"}\n"
"foo('hello', { })", fileName);
QVERIFY(ret.isArray());
QStringList slist = qscriptvalue_cast<QStringList>(ret);
QEXPECT_FAIL("", "Backtrace is not correct", Continue);
QCOMPARE(slist, expected);
}
static QScriptValue getScopeChain(QScriptContext *ctx, QScriptEngine *eng)
{
return qScriptValueFromValue(eng, ctx->parentContext()->scopeChain());
}
void tst_QScriptContext::scopeChain()
{
QScriptEngine eng;
{
QScriptValueList ret = eng.currentContext()->scopeChain();
QCOMPARE(ret.size(), 1);
QVERIFY(ret.at(0).strictlyEquals(eng.globalObject()));
}
{
eng.globalObject().setProperty("getScopeChain", eng.newFunction(getScopeChain));
QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("getScopeChain()"));
QCOMPARE(ret.size(), 1);
QVERIFY(ret.at(0).strictlyEquals(eng.globalObject()));
}
{
eng.evaluate("function foo() { function bar() { return getScopeChain(); } return bar() }");
QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("foo()"));
QEXPECT_FAIL("", "Number of items in returned scope chain is incorrect", Abort);
QCOMPARE(ret.size(), 3);
QVERIFY(ret.at(2).strictlyEquals(eng.globalObject()));
QCOMPARE(ret.at(1).toString(), QString::fromLatin1("activation"));
QVERIFY(ret.at(1).property("arguments").isObject());
QCOMPARE(ret.at(0).toString(), QString::fromLatin1("activation"));
QVERIFY(ret.at(0).property("arguments").isObject());
}
{
QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("o = { x: 123 }; with(o) getScopeChain();"));
QCOMPARE(ret.size(), 2);
QVERIFY(ret.at(1).strictlyEquals(eng.globalObject()));
QVERIFY(ret.at(0).isObject());
QCOMPARE(ret.at(0).property("x").toInt32(), 123);
}
{
QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(
eng.evaluate("o1 = { x: 123}; o2 = { y: 456 }; with(o1) { with(o2) { getScopeChain(); } }"));
QCOMPARE(ret.size(), 3);
QVERIFY(ret.at(2).strictlyEquals(eng.globalObject()));
QVERIFY(ret.at(1).isObject());
QCOMPARE(ret.at(1).property("x").toInt32(), 123);
QVERIFY(ret.at(0).isObject());
QCOMPARE(ret.at(0).property("y").toInt32(), 456);
}
}
void tst_QScriptContext::pushAndPopScope()
{
QScriptEngine eng;
QScriptContext *ctx = eng.currentContext();
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
QScriptValue obj = eng.newObject();
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(obj));
QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));
QVERIFY(ctx->popScope().strictlyEquals(obj));
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
{
QScriptValue ret = eng.evaluate("x");
QVERIFY(ret.isError());
eng.clearExceptions();
}
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
// task 236685
QScriptValue qobj = eng.newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::AutoCreateDynamicProperties);
ctx->pushScope(qobj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(qobj));
QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));
{
QScriptValue ret = eng.evaluate("print");
QVERIFY(ret.isFunction());
}
ctx->popScope();
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(obj));
obj.setProperty("x", 123);
{
QScriptValue ret = eng.evaluate("x");
QVERIFY(ret.isNumber());
QCOMPARE(ret.toInt32(), 123);
}
QVERIFY(ctx->popScope().strictlyEquals(obj));
QCOMPARE(ctx->scopeChain().size(), 1);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
ctx->pushScope(QScriptValue());
QCOMPARE(ctx->scopeChain().size(), 1);
qWarning("Popping the top-level scope causes crash");
#if 0
QVERIFY(ctx->popScope().strictlyEquals(eng.globalObject()));
QVERIFY(ctx->scopeChain().isEmpty());
#endif
ctx->pushScope(obj);
QCOMPARE(ctx->scopeChain().size(), 2);
QVERIFY(ctx->scopeChain().at(0).strictlyEquals(obj));
QVERIFY(!obj.property("foo").isValid());
eng.evaluate("function foo() {}");
// function declarations should always end up in the activation object (ECMA-262, chapter 13)
QVERIFY(!obj.property("foo").isValid());
QVERIFY(ctx->activationObject().property("foo").isFunction());
QScriptEngine eng2;
QScriptValue obj2 = eng2.newObject();
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::pushScope() failed: cannot push an object created in a different engine");
ctx->pushScope(obj2);
QVERIFY(ctx->popScope().strictlyEquals(obj));
QEXPECT_FAIL("", "This should work once the above crash issue has been fixed", Continue);
QVERIFY(ctx->scopeChain().isEmpty());
QSKIP("Crashes", SkipAll);
QVERIFY(!ctx->popScope().isValid());
}
static QScriptValue get_activationObject(QScriptContext *ctx, QScriptEngine *)
{
return ctx->activationObject();
}
void tst_QScriptContext::getSetActivationObject()
{
QScriptEngine eng;
QScriptContext *ctx = eng.currentContext();
QVERIFY(ctx->activationObject().equals(eng.globalObject()));
ctx->setActivationObject(QScriptValue());
QVERIFY(ctx->activationObject().equals(eng.globalObject()));
QCOMPARE(ctx->engine(), &eng);
QScriptValue obj = eng.newObject();
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::setActivationObject() failed: not an activation object");
ctx->setActivationObject(obj);
QEXPECT_FAIL("", "Normal object cannot be set as activation object", Continue);
QVERIFY(ctx->activationObject().equals(obj));
{
QScriptEngine eng2;
QScriptValue obj2 = eng2.newObject();
QTest::ignoreMessage(QtWarningMsg, "QScriptContext::setActivationObject() failed: cannot set an object created in a different engine");
QScriptValue was = ctx->activationObject();
ctx->setActivationObject(obj2);
QVERIFY(ctx->activationObject().equals(was));
}
ctx->setActivationObject(eng.globalObject());
QVERIFY(ctx->activationObject().equals(eng.globalObject()));
QScriptValue fun = eng.newFunction(get_activationObject);
eng.globalObject().setProperty("get_activationObject", fun);
{
QScriptValue ret = eng.evaluate("get_activationObject(1, 2, 3)");
QVERIFY(ret.isObject());
QScriptValue arguments = ret.property("arguments");
QEXPECT_FAIL("", "Getting arguments property of activation object doesn't work", Abort);
QVERIFY(arguments.isObject());
QCOMPARE(arguments.property("length").toInt32(), 3);
QCOMPARE(arguments.property("0").toInt32(), 1);
QCOMPARE(arguments.property("1").toInt32(), 1);
QCOMPARE(arguments.property("2").toInt32(), 1);
}
}
static QScriptValue myEval(QScriptContext *ctx, QScriptEngine *eng)
{
QString code = ctx->argument(0).toString();
ctx->setActivationObject(ctx->parentContext()->activationObject());
ctx->setThisObject(ctx->parentContext()->thisObject());
return eng->evaluate(code);
}
void tst_QScriptContext::inheritActivationAndThisObject()
{
QScriptEngine eng;
eng.globalObject().setProperty("myEval", eng.newFunction(myEval));
{
QScriptValue ret = eng.evaluate("var a = 123; myEval('a')");
QVERIFY(ret.isNumber());
QCOMPARE(ret.toInt32(), 123);
}
{
QScriptValue ret = eng.evaluate("(function() { return myEval('this'); }).call(Number)");
QVERIFY(ret.isFunction());
QVERIFY(ret.equals(eng.globalObject().property("Number")));
}
{
QScriptValue ret = eng.evaluate("(function(a) { return myEval('a'); })(123)");
QVERIFY(ret.isNumber());
QCOMPARE(ret.toInt32(), 123);
}
}
static QScriptValue parentContextToString(QScriptContext *ctx, QScriptEngine *)
{
return ctx->parentContext()->toString();
}
void tst_QScriptContext::toString()
{
QScriptEngine eng;
eng.globalObject().setProperty("parentContextToString", eng.newFunction(parentContextToString));
QScriptValue ret = eng.evaluate("function foo(first, second, third) {\n"
" return parentContextToString();\n"
"}; foo(1, 2, 3)", "script.qs");
QVERIFY(ret.isString());
QEXPECT_FAIL("", "", Continue);
QCOMPARE(ret.toString(), QString::fromLatin1("foo (first=1, second=2, third=3) at script.qs:2"));
}
static QScriptValue storeCalledAsConstructor(QScriptContext *ctx, QScriptEngine *eng)
{
ctx->callee().setProperty("calledAsConstructor", ctx->isCalledAsConstructor());
return eng->undefinedValue();
}
static QScriptValue storeCalledAsConstructorV2(QScriptContext *ctx, QScriptEngine *eng, void *)
{
ctx->callee().setProperty("calledAsConstructor", ctx->isCalledAsConstructor());
return eng->undefinedValue();
}
static QScriptValue storeCalledAsConstructorV3(QScriptContext *ctx, QScriptEngine *eng)
{
ctx->callee().setProperty("calledAsConstructor", ctx->parentContext()->isCalledAsConstructor());
return eng->undefinedValue();
}
void tst_QScriptContext::calledAsConstructor()
{
QScriptEngine eng;
QScriptValue fun1 = eng.newFunction(storeCalledAsConstructor);
{
fun1.call();
QVERIFY(!fun1.property("calledAsConstructor").toBool());
fun1.construct();
QVERIFY(fun1.property("calledAsConstructor").toBool());
}
{
QScriptValue fun = eng.newFunction(storeCalledAsConstructorV2, (void*)0);
fun.call();
QVERIFY(!fun.property("calledAsConstructor").toBool());
fun.construct();
QVERIFY(fun.property("calledAsConstructor").toBool());
}
{
eng.globalObject().setProperty("fun1", fun1);
eng.evaluate("fun1();");
QVERIFY(!fun1.property("calledAsConstructor").toBool());
eng.evaluate("new fun1();");
QVERIFY(fun1.property("calledAsConstructor").toBool());
}
{
QScriptValue fun3 = eng.newFunction(storeCalledAsConstructorV3);
eng.globalObject().setProperty("fun3", fun3);
eng.evaluate("function test() { fun3() }");
eng.evaluate("test();");
QVERIFY(!fun3.property("calledAsConstructor").toBool());
eng.evaluate("new test();");
QVERIFY(fun3.property("calledAsConstructor").toBool());
}
}
static QScriptValue argumentsObjectInNative_test1(QScriptContext *ctx, QScriptEngine *eng)
{
#define VERIFY(statement) \
do {\
if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
return QString::fromLatin1("Failed " #statement);\
} while (0)
QScriptValue obj = ctx->argumentsObject();
VERIFY(obj.isObject());
VERIFY(obj.property(0).toUInt32() == 123);
VERIFY(obj.property(1).toString() == QString::fromLatin1("456"));
obj.setProperty(0, "abc");
VERIFY(eng->evaluate("arguments[0]").toString() == QString::fromLatin1("abc") );
return QString::fromLatin1("success");
#undef VERIFY
}
void tst_QScriptContext::argumentsObjectInNative()
{
{
QScriptEngine eng;
QScriptValue fun = eng.newFunction(argumentsObjectInNative_test1);
QScriptValueList args;
args << QScriptValue(&eng, 123.0);
args << QScriptValue(&eng, QString::fromLatin1("456"));
QScriptValue result = fun.call(eng.undefinedValue(), args);
QVERIFY(!eng.hasUncaughtException());
QCOMPARE(result.toString(), QString::fromLatin1("success"));
}
{
QScriptEngine eng;
QScriptValue fun = eng.newFunction(argumentsObjectInNative_test1);
eng.globalObject().setProperty("func", fun);
QScriptValue result = eng.evaluate("func(123.0 , 456);");
QVERIFY(!eng.hasUncaughtException());
QCOMPARE(result.toString(), QString::fromLatin1("success"));
}
}
static QScriptValue get_jsActivationObject(QScriptContext *ctx, QScriptEngine *)
{
return ctx->parentContext()->parentContext()->activationObject();
}
void tst_QScriptContext::jsActivationObject()
{
QScriptEngine eng;
eng.globalObject().setProperty("get_jsActivationObject", eng.newFunction(get_jsActivationObject));
eng.evaluate("function f1() { var w = get_jsActivationObject('arg1'); return w; }");
eng.evaluate("function f2(x,y,z) { var v1 = 42;\n"
// "function foo() {};\n" //this would avoid JSC to optimize
"var v2 = f1(); return v2; }");
eng.evaluate("function f3() { var v1 = 'nothing'; return f2(1,2,3); }");
QScriptValue result1 = eng.evaluate("f2('hello', 'useless', 'world')");
QScriptValue result2 = eng.evaluate("f3()");
QVERIFY(result1.isObject());
QEXPECT_FAIL("", "JSC optimize away the activation object", Abort);
QCOMPARE(result1.property("v1").toInt32() , 42);
QCOMPARE(result1.property("arguments").property(1).toString() , QString::fromLatin1("useless"));
QVERIFY(result2.isObject());
QCOMPARE(result2.property("v1").toInt32() , 42);
QCOMPARE(result2.property("arguments").property(1).toString() , QString::fromLatin1("2"));
}
QTEST_MAIN(tst_QScriptContext)
#include "tst_qscriptcontext.moc"
|