public int this[object o] { get { if (dic.ContainsKey(o)) return dic[o]; else { dic.Add(o, dic.Count); Constant con = new Constant((ConstantType)(-1), null); con.Value = o; con.Number = dic.Count - 1; if (o is string) { con.Type = ConstantType.String; } else if (o is double) con.Type = ConstantType.Number; else if (o is bool) con.Type = ConstantType.Bool; else if (o == null) { con.Type = ConstantType.Nil; } else throw new Exception("Invalid constant type '" + o.GetType().ToString() + "'!"); b.Chunk.Constants.Add(con); return dic[o]; } }/* set { dic[o] = value; }*/ }
static Chunk ReadFunction() { Chunk c = new Chunk(); c.Name = ReadString(); c.FirstLine = (uint)ReadInt32(); c.LastLine = (ulong)ReadInt32(); c.UpvalueCount = ReadInt8(); // Upvalues c.ArgumentCount = ReadInt8(); c.Vararg = ReadInt8(); c.MaxStackSize = (uint)ReadInt8(); // Instructions long count = ReadInt32(); for (int i = 0; i < count; i++) { uint op = (uint)ReadInt32(); int opcode = (int)Lua.GET_OPCODE(op); //(int)Bit.Get(op, 1, 6); Instruction instr = Instruction.From(op); instr.Number = i; c.Instructions.Add(instr); } // Constants count = ReadInt32(); for (int i = 0; i < count; i++) { Constant cnst = new Constant(0, null); int t = ReadInt8(); cnst.Number = i; if (t == 0) { cnst.Type = ConstantType.Nil; cnst.Value = null; } else if (t == 1) { cnst.Type = ConstantType.Bool; cnst.Value = ReadInt8() != 0; } else if (t == 3) { cnst.Type = ConstantType.Number; cnst.Value = ReadNumber(); } else if (t == 4) { cnst.Type = ConstantType.String; cnst.Value = ReadString(); } c.Constants.Add(cnst); } // Protos count = ReadInt32(); for (int i = 0; i < count; i++) c.Protos.Add(ReadFunction()); // Line numbers count = ReadInt32(); for (int i = 0; i < count; i++) c.Instructions[i].LineNumber = ReadInt32(); // Locals count = ReadInt32(); for (int i = 0; i < count; i++) c.Locals.Add(new Local(ReadString(), ReadInt32(), ReadInt32())); // Upvalues count = ReadInt32(); for (int i = 0; i < count; i++) c.Upvalues.Add(new Upvalue(ReadString())); return c; }