Пример #1
0
        //#define S(x)	(int)(x),SS(x)

        private static void PrintHeader(Lua.Proto f)
        {
            Lua.CharPtr s = f.source != null?Lua.getstr(f.source) : "=?";

            if (s[0] == '@' || s[0] == '=')
            {
                s = s.next();
            }
            else if (s[0] == Lua.LUA_SIGNATURE[0])
            {
                s = "(bstring)";
            }
            else
            {
                s = "(string)";
            }
            Lua.printf("\n%s <%s:%d,%d> (%d Instruction%s at %p)\n",
                       (f.linedefined == 0)?"main":"function", s,
                       f.linedefined, f.lastlinedefined,
                       (int)(f.sizecode), SS(f.sizecode), VOID(f));
            Lua.printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
                       (int)(f.numparams), (f.is_vararg != 0) ? "+" : "", SS(f.numparams),
                       (int)(f.maxstacksize), SS(f.maxstacksize), (int)f.sizeupvalues, SS(f.sizeupvalues));
            Lua.printf("%d local%s, %d constant%s, %d function%s\n",
                       (int)(f.sizelocvars), SS(f.sizelocvars), (int)f.sizek, SS(f.sizek), (int)f.sizep, SS(f.sizep));
        }
Пример #2
0
 static Lua.Proto combine(Lua.lua_State L, int n)
 {
     if (n == 1)
     {
         return(toproto(L, -1));
     }
     else
     {
         int       i, pc;
         Lua.Proto f = Lua.luaF_newproto(L);
         Lua.setptvalue2s(L, L.top, f); Lua.incr_top(L);
         f.source       = Lua.luaS_newliteral(L, "=(" + PROGNAME + ")");
         f.maxstacksize = 1;
         pc             = 2 * n + 1;
         f.code         = (Instruction[])Lua.luaM_newvector <Instruction>(L, pc);
         f.sizecode     = pc;
         f.p            = Lua.luaM_newvector <Lua.Proto>(L, n);
         f.sizep        = n;
         pc             = 0;
         for (i = 0; i < n; i++)
         {
             f.p[i]       = toproto(L, i - n - 1);
             f.code[pc++] = (uint)Lua.CREATE_ABx(Lua.OpCode.OP_CLOSURE, 0, i);
             f.code[pc++] = (uint)Lua.CREATE_ABC(Lua.OpCode.OP_CALL, 0, 1, 1);
         }
         f.code[pc++] = (uint)Lua.CREATE_ABC(Lua.OpCode.OP_RETURN, 0, 1, 0);
         return(f);
     }
 }
Пример #3
0
        private static void PrintDebug(Lua.Proto f)
        {
            int i, n;

            n = f.sizek;
            Lua.printf("constants (%d) for %p:\n", n, VOID(f));
            for (i = 0; i < n; i++)
            {
                Lua.printf("\t%d\t", i + 1);
                PrintConstant(f, i);
                Lua.printf("\n");
            }
            n = f.sizelocvars;
            Lua.printf("locals (%d) for %p:\n", n, VOID(f));
            for (i = 0; i < n; i++)
            {
                Lua.printf("\t%d\t%s\t%d\t%d\n",
                           i, Lua.getstr(f.locvars[i].varname), f.locvars[i].startpc + 1, f.locvars[i].endpc + 1);
            }
            n = f.sizeupvalues;
            Lua.printf("upvalues (%d) for %p:\n", n, VOID(f));
            for (i = 0; i < n; i++)
            {
                Lua.printf("\t%d\t%s\t%d\t%d\n",
                           i, UPVALNAME(i, f), f.upvalues[i].instack, f.upvalues[i].idx);
            }
        }
Пример #4
0
        private static void PrintConstant(Lua.Proto f, int i)
        {
            /*const*/ TValue o = f.k[i];

            switch (ttypenv__(o))
            {
            case Lua.LUA_TNIL:
                Lua.printf("nil");
                break;

            case Lua.LUA_TBOOLEAN:
                Lua.printf(Lua.bvalue(o) != 0 ? "true" : "false");
                break;

            case Lua.LUA_TNUMBER:
                Lua.printf(Lua.LUA_NUMBER_FMT, nvalue__(o));
                break;

            case Lua.LUA_TSTRING:
                PrintString(Lua.rawtsvalue(o));
                break;

            default:                                    /* cannot happen */
                Lua.printf("? type=%d", Lua.ttype(o));
                break;
            }
        }
Пример #5
0
        private static void PrintConstant(Lua.Proto f, int i)
        {
            /*const*/ TValue o = f.k[i];

            switch (Lua.ttype(o))
            {
            case Lua.LUA_TNIL:
                Lua.printf("nil");
                break;

            case Lua.LUA_TBOOLEAN:
                Lua.printf(Lua.bvalue(o) != 0 ? "true" : "false");
                break;

            case Lua.LUA_TNUMFLT:
                Lua.printf(Lua.LUA_NUMBER_FMT, Lua.fltvalue(o));
                break;

            case Lua.LUA_TNUMINT:
                Lua.printf(Lua.LUA_INTEGER_FMT, Lua.ivalue(o));
                break;

            case Lua.LUA_TSHRSTR:
            case Lua.LUA_TLNGSTR:
                PrintString(Lua.tsvalue(o));
                break;

            default:                                    /* cannot happen */
                Lua.printf("? type=%d", Lua.ttype(o));
                break;
            }
        }
