public LuaFunction(BinaryReader reader, LuaModule module)
        {
            Module = module;

            Source = module.ReadString(reader);
            LineDefined = module.ReadInt(reader);
            UpValueCount = reader.ReadByte();
            ParameterCount = reader.ReadByte();
            HasVariableArguments = reader.ReadByte() != 0;
            MaxStackSize = reader.ReadByte();

            var lineInfoCount = module.ReadInt(reader);
            LineInfo = new Codex<int>(reader.ReadArrayInt32(lineInfoCount));

            var localVariableCount = module.ReadInt(reader);
            LuaLocalVariable[] localVariables = new LuaLocalVariable[localVariableCount];
            for (int index = 0; index < localVariableCount; index++)
                localVariables[index] = new LuaLocalVariable(reader, this);
            LocalVariables = new Codex<LuaLocalVariable>(localVariables);

            var upValueCount = module.ReadInt(reader);
            string[] upValues = new string[upValueCount];
            for (int index = 0; index < upValueCount; index++)
                upValues[index] = module.ReadString(reader);
            UpValues = new Codex<string>(upValues);

            var constantCount = module.ReadInt(reader);
            Codex<object> constants = new Codex<object>(constantCount);
            Constants = constants;
            for (int index = 0; index < constantCount; index++) {
                LuaType type = (LuaType)reader.ReadByte();

                switch (type) {
                    case LuaType.Nil: break;
                    case LuaType.Boolean: constants.Add(reader.ReadByte() != 0); break;
                    case LuaType.Number: constants.Add(module.ReadNumber(reader)); break;
                    case LuaType.String: constants.Add(module.ReadString(reader)); break;
                    default: throw new NotSupportedException();
                }
            }

            var functionCount = module.ReadInt(reader);
            LuaFunction[] functions = new LuaFunction[functionCount];
            for (int index = 0; index < functionCount; index++)
                functions[index] = new LuaFunction(reader, index, this);
            Closures = new Codex<LuaFunction>(functions);

            var codeCount = module.ReadInt(reader);
            LuaInstruction[] code = new LuaInstruction[codeCount];
            for (int index = 0; index < codeCount; index++)
                code[index] = new LuaInstruction(this, module.ReadInstructionCode(reader));
            Instructions = new Codex<LuaInstruction>(code);
        }
Beispiel #2
0
        public LuaFunction(BinaryReader reader, LuaModule module)
        {
            Module = module;

            Source               = module.ReadString(reader);
            LineDefined          = module.ReadInt(reader);
            UpValueCount         = reader.ReadByte();
            ParameterCount       = reader.ReadByte();
            HasVariableArguments = reader.ReadByte() != 0;
            MaxStackSize         = reader.ReadByte();

            var lineInfoCount = module.ReadInt(reader);

            LineInfo = new Codex <int>(reader.ReadArrayInt32(lineInfoCount));

            var localVariableCount = module.ReadInt(reader);

            LuaLocalVariable[] localVariables = new LuaLocalVariable[localVariableCount];
            for (int index = 0; index < localVariableCount; index++)
            {
                localVariables[index] = new LuaLocalVariable(reader, this);
            }
            LocalVariables = new Codex <LuaLocalVariable>(localVariables);

            var upValueCount = module.ReadInt(reader);

            string[] upValues = new string[upValueCount];
            for (int index = 0; index < upValueCount; index++)
            {
                upValues[index] = module.ReadString(reader);
            }
            UpValues = new Codex <string>(upValues);

            var            constantCount = module.ReadInt(reader);
            Codex <object> constants     = new Codex <object>(constantCount);

            Constants = constants;
            for (int index = 0; index < constantCount; index++)
            {
                LuaType type = (LuaType)reader.ReadByte();

                switch (type)
                {
                case LuaType.Nil: break;

                case LuaType.Boolean: constants.Add(reader.ReadByte() != 0); break;

                case LuaType.Number: constants.Add(module.ReadNumber(reader)); break;

                case LuaType.String: constants.Add(module.ReadString(reader)); break;

                default: throw new NotSupportedException();
                }
            }

            var functionCount = module.ReadInt(reader);

            LuaFunction[] functions = new LuaFunction[functionCount];
            for (int index = 0; index < functionCount; index++)
            {
                functions[index] = new LuaFunction(reader, index, this);
            }
            Closures = new Codex <LuaFunction>(functions);

            var codeCount = module.ReadInt(reader);

            LuaInstruction[] code = new LuaInstruction[codeCount];
            for (int index = 0; index < codeCount; index++)
            {
                code[index] = new LuaInstruction(this, module.ReadInstructionCode(reader));
            }
            Instructions = new Codex <LuaInstruction>(code);
        }