view Jint.Runtime/Main.cs @ 2:4aed85a1f558

implemented simple vm2
author cin
date Fri, 25 Oct 2013 15:52:16 +0400
parents e113095f1de0
children aced2ae9957f
line wrap: on
line source

using System;
using Jint.Runtime.VM.OpCodes;

namespace Jint.Runtime
{
    using VM;
    using System.IO;
    class MainClass
    {
        public static void Main(string[] args)
        {
			var runtime = new RuntimeContext ();

			var frame = new Frame (3,runtime);

			frame.Set (0, 0);
			frame.Set (1, 1);

			var op = new Add (0,1,0);

			var t = Environment.TickCount;

			for(int i=0; i < 10000000; i++)
				op.Invoke (frame);

			var res = frame.Get<int> (0);

			Console.WriteLine ("got: {0}, int {1} ms", res, Environment.TickCount - t );

			t = Environment.TickCount;

			object count = 0, inc = 1;
			for (int i=0; i< 10000000; i++)
				count = OpAdd(count,inc);

			Console.WriteLine ("reference results: {0}, int {1} ms", count, Environment.TickCount - t );

			var code = new VM2.Instruction[10000000];

			for (int i = 0; i < code.Length; i++)
				code[i] = new VM2.Instruction(){ code = VM2.OpCodes.Codes.Add, dest = 0, args = new int[] {0,1} };
			var machine = new VM2.Machine ();

			t = Environment.TickCount;

			machine.InitFrame (new object[] { 0, 1 });
			machine.Execute (code);

			Console.WriteLine ("vm2: {0}, int {1} ms", machine.Get(0), Environment.TickCount - t );
        }

		public static object OpAdd(object arg1, object arg2) {
			if (arg1.GetType () == arg2.GetType ()) {
				return OpAddIntegers(arg1,arg2);
			}
			throw new Exception ();
		}

		public static object OpAddIntegers(object arg1, object arg2) {
			return (int)arg1 + (int)arg2;
		}
    }
}