Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion SharpLua/Interfacing/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ internal void pushCSFunction(SharpLua.Lua.lua_CFunction function)
}*/

/// <summary>
/// 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
/// </summary>
/// <param name="t"></param>
Expand Down Expand Up @@ -980,6 +980,43 @@ public LuaTable RegisterModule(Type t)
return ret;
}

/// <summary>
/// If the class of the object has the LuaModuleAttribute, it registers all
/// the methods with the LuaFunctionAttribute into this instance of SharpLua
/// </summary>
/// <param name="t"></param>
/// <returns>The module, or null if there wasn't one</returns>
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)
Expand Down
6 changes: 3 additions & 3 deletions SharpLua/LuaCore/Libraries/lbaselib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions SharpLua/LuaCore/Libraries/ldblib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
Expand Down
6 changes: 3 additions & 3 deletions SharpLua/LuaCore/Libraries/liolib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand Down
4 changes: 2 additions & 2 deletions SharpLua/LuaCore/VM/lauxlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 16 additions & 1 deletion SharpLua/LuaCore/lstate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -150,6 +150,21 @@ public static CallInfo dec(ref CallInfo value)
*/
public class GlobalState
{

#if XBOX || SILVERLIGHT
public Stream stdout = Stream.Null;
public Stream stdin = Stream.Null;
public Stream stderr = Stream.Null;
#elif WindowsCE
public Stream stdout = new ConsoleStream(1);
public Stream stdin = new ConsoleStream(0);
public Stream stderr = new ConsoleStream(2);
#else
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 */
public lua_Alloc frealloc; /* function to reallocate memory */
public object ud; /* auxiliary data to `frealloc' */
Expand Down
25 changes: 6 additions & 19 deletions SharpLua/LuaCore/luaconf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
Loading