Example #1
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);
        }
Example #2
0
        public static UpVal getupvalref(lua_State L, int fidx, int n, ref LClosure pf)
        {
            TValue fi = index2addr(L, fidx);

            api_check(ttisLclosure(fi), "Lua function expected");
            LClosure f = clLvalue(fi);

            api_check((1 <= n && n <= f.p.sizeupvalues), "invalid upvalue index");
            if (pf != null)
            {
                pf = f;
            }
            return(f.upvals[n - 1]);  /* get its upvalue pointer */
        }
Example #3
0
        public static void lua_upvaluejoin(lua_State L, int fidx1, int n1, int fidx2, int n2)
        {
            LClosure f1  = null;
            UpVal    up2 = imp.getupvalref(L, fidx2, n2, ref f1);
            UpVal    up1 = imp.getupvalref(L, fidx1, n1, ref f1);

            imp.luaC_upvdeccount(L, up1);
            imp.uvcopy(up1, up2);
            up1.refcount++;
            if (imp.upisopen(up1))
            {
                up1.u.open.touched = 1;
            }
            imp.luaC_upvalbarrier(L, up1);
        }
Example #4
0
        public static object lua_upvalueid(lua_State L, int fidx, int n)
        {
            TValue   fi = imp.index2addr(L, fidx);
            LClosure pf = null;

            switch (imp.ttype(fi))
            {
            case imp.LUA_TLCL: {      /* lua closure */
                return(imp.getupvalref(L, fidx, n, ref pf));
            }

            case imp.LUA_TCCL: {      /* C closure */
                CClosure f = imp.clCvalue(fi);
                imp.api_check(1 <= n && n <= f.nupvalues, "invalid upvalue index");
                return(f.upvalue[n - 1]);
            }

            default: {
                imp.api_check(false, "closure expected");
                return(null);
            }
            }
        }
Example #5
0
        public static string aux_upvalue(TValue fi, int n, ref TValue val, ref CClosure owner, ref UpVal uv)
        {
            switch (ttype(fi))
            {
            case LUA_TCCL: {      /* C closure */
                CClosure f = clCvalue(fi);
                if ((1 <= n && n <= f.nupvalues) == false)
                {
                    return(null);
                }
                val = f.upvalue[n - 1];
                if (owner != null)
                {
                    owner = f;
                }
                return("");
            }

            case LUA_TLCL: {      /* Lua closure */
                LClosure f = clLvalue(fi);
                Proto    p = f.p;
                if ((1 <= n && n <= p.sizeupvalues) == false)
                {
                    return(null);
                }
                val = f.upvals[n - 1].v;
                if (uv != null)
                {
                    uv = f.upvals[n - 1];
                }
                TString name = p.upvalues[n - 1].name;
                return((name == null) ? "(*no name)" : byte2str(getstr(name)));
            }

            default: return(null);     /* not a closure */
            }
        }