Пример #6
0
        private static void PrintFunction(Lua.Proto f, int full)
        {
            int i, n = f.sizep;

            PrintHeader(f);
            PrintCode(f);
            if (full != 0)
            {
                PrintDebug(f);
            }
            for (i = 0; i < n; i++)
            {
                PrintFunction(f.p[i], full);
            }
        }
Пример #7
0
        private static void PrintConstant(Lua.Proto f, int i)
        {
            /*const*/ TValue o = f.k[i];

            switch (Lua.ttype(o))
            {
            case Lua.LUA_TNIL:
                Lua.printf("nil");
                break;

            case Lua.LUA_TBOOLEAN:
                Lua.printf(Lua.bvalue(o) != 0 ? "true" : "false");
                break;

            case Lua.LUA_TNUMFLT:
            {
                Lua.CharPtr buff = new Lua.CharPtr(new char[100]);
                Lua.sprintf(buff, Lua.LUA_NUMBER_FMT, Lua.fltvalue(o));
                Lua.printf("%s", buff);
                if (buff[Lua.strspn(buff, "-0123456789")] == '\0')
                {
                    Lua.printf(".0");
                }
                break;
            }

            case Lua.LUA_TNUMINT:
                Lua.printf(Lua.LUA_INTEGER_FMT, Lua.ivalue(o));
                break;

            case Lua.LUA_TSHRSTR:
            case Lua.LUA_TLNGSTR:
                PrintString(Lua.tsvalue(o));
                break;

            default:                                    /* cannot happen */
                Lua.printf("? type=%d", Lua.ttype(o));
                break;
            }
        }
Пример #8
0
        private static void PrintCode(Lua.Proto f)
        {
            Instruction[] code = f.code;
            int           pc, n = f.sizecode;

            for (pc = 0; pc < n; pc++)
            {
                Instruction i    = f.code[pc];
                OpCode      o    = Lua.GET_OPCODE(i);
                int         a    = Lua.GETARG_A(i);
                int         b    = Lua.GETARG_B(i);
                int         c    = Lua.GETARG_C(i);
                int         ax   = Lua.GETARG_Ax(i);
                int         bx   = Lua.GETARG_Bx(i);
                int         sbx  = Lua.GETARG_sBx(i);
                int         line = Lua.getfuncline(f, pc);
                Lua.printf("\t%d\t", pc + 1);
                if (line > 0)
                {
                    Lua.printf("[%d]\t", line);
                }
                else
                {
                    Lua.printf("[-]\t");
                }
                Lua.printf("%-9s\t", Lua.luaP_opnames[(int)o]);
                switch (Lua.getOpMode(o))
                {
                case OpMode.iABC:
                    Lua.printf("%d", a);
                    if (Lua.getBMode(o) != OpArgMask.OpArgN)
                    {
                        Lua.printf(" %d", Lua.ISK(b) != 0 ? (MYK(Lua.INDEXK(b))) : b);
                    }
                    if (Lua.getCMode(o) != OpArgMask.OpArgN)
                    {
                        Lua.printf(" %d", Lua.ISK(c) != 0 ? (MYK(Lua.INDEXK(c))) : c);
                    }
                    break;

                case OpMode.iABx:
                    Lua.printf("%d", a);
                    if (Lua.getBMode(o) == OpArgMask.OpArgK)
                    {
                        Lua.printf(" %d", MYK(bx));
                    }
                    if (Lua.getBMode(o) == OpArgMask.OpArgU)
                    {
                        Lua.printf(" %d", bx);
                    }
                    break;

                case OpMode.iAsBx:
                    Lua.printf("%d %d", a, sbx);
                    break;

                case OpMode.iAx:
                    Lua.printf("%d", MYK(ax));
                    break;
                }
                switch (o)
                {
                case OpCode.OP_LOADK:
                    Lua.printf("\t; "); PrintConstant(f, bx);
                    break;

                case OpCode.OP_GETUPVAL:
                case OpCode.OP_SETUPVAL:
                    Lua.printf("\t; %s", UPVALNAME(b, f));
                    break;

                case OpCode.OP_GETTABUP:
                    Lua.printf("\t; %s", UPVALNAME(b, f));
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_SETTABUP:
                    Lua.printf("\t; %s", UPVALNAME(a, f));
                    if (Lua.ISK(b) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(b));
                    }
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf(" "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_GETTABLE:
                case OpCode.OP_SELF:
                    if (Lua.ISK(c) != 0)
                    {
                        Lua.printf("\t; "); PrintConstant(f, Lua.INDEXK(c));
                    }
                    break;

                case OpCode.OP_SETTABLE:
                case OpCode.OP_ADD:
                case OpCode.OP_SUB:
                case OpCode.OP_MUL:
                case OpCode.OP_DIV:
                case OpCode.OP_POW:
                case OpCode.OP_EQ:
                case OpCode.OP_LT:
                case OpCode.OP_LE:
                    if (Lua.ISK(b) != 0 || Lua.ISK(c) != 0)
                    {
                        Lua.printf("\t; ");
                        if (Lua.ISK(b) != 0)
                        {
                            PrintConstant(f, Lua.INDEXK(b));
                        }
                        else
                        {
                            Lua.printf("-");
                        }
                        Lua.printf(" ");
                        if (Lua.ISK(c) != 0)
                        {
                            PrintConstant(f, Lua.INDEXK(c));
                        }
                        else
                        {
                            Lua.printf("-");
                        }
                    }
                    break;

                case OpCode.OP_JMP:
                case OpCode.OP_FORLOOP:
                case OpCode.OP_FORPREP:
                case OpCode.OP_TFORLOOP:
                    Lua.printf("\t; to %d", sbx + pc + 2);
                    break;

                case OpCode.OP_CLOSURE:
                    Lua.printf("\t; %p", VOID(f.p[bx]));
                    break;

                case OpCode.OP_SETLIST:
                    if (c == 0)
                    {
                        Lua.printf("\t; %d", (int)code[++pc]);
                    }
                    else
                    {
                        Lua.printf("\t; %d", c);
                    }
                    break;

                case OpCode.OP_EXTRAARG:
                    Lua.printf("\t; "); PrintConstant(f, ax);
                    break;

                default:
                    break;
                }
                Lua.printf("\n");
            }
        }
