Ejemplo n.º 1
0
        int ILuaAPI.RawLen(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            switch (addr.V.Tt)
            {
            case (int)LuaType.LUA_TSTRING:
            {
                var s = addr.V.SValue();
                return(s == null ? 0 : s.Length);
            }

            case (int)LuaType.LUA_TUSERDATA:
            {
                return(addr.V.RawUValue().Length);
            }

            case (int)LuaType.LUA_TTABLE:
            {
                return(addr.V.HValue().Length);
            }

            default: return(0);
            }
        }
Ejemplo n.º 2
0
        bool ILuaAPI.Compare(int index1, int index2, LuaEq op)
        {
            StkId addr1;

            if (!Index2Addr(index1, out addr1))
            {
                Utl.InvalidIndex();
            }

            StkId addr2;

            if (!Index2Addr(index2, out addr2))
            {
                Utl.InvalidIndex();
            }

            switch (op)
            {
            case LuaEq.LUA_OPEQ: return(EqualObj(ref addr1.V, ref addr2.V, false));

            case LuaEq.LUA_OPLT: return(V_LessThan(addr1, addr2));

            case LuaEq.LUA_OPLE: return(V_LessEqual(addr1, addr2));

            default: Utl.ApiCheck(false, "invalid option"); return(false);
            }
        }
Ejemplo n.º 3
0
        ThreadStatus ILuaAPI.PCallK(int numArgs, int numResults, int errFunc,
                                    int context, CSharpFunctionDelegate continueFunc)
        {
            Utl.ApiCheck(continueFunc == null || !CI.IsLua,
                         "cannot use continuations inside hooks");
            Utl.ApiCheckNumElems(this, numArgs + 1);
            Utl.ApiCheck(Status == ThreadStatus.LUA_OK,
                         "cannot do calls on non-normal thread");
            CheckResults(numArgs, numResults);

            int func;

            if (errFunc == 0)
            {
                func = 0;
            }
            else
            {
                StkId addr;
                if (!Index2Addr(errFunc, out addr))
                {
                    Utl.InvalidIndex();
                }

                func = addr.Index;
            }

            ThreadStatus status;
            CallS        c = new CallS();

            c.L         = this;
            c.FuncIndex = Top.Index - (numArgs + 1);
            if (continueFunc == null || NumNonYieldable > 0) // no continuation or no yieldable?
            {
                c.NumResults = numResults;
                status       = D_PCall(DG_F_Call, ref c, c.FuncIndex, func);
            }
            else
            {
                int ciIndex = CI.Index;
                CI.ContinueFunc = continueFunc;
                CI.Context      = context;
                CI.ExtraIndex   = c.FuncIndex;
                CI.OldAllowHook = AllowHook;
                CI.OldErrFunc   = ErrFunc;
                ErrFunc         = func;
                CI.CallStatus  |= CallStatus.CIST_YPCALL;

                D_Call(Stack[c.FuncIndex], numResults, true);

                CallInfo ci = BaseCI[ciIndex];
                ci.CallStatus &= ~CallStatus.CIST_YPCALL;
                ErrFunc        = ci.OldErrFunc;
                status         = ThreadStatus.LUA_OK;
            }
            AdjustResults(numResults);
            return(status);
        }
Ejemplo n.º 4
0
        void ILuaAPI.Copy(int fromIndex, int toIndex)
        {
            StkId fr;

            if (!Index2Addr(fromIndex, out fr))
            {
                Utl.InvalidIndex();
            }
            MoveTo(fr, toIndex);
        }
Ejemplo n.º 5
0
        private void MoveTo(StkId fr, int index)
        {
            StkId to;

            if (!Index2Addr(index, out to))
            {
                Utl.InvalidIndex();
            }

            to.V.SetObj(ref fr.V);
        }
Ejemplo n.º 6
0
        void ILuaAPI.PushValue(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            Top.V.SetObj(ref addr.V);
            ApiIncrTop();
        }
Ejemplo n.º 7
0
        void ILuaAPI.GetTable(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            var below = Stack[Top.Index - 1];

            V_GetTable(addr, below, below);
        }
