예제 #1
0
        public static void lua_insert(lua_State L, int idx)
        {
            StkId p;
            StkId q;

            p = index2adr(L, idx);
            api_checkvalidindex(L, p);
            for (q = L.top; q > p; StkId.dec(ref q))
            {
                setobjs2s(L, q, q - 1);
            }
            setobjs2s(L, p, L.top);
        }
예제 #2
0
        public static void lua_rawseti(lua_State L, int idx, lua_Integer n)
        {
            StkId t;

            lua_lock(L);
            api_checknelems(L, 1);
            t = index2addr(L, idx);
            api_check(L, ttistable(t), "table expected");
            luaH_setint(L, hvalue(t), n, L.top - 1);
            luaC_barrierback(L, gcvalue(t), L.top - 1);
            StkId.dec(ref L.top);
            lua_unlock(L);
        }
예제 #3
0
		static StkId tryfuncTM (lua_State L, StkId func) {
		  /*const*/ TValue tm = luaT_gettmbyobj(L, func, TMS.TM_CALL);
		  StkId p;
		  ptrdiff_t funcr = savestack(L, func);
		  if (!ttisfunction(tm))
			luaG_typeerror(L, func, "call");
		  /* Open a hole inside the stack at `func' */
		  for (p = L.top; p > func; StkId.dec(ref p)) setobjs2s(L, p, p - 1);
		  incr_top(L);
		  func = restorestack(L, funcr);  /* previous call may change stack */
		  setobj2s(L, func, tm);  /* tag method is the new function to be called */
		  return func;
		}
예제 #4
0
 public static void lua_replace(lua_State L, int idx)
 {
     lua_lock(L);
     /* explicit test for incompatible code */
     if (idx == LUA_ENVIRONINDEX && L.ci.previous == null)
     {
         luaG_runerror(L, "no calling environment");
     }
     api_checknelems(L, 1);
     moveto(L, L.top - 1, idx);
     StkId.dec(ref L.top);
     lua_unlock(L);
 }
예제 #5
0
        public static void lua_rawseti(lua_State L, int idx, int n)
        {
            StkId o;

            lua_lock(L);
            api_checknelems(L, 1);
            o = index2adr(L, idx);
            api_check(L, ttistable(o));
            setobj2t(L, luaH_setnum(L, hvalue(o), n), L.top - 1);
            luaC_barriert(L, hvalue(o), L.top - 1);
            StkId.dec(ref L.top);
            lua_unlock(L);
        }
예제 #6
0
        public static CharPtr lua_setlocal(lua_State L, lua_Debug ar, int n)
        {
            CallInfo ci   = L.base_ci[ar.i_ci];
            CharPtr  name = findlocal(L, ci, n);

            lua_lock(L);
            if (name != null)
            {
                setobjs2s(L, ci.base_[n - 1], L.top - 1);
            }
            StkId.dec(ref L.top);        /* pop value */
            lua_unlock(L);
            return(name);
        }
예제 #7
0
        public static CharPtr lua_setlocal(lua_State L, lua_Debug ar, int n)
        {
            StkId   pos  = null;     /* to avoid warnings */
            CharPtr name = findlocal(L, ar.i_ci, n, ref pos);

            lua_lock(L);
            if (name != null)
            {
                setobjs2s(L, pos, L.top - 1);
                StkId.dec(ref L.top);      /* pop value */
            }
            lua_unlock(L);
            return(name);
        }
예제 #8
0
        public static void lua_remove(lua_State L, int idx)
        {
            StkId p;

            lua_lock(L);
            p = index2addr(L, idx);
            api_checkstackindex(L, idx, p);
            while ((p = p[1]) < L.top)
            {
                setobjs2s(L, p - 1, p);
            }
            StkId.dec(ref L.top);
            lua_unlock(L);
        }
예제 #9
0
        public static void lua_setfield(lua_State L, int idx, CharPtr k)
        {
            StkId  t;
            TValue key = new TValue();

            lua_lock(L);
            api_checknelems(L, 1);
            t = index2adr(L, idx);
            api_checkvalidindex(L, t);
            setsvalue(L, key, luaS_new(L, k));
            luaV_settable(L, t, key, L.top - 1);
            StkId.dec(ref L.top);        /* pop value */
            lua_unlock(L);
        }
