Exemple #1
0
        public static int lua_getmetatable(lua_State L, int objindex)
        {
            lua_lock(L);
            int    res = 0;
            TValue obj = imp.index2addr(L, objindex);
            Table  mt  = null;

            switch (imp.ttnov(obj))
            {
            case LUA_TTABLE:
                mt = imp.hvalue(obj).metatable;
                break;

            case LUA_TUSERDATA:
                mt = imp.uvalue(obj).metatable;
                break;

            default:
                mt = imp.G(L).mt[imp.ttnov(obj)];
                break;
            }
            if (mt != null)
            {
                imp.sethvalue(L, L.top, mt);
                imp.api_incr_top(L);
                res = 1;
            }
            lua_unlock(L);
            return(res);
        }
Exemple #2
0
        public static int lua_load(lua_State L, lua_Reader reader, object data, string chunkname, string mode)
        {
            lua_lock(L);
            if (chunkname == null)
            {
                chunkname = "?";
            }
            Zio z = new Zio();

            imp.luaZ_init(L, z, reader, data);
            int status = imp.luaD_protectedparser(L, z, chunkname, mode);

            if (status == LUA_OK)                        /* no errors? */
            {
                LClosure f = imp.clLvalue(L, L.top - 1); /* get newly created function */
                if (f.nupvalues >= 1)                    /* does it have an upvalue? */
                /* get global table from registry */
                {
                    Table  reg = imp.hvalue(imp.G(L).l_registry);
                    TValue gt  = imp.luaH_getint(reg, LUA_RIDX_GLOBALS);
                    /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
                    imp.setobj(L, f.upvals[0].v, gt);
                    imp.luaC_upvalbarrier(L, f.upvals[0]);
                }
            }
            lua_unlock(L);
            return(status);
        }
Exemple #3
0
        /*
        ** get functions (Lua -> stack)
        */


        public static int lua_getglobal(lua_State L, string name)
        {
            Table reg = imp.hvalue(imp.G(L).l_registry);

            lua_lock(L);
            TValue gt = imp.luaH_getint(reg, LUA_RIDX_GLOBALS);

            imp.setsvalue2s(L, L.top++, imp.luaS_new(L, name));
            imp.luaV_gettable(L, gt, L.top - 1, L.top - 1);
            lua_unlock(L);
            return(imp.ttnov(L, L.top - 1));
        }
Exemple #4
0
        /*
        ** set functions (stack -> Lua)
        */


        public static void lua_setglobal(lua_State L, string name)
        {
            Table reg = imp.hvalue(imp.G(L).l_registry);

            lua_lock(L);
            imp.api_checknelems(L, 1);
            TValue gt = imp.luaH_getint(reg, LUA_RIDX_GLOBALS);   /* global table */

            imp.setsvalue2s(L, L.top++, imp.luaS_new(L, name));
            imp.luaV_settable(L, gt, L.top - 1, L.top - 2);
            L.top -= 2;  /* pop value and key */
            lua_unlock(L);
        }
Exemple #5
0
        public static void lua_rawseti(lua_State L, int idx, long n)
        {
            lua_lock(L);
            imp.api_checknelems(L, 1);
            TValue o = imp.index2addr(L, idx);

            imp.api_check(imp.ttistable(o), "table expected");
            Table t = imp.hvalue(o);

            imp.luaH_setint(L, t, n, L.top - 1);
            imp.luaC_barrierback(L, t, L.top - 1);
            L.top--;
            lua_unlock(L);
        }
Exemple #6
0
        public static void lua_createtable(lua_State L, int narray, int nrec)
        {
            lua_lock(L);
            imp.luaC_checkGC(L);
            Table t = imp.luaH_new(L);

            imp.sethvalue(L, L.top, t);
            imp.api_incr_top(L);
            if (narray > 0 || nrec > 0)
            {
                imp.luaH_resize(L, t, narray, nrec);
            }
            lua_unlock(L);
        }
Exemple #7
0
        public static void lua_rawset(lua_State L, int idx)
        {
            lua_lock(L);
            imp.api_checknelems(L, 2);
            TValue o = imp.index2addr(L, idx);

            imp.api_check(imp.ttistable(o), "table expected");
            Table t = imp.hvalue(o);

            imp.setobj2t(L, imp.luaH_set(L, t, L.top - 2), L.top - 1);
            imp.invalidateTMcache(t);
            imp.luaC_barrierback(L, t, L.top - 1);
            L.top -= 2;
            lua_unlock(L);
        }
Exemple #8
0
        public static void lua_rawsetp(lua_State L, int idx, object p)
        {
            lua_lock(L);
            imp.api_checknelems(L, 1);
            TValue o = imp.index2addr(L, idx);

            imp.api_check(imp.ttistable(o), "table expected");
            Table  t = imp.hvalue(o);
            TValue k = new TValue();

            imp.setpvalue(k, p);
            imp.setobj2t(L, imp.luaH_set(L, t, k), L.top - 1);
            imp.luaC_barrierback(L, t, L.top - 1);
            L.top--;
            lua_unlock(L);
        }
Exemple #9
0
        public static int lua_setmetatable(lua_State L, int objindex)
        {
            lua_lock(L);
            imp.api_checknelems(L, 1);
            TValue obj = imp.index2addr(L, objindex);
            Table  mt  = null;

            if (imp.ttisnil(L, L.top - 1))
            {
                mt = null;
            }
            else
            {
                imp.api_check(imp.ttistable(L, L.top - 1), "table expected");
                mt = imp.hvalue(L, L.top - 1);
            }
            switch (imp.ttnov(obj))
            {
            case LUA_TTABLE: {
                imp.hvalue(obj).metatable = mt;
                if (mt != null)
                {
                    imp.luaC_objbarrier(L, imp.gcvalue(obj), mt);
                    imp.luaC_checkfinalizer(L, imp.gcvalue(obj), mt);
                }
                break;
            }

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

            default: {
                imp.G(L).mt[imp.ttnov(obj)] = mt;
                break;
            }
            }
            L.top--;
            lua_unlock(L);
            return(1);
        }