/// <summary> /// Writes this instance to a binary stream /// </summary> internal void WriteBinary(BinDumpWriter bw) { bw.WriteByte((byte)this.i_Type); bw.WriteVarInt32(i_Index); bw.WriteString(i_Name); }
internal int Dump(Stream stream, int baseAddress, bool hasUpvalues, bool writeSourceRefs) { var bw = new BinDumpWriter(stream); Dictionary <SymbolRef, int> symbolMap = new Dictionary <SymbolRef, int>(); Instruction?meta = FindMeta(ref baseAddress); if (meta == null) { throw new ArgumentException("baseAddress"); } bw.WriteUInt64(DUMP_CHUNK_MAGIC); bw.WriteByte(DUMP_CHUNK_VERSION); bw.WriteBoolean(hasUpvalues); bw.WriteVarUInt32((uint)meta.Value.NumVal); for (int i = 0; i <= meta.Value.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.WriteVarUInt32((uint)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.Value.NumVal; i++) { m_RootChunk.Code[baseAddress + i].WriteBinary(bw, baseAddress, symbolMap); } for (int i = 0; i <= meta.Value.NumVal; i++) { if (m_RootChunk.SourceRefs[baseAddress + i] == null || !writeSourceRefs) { bw.WriteByte(0); } else if (i != 0 && m_RootChunk.SourceRefs[baseAddress + i] == m_RootChunk.SourceRefs[baseAddress + i - 1]) { bw.WriteByte(1); } else { bw.WriteByte(2); m_RootChunk.SourceRefs[baseAddress + i].WriteBinary(bw); } } return(meta.Value.NumVal + baseAddress + 1); }