Esempio n. 1
0
        public int ReadSizeT()
        {
            if (SizeOfSizeT <= 0)
            {
                throw new Exception("sizeof(size_t) is not valid:" + SizeOfSizeT);
            }

            var    bytes = ReadBytes(SizeOfSizeT);
            UInt64 ret;

            switch (SizeOfSizeT)
            {
            case 4:
                ret = BitConverter.ToUInt32(bytes, 0);
                break;

            case 8:
                ret = BitConverter.ToUInt64(bytes, 0);
                break;

            default:
                throw new NotImplementedException();
            }

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadSizeT: " + ret);
#endif

            if (ret > Int32.MaxValue)
            {
                throw new NotImplementedException();
            }

            return((int)ret);
        }
        public static int B_Print(ILuaState lua)
        {
            StringBuilder sb = new StringBuilder();
            int           n  = lua.GetTop();

            lua.GetGlobal("tostring");
            for (int i = 1; i <= n; ++i)
            {
                lua.PushValue(-1);
                lua.PushValue(i);
                lua.Call(1, 1);
                string s = lua.ToString(-1);
                if (s == null)
                {
                    return(lua.L_Error("'tostring' must return a string to 'print'"));
                }
                if (i > 1)
                {
                    sb.Append("\t");
                }
                sb.Append(s);
                lua.Pop(1);
            }
            ULDebug.Log(sb.ToString());
            return(0);
        }
Esempio n. 3
0
        private void LoadConstants(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("Load Constants:" + n);
#endif
            proto.K.Clear();
            for (int i = 0; i < n; ++i)
            {
                int t = (int)LoadByte();
#if DEBUG_UNDUMP
                ULDebug.Log("Constant Type:" + t);
#endif
                var v = new StkId();
                switch (t)
                {
                case (int)LuaType.LUA_TNIL:
                    v.V.SetNilValue();
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TBOOLEAN:
                    v.V.SetBValue(LoadBoolean());
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TNUMBER:
                    v.V.SetNValue(LoadNumber());
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TSTRING:
#if DEBUG_UNDUMP
                    ULDebug.Log("LuaType.LUA_TSTRING");
#endif
                    v.V.SetSValue(LoadString());
                    proto.K.Add(v);
                    break;

                default:
                    throw new UndumpException(
                              "LoadConstants unknown type: " + t);
                }
            }

            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load Functions:" + n);
#endif
            proto.P.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.P.Add(LoadFunction());
            }
        }
Esempio n. 4
0
        public double ReadDouble()
        {
            var    bytes = ReadBytes(8);
            double ret   = BitConverter.ToDouble(bytes, 0);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadDouble: " + ret);
#endif
            return(ret);
        }
Esempio n. 5
0
        public SizeT ReadSizeT()
        {
            var bytes = ReadBytes(PlatformCfg.SizeTypeLength);
            var ret   = PlatformCfg.ToSizeT(bytes, 0);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadSizeT: " + ret);
#endif
            return(ret);
        }
Esempio n. 6
0
        public uint ReadUInt()
        {
            var  bytes = ReadBytes(4);
            uint ret   = BitConverter.ToUInt32(bytes, 0);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadUInt: " + ret);
#endif
            return(ret);
        }
Esempio n. 7
0
        private LuaUpvalue F_FindUpval(StkId level)
        {
#if DEBUG_FIND_UPVALUE
            ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> level:" + level);
#endif

            var node = OpenUpval.First;
            LinkedListNode <LuaUpvalue> prev = null;
            while (node != null)
            {
                var upval = node.Value;
#if DEBUG_FIND_UPVALUE
                ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> upval.V:" + upval.V);
#endif
                if (upval.V.Index < level.Index)
                {
                    break;
                }

                var next = node.Next;
                if (upval.V == level)
                {
                    return(upval);
                }

                prev = node;
                node = next;
            }

            // not found: create a new one
            var ret = new LuaUpvalue();
            ret.V = level;
            // ret.Prev = G.UpvalHead;
            // ret.Next = G.UpvalHead.Next;
            // ret.Next.Prev = ret;
            // G.UpvalHead.Next = ret;

            if (prev == null)
            {
                OpenUpval.AddFirst(ret);
            }
            else
            {
                OpenUpval.AddAfter(prev, ret);
            }

#if DEBUG_FIND_UPVALUE
            ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> create new one:" + ret.V);
#endif

            return(ret);
        }
