Mercurial > pub > Jint1
annotate Jint.Runtime/Main.cs @ 6:a6329b092499
Added scopes, function builder
| author | cin |
|---|---|
| date | Wed, 30 Oct 2013 17:38:35 +0400 |
| parents | cb13da6e3349 |
| children |
| rev | line source |
|---|---|
| 0 | 1 using System; |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
2 using Jint.Runtime.VM; |
| 0 | 3 |
| 4 namespace Jint.Runtime | |
| 5 { | |
| 6 using VM; | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
7 using System.IO; |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
8 using Jint.Runtime.VM.OpCodes; |
| 0 | 9 class MainClass |
| 10 { | |
| 11 public static void Main(string[] args) | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
12 { |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
13 RuntimeContext runtime = new RuntimeContext(); |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
14 runtime.DefineBinaryOperation<int>(Codes.Add, (x, y) => x + y); |
| 5 | 15 runtime.DefineCompareOperation<int>((x, y) => x.CompareTo(y)); |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
16 |
| 5 | 17 var frame = new Frame(5,runtime); |
| 18 frame.SetValue(1, 10000000); | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
19 frame.SetValue(2,0); |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
20 frame.SetValue(3,1); |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
21 |
| 5 | 22 |
| 23 var code = new LteCmp(2, 1, 4); | |
| 24 var body = new BinaryOp(Codes.Add, 2, 3, 2); | |
| 25 body.Chain(code); | |
| 26 code.Chain( | |
| 27 new ConditionOp( | |
| 28 4, | |
| 29 body, | |
| 30 null | |
| 31 ) | |
| 32 ); | |
| 33 | |
| 0 | 34 |
| 35 var t = Environment.TickCount; | |
| 36 | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
37 /* |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
38 * mov r1, 10 000 000 |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
39 * mov r2, 0 |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
40 * mov r3, 1 |
| 5 | 41 * lte r2, r1, r4 |
| 42 * cmp: | |
| 43 * test r4 { success => body, fail => end } | |
| 44 * body: | |
| 45 * add r2,r3,r2 | |
| 46 * goto cmp | |
| 47 * end: | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
48 */ |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
49 |
| 5 | 50 for (IInstruction op = code; op != null; op = op.Invoke(frame)) |
| 51 ; | |
| 0 | 52 |
| 5 | 53 Console.WriteLine ("vm: {0}, int {1} ms", frame.GetConverted<int>(2), Environment.TickCount - t ); |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
54 |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
55 t = Environment.TickCount; |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
56 |
| 5 | 57 object count = 0, inc = 1, max = 10000000; |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
58 |
| 5 | 59 while(Compare(count,max) <= 0) |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
60 count = Add(count,inc); |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
61 |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
62 Console.WriteLine("native: {0}, int {1} ms", frame.GetValue<int>(2), Environment.TickCount - t); |
| 0 | 63 |
| 5 | 64 } |
| 65 | |
| 66 public static int Compare(object arg1, object arg2) { | |
| 67 return ((int)arg1).CompareTo((int)arg2); | |
|
3
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
68 } |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
69 |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
70 public static object Add(object arg1, object arg2) { |
|
aced2ae9957f
temp commit, new virtual machine concept (strongly typed version of VM2).
cin
parents:
2
diff
changeset
|
71 return (int)arg1 + (int)arg2; |
| 0 | 72 } |
| 73 } | |
| 74 } |
