예제 #1
0
        public static void lua_xmove(lua_State from, lua_State to, int n)
        {
            int i;

            if (from == to)
            {
                return;
            }
            lua_lock(to);
            api_checknelems(from, n);
            api_check(from, G(from) == G(to), "moving among independent states");
            api_check(from, to.ci.top - to.top >= n, "not enough elements to move");
            from.top -= n;
            for (i = 0; i < n; i++)
            {
                setobj2s(to, StkId.inc(ref to.top), from.top + i);
            }
            lua_unlock(to);
        }
예제 #2
0
 public static void lua_settop(lua_State L, int idx)
 {
     lua_lock(L);
     if (idx >= 0)
     {
         api_check(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
     {
         api_check(L, -(idx + 1) <= (L.top - L.base_));
         L.top += idx + 1;        /* `subtract' index (index is negative) */
     }
     lua_unlock(L);
 }
예제 #3
0
        public static void lua_xmove(lua_State from, lua_State to, int n)
        {
            int i;

            if (from == to)
            {
                return;
            }
            lua_lock(to);
            api_checknelems(from, n);
            api_check(from, G(from) == G(to));
            api_check(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);
            }
            lua_unlock(to);
        }
예제 #4
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);
        }
예제 #5
0
        private static void stack_init(lua_State L1, lua_State L)
        {
            int i;

            /* initialize stack array */
            L1.stack     = luaM_newvector <TValue>(L, BASIC_STACK_SIZE + EXTRA_STACK);
            L1.stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
            for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
            {
                setnilvalue(L1.stack[i]);          /* erase new stack */
            }
            L1.top        = L1.stack[0];
            L1.stack_last = L1.stack[L1.stacksize - EXTRA_STACK - 1];
            /* initialize first ci */
            L1.ci.func = L1.top;
            setnilvalue(StkId.inc(ref L1.top));        /* 'function' entry for this 'ci' */
            L1.ci.top        = L1.top + LUA_MINSTACK;
            L1.ci.callstatus = 0;
        }
예제 #6
0
 private static void stack_init(lua_State L1, lua_State L)
 {
     /* initialize CallInfo array */
     L1.base_ci = luaM_newvector <CallInfo>(L, BASIC_CI_SIZE);
     L1.ci      = L1.base_ci[0];
     L1.size_ci = BASIC_CI_SIZE;
     L1.end_ci  = L1.base_ci[L1.size_ci - 1];
     /* initialize stack array */
     L1.stack      = luaM_newvector <TValue>(L, BASIC_STACK_SIZE + EXTRA_STACK);
     L1.stacksize  = BASIC_STACK_SIZE + EXTRA_STACK;
     L1.top        = L1.stack[0];
     L1.stack_last = L1.stack[L1.stacksize - EXTRA_STACK - 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;
     L1.ci.callstatus = 0;
 }
예제 #7
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);
            }
        }
예제 #8
0
        private static void traversestack(global_State g, lua_State L)
        {
            StkId o;

            if (L.stack == null)
            {
                return;                                             /* stack not completely built yet */
            }
            markvalue(g, gt(L));                                    /* mark global table */
            for (o = new lua_TValue(L.stack); o < L.top; o = o + 1) //FIXME:L.stack->new StkId(L.stack[0]) //FIXME:don't use lua_TValue.inc(), overflow ([-1])
            {
                markvalue(g, o);
            }
            if (g.gcstate == GCSatomic)                     /* final traversal? */
            {
                for (; o <= L.stack_last; StkId.inc(ref o)) /* clear not-marked stack slice */
                {
                    setnilvalue(o);
                }
            }
        }
예제 #9
0
        public static void lua_settop(lua_State L, int idx)
        {
            StkId func = L.ci.func;

            lua_lock(L);
            if (idx >= 0)
            {
                api_check(L, idx <= L.stack_last - (func + 1), "new top too large");
                while (L.top < (func + 1) + idx)
                {
                    setnilvalue(StkId.inc(ref L.top));       //FIXME:???
                }
                L.top = (func + 1) + idx;
            }
            else
            {
                api_check(L, -(idx + 1) <= (L.top - (func + 1)), "invalid new top");
                L.top += idx + 1;        /* `subtract' index (index is negative) */
            }
            lua_unlock(L);
        }