예제 #10
0
        public static int lua_setmetatable(LuaState L, int objindex)
        {
            TValue obj;
            Table  mt;

            lua_lock(L);
            api_checknelems(L, 1);
            obj = index2adr(L, objindex);
            api_checkvalidindex(L, obj);
            if (ttisnil(L.top - 1))
            {
                mt = null;
            }
            else
            {
                api_check(L, ttistable(L.top - 1));
                mt = hvalue(L.top - 1);
            }
            switch (ttype(obj))
            {
            case LUA_TTABLE:
            {
                hvalue(obj).metatable = mt;
                if (mt != null)
                {
                    luaC_objbarriert(L, hvalue(obj), mt);
                }
                break;
            }

            case LUA_TUSERDATA:
            {
                uvalue(obj).metatable = mt;
                if (mt != null)
                {
                    luaC_objbarrier(L, rawuvalue(obj), mt);
                }
                break;
            }

            default:
            {
                G(L).mt[ttype(obj)] = mt;
                break;
            }
            }
            StkId.dec(ref L.top);
            lua_unlock(L);
            return(1);
        }
예제 #11
0
        public static int lua_setmetatable(lua_State L, int objindex)
        {
            TValue obj;
            Table  mt;

            lua_lock(L);
            api_checknelems(L, 1);
            obj = index2addr(L, objindex);
            api_checkvalidindex(L, obj);
            if (ttisnil(L.top - 1))
            {
                mt = null;
            }
            else
            {
                api_check(L, ttistable(L.top - 1), "table expected");
                mt = hvalue(L.top - 1);
            }
            switch (ttypenv(obj))
            {
            case LUA_TTABLE: {
                hvalue(obj).metatable = mt;
                if (mt != null)
                {
                    luaC_objbarrierback(L, gcvalue(obj), mt);
                }
                luaC_checkfinalizer(L, gcvalue(obj), mt); //FIXME: changed, delete space
                break;
            }

            case LUA_TUSERDATA: {
                uvalue(obj).metatable = mt;
                if (mt != null)
                {
                    luaC_objbarrier(L, rawuvalue(obj), mt);
                    luaC_checkfinalizer(L, gcvalue(obj), mt);
                }
                break;
            }

            default: {
                G(L).mt[ttypenv(obj)] = mt;
                break;
            }
            }
            StkId.dec(ref L.top);
            lua_unlock(L);
            return(1);
        }
예제 #12
0
        public static TString luaX_newstring(LexState ls, CharPtr str, uint l)
        {
            lua_State L = ls.L;
            TValue    o; /* entry for `str' */
            TString   ts = luaS_newlstr(L, str, l);

            setsvalue2s(L, StkId.inc(ref L.top), ts);        /* anchor string */
            o = luaH_setstr(L, ls.fs.h, ts);
            if (ttisnil(o))
            {
                setbvalue(o, 1);      /* make sure `str' will not be collected */
            }
            StkId.dec(ref L.top);
            return(ts);
        }
예제 #13
0
        public static CharPtr lua_setlocal(lua_State L, lua_Debug ar, int n)
        {
            CallInfo ci   = ar.i_ci;
            StkId    pos  = new StkId();
            CharPtr  name = findlocal(L, ci, n, ref pos);

            lua_lock(L);
            if (name != null)
            {
                setobjs2s(L, pos, L.top - 1);
            }
            StkId.dec(ref L.top);        /* pop value */
            lua_unlock(L);
            return(name);
        }
예제 #14
0
        private static void callTMres(lua_State L, StkId res, TValue f,
                                      TValue p1, TValue p2)
        {
            ptrdiff_t result = savestack(L, res);

            setobj2s(L, L.top, f);        /* push function */
            setobj2s(L, L.top + 1, p1);   /* 1st argument */
            setobj2s(L, L.top + 2, p2);   /* 2nd argument */
            luaD_checkstack(L, 3);
            L.top += 3;
            luaD_call(L, L.top - 3, 1);
            res = restorestack(L, result);
            StkId.dec(ref L.top);
            setobjs2s(L, res, L.top);
        }
