internal static SourceRef ReadBinary(BinDumpReader reader, int sourceID) { var fromChar = (int)reader.ReadVarUInt32(); int toChar = fromChar + reader.ReadVarInt32(); var fromLine = (int)reader.ReadVarUInt32(); int toLine = fromLine + reader.ReadVarInt32(); return(new SourceRef(sourceID, fromChar, toChar, fromLine, toLine, reader.ReadBoolean())); }
internal void ReadBinaryEnv(BinDumpReader br, SymbolRef[] symbolRefs) { uint idx = br.ReadVarUInt32(); if (idx >= 1) { i_Env = symbolRefs[idx - 1]; } }
public void BinDump_VarUInt() { using (var stream = new MemoryStream()) { var writer = new BinDumpWriter(stream); var reader = new BinDumpReader(stream); writer.WriteVarUInt32(12); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), 12); stream.Seek(0, SeekOrigin.Begin); writer.WriteVarUInt32(200); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), 200); stream.Seek(0, SeekOrigin.Begin); writer.WriteVarUInt32(17000); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), 17000); stream.Seek(0, SeekOrigin.Begin); writer.WriteVarUInt32(2100000); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), 2100000); stream.Seek(0, SeekOrigin.Begin); writer.WriteVarUInt32(200000118); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), 200000118); stream.Seek(0, SeekOrigin.Begin); writer.WriteVarUInt32(uint.MaxValue - 1); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(reader.ReadVarUInt32(), uint.MaxValue - 1); stream.Seek(0, SeekOrigin.Begin); } }
internal int Undump(Stream stream, int sourceID, Table envTable, out bool hasUpvalues) { int baseAddress = m_RootChunk.Code.Count; SourceRef sourceRef = new SourceRef(sourceID, 0, 0, 0, 0, false); var br = new BinDumpReader(stream); ulong headerMark = br.ReadUInt64(); if (headerMark != DUMP_CHUNK_MAGIC) { throw new ArgumentException("Not a MoonSharp chunk"); } int version = br.ReadByte(); if (version != DUMP_CHUNK_VERSION) { throw new ArgumentException("Invalid version"); } hasUpvalues = br.ReadBoolean(); int len = (int)br.ReadVarUInt32(); int numSymbs = (int)br.ReadVarUInt32(); SymbolRef[] allSymbs = new SymbolRef[numSymbs]; for (int i = 0; i < numSymbs; i++) { allSymbs[i] = SymbolRef.ReadBinary(br); } for (int i = 0; i < numSymbs; i++) { allSymbs[i].ReadBinaryEnv(br, allSymbs); } for (int i = 0; i <= len; i++) { Instruction I = Instruction.ReadBinary(br, baseAddress, envTable, allSymbs); m_RootChunk.Code.Add(I); } for (int i = 0; i <= len; i++) { var c = br.ReadByte(); if (c == 0) { m_RootChunk.SourceRefs.Add(sourceRef); } else if (c == 1) { m_RootChunk.SourceRefs.Add(m_RootChunk.SourceRefs[m_RootChunk.SourceRefs.Count - 1]); } else { m_RootChunk.SourceRefs.Add(SourceRef.ReadBinary(br, sourceID)); } } return(baseAddress); }