public void BinDumpBinaryStreams_TestStringWrites() { string[] values = new string[] { "hello", "you", "fool", "hello", "I", "love", "you" }; using (MemoryStream ms_orig = new MemoryStream()) { UndisposableStream ms = new UndisposableStream(ms_orig); using (BinDumpBinaryWriter bdbw = new BinDumpBinaryWriter(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { bdbw.Write(values[i]); } } ms.Seek(0, SeekOrigin.Begin); using (BinDumpBinaryReader bdbr = new BinDumpBinaryReader(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { string v = bdbr.ReadString(); Assert.AreEqual(values[i], v, "i = " + i.ToString()); } } } }
public void BinDumpBinaryStreams_TestUIntWrites() { uint[] values = new uint[] { 0, 1, 0x7F, 10, 0x7E, 32767, 32768, uint.MinValue, uint.MaxValue }; using (MemoryStream ms_orig = new MemoryStream()) { UndisposableStream ms = new UndisposableStream(ms_orig); using (BinDumpBinaryWriter bdbw = new BinDumpBinaryWriter(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { bdbw.Write(values[i]); } } ms.Seek(0, SeekOrigin.Begin); using (BinDumpBinaryReader bdbr = new BinDumpBinaryReader(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { uint v = bdbr.ReadUInt32(); Assert.AreEqual(values[i], v, "i = " + i.ToString()); } } } }
internal int Dump(Stream stream, int baseAddress, bool hasUpvalues) { using (BinaryWriter bw = new BinDumpBinaryWriter(stream, Encoding.UTF8)) { Dictionary<SymbolRef, int> symbolMap = new Dictionary<SymbolRef, int>(); Instruction meta = FindMeta(ref baseAddress); if (meta == null) throw new ArgumentException("baseAddress"); bw.Write(DUMP_CHUNK_MAGIC); bw.Write(DUMP_CHUNK_VERSION); bw.Write(hasUpvalues); bw.Write(meta.NumVal); for (int i = 0; i <= meta.NumVal; i++) { SymbolRef[] symbolList; SymbolRef symbol; m_RootChunk.Code[baseAddress + i].GetSymbolReferences(out symbolList, out symbol); if (symbol != null) AddSymbolToMap(symbolMap, symbol); if (symbolList != null) foreach (var s in symbolList) AddSymbolToMap(symbolMap, s); } foreach (SymbolRef sr in symbolMap.Keys.ToArray()) { if (sr.i_Env != null) AddSymbolToMap(symbolMap, sr.i_Env); } SymbolRef[] allSymbols = new SymbolRef[symbolMap.Count]; foreach (KeyValuePair<SymbolRef, int> pair in symbolMap) { allSymbols[pair.Value] = pair.Key; } bw.Write(symbolMap.Count); foreach (SymbolRef sym in allSymbols) sym.WriteBinary(bw); foreach (SymbolRef sym in allSymbols) sym.WriteBinaryEnv(bw, symbolMap); for (int i = 0; i <= meta.NumVal; i++) m_RootChunk.Code[baseAddress + i].WriteBinary(bw, baseAddress, symbolMap); return meta.NumVal + baseAddress + 1; } }
internal int Dump(Stream stream, int baseAddress, bool hasUpvalues) { using (BinaryWriter bw = new BinDumpBinaryWriter(stream, Encoding.UTF8)) { var symbolMap = new Dictionary<SymbolRef, int>(); var meta = m_RootChunk.Code[baseAddress]; // skip nops while (meta.OpCode == OpCode.Nop) { baseAddress++; meta = m_RootChunk.Code[baseAddress]; } if (meta.OpCode != OpCode.FuncMeta) throw new ArgumentException("baseAddress"); bw.Write(DUMP_CHUNK_MAGIC); bw.Write(DUMP_CHUNK_VERSION); bw.Write(hasUpvalues); bw.Write(meta.NumVal); for (var i = 0; i <= meta.NumVal; i++) { SymbolRef[] symbolList; SymbolRef symbol; m_RootChunk.Code[baseAddress + i].GetSymbolReferences(out symbolList, out symbol); if (symbol != null) AddSymbolToMap(symbolMap, symbol); if (symbolList != null) foreach (var s in symbolList) AddSymbolToMap(symbolMap, s); } foreach (var sr in symbolMap.Keys.ToArray()) { if (sr.i_Env != null) AddSymbolToMap(symbolMap, sr.i_Env); } var allSymbols = new SymbolRef[symbolMap.Count]; foreach (var pair in symbolMap) { allSymbols[pair.Value] = pair.Key; } bw.Write(symbolMap.Count); foreach (var sym in allSymbols) sym.WriteBinary(bw); foreach (var sym in allSymbols) sym.WriteBinaryEnv(bw, symbolMap); for (var i = 0; i <= meta.NumVal; i++) m_RootChunk.Code[baseAddress + i].WriteBinary(bw, baseAddress, symbolMap); return meta.NumVal + baseAddress + 1; } }