예제 #15
0
        public static void lua_insert(lua_State L, int idx)
        {
            StkId p;
            StkId q;

            lua_lock(L);
            p = index2addr(L, idx);
            api_checkstackindex(L, idx, p);
            for (q = L.top; q > p; StkId.dec(ref q))      /* use L->top as a temporary */
            {
                setobjs2s(L, q, q - 1);
            }
            setobjs2s(L, p, L.top);
            lua_unlock(L);
        }
예제 #16
0
        /*
        ** creates a new string and anchors it in function's table so that
        ** it will not be collected until the end of the function's compilation
        ** (by that time it should be anchored in function's prototype)
        */
        public static TString luaX_newstring(LexState ls, CharPtr str, uint l)
        {
            lua_State L = ls.L;
            TValue    o;                              /* entry for `str' */
            TString   ts = luaS_newlstr(L, str, l);   /* create new string */

            setsvalue2s(L, StkId.inc(ref L.top), ts); /* temporarily anchor it in stack */
            o = luaH_setstr(L, ls.fs.h, ts);
            if (ttisnil(o))
            {
                setbvalue(o, 1);      /* t[string] = true */
                luaC_checkGC(L);
            }
            StkId.dec(ref L.top);        /* remove string from stack */
            return(ts);
        }
예제 #17
0
        public static string lua_setupvalue(lua_State L, int funcindex, int n)
        {
            TValue val = new TValue();
            StkId  fi  = index2adr(L, funcindex);

            api_checknelems(L, 1);
            string name = aux_upvalue(fi, n, ref val);

            if (name != null)
            {
                StkId.dec(ref L.top);
                setobj(L, val, L.top);
                luaC_barrier(L, clvalue(fi), L.top);
            }
            return(name);
        }
예제 #18
0
        private static StkId adjust_varargs(lua_State 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);
                htab = luaH_new(L);          /* create `arg' table */
                sethvalue(L, StkId.inc(ref L.top), htab);
                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));
                StkId.dec(ref L.top);
            }
                #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++)
            {
                setobjs2s(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);
                lua_assert(iswhite(obj2gco(htab)));
            }
            return(base_);
        }
예제 #19
0
        private static Proto LoadFunction(LoadState S)
        {
            Proto f = luaF_newproto(S.L);

            setptvalue2s(S.L, S.L.top, f); incr_top(S.L);
            f.linedefined     = LoadInt(S);
            f.lastlinedefined = LoadInt(S);
            f.numparams       = LoadByte(S);
            f.is_vararg       = LoadByte(S);
            f.maxstacksize    = LoadByte(S);
            LoadCode(S, f);
            LoadConstants(S, f);
            LoadUpvalues(S, f);
            LoadDebug(S, f);
            StkId.dec(ref S.L.top);
            return(f);
        }
예제 #20
0
        public static int lua_next(lua_State L, int idx)
        {
            StkId t;
            int   more;

            t = index2adr(L, idx);
            api_check(L, ttistable(t));
            more = luaH_next(L, hvalue(t), L.top - 1);
            if (more != 0)
            {
                api_incr_top(L);
            }
            else                      /* no more elements */
            {
                StkId.dec(ref L.top); /* remove key */
            }
            return(more);
        }
예제 #21
0
        public static CharPtr lua_setupvalue(lua_State L, int funcindex, int n)
        {
            CharPtr name;
            TValue  val = new TValue();
            StkId   fi;

            lua_lock(L);
            fi = index2adr(L, funcindex);
            api_checknelems(L, 1);
            name = aux_upvalue(fi, n, ref val);
            if (name != null)
            {
                StkId.dec(ref L.top);
                setobj(L, val, L.top);
                luaC_barrier(L, clvalue(fi), L.top);
            }
            lua_unlock(L);
            return(name);
        }
