/
InterpreterTests.cs
1210 lines (1037 loc) · 50.4 KB
/
InterpreterTests.cs
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) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_CORE_DLR
using System.Linq.Expressions;
#else
using Microsoft.Scripting.Ast;
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Utils;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Reflection;
namespace IronRuby.Tests {
using Ast = Expression;
using AstUtils = Microsoft.Scripting.Ast.Utils;
public partial class Tests {
#region Tracing Helpers
[ThreadStatic]
public static List<object> _trace = new List<object>();
public static void Trace(object value) {
_trace.Add(value);
}
private Expression TraceCall(Expression arg) {
return Ast.Call(null, typeof(Tests).GetMethod("Trace"), Ast.Convert(arg, typeof(object)));
}
private void RunTraceTest(Expression<Action> lambda, Action<Action> invoker, out object[] compiled, out object[] interpreted, out object[] sync) {
if (invoker == null) {
invoker = (a) => a();
}
_trace.Clear();
invoker(lambda.Compile());
compiled = _trace.ToArray();
_trace.Clear();
// force synchronous compilation:
invoker(lambda.LightCompile(0));
sync = _trace.ToArray();
_trace.Clear();
// force interpretation:
invoker(lambda.LightCompile(Int32.MaxValue));
interpreted = _trace.ToArray();
_trace.Clear();
}
private void TraceTestLambda(Expression<Action> lambda, Type expectedException) {
TraceTestLambda(lambda, (a) => {
try {
a();
Assert(false, "Expected exception " + expectedException);
} catch (Exception e) {
Assert(e.GetType() == expectedException, "Expected exception " + expectedException);
}
});
}
private void TraceTestLambda(Expression<Action> lambda) {
TraceTestLambda(lambda, (Action<Action>)null);
}
private void TraceTestLambda(Expression<Action> lambda, Action<Action> invoker) {
object[] compiled, interpreted, sync;
RunTraceTest(lambda, invoker, out compiled, out interpreted, out sync);
Assert(compiled.ValueEquals(interpreted));
Assert(compiled.ValueEquals(sync));
}
private void XTraceTestLambda(Expression<Action> lambda) {
object[] compiled, interpreted, sync;
RunTraceTest(lambda, null, out compiled, out interpreted, out sync);
Console.WriteLine("-- compiled --");
foreach (var obj in compiled) {
Console.WriteLine(obj);
}
Console.WriteLine("-- interpreted --");
foreach (var obj in interpreted) {
Console.WriteLine(obj);
}
Console.WriteLine("-- sync --");
foreach (var obj in sync) {
Console.WriteLine(obj);
}
}
#endregion
[Options(NoRuntime = true)]
public void Interpreter1A() {
var m_AddValue = new Action<StrongBox<int>, int>(Interpreter1_AddValue).Method;
var m_ThrowNSE = new Action(Interpreter1_ThrowNSE).Method;
var m_f = new Func<int, int, int, int, int>(Interpreter1_f).Method;
var m_g = new Func<int, int, int, int>(Interpreter1_g).Method;
var value = new StrongBox<int>(0);
LabelTarget label;
int rc, ri;
// value of try-catch:
var l0 = Ast.Lambda<Func<int>>(
Ast.TryCatch(Ast.Constant(1, typeof(int)), Ast.Catch(typeof(Exception), Ast.Constant(2, typeof(int))))
);
rc = l0.Compile()();
ri = l0.LightCompile()();
Assert(rc == ri);
// cross-block goto in try-catch-finally:
label = Ast.Label(typeof(int));
var l1 = Ast.Lambda<Func<int>>(
Ast.Label(label, Ast.TryCatchFinally(
Ast.Goto(label, Ast.Constant(1), typeof(int)),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(1)),
Ast.Catch(typeof(Exception), Ast.Constant(2, typeof(int)))
)));
value.Value = 0;
rc = l1.Compile()();
ri = l1.LightCompile()();
Assert(value.Value == 11);
Assert(rc == ri);
// cross-block goto in try-catch-finally with an exception thrown and caught in finally:
label = Ast.Label(typeof(int));
var l2 = Ast.Lambda<Func<int>>(
Ast.Label(label, Ast.TryCatchFinally(
Ast.Goto(label, Ast.Constant(1), typeof(int)),
Ast.Block(
Ast.TryCatch(
Ast.Call(null, m_ThrowNSE),
Ast.Catch(typeof(NotSupportedException),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(1))
)
),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(2))
),
Ast.Catch(typeof(Exception), Ast.Constant(2, typeof(int)))
)));
value.Value = 0;
rc = l2.Compile()();
ri = l2.LightCompile()();
Assert(value.Value == 1212);
Assert(rc == ri);
// executing fault and finally blocks for an exception coming from a method call:
label = Ast.Label(typeof(int));
var a = Ast.Lambda<Action>(
Ast.TryCatch(
Ast.TryFinally(// TODO: faults not supported yet: Ast.TryFault(
Ast.TryFinally(
Ast.Call(null, m_ThrowNSE),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(1))
),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(2))
),
Ast.Catch(typeof(NotSupportedException),
Ast.Call(null, m_AddValue, Ast.Constant(value), Ast.Constant(3))
)
)
);
value.Value = 0;
a.Compile()();
a.LightCompile()();
Assert(value.Value == 123123);
// try-catch with non-empty stack:
var l3 = Ast.Lambda<Func<int>>(
Ast.Call(null, m_f,
Ast.Constant(1),
Ast.Constant(2),
Ast.TryCatch(
Ast.Call(null, m_g,
Ast.Constant(3),
Ast.Constant(4),
Ast.Throw(Ast.Constant(new Exception("!!!")), typeof(int))
),
Ast.Catch(
typeof(Exception),
Ast.Constant(5)
)
),
Ast.Constant(7)
)
);
rc = l3.Compile()();
ri = l3.LightCompile()();
Assert(rc == ri);
// goto carrying a value needs to pop the value from the stack before executing finally
// clauses to prevent stack overflow (the finally clause doesn't expect the value on the stack):
var label3 = Ast.Label(typeof(int));
var l4 = Ast.Lambda<Func<int>>(
Ast.Label(label3,
Ast.Block(
Ast.TryFinally(
Ast.Goto(label3, Ast.Constant(1), typeof(void)),
Ast.Call(null, m_g, Ast.Constant(2), Ast.Constant(3), Ast.Constant(4))
),
Ast.Constant(3)
)
)
);
rc = l4.Compile()();
ri = l4.LightCompile()();
Assert(rc == ri);
// goto needs to pop unused values from the stack before it executes finally:
var label4 = Ast.Label(typeof(int));
var l5 = Ast.Lambda<Func<int>>(
Ast.Label(label4,
Ast.Block(
Ast.TryFinally(
Ast.Call(null, m_g, Ast.Constant(9), Ast.Constant(8), Ast.Goto(label4, Ast.Constant(1), typeof(int))),
Ast.Call(null, m_f, Ast.Constant(2), Ast.Constant(3), Ast.Constant(4), Ast.Constant(5))
),
Ast.Constant(3)
)
)
);
rc = l5.Compile()();
ri = l5.LightCompile()();
Assert(rc == ri);
}
public static void Interpreter1_AddValue(StrongBox<int> value, int d) {
value.Value = value.Value * 10 + d;
}
public static void Interpreter1_ThrowNSE() {
throw new NotSupportedException();
}
public static int Interpreter1_f(int a, int b, int c, int d) {
return a * 1000 + b * 100 + c * 10 + d;
}
public static int Interpreter1_g(int a, int b, int c) {
return 20;
}
[Options(NoRuntime = true)]
public void Interpreter1B() {
LabelTarget label = Ast.Label();
// throw in a finally that is executed while jumping to a label cancels the jump:
var l0 = Ast.Lambda<Action>(
Ast.TryFinally(
Ast.TryCatch(
Ast.Block(
Ast.TryFinally(
Ast.TryFinally(
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Goto(label)
),
// F2:
Ast.Block(
TraceCall(Ast.Constant(1)),
Ast.Throw(Ast.Constant(new Exception("foo")))
)
),
// F1:
TraceCall(Ast.Constant(2))
),
// LABEL:
Ast.Label(label),
TraceCall(Ast.Constant(3))
),
// CATCH:
Ast.Catch(typeof(Exception),
TraceCall(Ast.Constant(4))
)
),
// F0:
TraceCall(Ast.Constant(5))
)
);
TraceTestLambda(l0);
// throw in a finally that is executed while jumping to a label cancels the jump:
var l1 = Ast.Lambda<Action>(
Ast.Block(
Ast.TryFinally(
Ast.TryCatch(
Ast.Block(
Ast.TryFinally(
Ast.TryFinally(
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Goto(label)
),
// F2:
Ast.Block(
TraceCall(Ast.Constant(1)),
Ast.Throw(Ast.Constant(new Exception("foo")))
)
),
// F1:
TraceCall(Ast.Constant(2))
),
TraceCall(Ast.Constant(3))
),
// CATCH:
Ast.Catch(typeof(Exception),
TraceCall(Ast.Constant(4))
)
),
// F0:
TraceCall(Ast.Constant(5))
),
Ast.Block(
TraceCall(Ast.Constant(6)),
// LABEL:
Ast.Label(label),
TraceCall(Ast.Constant(7))
)
)
);
TraceTestLambda(l1);
// throw caught a try-catch in a finally that is executed while jumping to a label doesn't cancel the jump
var l2 = Ast.Lambda<Action>(
Ast.TryCatch(
Ast.Block(
Ast.TryFinally(
Ast.TryFinally(
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Goto(label)
),
Ast.Block(
TraceCall(Ast.Constant(1)),
Ast.TryCatch(
Ast.Throw(Ast.Constant(new Exception("foo"))),
Ast.Catch(typeof(Exception),
TraceCall(Ast.Constant(2))
)
)
)
),
TraceCall(Ast.Constant(3))
),
TraceCall(Ast.Constant(4)),
Ast.Label(label),
TraceCall(Ast.Constant(5))
),
Ast.Catch(typeof(Exception),
TraceCall(Ast.Constant(6))
)
)
);
TraceTestLambda(l2);
}
/// <summary>
/// Faults.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter1C() {
var inner =
Ast.TryFinally(
Ast.TryCatch(
Ast.TryFinally(
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Throw(Ast.Constant(new NotSupportedException("ex1")))
),
TraceCall(Ast.Constant(1))
),
Ast.Catch(typeof(Exception),
Ast.Block(
TraceCall(Ast.Constant(2)),
Ast.Rethrow()
)
)
),
TraceCall(Ast.Constant(3))
);
var l0 = Ast.Lambda<Action>(
Ast.TryCatch(
inner,
Ast.Catch(typeof(Exception),
TraceCall(Ast.Constant(4))
)
)
);
TraceTestLambda(l0);
var l1 = Ast.Lambda<Action>(
Ast.TryCatch(
inner,
Ast.Catch(typeof(InvalidOperationException),
TraceCall(Ast.Constant(4))
)
)
);
TraceTestLambda(l1, typeof(NotSupportedException));
var l2 = Ast.Lambda<Action>(
inner
);
TraceTestLambda(l2, typeof(NotSupportedException));
}
/// <summary>
/// Faults.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter1D() {
var l1 = Ast.Lambda<Action>(
TraceCall(
Ast.TryCatch(
Ast.Call(null, new Func<int>(ThrowNSEReturnInt).Method),
Ast.Catch(typeof(Exception),
Ast.Block(
Ast.Constant(2),
Ast.Rethrow(typeof(int))
)
)
)
)
);
l1.LightCompile();
}
public static int ThrowNSEReturnInt() {
throw new NotSupportedException();
}
/// <summary>
/// Jump out of finally.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter_JumpFromFinally1() {
var label1 = Ast.Label();
var label2 = Ast.Label();
var l0 = Ast.Lambda<Action>(
Ast.Block(
Ast.TryFinally(
Ast.Block(
AstUtils.FinallyFlowControl(
Ast.TryFinally(
Ast.TryFinally(
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Goto(label1)
),
// F2:
Ast.Block(
TraceCall(Ast.Constant(1)),
Ast.Goto(label2)
)
),
// F1:
TraceCall(Ast.Constant(2))
)
),
TraceCall(Ast.Constant(3)),
// LABEL2:
Ast.Label(label2),
TraceCall(Ast.Constant(4))
),
// F0:
TraceCall(Ast.Constant(5))
),
Ast.Block(
TraceCall(Ast.Constant(6)),
// LABEL1:
Ast.Label(label1),
TraceCall(Ast.Constant(7))
)
)
);
TraceTestLambda(l0);
}
/// <summary>
/// Pending continuation override: jump from finally should cancel any pending jumps (gotos or throws) from try-block.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter_JumpFromFinally2() {
var label = Ast.Label();
var l0 = Ast.Lambda<Action>(
AstUtils.FinallyFlowControl(
Ast.Block(
Ast.TryFinally(
Ast.Throw(Ast.Constant(new Exception("ex"))),
Ast.Block(
TraceCall(Ast.Constant(0)),
Ast.Goto(label)
)
),
Ast.Label(label),
TraceCall(Ast.Constant(1))
)
)
);
TraceTestLambda(l0);
}
[Options(NoRuntime = true)]
public void Interpreter2() {
Interpreter2_Test<Func<int>>(
(var) => Ast.Lambda<Func<int>>(var),
(c, i) => c() == i()
);
Interpreter2_Test<IRuntimeVariables>(
(var) => Ast.RuntimeVariables(var),
(c, i) => (int)c[0] == (int)i[0]
);
}
private void Interpreter2_Test<TClosure>(Func<ParameterExpression, Expression> closure, Func<TClosure, TClosure, bool> comparer) {
// value of try-catch:
var closureVar = Ast.Parameter(typeof(int), "x");
var indexVar = Ast.Parameter(typeof(int), "i");
var limitVar = Ast.Parameter(typeof(int), "limit");
var closuresVar = Ast.Parameter(typeof(TClosure[]), "closures");
var returnLabel = Ast.Label(typeof(void));
var l0 = Ast.Lambda<Action<int, int, TClosure[]>>(
Ast.Label(returnLabel,
Ast.Loop(
Ast.IfThenElse(
Ast.NotEqual(indexVar, limitVar),
Ast.Block(new[] { closureVar },
Ast.Assign(closureVar, indexVar),
Ast.Assign(Ast.ArrayAccess(closuresVar, indexVar), closure(closureVar)),
Ast.Assign(indexVar, Ast.Add(indexVar, Ast.Constant(1)))
),
Ast.Return(returnLabel)
)
)
),
new[] { indexVar, limitVar, closuresVar }
);
var fsc = new TClosure[2];
l0.Compile()(0, 2, fsc);
var fsi = new TClosure[2];
l0.LightCompile()(0, 2, fsi);
Assert(comparer(fsc[0], fsi[0]));
Assert(comparer(fsc[1], fsi[1]));
}
/// <summary>
/// ThreadAbortException handling.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter3() {
if (_driver.PartialTrust) return;
var label = Ast.Label(typeof(void));
foreach (var gotoLabel in new Expression[] { Ast.Goto(label), Ast.Empty() }) {
var var_tracker = Ast.Parameter(typeof(List<object>));
var var_abort1 = Ast.Parameter(typeof(ThreadAbortException));
var var_e = Ast.Parameter(typeof(Exception));
var var_abort2 = Ast.Parameter(typeof(ThreadAbortException));
var m_Interpreter3_abort = new Action(Interpreter3_abort).Method;
var m_Interpreter3_catchAbortThrowNSE = new Action<List<object>, ThreadAbortException>(Interpreter3_catchAbortThrowNSE).Method;
var m_Interpreter3_catchE = new Action<List<object>, Exception>(Interpreter3_catchE).Method;
var m_Interpreter3_unreachable = new Action<List<object>>(Interpreter3_unreachable).Method;
var m_Interpreter3_catchAbort = new Action<List<object>, ThreadAbortException>(Interpreter3_catchAbort).Method;
var l = Ast.Lambda<Action<List<object>>>(
AstUtils.Try(
AstUtils.Try(
AstUtils.Try(
Ast.Call(null, m_Interpreter3_abort)
).Catch(var_abort1,
Ast.Call(null, m_Interpreter3_catchAbortThrowNSE, var_tracker, var_abort1)
)
).Catch(var_e,
Ast.Call(null, m_Interpreter3_catchE, var_tracker, var_e),
gotoLabel
),
Ast.Call(null, m_Interpreter3_unreachable, var_tracker),
Ast.Label(label, Ast.Call(null, m_Interpreter3_unreachable, var_tracker))
).Catch(var_abort2,
Ast.Call(null, m_Interpreter3_catchAbort, var_tracker, var_abort2)
),
new[] { var_tracker }
);
var ctracker = new List<object>();
var itracker = new List<object>();
try {
l.Compile()(ctracker);
l.LightCompile()(itracker);
} catch (ThreadAbortException) {
Thread.ResetAbort();
Assert(false);
return;
}
foreach (var t in new[] { ctracker, itracker }) {
Assert(t.Count == 10);
Assert(t[0] as string == "1");
Assert(t[1] is ThreadAbortException);
Assert(t[2] as string == "stateInfo");
Assert(t[3] as string == "2");
Assert(t[4] is NotSupportedException);
Assert((ThreadState)t[5] == ThreadState.AbortRequested);
Assert(t[6] as string == "3");
Assert(t[7] is ThreadAbortException);
Assert(ReferenceEquals(t[8], t[2]));
Assert((ThreadState)t[9] == ThreadState.AbortRequested);
}
}
}
public static void Interpreter3_abort() {
Thread.CurrentThread.Abort("stateInfo");
}
public static void Interpreter3_catchAbortThrowNSE(List<object> tracker, ThreadAbortException e) {
tracker.Add("1");
tracker.Add(e);
tracker.Add(e.ExceptionState);
throw new NotSupportedException();
}
public static void Interpreter3_catchE(List<object> tracker, Exception e) {
tracker.Add("2");
tracker.Add(e);
tracker.Add(Thread.CurrentThread.ThreadState);
}
public static void Interpreter3_unreachable(List<object> tracker) {
tracker.Add("UNREACHABLE");
}
public static void Interpreter3_catchAbort(List<object> tracker, ThreadAbortException e) {
tracker.Add("3");
tracker.Add(e);
tracker.Add(e.ExceptionState);
tracker.Add(Thread.CurrentThread.ThreadState);
Thread.ResetAbort();
}
public static void Interpreter4() {
// TODO: figure out if this is specified behavior of thread abort:
//var var_tracker = Ast.Parameter(typeof(List<object>));
//var var_abort1 = Ast.Parameter(typeof(ThreadAbortException));
//var var_abort2 = Ast.Parameter(typeof(ThreadAbortException));
//var var_abort3 = Ast.Parameter(typeof(ThreadAbortException));
//var var_abort4 = Ast.Parameter(typeof(ThreadAbortException));
//var m_Interpreter4_abort = new Action(Interpreter4_abort).Method;
//var m_Interpreter4_traceE = new Action<List<object>, ThreadAbortException>(Interpreter4_traceE).Method;
//var m_Interpreter4_traceS = new Action<List<object>, string>(Interpreter4_traceS).Method;
//var m_Interpreter4_reset = new Action<List<object>, ThreadAbortException>(Interpreter4_reset).Method;
//var l = Ast.Lambda<Action<List<object>>>(
// AstUtils.Try(
// AstUtils.Try(
// Ast.Call(null, m_Interpreter4_abort, Ast.Constant("r1"))
// ).Catch(var_abort1,
// Ast.Call(null, m_Interpreter4_traceE, var_tracker, var_abort1),
// AstUtils.Try(
// AstUtils.Try(
// Ast.Call(null, m_Interpreter4_abort, Ast.Constant("r2"))
// ).Catch(var_abort2,
// Ast.Call(null, m_Interpreter4_traceE, var_tracker, var_abort2)
// ),
// Ast.Call(null, m_Interpreter4_traceS, var_tracker, Ast.Constant("1"))
// ).Catch(var_abort3,
// Ast.Call(null, m_Interpreter4_traceE, var_tracker, var_abort3)
// ),
// Ast.Call(null, m_Interpreter4_traceS, var_tracker, Ast.Constant("2"))
// ),
// Ast.Call(null, m_Interpreter4_traceS, var_tracker, Ast.Constant("2"))
// ).Catch(var_abort4,
// Ast.Call(null, m_Interpreter4_reset, var_tracker, var_abort4)
// ),
// new[] { var_tracker }
//);
//var ctracker = new List<object>();
//var itracker = new List<object>();
//try {
// l.Compile()(ctracker);
// //l.LightCompile()(itracker);
//} catch (ThreadAbortException) {
// Thread.ResetAbort();
// Assert(false);
// return;
//}
}
public static void Interpreter4_abort(string value) {
Thread.CurrentThread.Abort(value);
}
public static void Interpreter4_traceE(List<object> tracker, ThreadAbortException e) {
tracker.Add(e.ExceptionState);
}
public static void Interpreter4_traceS(List<object> tracker, string s) {
tracker.Add(s);
}
public static void Interpreter4_reset(List<object> tracker, ThreadAbortException e) {
tracker.Add(e.ExceptionState);
Thread.ResetAbort();
tracker.Add(e.ExceptionState);
}
[Options(NoRuntime = true)]
public void Interpreter5() {
var strArray1 = new string[] { "foo" };
var strArray2 = new string[1, 1];
var strArray3 = new string[1, 1, 1];
var strArray8 = new string[1, 1, 1, 1, 1, 1, 1, 1];
strArray2[0, 0] = "foo";
strArray3[0, 0, 0] = "foo";
strArray8[0, 0, 0, 0, 0, 0, 0, 0] = "foo";
// T[]::Get
Assert("foo" == Ast.Lambda<Func<string>>(
Ast.Call(Ast.Constant(strArray1), typeof(string[]).GetMethod("Get"), Ast.Constant(0))
).LightCompile()());
// T[]::Set
Ast.Lambda<Action>(
Ast.Call(Ast.Constant(strArray1), typeof(string[]).GetMethod("Set"), Ast.Constant(0), Ast.Constant("bar"))
).LightCompile()();
Assert(strArray1[0] == "bar");
// T[,]::Get
Assert("foo" == Ast.Lambda<Func<string>>(
Ast.Call(Ast.Constant(strArray2), typeof(string[,]).GetMethod("Get"), Ast.Constant(0), Ast.Constant(0))
).LightCompile()());
// T[,]::Set
Ast.Lambda<Action>(
Ast.Call(Ast.Constant(strArray2), typeof(string[,]).GetMethod("Set"), Ast.Constant(0), Ast.Constant(0), Ast.Constant("bar"))
).LightCompile()();
Assert(strArray2[0, 0] == "bar");
// T[,,]::Get
Assert("foo" == Ast.Lambda<Func<string>>(
Ast.Call(Ast.Constant(strArray3), typeof(string[, ,]).GetMethod("Get"), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0))
).LightCompile()());
// T[,,]::Set
Ast.Lambda<Action>(
Ast.Call(Ast.Constant(strArray3), typeof(string[, ,]).GetMethod("Set"), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0), Ast.Constant("bar"))
).LightCompile()();
Assert(strArray3[0, 0, 0] == "bar");
// T[*]::Get
Assert("foo" == Ast.Lambda<Func<string>>(
Ast.Call(Ast.Constant(strArray8), typeof(string[, , , , , , ,]).GetMethod("Get"),
Ast.Constant(0), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0),
Ast.Constant(0), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0)
)
).LightCompile()());
// T[*]::Set
Ast.Lambda<Action>(
Ast.Call(Ast.Constant(strArray8), typeof(string[, , , , , , ,]).GetMethod("Set"),
Ast.Constant(0), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0),
Ast.Constant(0), Ast.Constant(0), Ast.Constant(0), Ast.Constant(0),
Ast.Constant("bar")
)
).LightCompile()();
Assert(strArray8[0, 0, 0, 0, 0, 0, 0, 0] == "bar");
}
/// <summary>
/// Variable shadowing.
/// </summary>
[Options(NoRuntime = true)]
public void Interpreter6() {
var i_var = Ast.Parameter(typeof(int), "i");
var l1 = Ast.Lambda<Action>(
Ast.Block(new[] { i_var },
Ast.Assign(i_var, Ast.Constant(1)),
TraceCall(i_var),
Ast.Block(new[] { i_var },
Ast.Assign(i_var, Ast.Constant(2)),
TraceCall(i_var)
),
Ast.Block(new[] { i_var },
Ast.Assign(i_var, Ast.Constant(3)),
TraceCall(i_var)
),
TraceCall(i_var)
)
);
//XTraceTestLambda(l1);
var e_var = Ast.Parameter(typeof(Exception), "e");
var ex1 = new Exception("ex1");
var ex2 = new Exception("ex2");
// each catch block actually defines e variable again shadowing the block's e variable.
// Thus assignments within the catch block don't affect block's e variable.
var l2 = Ast.Lambda<Action>(
Ast.Block(new[] { e_var },
Ast.TryCatch(Ast.Throw(Ast.Constant(ex1)), Ast.Catch(e_var, TraceCall(Ast.Property(e_var, "Message")))),
TraceCall(e_var),
Ast.TryCatch(Ast.Throw(Ast.Constant(ex2)), Ast.Catch(e_var, TraceCall(Ast.Property(e_var, "Message")))),
TraceCall(e_var)
)
);
l2.Compile()();
//XTraceTestLambda(l2);
var l3 = Ast.Lambda<Action>(
Ast.Block(new[] { i_var },
TraceCall(
Ast.Invoke(
Ast.Lambda<Func<int, int>>(Ast.Add(i_var, Ast.Constant(1)), new[] { i_var }),
Ast.Constant(1)
)
)
)
);
l3.Compile()();
//XTraceTestLambda(l2);
// TODO: add tests for loop compiler
}
[Options(NoRuntime = true)]
public void InterpreterNumeric1() {
Assert(Expression.Lambda<Func<short>>(
Expression.Add(Expression.Constant((short)1), Expression.Constant((short)2))
).LightCompile()() == 3);
Assert(Expression.Lambda<Func<int>>(
Expression.Add(Expression.Constant((int)1), Expression.Constant((int)2))
).LightCompile()() == 3);
Assert(Expression.Lambda<Func<short>>(
Expression.AddChecked(Expression.Constant((short)1), Expression.Constant((short)2))
).LightCompile()() == 3);
Assert(Expression.Lambda<Func<bool>>(
Expression.LessThan(Expression.Constant((byte)1), Expression.Constant((byte)2))
).LightCompile()() == true);
Assert(Expression.Lambda<Func<bool>>(
Expression.Equal(Expression.Constant(true), Expression.Constant(false))
).LightCompile()() == false);
object obj1 = 1;
object obj2 = 1;
Assert(Expression.Lambda<Func<bool>>(
Expression.Equal(Expression.Constant(obj1, typeof(object)), Expression.Constant(obj2, typeof(object)))
).LightCompile()() == false);
Assert(Expression.Lambda<Func<bool>>(
Expression.Equal(Expression.Constant(1), Expression.Constant(1))
).LightCompile()() == true);
}
public class ClassWithMethods2 {
private readonly string Str = "<this>";
public static void SF0() { TestValues.Add("0"); }
public static void SF1(string a) { TestValues.Add(a); }
public static void SF2(string a, string b) { TestValues.Add(a + b); }
public static void SF3(string a, string b, string c) { TestValues.Add(a + b + c); }
public static void SF4(string a, string b, string c, string d) { TestValues.Add(a + b + c + d); }
public static void SF5(string a, string b, string c, string d, string e) { TestValues.Add(a + b + c + d + e); }
public static string SG0() { TestValues.Add("0"); return "G0"; }
public static string SG1(string a) { TestValues.Add(a); return "G1"; }
public static string SG2(string a, string b) { TestValues.Add(a + b); return "G2"; }
public static string SG3(string a, string b, string c) { TestValues.Add(a + b + c); return "G3"; }
public static string SG4(string a, string b, string c, string d) { TestValues.Add(a + b + c + d); return "G4"; }
public static string SG5(string a, string b, string c, string d, string e) { TestValues.Add(a + b + c + d + e); return "G5"; }
public void F0() { TestValues.Add(Str + "0"); }
public void F1(string a) { TestValues.Add(Str + a); }
public void F2(string a, string b) { TestValues.Add(Str + a + b); }
public void F3(string a, string b, string c) { TestValues.Add(Str + a + b + c); }
public void F4(string a, string b, string c, string d) { TestValues.Add(Str + a + b + c + d); }
public void F5(string a, string b, string c, string d, string e) { TestValues.Add(Str + a + b + c + d + e); }
public string G0() { TestValues.Add(Str + "0"); return "G0"; }
public string G1(string a) { TestValues.Add(Str + a); return "G1"; }
public string G2(string a, string b) { TestValues.Add(Str + a + b); return "G2"; }
public string G3(string a, string b, string c) { TestValues.Add(Str + a + b + c); return "G3"; }
public string G4(string a, string b, string c, string d) { TestValues.Add(Str + a + b + c + d); return "G4"; }
public string G5(string a, string b, string c, string d, string e) { TestValues.Add(Str + a + b + c + d + e); return "G5"; }
}
private static MethodInfo GM2(string name) {
return typeof(ClassWithMethods2).GetMethod(name);
}
[ThreadStatic]
private static List<string> TestValues = new List<string>();
[Options(NoRuntime = true)]
public void InterpreterMethodCalls1() {
var sf = Expression.Lambda<Action>(Ast.Block(
Ast.Call(null, GM2("SF0")),
Ast.Call(null, GM2("SF1"), Ast.Constant("1")),
Ast.Call(null, GM2("SF2"), Ast.Constant("1"), Ast.Constant("2")),
Ast.Call(null, GM2("SF3"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3")),
Ast.Call(null, GM2("SF4"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4")),
Ast.Call(null, GM2("SF5"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4"), Ast.Constant("5"))
));
var sg = Expression.Lambda<Func<string[]>>(Ast.NewArrayInit(typeof(string),
Ast.Call(null, GM2("SG0")),
Ast.Call(null, GM2("SG1"), Ast.Constant("1")),
Ast.Call(null, GM2("SG2"), Ast.Constant("1"), Ast.Constant("2")),
Ast.Call(null, GM2("SG3"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3")),
Ast.Call(null, GM2("SG4"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4")),
Ast.Call(null, GM2("SG5"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4"), Ast.Constant("5"))
));
var i = Expression.Constant(new ClassWithMethods2());
var f = Expression.Lambda<Action>(Ast.Block(
Ast.Call(i, GM2("F0")),
Ast.Call(i, GM2("F1"), Ast.Constant("1")),
Ast.Call(i, GM2("F2"), Ast.Constant("1"), Ast.Constant("2")),
Ast.Call(i, GM2("F3"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3")),
Ast.Call(i, GM2("F4"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4")),
Ast.Call(i, GM2("F5"), Ast.Constant("1"), Ast.Constant("2"), Ast.Constant("3"), Ast.Constant("4"), Ast.Constant("5"))
));
var g = Expression.Lambda<Func<string[]>>(Ast.NewArrayInit(typeof(string),