예제 #10
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:--
            }
        }
예제 #11
0
        /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
        public static CharPtr luaO_pushvfstring(lua_State L, CharPtr fmt, params object[] argp)
        {
            int parm_index = 0;       //FIXME: added, for emulating va_arg(argp, xxx)
            int n          = 0;

            for (;;)
            {
                CharPtr e = strchr(fmt, '%');
                if (e == null)
                {
                    break;
                }
                luaD_checkstack(L, 2);          /* fmt + item */
                pushstr(L, fmt, (uint)(e - fmt));
                switch (e[1])
                {
                case 's': {
                    object  o = argp[parm_index++]; //FIXME: changed
                    CharPtr s = o as CharPtr;       //FIXME: changed
                    if (s == null)                  //FIXME: changed
                    {
                        s = (string)o;              //FIXME: changed
                    }
                    if (s == null)
                    {
                        s = "(null)";
                    }
                    pushstr(L, s, (uint)strlen(s));               //FIXME:changed, (uint)
                    break;
                }

                case 'c': {
                    CharPtr buff = new char[1]; //FIXME:???char->CharPtr
                    buff[0] = (char)(int)argp[parm_index++];
                    pushstr(L, buff, 1);        //FIXME:???&buff
                    break;
                }

                case 'd': {
                    setivalue(L.top, (int)argp[parm_index++]); StkId.inc(ref L.top);
                    break;
                }

                case 'I': {
                    setivalue(L.top, cast_integer((lua_Integer)argp[parm_index++])); StkId.inc(ref L.top);
                    break;
                }

                case 'f': {
                    setnvalue(L.top, (l_uacNumber)argp[parm_index++]); StkId.inc(ref L.top);
                    break;
                }

                case 'p': {
                    CharPtr buff = new char[32]; /* should be enough space for a `%p' */            //FIXME: changed, char buff[4*sizeof(void *) + 8];
                    uint    l    = (uint)sprintf(buff, "0x%08x", argp[parm_index++].GetHashCode()); //FIXME: changed, %p->%08x  //FIXME:changed, (uint)
                    pushstr(L, buff, l);
                    break;
                }

                case '%': {
                    pushstr(L, "%", 1);
                    break;
                }

                default: {
                    luaG_runerror(L,
                                  "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("lua_pushfstring"),
                                  (e + 1).ToString()); //FIXME: changed, *(e+1)
                    break;                             //FIXME:added
                }
                }
                n  += 2;
                fmt = e + 2;
            }
            luaD_checkstack(L, 1);
            pushstr(L, fmt, (uint)strlen(fmt));       //FIXME:changed, (uint)
            if (n > 0)
            {
                luaV_concat(L, n + 1);
            }
            return(svalue(L.top - 1));
        }
예제 #12
0
 private static void pushstr(lua_State L, CharPtr str, uint l)
 {
     setsvalue2s(L, L.top, luaS_newlstr(L, str, l)); StkId.inc(ref L.top);
 }
예제 #13
0
 public static void api_incr_top(lua_State L)
 {
     api_check(L, L.top < L.ci.top);
     StkId.inc(ref L.top);
 }
예제 #14
0
        public static int luaD_precall(lua_State 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;
                luaD_checkstack(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_ = adjust_varargs(L, p, nargs);
                    func  = restorestack(L, funcr); /* previous call may change the stack */
                }
                ci      = inc_ci(L);                /* now `enter' new function */
                ci.func = func;
                L.base_ = ci.base_ = base_;
                ci.top  = L.base_ + p.maxstacksize;
                lua_assert(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 */
                    luaD_callhook(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;
                luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
                ci      = inc_ci(L);              /* now `enter' new function */
                ci.func = restorestack(L, funcr);
                L.base_ = ci.base_ = ci.func + 1;
                ci.top  = L.top + LUA_MINSTACK;
                lua_assert(ci.top <= L.stack_last);
                ci.nresults = nresults;
                if ((L.hookmask & LUA_MASKCALL) != 0)
                {
                    luaD_callhook(L, LUA_HOOKCALL, -1);
                }
                lua_unlock(L);
                n = curr_func(L).c.f(L); /* do the actual call */
                lua_lock(L);
                if (n < 0)               /* yielding? */
                {
                    return(PCRYIELD);
                }
                else
                {
                    luaD_poscall(L, L.top - n);
                    return(PCRC);
                }
            }
        }
예제 #15
0
 public static void incr_top(lua_State L)
 {
     luaD_checkstack(L, 1);
     StkId.inc(ref L.top);
 }
예제 #16
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);
        }