Esempio n. 8
0
        public byte ReadByte()
        {
            var c = LoadInfo.ReadByte();

            if (c == -1)
            {
                throw new UndumpException("truncated");
            }
#if DEBUG_BINARY_READER
            ULDebug.Log("ReadBytes: " + c);
#endif
            return((byte)c);
        }
Esempio n. 9
0
        private void LoadHeader()
        {
            byte[] header = LoadBytes(4   // Signature
                                      + 8 // version, format version, size of int ... etc
                                      + 6 // Tail
                                      );
            byte v = header[4             /* skip signature */
                            + 4           /* offset of sizeof(size_t) */
                     ];

#if DEBUG_UNDUMP
            ULDebug.Log(string.Format("sizeof(size_t): {0}", v));
#endif
            Reader.SizeOfSizeT = v;
        }
Esempio n. 10
0
        private void LoadCode(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("LoadCode n:" + n);
#endif
            proto.Code.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.Code.Add(LoadInstruction());
#if DEBUG_UNDUMP
                ULDebug.Log("Count:" + proto.Code.Count);
                ULDebug.Log("LoadInstruction:" + proto.Code[proto.Code.Count - 1]);
#endif
            }
        }
Esempio n. 11
0
 private void DumpParts()
 {
     ULDebug.Log("------------------ [DumpParts] enter -----------------------");
     ULDebug.Log("<< Array Part >>");
     for (var i = 0; i < ArrayPart.Length; ++i)
     {
         var n = ArrayPart[i];
         ULDebug.Log(string.Format("i:{0} val:{1}", i, n.V));
     }
     ULDebug.Log("<< Hash Part >>");
     for (var i = 0; i < HashPart.Length; ++i)
     {
         var n    = HashPart[i];
         var next = (n.Next == null) ? -1 : n.Next.Index;
         ULDebug.Log(string.Format("i:{0} index:{1} key:{2} val:{3} next:{4}", i, n.Index, n.Key.V, n.Val.V, next));
     }
     ULDebug.Log("++++++++++++++++++ [DumpParts] leave +++++++++++++++++++++++");
 }
Esempio n. 12
0
        private void LoadUpvalues(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("Load Upvalues:" + n);
#endif
            proto.Upvalues.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.Upvalues.Add(
                    new UpvalDesc()
                {
                    Name    = null,
                    InStack = LoadBoolean(),
                    Index   = (int)LoadByte()
                });
            }
        }
Esempio n. 13
0
        private LuaProto LoadFunction()
        {
#if DEBUG_UNDUMP
            ULDebug.Log("LoadFunction enter");
#endif

            LuaProto proto = new LuaProto();
            proto.LineDefined     = LoadInt();
            proto.LastLineDefined = LoadInt();
            proto.NumParams       = LoadByte();
            proto.IsVarArg        = LoadBoolean();
            proto.MaxStackSize    = LoadByte();

            LoadCode(proto);
            LoadConstants(proto);
            LoadUpvalues(proto);
            LoadDebug(proto);
            return(proto);
        }
Esempio n. 14
0
        public string ReadString()
        {
            var n = ReadSizeT();

            if (n == 0)
            {
                return(null);
            }

            var bytes = ReadBytes(n);

            // n=1: removing trailing '\0'
            string ret = System.Text.Encoding.UTF8.GetString(bytes, 0, n - 1);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadString n:" + n + " ret:" + ret);
#endif
            return(ret);
        }
Esempio n. 15
0
        private void LoadDebug(LuaProto proto)
        {
            int n;

            proto.Source = LoadString();

            // LineInfo
            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load LineInfo:" + n);
#endif
            proto.LineInfo.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.LineInfo.Add(LoadInt());
            }

            // LocalVar
            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load LocalVar:" + n);
#endif
            proto.LocVars.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.LocVars.Add(
                    new LocVar()
                {
                    VarName = LoadString(),
                    StartPc = LoadInt(),
                    EndPc   = LoadInt(),
                });
            }

            // Upvalues' name
            n = LoadInt();
            for (int i = 0; i < n; ++i)
            {
                proto.Upvalues[i].Name = LoadString();
            }
        }
