Skip to content

Commit

Permalink
Fixed stack tracebacks and LuaInterface error messages!
Browse files Browse the repository at this point in the history
  • Loading branch information
efrederickson committed Nov 16, 2012
1 parent dd51855 commit 8e20b8e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 152 deletions.
28 changes: 11 additions & 17 deletions SharpLua.Interactive/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public static void Main(string[] args)
#endif

// how to handle errors
LuaRuntime.SetVariable("DEBUG", true);
//LuaRuntime.SetVariable("DEBUG", true);
LuaRuntime.SetVariable("DEBUG", false);
// We don't need the C# traceback.
// All it is is [error]
// at LuaInterface.ThrowErrorFromException
// [...]
// Which we don't need

#else
LuaRuntime.SetVariable("DEBUG", false);
#endif
Expand Down Expand Up @@ -113,7 +120,7 @@ public static void Main(string[] args)
{
try
{
object[] v = LuaRuntime.Run(line);
object[] v = LuaRuntime.GetLua().DoString(line, "<stdin>");
if (v == null || v.Length == 0)
#if DEBUG
Console.WriteLine("=> [no returned value]");
Expand Down Expand Up @@ -148,25 +155,12 @@ public static void Main(string[] args)
}
catch (Exception error)
{
// TODO: show lua script traceback
// Possible fix... this isn't safe though.

//Console.WriteLine(error.Message);
// doesnt work :(
//LuaRuntime.Run("pcall(function() print(debug.traceback()) end)");
//Console.WriteLine();

//Lua.LuaState l = LuaRuntime.GetLua().LuaState;
//LuaRuntime.GetLua().traceback(l);
//Lua.lua_getglobal(l, "print");
//Lua.lua_call(l, 1, 0);

object dbg = LuaRuntime.GetVariable("DEBUG");

if ((dbg is bool && (bool)dbg == true) || dbg is int == false)
if (dbg != null && (dbg is bool && (bool)dbg == true))
Console.WriteLine(error.ToString());
else
Console.WriteLine("Error: " + error.Message);
Console.WriteLine(error.Message);
LuaRuntime.SetVariable("LASTERROR", error);
}
}
Expand Down
46 changes: 31 additions & 15 deletions SharpLua/Interfacing/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public LuaInterface()
panicCallback = new SharpLua.Lua.lua_CFunction(PanicCallback);
Lua.lua_CFunction oldpanicFunc =
//LuaDLL.lua_atpanic(luaState, tracebackFunction);
LuaDLL.lua_atpanic(luaState, panicCallback);
LuaDLL.lua_atpanic(luaState, panicCallback);

LuaDLL.luaL_dostring(luaState, ScriptStrings.InitClrLib);
LuaDLL.luaL_dostring(luaState, ScriptStrings.InitExtLib);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void Close()
}

/// <summary>
/// Called for each lua_lock call
/// Called for each lua_lock call
/// </summary>
/// <param name="luaState"></param>
/// Not yet used
Expand All @@ -135,7 +135,7 @@ int LockCallback(SharpLua.Lua.LuaState luaState)
}

/// <summary>
/// Called for each lua_unlock call
/// Called for each lua_unlock call
/// </summary>
/// <param name="luaState"></param>
/// Not yet used
Expand Down Expand Up @@ -166,19 +166,34 @@ static int PanicCallback(SharpLua.Lua.LuaState luaState)
/// <exception cref="LuaScriptException">Thrown if the script caused an exception</exception>
void ThrowExceptionFromError(int oldTop)
{
object err = translator.getObject(luaState, -1);
LuaDLL.lua_settop(luaState, oldTop);

// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
LuaException luaEx = err as LuaException;
if (luaEx != null)
throw luaEx;
string msg = "";
if (Lua.lua_isstring(luaState, -1) == 1)
msg = Lua.lua_tostring(luaState, -1);
else if (Lua.lua_isnumber(luaState, -1) == 1
|| Lua.lua_isboolean(luaState, -1)
|| Lua.lua_iscfunction(luaState, -1)
|| Lua.lua_isfunction(luaState, -1)
|| Lua.lua_isthread(luaState, -1)
|| Lua.lua_istable(luaState, -1)
)
msg = "Unknown Lua Error";
else
{
object err = translator.getObject(luaState, -1);
LuaDLL.lua_settop(luaState, oldTop);

// A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it
if (err == null)
err = "Unknown Lua Error";
// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
LuaException luaEx = err as LuaException;
if (luaEx != null)
throw luaEx;

throw new LuaException(err.ToString());
// A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it
if (err == null)
msg = "Unknown Lua Error";
else
msg = err.ToString();
}
throw new LuaException(msg);
}