Ejemplo n.º 8
0
        void ILuaAPI.SetField(int index, string key)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            StkId.inc(ref Top).V.SetSValue(key);
            V_SetTable(addr, Stack[Top.Index - 1], Stack[Top.Index - 2]);
            Top = Stack[Top.Index - 2];
        }
Ejemplo n.º 9
0
        void ILuaAPI.Len(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            V_ObjLen(Top, addr);

            ApiIncrTop();
        }
Ejemplo n.º 10
0
        bool ILuaAPI.SetMetaTable(int index)
        {
            Utl.ApiCheckNumElems(this, 1);

            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            var      below = Stack[Top.Index - 1];
            LuaTable mt;

            if (below.V.TtIsNil())
            {
                mt = null;
            }
            else
            {
                Utl.ApiCheck(below.V.TtIsTable(), "table expected");
                mt = below.V.HValue();
            }

            switch (addr.V.Tt)
            {
            case (int)LuaType.LUA_TTABLE:
            {
                var tbl = addr.V.HValue();
                tbl.MetaTable = mt;
                break;
            }

            case (int)LuaType.LUA_TUSERDATA:
            {
                var ud = addr.V.RawUValue();
                ud.MetaTable = mt;
                break;
            }

            default:
            {
                G.MetaTables[addr.V.Tt] = mt;
                break;
            }
            }
            Top = Stack[Top.Index - 1];
            return(true);
        }
Ejemplo n.º 11
0
        public void RawSetI(int index, int n)
        {
            Utl.ApiCheckNumElems(this, 1);
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }
            Utl.ApiCheck(addr.V.TtIsTable(), "table expected");
            var tbl = addr.V.HValue();

            tbl.SetInt(n, ref Stack[Top.Index - 1].V);
            Top = Stack[Top.Index - 1];
        }
Ejemplo n.º 12
0
        void ILuaAPI.GetField(int index, string key)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            Top.V.SetSValue(key);
            var below = Top;

            ApiIncrTop();
            V_GetTable(addr, below, below);
        }
Ejemplo n.º 13
0
        void ILuaAPI.RawSet(int index)
        {
            Utl.ApiCheckNumElems(this, 2);
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }
            Utl.ApiCheck(addr.V.TtIsTable(), "table expected");
            var tbl = addr.V.HValue();

            tbl.Set(ref Stack[Top.Index - 2].V, ref Stack[Top.Index - 1].V);
            Top = Stack[Top.Index - 2];
        }
Ejemplo n.º 14
0
        void ILuaAPI.Remove(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            for (int i = addr.Index + 1; i < Top.Index; ++i)
            {
                Stack[i - 1].V.SetObj(ref Stack[i].V);
            }

            Top = Stack[Top.Index - 1];
        }
Ejemplo n.º 15
0
        void ILuaAPI.SetTable(int index)
        {
            StkId addr;

            Utl.ApiCheckNumElems(this, 2);
            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            var key = Stack[Top.Index - 2];
            var val = Stack[Top.Index - 1];

            V_SetTable(addr, key, val);
            Top = Stack[Top.Index - 2];
        }
Ejemplo n.º 16
0
        void ILuaAPI.Insert(int index)
        {
            StkId p;

            if (!Index2Addr(index, out p))
            {
                Utl.InvalidIndex();
            }

            int i = Top.Index;

            while (i > p.Index)
            {
                Stack[i].V.SetObj(ref Stack[i - 1].V);
                i--;
            }
            p.V.SetObj(ref Top.V);
        }
Ejemplo n.º 17
0
        bool ILuaAPI.GetMetaTable(int index)
        {
            StkId addr;

            if (!Index2Addr(index, out addr))
            {
                Utl.InvalidIndex();
            }

            LuaTable mt;

            switch (addr.V.Tt)
            {
            case (int)LuaType.LUA_TTABLE:
            {
                var tbl = addr.V.HValue();
                mt = tbl.MetaTable;
                break;
            }

            case (int)LuaType.LUA_TUSERDATA:
            {
                var ud = addr.V.RawUValue();
                mt = ud.MetaTable;
                break;
            }

            default:
            {
                mt = G.MetaTables[addr.V.Tt];
                break;
            }
            }
            if (mt == null)
            {
                return(false);
            }
            else
            {
                Top.V.SetHValue(mt);
                ApiIncrTop();
                return(true);
            }
        }