Пример #9
0
 private static Lua.CharPtr UPVALNAME(int x, Lua.Proto f)
 {
     return((f.upvalues[x].name != null) ? Lua.getstr(f.upvalues[x].name) : "-");
 }
Пример #10
0
        //#include <errno.h>
        //#include <stdio.h>
        //#include <stdlib.h>
        //#include <string.h>

        //#define luac_c
        //#define LUA_CORE

        //#include "lua.h"
        //#include "lauxlib.h"

        //#include "lobject.h"
        //#include "lstate.h"
        //#include "lundump.h"

        //static void PrintFunction(const Proto* f, int full);
        public static void luaU_print(Lua.Proto f, int full)
        {
            PrintFunction(f, full);
        }
        static int pmain(Lua.lua_State L)
        {
            Smain s = (Smain)Lua.lua_touserdata(L, 1);

            Lua.CharPtr inputPath = s.argv[0];
            string      fileText  = System.IO.File.ReadAllText(inputPath.ToString());

            // Extract function name
            string funcName = "";
            Match  m        = Regex.Match(fileText, @"function (.+)\(");

            if (m.Success)
            {
                funcName = m.Result("$1");
            }
            else
            {
                Lua.luaL_error(L, "input file missing function definition!");
            }

            // Extract function argument(s) if any
            string argsStr = "";

            m = Regex.Match(fileText, @"function .+\((.+)\)");
            if (m.Success)
            {
                argsStr = Regex.Replace(m.Result("$1"), @"\s", "");
            }

            if (Lua.luaL_loadfile_DAI(L, inputPath, fileText) != 0)
            {
                fatal(Lua.lua_tostring(L, -1));
            }

            Lua.CharPtr outputPath = Path.ChangeExtension(inputPath.ToString(), ".luac");
            Stream      D          = Lua.fopen(outputPath, "wb");

            if (D == null)
            {
                cannot(outputPath, "open");
            }

            // Write DAI header
            BinaryWriter bw = new BinaryWriter(D);

            bw.Write((uint)0xE1850009);
            bw.Write((uint)1);
            bw.Write((uint)funcName.Length + 1);
            uint argsCnt = 0;

            if (argsStr != "")
            {
                argsCnt = 1;
                foreach (char c in argsStr)
                {
                    if (c == ',')
                    {
                        argsCnt++;
                    }
                }
            }
            bw.Write((uint)argsStr.Length + 1);
            bw.Write((uint)argsCnt);
            long dataSizePos = bw.BaseStream.Position;

            bw.Write((uint)0);
            bw.Write((char[])funcName.ToCharArray(), 0, funcName.Length);
            bw.Write((byte)0);
            if (argsCnt > 0)
            {
                bw.Write((char[])argsStr.ToCharArray(), 0, argsStr.Length);
            }
            bw.Write((byte)0);

            // Write luac data
            Lua.Proto f        = toproto(L, -1);
            long      startPos = D.Position;

            Lua.luaU_dump(L, f, writer, D, 0);
            if (Lua.ferror(D) != 0)
            {
                cannot(outputPath, "write");
            }
            // Update data size in DAI header
            long dataSize = (D.Position - startPos);

            bw.Seek((int)dataSizePos, SeekOrigin.Begin);
            bw.Write((uint)dataSize);

            if (Lua.fclose(D) != 0)
            {
                cannot(outputPath, "close");
            }
            return(0);
        }