From 887adadfc96ab4fa2b80ff5256b8112ea90638fa Mon Sep 17 00:00:00 2001 From: WiiPlayer2 Date: Mon, 24 Oct 2016 16:01:26 +0200 Subject: [PATCH 1/3] Moved std* streams to GlobalState --- SharpLua/LuaCore/lstate.cs | 17 ++++++++++++++++- SharpLua/LuaCore/luaconf.cs | 13 ------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/SharpLua/LuaCore/lstate.cs b/SharpLua/LuaCore/lstate.cs index 545bf2a..076c384 100644 --- a/SharpLua/LuaCore/lstate.cs +++ b/SharpLua/LuaCore/lstate.cs @@ -19,7 +19,7 @@ namespace SharpLua using StkId = Lua.lua_TValue; using ptrdiff_t = System.Int32; using Instruction = System.UInt32; - + using System.IO; public partial class Lua { /* table of globals */ @@ -150,6 +150,21 @@ public static CallInfo dec(ref CallInfo value) */ public class GlobalState { + +#if XBOX || SILVERLIGHT + public static Stream stdout = Stream.Null; + public static Stream stdin = Stream.Null; + public static Stream stderr = Stream.Null; +#elif WindowsCE + public static Stream stdout = new ConsoleStream(1); + public static Stream stdin = new ConsoleStream(0); + public static Stream stderr = new ConsoleStream(2); +#else + public static Stream stdout = Console.OpenStandardOutput(); + public static Stream stdin = Console.OpenStandardInput(); + public static Stream stderr = Console.OpenStandardError(); +#endif + public stringtable strt = new stringtable(); /* hash table for strings */ public lua_Alloc frealloc; /* function to reallocate memory */ public object ud; /* auxiliary data to `frealloc' */ diff --git a/SharpLua/LuaCore/luaconf.cs b/SharpLua/LuaCore/luaconf.cs index 6bee1d6..c83c5ca 100644 --- a/SharpLua/LuaCore/luaconf.cs +++ b/SharpLua/LuaCore/luaconf.cs @@ -1354,19 +1354,6 @@ public static int ungetc(char c, Stream f) return -1; } -#if XBOX || SILVERLIGHT - public static Stream stdout = Stream.Null; - public static Stream stdin = Stream.Null; - public static Stream stderr = Stream.Null; -#elif WindowsCE - public static Stream stdout = new ConsoleStream(1); - public static Stream stdin = new ConsoleStream(0); - public static Stream stderr = new ConsoleStream(2); -#else - public static Stream stdout = Console.OpenStandardOutput(); - public static Stream stdin = Console.OpenStandardInput(); - public static Stream stderr = Console.OpenStandardError(); -#endif public static int EOF = -1; static int WriteArrayToStream(Stream s, byte[] buffer) From c391577c9f26fb2ed786fe87a26bd4d6e4d91c2e Mon Sep 17 00:00:00 2001 From: WiiPlayer2 Date: Tue, 25 Oct 2016 11:10:23 +0200 Subject: [PATCH 2/3] Fixed std* references --- SharpLua/LuaCore/Libraries/lbaselib.cs | 6 +- SharpLua/LuaCore/Libraries/ldblib.cs | 8 +-- SharpLua/LuaCore/Libraries/liolib.cs | 6 +- SharpLua/LuaCore/VM/lauxlib.cs | 4 +- SharpLua/LuaCore/lstate.cs | 18 ++--- SharpLua/LuaCore/luaconf.cs | 12 ++-- SharpLua/LuaCore/print.cs | 96 +++++++++++++------------- 7 files changed, 75 insertions(+), 75 deletions(-) diff --git a/SharpLua/LuaCore/Libraries/lbaselib.cs b/SharpLua/LuaCore/Libraries/lbaselib.cs index dd595bf..0c939a6 100644 --- a/SharpLua/LuaCore/Libraries/lbaselib.cs +++ b/SharpLua/LuaCore/Libraries/lbaselib.cs @@ -34,11 +34,11 @@ private static int luaB_print(LuaState L) if (s == null) return luaL_error(L, LUA_QL("tostring") + " must return a string to " + LUA_QL("print")); - if (i > 1) fputs("\t", stdout); - fputs(s, stdout); + if (i > 1) fputs("\t", L.l_G.stdout); + fputs(s, L.l_G.stdout); lua_pop(L, 1); /* pop result */ } - fputs("\n", stdout); + fputs("\n", L.l_G.stdout); return 0; } diff --git a/SharpLua/LuaCore/Libraries/ldblib.cs b/SharpLua/LuaCore/Libraries/ldblib.cs index 31a09c5..b416b91 100644 --- a/SharpLua/LuaCore/Libraries/ldblib.cs +++ b/SharpLua/LuaCore/Libraries/ldblib.cs @@ -334,15 +334,15 @@ private static int db_debug(LuaState L) for (; ; ) { CharPtr buffer = new char[250]; - fputs("lua_debug> ", stderr); - if (fgets(buffer, stdin) == null || + fputs("lua_debug> ", L.l_G.stderr); + if (fgets(buffer, L.l_G.stdin) == null || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, (uint)strlen(buffer), "=(debug command)") != 0 || lua_pcall(L, 0, 0, 0) != 0) { - fputs(lua_tostring(L, -1), stderr); - fputs("\n", stderr); + fputs(lua_tostring(L, -1), L.l_G.stderr); + fputs("\n", L.l_G.stderr); } lua_settop(L, 0); /* remove eventual returns */ } diff --git a/SharpLua/LuaCore/Libraries/liolib.cs b/SharpLua/LuaCore/Libraries/liolib.cs index bf2c756..d9a2fd0 100644 --- a/SharpLua/LuaCore/Libraries/liolib.cs +++ b/SharpLua/LuaCore/Libraries/liolib.cs @@ -552,9 +552,9 @@ public static int luaopen_io (LuaState L) { luaL_register(L, LUA_IOLIBNAME, iolib); /* create (and set) default files */ newfenv(L, io_noclose); /* close function for default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, 0, "stderr"); + createstdfile(L, L.l_G.stdin, IO_INPUT, "stdin"); + createstdfile(L, L.l_G.stdout, IO_OUTPUT, "stdout"); + createstdfile(L, L.l_G.stderr, 0, "stderr"); lua_pop(L, 1); /* pop environment for default files */ lua_getfield(L, -1, "popen"); newfenv(L, io_pclose); /* create environment for 'popen' */ diff --git a/SharpLua/LuaCore/VM/lauxlib.cs b/SharpLua/LuaCore/VM/lauxlib.cs index e2428aa..77e1e01 100644 --- a/SharpLua/LuaCore/VM/lauxlib.cs +++ b/SharpLua/LuaCore/VM/lauxlib.cs @@ -711,7 +711,7 @@ public static int luaL_loadfile(LuaState L, CharPtr filename) if (filename == null) { lua_pushliteral(L, "=stdin"); - lf.f = stdin; + lf.f = L.l_G.stdin; } else { @@ -796,7 +796,7 @@ private static object l_alloc(Type t) private static int panic(LuaState L) { //(void)L; /* to avoid warnings */ - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", + fprintf(L.l_G.stderr, "PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); return 0; } diff --git a/SharpLua/LuaCore/lstate.cs b/SharpLua/LuaCore/lstate.cs index 076c384..fca1f09 100644 --- a/SharpLua/LuaCore/lstate.cs +++ b/SharpLua/LuaCore/lstate.cs @@ -152,17 +152,17 @@ public class GlobalState { #if XBOX || SILVERLIGHT - public static Stream stdout = Stream.Null; - public static Stream stdin = Stream.Null; - public static Stream stderr = Stream.Null; + public Stream stdout = Stream.Null; + public Stream stdin = Stream.Null; + public Stream stderr = Stream.Null; #elif WindowsCE - public static Stream stdout = new ConsoleStream(1); - public static Stream stdin = new ConsoleStream(0); - public static Stream stderr = new ConsoleStream(2); + public Stream stdout = new ConsoleStream(1); + public Stream stdin = new ConsoleStream(0); + public Stream stderr = new ConsoleStream(2); #else - public static Stream stdout = Console.OpenStandardOutput(); - public static Stream stdin = Console.OpenStandardInput(); - public static Stream stderr = Console.OpenStandardError(); + public Stream stdout = Console.OpenStandardOutput(); + public Stream stdin = Console.OpenStandardInput(); + public Stream stderr = Console.OpenStandardError(); #endif public stringtable strt = new stringtable(); /* hash table for strings */ diff --git a/SharpLua/LuaCore/luaconf.cs b/SharpLua/LuaCore/luaconf.cs index c83c5ca..38600fe 100644 --- a/SharpLua/LuaCore/luaconf.cs +++ b/SharpLua/LuaCore/luaconf.cs @@ -299,9 +299,9 @@ @@ lua_freeline defines how to free a line read by lua_readline. #else public static bool lua_readline(LuaState L, CharPtr b, CharPtr p) { - fputs(p, stdout); - fflush(stdout); /* show prompt */ - return (fgets(b, stdin) != null); /* get line */ + fputs(p, L.l_G.stdout); + fflush(L.l_G.stdout); /* show prompt */ + return (fgets(b, L.l_G.stdin) != null); /* get line */ } public static void lua_saveline(LuaState L, int idx) { } public static void lua_freeline(LuaState L, CharPtr b) { } @@ -944,12 +944,12 @@ public static ulong strtoul(CharPtr s, out CharPtr end, int base_) } } - public static void putchar(char ch) + public static void putchar(char ch, Stream stdout) { fputc(ch, stdout); } - public static int putchar(int ch) + public static int putchar(int ch, Stream stdout) { return fputc(ch, stdout); } @@ -959,7 +959,7 @@ public static bool isprint(byte c) return (c >= (byte)' ') && (c <= (byte)127); } - public static void printf(CharPtr str, params object[] argv) + public static void printf(Stream stdout, CharPtr str, params object[] argv) { Tools.fprintf(stdout, str.ToString(), argv); } diff --git a/SharpLua/LuaCore/print.cs b/SharpLua/LuaCore/print.cs index b33b0c5..279f343 100644 --- a/SharpLua/LuaCore/print.cs +++ b/SharpLua/LuaCore/print.cs @@ -27,29 +27,29 @@ public static void PrintString(TString ts) { CharPtr s = getstr(ts); uint i, n = ts.tsv.len; - putchar('"'); + putchar('"', ts.th.l_G.stdout); for (i = 0; i < n; i++) { int c = s[i]; switch (c) { - case '"': printf("\\\""); break; - case '\\': printf("\\\\"); break; - case '\a': printf("\\a"); break; - case '\b': printf("\\b"); break; - case '\f': printf("\\f"); break; - case '\n': printf("\\n"); break; - case '\r': printf("\\r"); break; - case '\t': printf("\\t"); break; - case '\v': printf("\\v"); break; + case '"': printf(ts.th.l_G.stdout, "\\\""); break; + case '\\': printf(ts.th.l_G.stdout, "\\\\"); break; + case '\a': printf(ts.th.l_G.stdout, "\\a"); break; + case '\b': printf(ts.th.l_G.stdout, "\\b"); break; + case '\f': printf(ts.th.l_G.stdout, "\\f"); break; + case '\n': printf(ts.th.l_G.stdout, "\\n"); break; + case '\r': printf(ts.th.l_G.stdout, "\\r"); break; + case '\t': printf(ts.th.l_G.stdout, "\\t"); break; + case '\v': printf(ts.th.l_G.stdout, "\\v"); break; default: if (isprint((byte)c)) - putchar(c); + putchar(c, ts.th.l_G.stdout); else - printf("\\%03u", (byte)c); + printf(ts.th.l_G.stdout, "\\%03u", (byte)c); break; } } - putchar('"'); + putchar('"', ts.th.l_G.stdout); } private static void PrintConstant(Proto f, int i) @@ -59,19 +59,19 @@ private static void PrintConstant(Proto f, int i) switch (ttype(o)) { case LUA_TNIL: - printf("nil"); + printf(f.th.l_G.stdout, "nil"); break; case LUA_TBOOLEAN: - printf(bvalue(o) != 0 ? "true" : "false"); + printf(f.th.l_G.stdout, bvalue(o) != 0 ? "true" : "false"); break; case LUA_TNUMBER: - printf(LUA_NUMBER_FMT, nvalue(o)); + printf(f.th.l_G.stdout, LUA_NUMBER_FMT, nvalue(o)); break; case LUA_TSTRING: PrintString(rawtsvalue(o)); break; default: /* cannot happen */ - printf("? type=%d", ttype(o)); + printf(f.th.l_G.stdout, "? type=%d", ttype(o)); break; } } @@ -90,39 +90,39 @@ private static void PrintCode(Proto f) int bx = GETARG_Bx(i); int sbx = GETARG_sBx(i); int line = getline(f, pc); - printf("\t%d\t", pc + 1); - if (line > 0) printf("[%d]\t", line); else printf("[-]\t"); - printf("%-9s\t", luaP_opnames[(int)o]); + printf(f.th.l_G.stdout, "\t%d\t", pc + 1); + if (line > 0) printf(f.th.l_G.stdout, "[%d]\t", line); else printf(f.th.l_G.stdout, "[-]\t"); + printf(f.th.l_G.stdout, "%-9s\t", luaP_opnames[(int)o]); switch (getOpMode(o)) { case OpMode.iABC: - printf("%d", a); - if (getBMode(o) != OpArgMask.OpArgN) printf(" %d", (ISK(b) != 0) ? (-1 - INDEXK(b)) : b); - if (getCMode(o) != OpArgMask.OpArgN) printf(" %d", (ISK(c) != 0) ? (-1 - INDEXK(c)) : c); + printf(f.th.l_G.stdout, "%d", a); + if (getBMode(o) != OpArgMask.OpArgN) printf(f.th.l_G.stdout, " %d", (ISK(b) != 0) ? (-1 - INDEXK(b)) : b); + if (getCMode(o) != OpArgMask.OpArgN) printf(f.th.l_G.stdout, " %d", (ISK(c) != 0) ? (-1 - INDEXK(c)) : c); break; case OpMode.iABx: - if (getBMode(o) == OpArgMask.OpArgK) printf("%d %d", a, -1 - bx); else printf("%d %d", a, bx); + if (getBMode(o) == OpArgMask.OpArgK) printf(f.th.l_G.stdout, "%d %d", a, -1 - bx); else printf(f.th.l_G.stdout, "%d %d", a, bx); break; case OpMode.iAsBx: - if (o == OpCode.OP_JMP) printf("%d", sbx); else printf("%d %d", a, sbx); + if (o == OpCode.OP_JMP) printf(f.th.l_G.stdout, "%d", sbx); else printf(f.th.l_G.stdout, "%d %d", a, sbx); break; } switch (o) { case OpCode.OP_LOADK: - printf("\t; "); PrintConstant(f, bx); + printf(f.th.l_G.stdout, "\t; "); PrintConstant(f, bx); break; case OpCode.OP_GETUPVAL: case OpCode.OP_SETUPVAL: - printf("\t; %s", (f.sizeupvalues > 0) ? getstr(f.upvalues[b]).ToString() : "-"); + printf(f.th.l_G.stdout, "\t; %s", (f.sizeupvalues > 0) ? getstr(f.upvalues[b]).ToString() : "-"); break; case OpCode.OP_GETGLOBAL: case OpCode.OP_SETGLOBAL: - printf("\t; %s", svalue(f.k[bx])); + printf(f.th.l_G.stdout, "\t; %s", svalue(f.k[bx])); break; case OpCode.OP_GETTABLE: case OpCode.OP_SELF: - if (ISK(c) != 0) { printf("\t; "); PrintConstant(f, INDEXK(c)); } + if (ISK(c) != 0) { printf(f.th.l_G.stdout, "\t; "); PrintConstant(f, INDEXK(c)); } break; case OpCode.OP_SETTABLE: case OpCode.OP_ADD: @@ -135,28 +135,28 @@ private static void PrintCode(Proto f) case OpCode.OP_LE: if (ISK(b) != 0 || ISK(c) != 0) { - printf("\t; "); - if (ISK(b) != 0) PrintConstant(f, INDEXK(b)); else printf("-"); - printf(" "); - if (ISK(c) != 0) PrintConstant(f, INDEXK(c)); else printf("-"); + printf(f.th.l_G.stdout, "\t; "); + if (ISK(b) != 0) PrintConstant(f, INDEXK(b)); else printf(f.th.l_G.stdout, "-"); + printf(f.th.l_G.stdout, " "); + if (ISK(c) != 0) PrintConstant(f, INDEXK(c)); else printf(f.th.l_G.stdout, "-"); } break; case OpCode.OP_JMP: case OpCode.OP_FORLOOP: case OpCode.OP_FORPREP: - printf("\t; to %d", sbx + pc + 2); + printf(f.th.l_G.stdout, "\t; to %d", sbx + pc + 2); break; case OpCode.OP_CLOSURE: - printf("\t; %p", VOID(f.p[bx])); + printf(f.th.l_G.stdout, "\t; %p", VOID(f.p[bx])); break; case OpCode.OP_SETLIST: - if (c == 0) printf("\t; %d", (int)code[++pc]); - else printf("\t; %d", c); + if (c == 0) printf(f.th.l_G.stdout, "\t; %d", (int)code[++pc]); + else printf(f.th.l_G.stdout, "\t; %d", c); break; default: break; } - printf("\n"); + printf(f.th.l_G.stdout, "\n"); } } @@ -172,36 +172,36 @@ private static void PrintHeader(Proto f) s = "(bstring)"; else s = "(string)"; - printf("\n%s <%s:%d,%d> (%d Instruction%s, %d bytes at %p)\n", + printf(f.th.l_G.stdout, "\n%s <%s:%d,%d> (%d Instruction%s, %d bytes at %p)\n", (f.linedefined == 0) ? "main" : "function", s, f.linedefined, f.lastlinedefined, f.sizecode, SS(f.sizecode), f.sizecode * GetUnmanagedSize(typeof(Instruction)), VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", + printf(f.th.l_G.stdout, "%d%s param%s, %d slot%s, %d upvalue%s, ", f.numparams, (f.is_vararg != 0) ? "+" : "", SS(f.numparams), f.maxstacksize, SS(f.maxstacksize), f.nups, SS(f.nups)); - printf("%d local%s, %d constant%s, %d function%s\n", + printf(f.th.l_G.stdout, "%d local%s, %d constant%s, %d function%s\n", f.sizelocvars, SS(f.sizelocvars), f.sizek, SS(f.sizek), f.sizep, SS(f.sizep)); } private static void PrintConstants(Proto f) { int i, n = f.sizek; - printf("constants (%d) for %p:\n", n, VOID(f)); + printf(f.th.l_G.stdout, "constants (%d) for %p:\n", n, VOID(f)); for (i = 0; i < n; i++) { - printf("\t%d\t", i + 1); + printf(f.th.l_G.stdout, "\t%d\t", i + 1); PrintConstant(f, i); - printf("\n"); + printf(f.th.l_G.stdout, "\n"); } } private static void PrintLocals(Proto f) { int i, n = f.sizelocvars; - printf("locals (%d) for %p:\n", n, VOID(f)); + printf(f.th.l_G.stdout, "locals (%d) for %p:\n", n, VOID(f)); for (i = 0; i < n; i++) { - printf("\t%d\t%s\t%d\t%d\n", + printf(f.th.l_G.stdout, "\t%d\t%s\t%d\t%d\n", i, getstr(f.locvars[i].varname), f.locvars[i].startpc + 1, f.locvars[i].endpc + 1); } } @@ -209,11 +209,11 @@ private static void PrintLocals(Proto f) private static void PrintUpvalues(Proto f) { int i, n = f.sizeupvalues; - printf("upvalues (%d) for %p:\n", n, VOID(f)); + printf(f.th.l_G.stdout, "upvalues (%d) for %p:\n", n, VOID(f)); if (f.upvalues == null) return; for (i = 0; i < n; i++) { - printf("\t%d\t%s\n", i, getstr(f.upvalues[i])); + printf(f.th.l_G.stdout, "\t%d\t%s\n", i, getstr(f.upvalues[i])); } } From 0d502a76fafd6780445fc08caa7c3944b893c30a Mon Sep 17 00:00:00 2001 From: WiiPlayer2 Date: Tue, 25 Oct 2016 11:33:09 +0200 Subject: [PATCH 3/3] Added RegisterModule(object) --- SharpLua/Interfacing/Lua.cs | 39 ++++++++++++++++++++++++++++++++++++- SharpLua/LuaRuntime.cs | 5 +++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/SharpLua/Interfacing/Lua.cs b/SharpLua/Interfacing/Lua.cs index a7c7e4a..8559f13 100644 --- a/SharpLua/Interfacing/Lua.cs +++ b/SharpLua/Interfacing/Lua.cs @@ -947,7 +947,7 @@ internal void pushCSFunction(SharpLua.Lua.lua_CFunction function) }*/ /// - /// If the class has the ModuleAttribute, it registers all the methods with + /// If the class has the LuaModuleAttribute, it registers all the methods with /// the LuaFunctionAttribute into this instance of SharpLua /// /// @@ -980,6 +980,43 @@ public LuaTable RegisterModule(Type t) return ret; } + /// + /// If the class of the object has the LuaModuleAttribute, it registers all + /// the methods with the LuaFunctionAttribute into this instance of SharpLua + /// + /// + /// The module, or null if there wasn't one + public LuaTable RegisterModule(object o) + { + LuaTable ret = null; + var t = o.GetType(); + + //Shouldn't ever happen, or can it? + //if (t.IsClass == false) + // throw new ArgumentException("Not a class", "t"); + + LuaModuleAttribute[] attribs = (LuaModuleAttribute[])t.GetCustomAttributes(typeof(LuaModuleAttribute), false); + if (attribs.Length > 0) + { + if (attribs[0].ModuleName == "") + attribs[0].ModuleName = t.Name; // Default to class name if not specified + // its a module + string module = attribs[0].ModuleName; + ret = this.NewTable(module); + foreach (MethodInfo mi in t.GetMethods()) + { + LuaFunctionAttribute[] a2 = (LuaFunctionAttribute[])mi.GetCustomAttributes(typeof(LuaFunctionAttribute), false); + if (a2.Length > 0 && !mi.IsStatic) + { + if (a2[0].FunctionName == "") + a2[0].FunctionName = mi.Name; + this.RegisterFunction(module + "." + a2[0].FunctionName, o, mi); + } + } + } + return ret; + } + public virtual void Dispose() { if (translator != null) diff --git a/SharpLua/LuaRuntime.cs b/SharpLua/LuaRuntime.cs index 41275f4..849e750 100644 --- a/SharpLua/LuaRuntime.cs +++ b/SharpLua/LuaRuntime.cs @@ -117,6 +117,11 @@ public static void RegisterModule(Type t) _interface.RegisterModule(t); } + public static void RegisterModule(object o) + { + _interface.RegisterModule(o); + } + /// /// Gets the global LuaInterface object used by LuaRuntime methods. ///