Пример #1
0
        static void SerializerTest()
        {
            var sources = Shovel.Api.MakeSources("test.sho", @"
var makeCounter = fn () {
  var counter = 0
  fn () counter = counter + 1
}
var main = fn () {
  var c1 = makeCounter()
  var arr = array(1, 2, 3, 4)
  @print(string(arr[0]))
  @print(string(arr[1]))
  c1()
  @print(string(c1()))
  @stop()
  @print(string(arr[2]))
  @print(string(arr[3]))
}
main()
"
                                                 );
            Action <Shovel.VmApi, Shovel.Value[], Shovel.UdpResult> print = (api, args, result) => {
                if (args.Length > 0 && args [0].Kind == Shovel.Value.Kinds.String)
                {
                    Console.WriteLine(args [0].String);
                }
            };
            Action <Shovel.VmApi, Shovel.Value[], Shovel.UdpResult> stop = (api, args, result) => {
                result.After = Shovel.UdpResult.AfterCall.Nap;
            };
            var bytecode       = Shovel.Api.GetBytecode(sources);
            var userPrimitives = new Shovel.Callable[] {
                Shovel.Callable.MakeUdp("print", print, 1),
                Shovel.Callable.MakeUdp("stop", stop, 0),
            };

            Console.WriteLine(Shovel.Api.PrintAssembledBytecode(bytecode));
            var vm = Shovel.Api.RunVm(bytecode, sources, userPrimitives);

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            var state = Shovel.Api.SerializeVmState(vm);

            for (var i = 0; i < 9999; i++)
            {
                state = Shovel.Api.SerializeVmState(vm);
            }
            sw.Stop();
            Console.WriteLine("Serialization time: {0}", sw.ElapsedMilliseconds / 1000.0);


            Console.WriteLine(state.Length);
            File.WriteAllBytes("test.bin", state);

            Shovel.Api.RunVm(bytecode, sources, userPrimitives, state);
        }
Пример #2
0
        static void SerializerTest()
        {
            var sources = Shovel.Api.MakeSources ("test.sho", @"
            var makeCounter = fn () {
              var counter = 0
              fn () counter = counter + 1
            }
            var main = fn () {
              var c1 = makeCounter()
              var arr = array(1, 2, 3, 4)
              @print(string(arr[0]))
              @print(string(arr[1]))
              c1()
              @print(string(c1()))
              @stop()
              @print(string(arr[2]))
              @print(string(arr[3]))
            }
            main()
            "
            );
            Action<Shovel.VmApi, Shovel.Value[], Shovel.UdpResult> print = (api, args, result) => {
                if (args.Length > 0 && args [0].Kind == Shovel.Value.Kinds.String) {
                    Console.WriteLine (args [0].StringValue);
                }
            };
            Action<Shovel.VmApi, Shovel.Value[], Shovel.UdpResult> stop = (api, args, result) => {
                result.After = Shovel.UdpResult.AfterCall.Nap;
            };
            var bytecode = Shovel.Api.GetBytecode (sources);
            var userPrimitives = new Shovel.Callable[] {
                Shovel.Callable.MakeUdp ("print", print, 1),
                Shovel.Callable.MakeUdp ("stop", stop, 0),
            };
            Console.WriteLine (Shovel.Api.PrintAssembledBytecode (bytecode));
            var vm = Shovel.Api.RunVm (bytecode, sources, userPrimitives);

            var sw = new System.Diagnostics.Stopwatch ();
            sw.Start ();
            var state = Shovel.Api.SerializeVmState (vm);
            for (var i = 0; i < 9999; i++) {
                state = Shovel.Api.SerializeVmState (vm);
            }
            sw.Stop ();
            Console.WriteLine ("Serialization time: {0}", sw.ElapsedMilliseconds / 1000.0);

            Console.WriteLine (state.Length);
            File.WriteAllBytes ("test.bin", state);

            Shovel.Api.RunVm (bytecode, sources, userPrimitives, state);
        }