Esempio n. 16
0
        private int D_PosCall(int firstResultIndex)
        {
            // TODO: hook
            // be careful: CI may be changed after hook

            int resIndex = CI.FuncIndex;
            int wanted   = CI.NumResults;

#if DEBUG_D_POS_CALL
            ULDebug.Log("[D] ==== PosCall enter");
            ULDebug.Log("[D] ==== PosCall res:" + res);
            ULDebug.Log("[D] ==== PosCall wanted:" + wanted);
#endif

            CI = BaseCI[CI.Index - 1];

            int i = wanted;
            for (; i != 0 && firstResultIndex < Top.Index; --i)
            {
#if DEBUG_D_POS_CALL
                ULDebug.Log("[D] ==== PosCall assign lhs res:" + res);
                ULDebug.Log("[D] ==== PosCall assign rhs firstResult:" + firstResult);
#endif
                Stack[resIndex++].V.SetObj(ref Stack[firstResultIndex++].V);
            }

            while (i-- > 0)
            {
#if DEBUG_D_POS_CALL
                ULDebug.Log("[D] ==== PosCall new LuaNil()");
#endif
                Stack[resIndex++].V.SetNilValue();
            }

            Top = Stack[resIndex];
#if DEBUG_D_POS_CALL
            ULDebug.Log("[D] ==== PosCall return " + (wanted - LuaDef.LUA_MULTRET));
#endif
            return(wanted - LuaDef.LUA_MULTRET);
        }
Esempio n. 17
0
        public byte[] ReadBytes(int count)
        {
            byte[] ret = new byte[count];
            for (int i = 0; i < count; ++i)
            {
                var c = LoadInfo.ReadByte();
                if (c == -1)
                {
                    throw new UndumpException("truncated");
                }
                ret[i] = (byte)c;
            }
#if DEBUG_BINARY_READER
            var sb = new System.Text.StringBuilder();
            sb.Append("ReadBytes:");
            for (var i = 0; i < ret.Length; ++i)
            {
                sb.Append(string.Format(" {0:X02}", ret[i]));
            }
            ULDebug.Log(sb.ToString());
#endif
            return(ret);
        }
Esempio n. 18
0
        /// <summary>
        /// return true if function has been executed
        /// </summary>
        private bool D_PreCall(StkId func, int nResults)
        {
            // prepare for Lua call

#if DEBUG_D_PRE_CALL
            ULDebug.Log("============================ D_PreCall func:" + func);
#endif

            int funcIndex = func.Index;
            if (!func.V.TtIsFunction())
            {
                // not a function
                // retry with `function' tag method
                func = tryFuncTM(func);

                // now it must be a function
                return(D_PreCall(func, nResults));
            }

            if (func.V.ClIsLuaClosure())
            {
                var cl = func.V.ClLValue();
                Utl.Assert(cl != null);
                var p = cl.Proto;

                D_CheckStack(p.MaxStackSize + p.NumParams);
                func = Stack[funcIndex];

                // 补全参数
                int n = (Top.Index - func.Index) - 1;
                for ( ; n < p.NumParams; ++n)
                {
                    StkId.inc(ref Top).V.SetNilValue();
                }

                int stackBase = (!p.IsVarArg) ? (func.Index + 1) : AdjustVarargs(p, n);

                CI            = ExtendCI();
                CI.NumResults = nResults;
                CI.FuncIndex  = func.Index;
                CI.BaseIndex  = stackBase;
                CI.TopIndex   = stackBase + p.MaxStackSize;
                Utl.Assert(CI.TopIndex <= StackLast);
                CI.SavedPc    = new InstructionPtr(p.Code, 0);
                CI.CallStatus = CallStatus.CIST_LUA;

                Top = Stack[CI.TopIndex];

                return(false);
            }

            if (func.V.ClIsCsClosure())
            {
                var cscl = func.V.ClCsValue();
                Utl.Assert(cscl != null);

                D_CheckStack(LuaDef.LUA_MINSTACK);
                func = Stack[funcIndex];

                CI            = ExtendCI();
                CI.NumResults = nResults;
                CI.FuncIndex  = func.Index;
                CI.TopIndex   = Top.Index + LuaDef.LUA_MINSTACK;
                CI.CallStatus = CallStatus.CIST_NONE;

                // do the actual call
                int n = cscl.F(this);

                // poscall
                D_PosCall(Top.Index - n);

                return(true);
            }

            throw new System.NotImplementedException();
        }
Esempio n. 19
0
        public void DumpStack(int baseIndex, string tag = "")
        {
#if ENABLE_DUMP_STACK
            ULDebug.Log(DumpStackToString(baseIndex, tag));
#endif
        }