예제 #1
0
        private static void TraverseStack(GlobalState g, LuaState l)
        {
            StkId    o, lim;
            CallInfo ci;

            MarkValue(g, Gt(l));
            lim = l.top;
            for (ci = l.base_ci[0]; ci < l.ci; CallInfo.Inc(ref ci))
            {
                LuaAssert(ci.top <= l.stack_last);
                if (lim < ci.top)
                {
                    lim = ci.top;
                }
            }
            for (o = l.stack[0]; o < l.top; StkId.Inc(ref o))
            {
                MarkValue(g, o);
            }
            for (; o <= lim; StkId.Inc(ref o))
            {
                SetNilValue(o);
            }
            CheckStackSizes(l, lim);
        }
예제 #2
0
        public static int LuaDPosCall(LuaState L, StkId firstResult)
        {
            StkId    res;
            int      wanted, i;
            CallInfo ci;

            if ((L.hookmask & LUA_MASKRET) != 0)
            {
                firstResult = CallRetHooks(L, firstResult);
            }
            ci        = CallInfo.Dec(ref L.ci);
            res       = ci.func;                                 /* res == final position of 1st result */
            wanted    = ci.nresults;
            L.base_   = (ci - 1).base_;                          /* restore base */
            L.savedpc = InstructionPtr.Assign((ci - 1).savedpc); /* restore savedpc */
            /* move results to correct place */
            for (i = wanted; i != 0 && firstResult < L.top; i--)
            {
                SetObj2S(L, res, firstResult);
                res         = res + 1;
                firstResult = firstResult + 1;
            }
            while (i-- > 0)
            {
                SetNilValue(StkId.Inc(ref res));
            }
            L.top = res;
            return(wanted - LUA_MULTRET);         /* 0 iff wanted == LUA_MULTRET */
        }
예제 #3
0
        /* only ANSI way to check whether a pointer points to an array */
        private static int IsInStack(CallInfo ci, TValue o)
        {
            StkId p;

            for (p = ci.base_; p < ci.top; StkId.Inc(ref p))
            {
                if (o == p)
                {
                    return(1);
                }
            }
            return(0);
        }
예제 #4
0
        private static StkId AdjustVarArgs(LuaState L, Proto p, int actual)
        {
            int   i;
            int   nfixargs = p.numparams;
            Table htab = null;
            StkId base_, fixed_;

            for (; actual < nfixargs; ++actual)
            {
                SetNilValue(StkId.Inc(ref L.top));
            }
                #if LUA_COMPAT_VARARG
            if ((p.is_vararg & VARARG_NEEDSARG) != 0)  /* compat. with old-style vararg? */
            {
                int nvar = actual - nfixargs;          /* number of extra arguments */
                lua_assert(p.is_vararg & VARARG_HASARG);
                luaC_checkGC(L);
                luaD_checkstack(L, p.maxstacksize);
                htab = luaH_new(L, nvar, 1);    /* create `arg' table */
                for (i = 0; i < nvar; i++)      /* put extra arguments into `arg' table */
                {
                    setobj2n(L, luaH_setnum(L, htab, i + 1), L.top - nvar + i);
                }
                /* store counter in field `n' */
                setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
            }
                #endif
            /* move fixed parameters to final position */
            fixed_ = L.top - actual; /* first fixed argument */
            base_  = L.top;          /* final position of first argument */
            for (i = 0; i < nfixargs; i++)
            {
                SetObj2S(L, StkId.Inc(ref L.top), fixed_ + i);
                SetNilValue(fixed_ + i);
            }
            /* add `arg' parameter */
            if (htab != null)
            {
                StkId top = L.top;
                StkId.Inc(ref L.top);
                SetHValue(L, top, htab);
                LuaAssert(IsWhite(obj2gco(htab)));
            }
            return(base_);
        }
예제 #5
0
 private static void stack_init(LuaState L1, LuaState L)
 {
     /* initialize CallInfo array */
     L1.base_ci = LuaMNewVector <CallInfo>(L, BASICCISIZE);
     L1.ci      = L1.base_ci[0];
     L1.size_ci = BASICCISIZE;
     L1.end_ci  = L1.base_ci[L1.size_ci - 1];
     /* initialize stack array */
     L1.stack      = LuaMNewVector <TValue>(L, BASICSTACKSIZE + EXTRASTACK);
     L1.stacksize  = BASICSTACKSIZE + EXTRASTACK;
     L1.top        = L1.stack[0];
     L1.stack_last = L1.stack[L1.stacksize - EXTRASTACK - 1];
     /* initialize first ci */
     L1.ci.func = L1.top;
     SetNilValue(StkId.Inc(ref L1.top));        /* `function' entry for this `ci' */
     L1.base_  = L1.ci.base_ = L1.top;
     L1.ci.top = L1.top + LUA_MINSTACK;
 }