Expand Down Expand Up @@ -269,13 +284,14 @@ public object[] DoString(string chunk)
/// <returns></returns>
public object[] DoString(string chunk, string chunkName)
{
LuaDLL.lua_pushstdcallcfunction(luaState, tracebackFunction);
int oldTop = LuaDLL.lua_gettop(luaState);
executing = true;
if (LuaDLL.luaL_loadbuffer(luaState, chunk, chunk.Length, chunkName) == 0)
{
try
{
if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
if (LuaDLL.lua_pcall(luaState, 0, -1, -2) == 0)
return translator.popValues(luaState, oldTop);
else
ThrowExceptionFromError(oldTop);
Expand Down
72 changes: 0 additions & 72 deletions SharpLua/Interfacing/Main.cs

This file was deleted.

1 change: 1 addition & 0 deletions SharpLua/Interfacing/ObjectTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ internal void pushFunction(SharpLua.Lua.LuaState luaState, SharpLua.Lua.lua_CFun
if (true)
// 11/16/12 - Fix function pushing (Dirk Weltz metatable problem)
// SharpLua.InterfacingTests still works
// This allows metatables to have CLR defined functions
Lua.lua_pushcfunction(luaState, func);
else
pushObject(luaState, func, "luaNet_function");
Expand Down
45 changes: 1 addition & 44 deletions SharpLua/LuaCore/lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public static bool lua_isnoneornil(LuaState L, lua_Number n)
public static void lua_pushliteral(LuaState L, CharPtr s)
{
//TODO: Implement use using lua_pushlstring instead of lua_pushstring
//lua_pushlstring(L, s, s.chars.Length);
//lua_pushlstring(L, "" s, (sizeof(s)/GetUnmanagedSize(typeof(char)))-1)
lua_pushstring(L, s);
}
Expand Down Expand Up @@ -227,50 +228,6 @@ public static int lua_getgccount(LuaState L)
return lua_gc(L, LUA_GCCOUNT, 0);
}

/*
public static lua_Unsigned luaL_checkunsigned(LuaState L, int narg)
{
bool isnum = false;
lua_Unsigned d = lua_tounsignedx(L, narg, ref isnum);
if (!isnum)
tag_error(L, narg, LUA_TNUMBER);
return d;
}
public static lua_Unsigned lua_tounsignedx(LuaState L, int idx, ref bool isnum)
{
TValue n = null;
TValue o = index2adr(L, idx);
if (tonumber(ref o, n) == 1)
{
lua_Unsigned res;
lua_Number num = nvalue(o);
res = (lua_Unsigned)num;
if (isnum)
isnum = true;
return res;
}
else
{
if (isnum)
isnum = false;
return 0;
}
}
public static void lua_pushunsigned(LuaState L, lua_Unsigned u)
{
lua_Number n;
lua_lock(L);
n = (((u) <= (lua_Unsigned)int.MaxValue) ? (lua_Number)(int)(u) : (lua_Number)(u));
setnvalue(L._top, n);
api_incr_top(L);
lua_unlock(L);
}
*/



//#define lua_Chunkreader lua_Reader
//#define lua_Chunkwriter lua_Writer

Expand Down
1 change: 0 additions & 1 deletion SharpLua/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public static string FindFullPath(string spath)
if (File.Exists(spath + ".exe"))
return spath + ".exe";
*/
// TODO: .cs, .luac; possibly C

return spath; // let the caller handle the invalid filename
}
Expand Down
2 changes: 1 addition & 1 deletion SharpLua/NewParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Expression ParseSuffixedExpr(Scope scope, bool onlyDotColon = false)
if (reader.IsSymbol('.') || reader.IsSymbol(':'))
{
string symb = reader.Get().Data; // '.' or ':'
// TODO: should we allow keywords?
// TODO: should we allow keywords? I vote no.
if (!reader.Is(TokenType.Ident))
error("<Ident> expected");

Expand Down
5 changes: 4 additions & 1 deletion SharpLua/Resources/clrlib.slua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ end

local clr = { }

clr.ns = { "System", "System.IO", "System.Windows.Forms", "System.Collections.Generic", "System.Text", "SharpLua", "System.Threading" }
clr.ns = {
"System", "System.IO", "System.Windows.Forms", "System.Collections.Generic",
"System.Text", "SharpLua", "System.Threading",
"System.Drawing" }

clr.load = luanet.load_assembly

Expand Down
Binary file modified SharpLua/Resources/clrlib.sluac
Binary file not shown.
1 change: 0 additions & 1 deletion SharpLua/SharpLua.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<Link>TODO.txt</Link>
</None>
<None Include="Decompiler\disassembler example.lua" />
<None Include="Interfacing\Main.cs" />
<Compile Include="Interfacing\LuaRegistrationHelper.cs" />
<Compile Include="Interfacing\LuaScriptException.cs" />
<Compile Include="Interfacing\LuaTable.cs" />
Expand Down

0 comments on commit 8e20b8e

Please sign in to comment.