Example #1
0
File: Vm.cs Project: mbrezu/Shovel
 internal void SerializeState(Stream s)
 {
     CheckVmWithoutError ();
     var ser = new Serialization.VmStateSerializer ();
     var usedStack = this.stack.GetUsedStack ();
     int stackIndex = ser.Serialize (usedStack);
     int envIndex = ser.Serialize (this.currentEnvironment);
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (Shovel.Api.Version));
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (stackIndex));
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (envIndex));
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (this.programCounter));
     Serialization.Utils.WriteBytes (s, Encoding.UTF8.GetBytes (Utils.GetBytecodeMd5 (this.bytecode)));
     Serialization.Utils.WriteBytes (s, Encoding.UTF8.GetBytes (Utils.GetSourcesMd5 (this.bytecode)));
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (this.executedTicks));
     Serialization.Utils.WriteBytes (s, BitConverter.GetBytes (this.usedCells));
     ser.WriteToStream (s);
 }
Example #2
0
File: Vm.cs Project: mbrezu/Shovel
 void DeserializeState(byte[] serializedState)
 {
     using (var ms = new MemoryStream(serializedState)) {
         Serialization.Utils.DeserializeWithMd5CheckSum (ms, str => {
             var version = Serialization.Utils.ReadInt (str);
             if (version > Shovel.Api.Version) {
                 throw new Exceptions.VersionNotSupportedException ();
             }
             var stackIndex = Serialization.Utils.ReadInt (str);
             var envIndex = Serialization.Utils.ReadInt (str);
             this.programCounter = Serialization.Utils.ReadInt (str);
             var bytes = new byte[32];
             str.Read (bytes, 0, 32);
             var actualBytecodeMd5 = Encoding.UTF8.GetString (bytes);
             if (actualBytecodeMd5 != Utils.GetBytecodeMd5 (this.bytecode)) {
                 throw new Exceptions.BytecodeDoesntMatchState ();
             }
             // Read and ignore the source MD5.
             str.Read (bytes, 0, 32);
             // Read the number of ticks executed so far.
             this.executedTicks = Serialization.Utils.ReadLong (ms);
             // Read the number of used cells.
             this.usedCells = Serialization.Utils.ReadInt (ms);
             var ser = new Serialization.VmStateSerializer ();
             ser.Deserialize (str, version, reader => {
                 this.stack = new Stack ((Value[])reader (stackIndex));
                 this.currentEnvironment = (VmEnvironment)reader (envIndex);
             }
             );
             return null;
         });
     }
 }