예제 #6
0
 public static void LuaSetTop(LuaState L, int idx)
 {
     LuaLock(L);
     if (idx >= 0)
     {
         ApiCheck(L, idx <= L.stack_last - L.base_);
         while (L.top < L.base_ + idx)
         {
             SetNilValue(StkId.Inc(ref L.top));
         }
         L.top = L.base_ + idx;
     }
     else
     {
         ApiCheck(L, -(idx + 1) <= (L.top - L.base_));
         L.top += idx + 1;        /* `subtract' index (index is negative) */
     }
     LuaUnlock(L);
 }
예제 #7
0
        public static void LuaXMove(LuaState from, LuaState to, int n)
        {
            int i;

            if (from == to)
            {
                return;
            }
            LuaLock(to);
            CheckNElements(from, n);
            ApiCheck(from, G(from) == G(to));
            ApiCheck(from, to.ci.top - to.top >= n);
            from.top -= n;
            for (i = 0; i < n; i++)
            {
                SetObj2S(to, StkId.Inc(ref to.top), from.top + i);
            }
            LuaUnlock(to);
        }
예제 #8
0
 public static void IncrTop(LuaState L)
 {
     LuaDCheckStack(L, 1);
     StkId.Inc(ref L.top);
 }
예제 #9
0
        public static int LuaDPreCall(LuaState L, StkId func, int nresults)
        {
            LClosure  cl;
            ptrdiff_t funcr;

            if (!TTIsFunction(func))       /* `func' is not a function? */
            {
                func = TryFuncTM(L, func); /* check the `function' tag method */
            }
            funcr        = SaveStack(L, func);
            cl           = CLValue(func).l;
            L.ci.savedpc = InstructionPtr.Assign(L.savedpc);

            if (cl.isC == 0)        /* Lua function? prepare its call */
            {
                CallInfo ci;
                StkId    st, base_;
                Proto    p = cl.p;
                LuaDCheckStack(L, p.maxstacksize);
                func = RestoreStack(L, funcr);
                if (p.is_vararg == 0)            /* no varargs? */
                {
                    base_ = L.stack[func + 1];
                    if (L.top > base_ + p.numparams)
                    {
                        L.top = base_ + p.numparams;
                    }
                }
                else            /* vararg function */
                {
                    int nargs = L.top - func - 1;
                    base_ = AdjustVarArgs(L, p, nargs);
                    func  = RestoreStack(L, funcr); /* previous call may change the stack */
                }
                ci      = IncCI(L);                 /* now `enter' new function */
                ci.func = func;
                L.base_ = ci.base_ = base_;
                ci.top  = L.base_ + p.maxstacksize;
                LuaAssert(ci.top <= L.stack_last);
                L.savedpc    = new InstructionPtr(p.code, 0);       /* starting point */
                ci.tailcalls = 0;
                ci.nresults  = nresults;
                for (st = L.top; st < ci.top; StkId.Inc(ref st))
                {
                    SetNilValue(st);
                }
                L.top = ci.top;
                if ((L.hookmask & LUA_MASKCALL) != 0)
                {
                    InstructionPtr.inc(ref L.savedpc);        /* hooks assume 'pc' is already incremented */
                    LuaDCallHook(L, LUA_HOOKCALL, -1);
                    InstructionPtr.dec(ref L.savedpc);        /* correct 'pc' */
                }
                return(PCRLUA);
            }
            else          /* if is a C function, call it */
            {
                CallInfo ci;
                int      n;
                LuaDCheckStack(L, LUA_MINSTACK); /* ensure minimum stack size */
                ci      = IncCI(L);              /* now `enter' new function */
                ci.func = RestoreStack(L, funcr);
                L.base_ = ci.base_ = ci.func + 1;
                ci.top  = L.top + LUA_MINSTACK;
                LuaAssert(ci.top <= L.stack_last);
                ci.nresults = nresults;
                if ((L.hookmask & LUA_MASKCALL) != 0)
                {
                    LuaDCallHook(L, LUA_HOOKCALL, -1);
                }
                LuaUnlock(L);
                n = CurrFunc(L).c.f(L); /* do the actual call */
                LuaLock(L);
                if (n < 0)              /* yielding? */
                {
                    return(PCRYIELD);
                }
                else
                {
                    LuaDPosCall(L, L.top - n);
                    return(PCRC);
                }
            }
        }
예제 #10
0
파일: ldo.cs 프로젝트: jiaguoxinzhi/KopiLua
 public static void IncrTop(LinyeeState L)
 {
     LinyeeDCheckStack(L, 1);
     StkId.Inc(ref L.top);
 }
예제 #11
0
 public static void IncrementTop(LuaState L)
 {
     ApiCheck(L, L.top < L.ci.top);
     StkId.Inc(ref L.top);
 }