예제 #17
0
        /* this function handles only '%d', '%c', '%f', '%p', and '%s'
         * conventional formats, plus Lua-specific '%I' and '%U' */
        public static CharPtr luaO_pushvfstring(lua_State L, CharPtr fmt, params object[] argp)
        {
            int parm_index = 0;       //FIXME: added, for emulating va_arg(argp, xxx)
            int n          = 0;

            for (;;)
            {
                CharPtr e = strchr(fmt, '%');
                if (e == null)
                {
                    break;
                }
                luaD_checkstack(L, 2);          /* fmt + item */
                pushstr(L, fmt, (uint)(e - fmt));
                switch (e[1])
                {
                case 's': {
                    object  o = argp[parm_index++]; //FIXME: changed
                    CharPtr s = o as CharPtr;       //FIXME: changed
                    if (s == null)                  //FIXME: changed
                    {
                        s = (string)o;              //FIXME: changed
                    }
                    if (s == null)
                    {
                        s = "(null)";
                    }
                    pushstr(L, s, (uint)strlen(s));               //FIXME:changed, (uint)
                    break;
                }

                case 'c': {
                    CharPtr buff = new char[1]; buff[0] = (char)(int)argp[parm_index++];     //FIXME:???char->CharPtr
                    if (0 != lisprint(cast_uchar(buff)))
                    {
                        pushstr(L, buff, 1); //FIXME:???&buff
                    }
                    else                     /* non-printable character; print its code */
                    {
                        luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
                    }
                    break;
                }

                case 'd': {
                    setivalue(L.top, (int)argp[parm_index++]); StkId.inc(ref L.top);
                    luaO_tostring(L, L.top - 1);
                    break;
                }

                case 'I': {
                    setivalue(L.top, (lua_Integer)((l_uacInt)argp[parm_index++])); StkId.inc(ref L.top);
                    luaO_tostring(L, L.top - 1);
                    break;
                }

                case 'f': {
                    setfltvalue(L.top, cast_num((l_uacNumber)argp[parm_index++])); StkId.inc(ref L.top);
                    luaO_tostring(L, L.top - 1);
                    break;
                }

                case 'p': {
                    CharPtr buff = new char[32]; /* should be enough space for a '%p' */            //FIXME: changed, char buff[4*sizeof(void *) + 8];
                    uint    l    = (uint)sprintf(buff, "0x%08x", argp[parm_index++].GetHashCode()); //FIXME: changed, %p->%08x  //FIXME:changed, (uint)
                    pushstr(L, buff, l);
                    break;
                }

                case 'U': {
                    CharPtr buff = new CharPtr(new char[UTF8BUFFSZ]);
                    int     l    = luaO_utf8esc(buff, (ulong)(long)argp[parm_index++]);
                    pushstr(L, buff + UTF8BUFFSZ - l, (uint)l);
                    break;
                }

                case '%': {
                    pushstr(L, "%", 1);
                    break;
                }

                default: {
                    luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
                                  (e + 1).ToString()); //FIXME: changed, *(e+1)
                    break;                             //FIXME:added
                }
                }
                n  += 2;
                fmt = e + 2;
            }
            luaD_checkstack(L, 1);
            pushstr(L, fmt, (uint)strlen(fmt));       //FIXME:changed, (uint)
            if (n > 0)
            {
                luaV_concat(L, n + 1);
            }
            return(svalue(L.top - 1));
        }