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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
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
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
3
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
4 namespace Jint.Runtime
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
5 {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
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
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
9 class MainClass
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
10 {
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
17 var frame = new Frame(5,runtime);
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
22
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
23 var code = new LteCmp(2, 1, 4);
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
24 var body = new BinaryOp(Codes.Add, 2, 3, 2);
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
25 body.Chain(code);
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
26 code.Chain(
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
27 new ConditionOp(
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
28 4,
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
29 body,
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
30 null
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
31 )
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
32 );
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
33
0
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
34
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
35 var t = Environment.TickCount;
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
41 * lte r2, r1, r4
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
42 * cmp:
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
43 * test r4 { success => body, fail => end }
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
44 * body:
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
45 * add r2,r3,r2
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
46 * goto cmp
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
50 for (IInstruction op = code; op != null; op = op.Invoke(frame))
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
51 ;
0
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
52
5
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
63
5
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
64 }
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
65
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
66 public static int Compare(object arg1, object arg2) {
cb13da6e3349 simple loop test
cin
parents: 3
diff changeset
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
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
72 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
73 }
e113095f1de0 initial commit, proof of concept
cin
parents:
diff changeset
74 }