Esempio n. 1
0
        static int Main(string[] args)
        {
            // prepend the exe name to the arg list as it's done in C
            // so that we don't have to change any of the args indexing
            // code above
            List<string> newargs = new List<string>(args);
            newargs.Insert(0, Assembly.GetExecutingAssembly().Location);
            args = (string[])newargs.ToArray();

            int status;
            Smain s = new Smain();
            LuaState L = Lua.LuaOpen();  /* create state */
            if (L == null)
            {
                l_message(args[0], "cannot create state: not enough memory");
                return Lua.EXIT_FAILURE;
            }
            s.argc = args.Length;
            s.argv = args;
            status = Lua.LuaCPCall(L, pmain, s);
            report(L, status);
            Lua.LuaClose(L);
            return (status!=0) || (s.status!=0) ? Lua.EXIT_FAILURE : Lua.EXIT_SUCCESS;
        }
Esempio n. 2
0
        static int pmain(lua_State L)
        {
            Smain s = (Smain)lua_touserdata(L, 1);

            string[] argv = s.argv;
            int      script;
            int      has_i = 0, has_v = 0, has_e = 0;

            globalL = L;
            if ((argv.Length > 0) && (argv[0] != ""))
            {
                progname = argv[0];
            }
            lua_gc(L, LUA_GCSTOP, 0);      /* stop collector during initialization */
            luaL_openlibs(L);              /* open libraries */
            lua_gc(L, LUA_GCRESTART, 0);
            s.status = handle_luainit(L);
            if (s.status != 0)
            {
                return(0);
            }
            script = collectargs(argv, ref has_i, ref has_v, ref has_e);
            if (script < 0)
            {              /* invalid args? */
                print_usage();
                s.status = 1;
                return(0);
            }
            if (has_v != 0)
            {
                print_version();
            }
            s.status = runargs(L, argv, (script > 0) ? script : s.argc);
            if (s.status != 0)
            {
                return(0);
            }
            if (script != 0)
            {
                s.status = handle_script(L, argv, script);
            }
            if (s.status != 0)
            {
                return(0);
            }
            if (has_i != 0)
            {
                dotty(L);
            }
            else if ((script == 0) && (has_e == 0) && (has_v == 0))
            {
                if (lua_stdin_is_tty() != 0)
                {
                    print_version();
                    dotty(L);
                }
                else
                {
                    dofile(L, null);                   /* executes stdin as a file */
                }
            }
            return(0);
        }
Esempio n. 3
0
        static int Main(string[] args)
        {
            // prepend the exe name to the arg list as it's done in C
            // so that we don't have to change any of the args indexing
            // code above
            List<string> newargs = new List<string>(args);
            newargs.Insert(0, Assembly.GetExecutingAssembly().Location);
            args = (string[])newargs.ToArray();

            Lua.LuaState L;
            Smain s = new Smain();
            int argc = args.Length;
            int i = doargs(argc, args);
            newargs.RemoveRange(0, i);
            argc -= i; args = (string[])newargs.ToArray();
            if (argc <= 0) usage("no input files given");
            L = Lua.lua_open();
            if (L == null)
                fatal("not enough memory for state");
            s.argc = argc;
            s.argv = args;
            if (Lua.lua_cpcall(L, pmain, s) != 0)
                fatal(Lua.lua_tostring(L, -1));
            Lua.lua_close(L);
            return Lua.EXIT_SUCCESS;
        }
        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);
        }