diff --git a/src/kOS.Safe/Compilation/CompiledObject.cs b/src/kOS.Safe/Compilation/CompiledObject.cs index ec7f7f9e05..745f281de1 100644 --- a/src/kOS.Safe/Compilation/CompiledObject.cs +++ b/src/kOS.Safe/Compilation/CompiledObject.cs @@ -168,7 +168,9 @@ public static ulong DecodeNumberFromBytes(byte[] encodedForm) /// private static Dictionary argumentPackFinder; - private static string previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through. + private const string LABEL_INIT = "######"; // bogus value that is ensured to differ from any real value the first time through. + + private static string previousLabel = LABEL_INIT; /// /// Returns the compiled program's opcodes packed into a tight form, that is a direct @@ -185,7 +187,7 @@ public static byte[] Pack(List program) argumentPackLogicalLength = 0; // nothing in the argumentPack yet. argumentPackFinder = new Dictionary(); lineMap = new DebugLineMap(); - previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through. + previousLabel = LABEL_INIT; for (int index = 0 ; index < program.Count ; ++index) // --. This can be replaced with a { // |--- foreach. I do it this way so I @@ -198,7 +200,7 @@ public static byte[] Pack(List program) // Now that we've seen every argument, we know how many bytes are needed // to store the argumentPack, and thus the largest possible index into it. // This will be how many bytes our indeces will be in this packed ML file. - int numArgIndexBytes = FewestBytesToHold(argumentPackLogicalLength); + int numArgIndexBytes = FewestBytesToHold(argumentPackFinder.Count); headBuff.Add((byte)'%'); headBuff.Add((byte)'A'); headBuff.Add(((byte)numArgIndexBytes)); @@ -208,6 +210,7 @@ public static byte[] Pack(List program) headBuff.AddRange(truncatedArgumentPack); + previousLabel = LABEL_INIT; for (int index = 0 ; index < program.Count ; ++index) // --. This can be replaced with a { // |--- foreach. I do it this way so I CodePart codePart = program[index]; // --' can print the index in debugging. @@ -343,8 +346,6 @@ private static byte[] PackCode(List fragment, int argIndexSize, int star /// byte index of where it starts in the argument pack. private static int PackedArgumentLocation(object argument) { - const int LABEL_OFFSET = 3; // Account for the %An at the front of the argument pack. - object arg = argument ?? new PseudoNull(); int returnValue; // bogus starting value before it's calculated. @@ -357,8 +358,8 @@ private static int PackedArgumentLocation(object argument) // When it gets added, it's going to be tacked on right at the end. // We already know that, se let's get that populated now: - argumentPackFinder.Add(arg, LABEL_OFFSET + argumentPackLogicalLength); - returnValue = LABEL_OFFSET + argumentPackLogicalLength; + returnValue = argumentPackFinder.Count; + argumentPackFinder.Add(arg, returnValue); // Borrow C#'s Binary IO writer to pack the object into the byte form, // rather than writing our own for each type: @@ -487,8 +488,8 @@ public static List UnPack(GlobalPath path, string prefix, byte[] conte int argIndexSize; Dictionary arguments = ReadArgumentPack(reader, out argIndexSize); lineMap = new DebugLineMap(); - - previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through. + + previousLabel = LABEL_INIT; int codeStart = 0; @@ -555,14 +556,13 @@ private static Dictionary ReadArgumentPack(BinaryReader reader, out bool sectionEnded = false; while (reader.BaseStream.Position < reader.BaseStream.Length && !(sectionEnded)) { - int offsetLocation = (int)(reader.BaseStream.Position) - startPos; byte argTypeId = reader.ReadByte(); Type argCSharpType; if (typeFromId.TryGetValue(argTypeId, out argCSharpType)) { object arg = ReadSomeBinaryPrimitive(reader, argCSharpType); - returnArgs.Add(offsetLocation,arg); + returnArgs.Add(returnArgs.Count,arg); } else { diff --git a/src/kOS.Safe/Execution/KOSArgMarkerType.cs b/src/kOS.Safe/Execution/KOSArgMarkerType.cs index db081402bc..8b161277d2 100644 --- a/src/kOS.Safe/Execution/KOSArgMarkerType.cs +++ b/src/kOS.Safe/Execution/KOSArgMarkerType.cs @@ -10,6 +10,13 @@ namespace kOS.Safe.Execution /// public class KOSArgMarkerType { + // all instances of KOSArgMarkerType should be considered identical: + public override bool Equals(object o) + { + return o is KOSArgMarkerType; + } + + public override int GetHashCode() { return 0; } public override string ToString() { return "_KOSArgMarker_";