Example #1
0
    public static bool Load(string filename, out ScriptContext context, ErrorManager errorHandler)
    {
        try
        {
            FileStream fs = new FileStream(filename, FileMode.Open);

            return(Load(fs, out context, errorHandler));
        }
        catch (System.Exception e)
        {
            errorHandler.ByteCodeLogError("Log Error");
            context = new ScriptContext();

            return(false);
        }
    }
Example #2
0
    public static bool Load(Stream s, out ScriptContext context, ErrorManager errorHandler)
    {
        context           = new ScriptContext();
        context.hostFuncs = new List <HostFuncs>();
        context.CallStack = new Stack <CallStack>();

        s.Seek(0, SeekOrigin.Begin);

        BinaryReader br = new BinaryReader(s);

        Header header = new Header();

        header.Magic = br.ReadString();

        if (header.Magic != HeaderConst.Magic)
        {
            br.Close();

            errorHandler.ByteCodeLogError("Bad Magic");
            return(false);
        }

        // TODO: check for version number
        header.MajorVersion = br.ReadByte();
        header.MinorVersion = br.ReadByte();

        header.InstructionsCount = br.ReadInt32();
        header.PCStartIdx        = br.ReadInt32();

        header.StackSize      = br.ReadInt32();
        header.GlobalVarsSize = br.ReadInt32();

        context.instrStream.StartPC      = context.instrStream.PC = header.PCStartIdx;
        context.instrStream.Instructions = new Instruction[header.InstructionsCount];

        context.stack.Elements      = new Value[header.StackSize];
        context.stack.StackStartIdx = header.GlobalVarsSize;
        context.stack.TopStackIdx   = 0;

        for (int i = 0; i < header.InstructionsCount; i++)
        {
            Instruction inst = new Instruction();

            inst.OpCode = br.ReadInt32();

            int valuesCount = br.ReadInt32();

            if (valuesCount > 0)
            {
                inst.Values = new Value [valuesCount];

                for (int j = 0; j < inst.Values.Length; j++)
                {
                    OpType type = (OpType)br.ReadInt32();

                    inst.Values[j].Type = type;

                    switch (type)
                    {
                    case OpType.Int:
                    case OpType.AbsMemIdx:
                    case OpType.RelMemIdx:
                    case OpType.ArgMemIdx:
                    case OpType.InstrIdx:
                    case OpType.FuncIdx:
                        inst.Values[j].IntLiteral = br.ReadInt32();
                        break;

                    case OpType.Float:
                        inst.Values[j].FloatLiteral = br.ReadSingle();
                        break;

                    case OpType.String:
                    case OpType.HostAPICallString:
                        inst.Values[j].StringLiteral = br.ReadString();
                        break;
                    }
                }
            }

            context.instrStream.Instructions[i] = inst;
        }

        int count = br.ReadInt32();

        context.Funcs = new List <Function>();
        Function newFunc = new Function();

        for (int i = 0; i < count; i++)
        {
            newFunc.StartIdx     = br.ReadInt32();
            newFunc.frameSize    = br.ReadInt32();
            newFunc.argFrameSize = br.ReadInt32();
            context.Funcs.Add(newFunc);
        }

        br.Close();

        return(true);
    }