예제 #1
0
        static int handle_script(Lua.LuaState L, string[] argv, int n)
        {
            int status;

            Lua.CharPtr fname;
            int         narg = getargs(L, argv, n); /* collect arguments */

            Lua.lua_setglobal(L, "arg");
            fname = argv[n];
            if (Lua.strcmp(fname, "-") == 0 && Lua.strcmp(argv[n - 1], "--") != 0)
            {
                fname = null;  /* stdin */
            }
            status = Lua.luaL_loadfile(L, fname);
            Lua.lua_insert(L, -(narg + 1));
            if (status == 0)
            {
                status = docall(L, narg, 0);
            }
            else
            {
                Lua.lua_pop(L, narg);
            }
            return(report(L, status));
        }
예제 #2
0
        static int loadline(Lua.LuaState L)
        {
            int status;

            Lua.lua_settop(L, 0);
            if (pushline(L, 1) == 0)
            {
                return(-1);  /* no input */
            }
            for (; ;)
            {  /* repeat until gets a complete line */
                status = Lua.luaL_loadbuffer(L, Lua.lua_tostring(L, 1), Lua.lua_strlen(L, 1), "=stdin");
                if (incomplete(L, status) == 0)
                {
                    break;                /* cannot try to add lines? */
                }
                if (pushline(L, 0) == 0)  /* no more input? */
                {
                    return(-1);
                }
                Lua.lua_pushliteral(L, "\n"); /* add a new line... */
                Lua.lua_insert(L, -2);        /* ...between the two lines */
                Lua.lua_concat(L, 3);         /* join them */
            }
            Lua.lua_saveline(L, 1);
            Lua.lua_remove(L, 1);  /* remove line */
            return(status);
        }
예제 #3
0
        static void dotty(Lua.LuaState L)
        {
            int status;

            Lua.CharPtr oldprogname = progname;
            progname = null;
            while ((status = loadline(L)) != -1)
            {
                if (status == 0)
                {
                    status = docall(L, 0, 0);
                }
                report(L, status);
                if (status == 0 && Lua.lua_gettop(L) > 0)
                {  /* any result to print? */
                    Lua.lua_getglobal(L, "print");
                    Lua.lua_insert(L, 1);
                    if (Lua.lua_pcall(L, Lua.lua_gettop(L) - 1, 0, 0) != 0)
                    {
                        l_message(progname, Lua.lua_pushfstring(L,
                                                                "error calling " + Lua.LUA_QL("print").ToString() + " (%s)",
                                                                Lua.lua_tostring(L, -1)));
                    }
                }
            }
            Lua.lua_settop(L, 0);  /* clear stack */
            Lua.fputs("\n", Lua.stdout);
            Lua.fflush(Lua.stdout);
            progname = oldprogname;
        }
예제 #4
0
        static int docall(Lua.LuaState L, int narg, int clear)
        {
            int status;
            int base_ = Lua.lua_gettop(L) - narg; /* function index */

            Lua.lua_pushcfunction(L, traceback);  /* push traceback function */
            Lua.lua_insert(L, base_);             /* put it under chunk and args */
            //signal(SIGINT, laction);
            status = Lua.lua_pcall(L, narg, ((clear != 0) ? 0 : Lua.LUA_MULTRET), base_);
            //signal(SIGINT, SIG_DFL);
            Lua.lua_remove(L, base_);  /* remove traceback function */
            /* force a complete garbage collection in case of errors */
            if (status != 0)
            {
                Lua.lua_gc(L, Lua.LUA_GCCOLLECT, 0);
            }
            return(status);
        }