예제 #22
0
        /*
        ** creates a new string and anchors it in function's table so that
        ** it will not be collected until the end of the function's compilation
        ** (by that time it should be anchored in function's prototype)
        */
        public static TString luaX_newstring(LexState ls, CharPtr str, uint l)
        {
            lua_State L = ls.L;
            TValue    o;                              /* entry for `str' */
            TString   ts = luaS_newlstr(L, str, l);   /* create new string */

            setsvalue2s(L, StkId.inc(ref L.top), ts); /* temporarily anchor it in stack */
            o = luaH_set(L, ls.fs.h, L.top - 1);
            if (ttisnil(o))                           /* not in use yet? (see 'addK') */

            /* boolean value does not need GC barrier;
             * table has no metatable, so it does not need to invalidate cache */
            {
                setbvalue(o, 1);      /* t[string] = true */
                luaC_checkGC(L);
            }
            StkId.dec(ref L.top);        /* remove string from stack */
            return(ts);
        }
예제 #23
0
        public static int luaK_numberK(FuncState fs, lua_Number r)
        {
            int       n;
            lua_State L = fs.ls.L;
            TValue    o = new TValue();

            setnvalue(o, r);
            if (r == 0 || luai_numisnan(null, r))          /* handle -0 and NaN */
            /* use raw representation as key to avoid numeric problems */
            {
                setsvalue(L, L.top, luaS_newlstr(L, CharPtr.FromNumber(r), (uint)GetUnmanagedSize(typeof(lua_Number)))); StkId.inc(ref L.top);     //FIXME:???
                n = addk(fs, L.top - 1, o);
                StkId.dec(ref L.top);
            }
            else
            {
                n = addk(fs, o, o);      /* regular case */
            }
            return(n);
        }
예제 #24
0
        public static void luaT_callTM(lua_State L, TValue f, TValue p1,
                                       TValue p2, TValue p3, int hasres)
        {
            ptrdiff_t result = savestack(L, p3);

            setobj2s(L, L.top, f); StkId.inc(ref L.top);      /* push function (assume EXTRA_STACK) */
            setobj2s(L, L.top, p1); StkId.inc(ref L.top);     /* 1st argument */
            setobj2s(L, L.top, p2); StkId.inc(ref L.top);     /* 2nd argument */
            if (0 == hasres)                                  /* no result? 'p3' is third argument */
            {
                setobj2s(L, L.top, p3); StkId.inc(ref L.top); /* 3rd argument */
            }
            /* metamethod may yield only when called from Lua code */
            luaD_call(L, L.top - (4 - hasres), hasres, isLua(L.ci));
            if (hasres != 0)        /* if has result, move it to its place */
            {
                p3 = restorestack(L, result);
                StkId.dec(ref L.top); setobjs2s(L, p3, L.top);
            }
        }
예제 #25
0
        public static CharPtr lua_setupvalue(lua_State L, int funcindex, int n)
        {
            CharPtr  name;
            TValue   val   = null;    /* to avoid warnings */
            GCObject owner = null;    /* to avoid warnings */
            StkId    fi;

            lua_lock(L);
            fi = index2addr(L, funcindex);
            api_checknelems(L, 1);
            name = aux_upvalue(fi, n, ref val, ref owner);
            if (name != null)
            {
                StkId.dec(ref L.top);
                setobj(L, val, L.top);
                luaC_barrier(L, owner, L.top);
            }
            lua_unlock(L);
            return(name);
        }
예제 #26
0
        public static int lua_getinfo(lua_State L, CharPtr what, lua_Debug ar)
        {
            int      status;
            Closure  f  = null;
            CallInfo ci = null;

            lua_lock(L);
            if (what == '>')
            {
                StkId func = L.top - 1;
                luai_apicheck(L, ttisfunction(func));
                what = what.next();         /* skip the '>' */
                f    = clvalue(func);
                StkId.dec(ref L.top);       /* pop function */
            }
            else if (ar.i_ci != 0)          /* no tail call? */
            {
                ci = L.base_ci[ar.i_ci];
                lua_assert(ttisfunction(ci.func));
                f = clvalue(ci.func);
            }
            status = auxgetinfo(L, what, ar, f, ci);
            if (strchr(what, 'f') != null)
            {
                if (f == null)
                {
                    setnilvalue(L.top);
                }
                else
                {
                    setclvalue(L, L.top, f);
                }
                incr_top(L);
            }
            if (strchr(what, 'L') != null)
            {
                collectvalidlines(L, f);
            }
            lua_unlock(L);
            return(status);
        }
