static int getargs(Lua.lua_State L, string[] argv, int n) { int narg; int i; int argc = argv.Length; /* count total number of arguments */ narg = argc - (n + 1); /* number of arguments to the script */ Lua.luaL_checkstack(L, narg + 3, "too many arguments to script"); for (i = n + 1; i < argc; i++) { Lua.lua_pushstring(L, argv[i]); } Lua.lua_createtable(L, narg, n + 1); for (i = 0; i < argc; i++) { Lua.lua_pushstring(L, argv[i]); Lua.lua_rawseti(L, -2, i - n); } return(narg); }
static int docall(Lua.lua_State 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); }
static int traceback(Lua.lua_State L) { if (Lua.lua_isstring(L, 1) == 0) /* 'message' not a string? */ { return(1); /* keep it intact */ } Lua.lua_getfield(L, Lua.LUA_GLOBALSINDEX, "debug"); if (!Lua.lua_istable(L, -1)) { Lua.lua_pop(L, 1); return(1); } Lua.lua_getfield(L, -1, "traceback"); if (!Lua.lua_isfunction(L, -1)) { Lua.lua_pop(L, 2); return(1); } Lua.lua_pushvalue(L, 1); /* pass error message */ Lua.lua_pushinteger(L, 2); /* skip this function and traceback */ Lua.lua_call(L, 2, 1); /* call debug.traceback */ return(1); }
static int dolibrary(Lua.lua_State L, Lua.CharPtr name) { Lua.lua_getglobal(L, "require"); Lua.lua_pushstring(L, name); return(report(L, docall(L, 1, 1))); }
static int dostring(Lua.lua_State L, Lua.CharPtr s, Lua.CharPtr name) { int status = (Lua.luaL_loadbuffer(L, s, (uint)Lua.strlen(s), name) != 0) || (docall(L, 0, 1) != 0) ? 1 : 0; return(report(L, status)); }
static int dofile(Lua.lua_State L, Lua.CharPtr name) { int status = (Lua.luaL_loadfile(L, name) != 0) || (docall(L, 0, 1) != 0) ? 1 : 0; return(report(L, status)); }
public static int ToLua_LuaBindingTest_StaticPrintMessage(KopiLua.Lua.lua_State L) { LuaBindingTest.StaticPrintMessage("StaticPrintMessage"); return(1); }
//#endif /* } */ //#endif /* } */ /* ** lua_readline defines how to show a prompt and then read a line from ** the standard input. ** lua_saveline defines how to "save" a read line in a "history". ** lua_freeline defines how to free a line read by lua_readline. */ //#if !defined(lua_readline) /* { */ //#if defined(LUA_USE_READLINE) /* { */ //#include <readline/readline.h> //#include <readline/history.h> //#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) //#define lua_saveline(L,line) ((void)L, add_history(line)) //#define lua_freeline(L,b) ((void)L, free(b)) //#else /* }{ */ private static int lua_readline(Lua.lua_State L, Lua.CharPtr b, Lua.CharPtr p) { /*(void)L,*/ Lua.fputs(p, Lua.stdout); Lua.fflush(Lua.stdout); /* show prompt */ return((Lua.fgets(b /*, LUA_MAXINPUT*/, Lua.stdin) != null) ? 1 : 0); } /* get line */ //FIXME: no Lua_MAXINPUT
static Lua.Proto toproto(Lua.lua_State L, int i) { return(Lua.getproto(L.top + i)); }
public static string dolua_(string message) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>" + message); } if (L_ == null) { L_ = Lua.luaL_newstate(); Lua.luaL_openlibs(L_); } if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>2"); } string errorMessage = null; bool printResult = true; int status = Lua.luaL_loadbuffer(L_, message, (uint)Lua.strlen(message), "=stdin"); if (status == 0) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>3"); } status = docall_(L_, 0, printResult ? Lua.LUA_MULTRET : 0); } if ((status != 0) && !Lua.lua_isnil(L_, -1)) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>4"); } Lua.CharPtr msg = Lua.lua_tostring(L_, -1); if (msg == null) { msg = "(error object is not a string)"; } errorMessage = msg.ToString(); Lua.lua_pop(L_, 1); /* force a complete garbage collection in case of errors */ Lua.lua_gc(L_, Lua.LUA_GCCOLLECT, 0); } if (printResult) { //see Lua.LUA_MULTRET if (status == 0 && Lua.lua_gettop(L_) > 0) /* any result to print? */ { Lua.luaL_checkstack(L_, Lua.LUA_MINSTACK, "too many results 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))); } } } return(errorMessage); }
static void lstop(Lua.lua_State L, Lua.lua_Debug ar) { //(void)ar; /* unused arg. */ Lua.lua_sethook(L, null, 0, 0); Lua.luaL_error(L, "interrupted!"); }
static int pmain(Lua.lua_State L) { Smain s = (Smain)Lua.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.lua_gc(L, Lua.LUA_GCSTOP, 0); /* stop collector during initialization */ Lua.luaL_openlibs(L); /* open libraries */ Lua.lua_gc(L, Lua.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.lua_stdin_is_tty()!=0) { print_version(); dotty(L); } else dofile(L, null); /* executes stdin as a file */ } return 0; }
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); }
static void lstop(Lua.lua_State L, Lua.lua_Debug ar) { Lua.lua_sethook(L, null, 0, 0); Lua.luaL_error(L, "interrupted!"); }
public static void lua_pushvector(Lua.lua_State L, Vector3 v) { lua_pushvector(L, (double)v.x, (double)v.y, (double)v.z); }
static int pmain(Lua.lua_State L) { Smain s = (Smain)Lua.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.lua_gc(L, Lua.LUA_GCSTOP, 0); /* stop collector during initialization */ Lua.luaL_openlibs(L); /* open libraries */ Lua.lua_gc(L, Lua.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.lua_stdin_is_tty() != 0) { print_version(); dotty(L); } else { dofile(L, null); /* executes stdin as a file */ } } return(0); }
static Lua.Proto toproto(Lua.lua_State L, int i) { return(Lua.clvalue(L.top + (i)).l.p); }
} /* get line */ //FIXME: no Lua_MAXINPUT private static void lua_saveline(Lua.lua_State L, int idx) /*(void)L; (void)idx;*/ }
static int writer(Lua.lua_State L, Lua.CharPtr p, uint size, object u) { //UNUSED(L); return(((Lua.fwrite(p, (int)size, 1, (Stream)u) != 1) && (size != 0)) ? 1 : 0); }
} /* get line */ //FIXME: no Lua_MAXINPUT private static void lua_saveline(Lua.lua_State L, Lua.CharPtr line) /*(void)L; (void)line;*/ }
public static void lua_pushangle(Lua.lua_State L, Angles v) { lua_pushangle(L, (double)v.pitch, (double)v.yaw, (double)v.roll); }