예제 #27
0
        private static Proto LoadFunction(LoadState S, TString p)
        {
            Proto f = luaF_newproto(S.L);

            setptvalue2s(S.L, S.L.top, f); incr_top(S.L);
            f.source = LoadString(S); if (f.source == null)
            {
                f.source = p;
            }
            f.linedefined     = LoadInt(S);
            f.lastlinedefined = LoadInt(S);
            f.nups            = LoadByte(S);
            f.numparams       = LoadByte(S);
            f.is_vararg       = LoadByte(S);
            f.maxstacksize    = LoadByte(S);
            LoadCode(S, f);
            LoadConstants(S, f);
            LoadDebug(S, f);
            IF(luaG_checkcode(f) == 0 ? 1 : 0, "bad code");
            StkId.dec(ref S.L.top);
            return(f);
        }
예제 #28
0
        private static void callTM(lua_State L, TValue f, TValue p1,
                                   TValue p2, TValue p3, int hasres)
        {
            ptrdiff_t result = savestack(L, p3);

            setobj2s(L, L.top, f); StkId.inc(ref L.top); /* push function */ //FIXME:++
            setobj2s(L, L.top, p1); StkId.inc(ref L.top); /* 1st argument */ //FIXME:++
            setobj2s(L, L.top, p2); StkId.inc(ref L.top); /* 2nd argument */ //FIXME:++
            if (hasres == 0) /* no result? 'p3' is third argument */         //FIXME:++
            {
                setobj2s(L, L.top, p3);
            }
            StkId.inc(ref L.top); /* 3th argument */                                 //FIXME:++
            luaD_checkstack(L, 0);
            /* metamethod may yield only when called from Lua code */
            luaD_call(L, L.top - (4 - hasres), hasres, isLua(L.ci));
            if (hasres != 0)          /* if has result, move it to its place */
            {
                p3 = restorestack(L, result);
                setobjs2s(L, p3, StkId.dec(ref L.top));     //FIXME:--
            }
        }
예제 #29
0
        public static int lua_getinfo(lua_State L, CharPtr what, lua_Debug ar)
        {
            int      status;
            Closure  cl;
            CallInfo ci;
            StkId    func;

            lua_lock(L);
            swapextra(L);
            if (what == '>')
            {
                ci   = null;
                func = L.top - 1;
                api_check(L, ttisfunction(func), "function expected");
                what = what.next();          /* skip the '>' */
                StkId.dec(ref L.top);        /* pop function */
            }
            else
            {
                ci   = ar.i_ci;
                func = ci.func;
                lua_assert(ttisfunction(ci.func));
            }
            cl     = ttisclosure(func) ? clvalue(func) : null;
            status = auxgetinfo(L, what, ar, cl, ci);
            if (strchr(what, 'f') != null)
            {
                setobjs2s(L, L.top, func);
                api_incr_top(L);
            }
            swapextra(L);        /* correct before option 'L', which can raise a mem. error */
            if (strchr(what, 'L') != null)
            {
                collectvalidlines(L, cl);
            }
            lua_unlock(L);
            return(status);
        }
예제 #30
0
        public static int lua_getinfo(lua_State L, string what, lua_Debug ar)
        {
            int      status;
            Closure  f  = null;
            CallInfo ci = null;

            if (what[0] == '>')
            {
                StkId func = L.top - 1;
                what = what.Substring(1);                /* skip the '>' */
                f    = clvalue(func);
                StkId.dec(ref L.top);                    /* pop function */
            }
            else if (ar.i_ci != 0)
            {              /* no tail call? */
                ci = L.base_ci[ar.i_ci];
                f  = clvalue(ci.func);
            }
            status = auxgetinfo(L, what, ar, f, ci);
            if (what.Contains('f'))
            {
                if (f == null)
                {
                    setnilvalue(L.top);
                }
                else
                {
                    setclvalue(L, L.top, f);
                }
                incr_top(L);
            }
            if (what.Contains('L'))
            {
                collectvalidlines(L, f);
            }
            return(status);
        }