Commit c389ad49 authored by Sinus Pi's avatar Sinus Pi
Browse files

Add debug.traceback results to more exception throwers, either setting...

Add debug.traceback results to more exception throwers, either setting .Data["Traceback"] to C# exceptions, or adding the traceback at the end of Lua message-only exceptions.
parent 983af940
...@@ -390,6 +390,18 @@ end ...@@ -390,6 +390,18 @@ end
return errindex; return errindex;
} }
/// <summary>
/// </summary>
public string GetDebugTraceback()
{
int oldTop = LuaLib.LuaGetTop(luaState);
LuaLib.LuaGetGlobal(luaState, "debug"); // stack: debug
LuaLib.LuaGetField(luaState, -1, "traceback"); // stack: debug,traceback
LuaLib.LuaRemove(luaState, -2); // stack: traceback
LuaLib.LuaPCall(luaState, 0, -1, 0);
return translator.PopValues(luaState, oldTop)[0] as string;
}
/// <summary> /// <summary>
/// Convert C# exceptions into Lua errors /// Convert C# exceptions into Lua errors
/// </summary> /// </summary>
...@@ -483,9 +495,10 @@ end ...@@ -483,9 +495,10 @@ end
if (LuaLib.LuaLLoadBuffer(luaState, chunk, chunkName) == 0) if (LuaLib.LuaLLoadBuffer(luaState, chunk, chunkName) == 0)
{ {
int errfunction=0; int errfunction = 0;
if (use_traceback) { if (use_traceback)
errfunction=PushDebugTraceback(luaState, 0); {
errfunction = PushDebugTraceback(luaState, 0);
oldTop++; oldTop++;
} }
...@@ -530,7 +543,7 @@ end ...@@ -530,7 +543,7 @@ end
try try
{ {
if (LuaLib.LuaPCall(luaState, 0, -1, errfunction) == 0) if (LuaLib.LuaPCall(luaState, 0, -1, errfunction) == 0)
return translator.PopValues(luaState, oldTop); return translator.PopValues (luaState, oldTop);
else else
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
} }
...@@ -563,12 +576,15 @@ end ...@@ -563,12 +576,15 @@ end
oldTop++; oldTop++;
} }
try { try
{
if (LuaLib.LuaPCall(luaState, 0, -1, errfunction) == 0) if (LuaLib.LuaPCall(luaState, 0, -1, errfunction) == 0)
return translator.PopValues (luaState, oldTop); return translator.PopValues (luaState, oldTop);
else else
ThrowExceptionFromError (oldTop); ThrowExceptionFromError(oldTop);
} finally { }
finally
{
executing = false; executing = false;
} }
} else } else
......
...@@ -191,8 +191,9 @@ namespace NLua.Method ...@@ -191,8 +191,9 @@ namespace NLua.Method
} }
failedCall = false; failedCall = false;
} catch (TargetInvocationException e) { } catch (TargetInvocationException e) {
// Failure of method invocation // Failure of method invocation
if (_Translator.interpreter.use_traceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback() as string;
return SetPendingException (e.GetBaseException ()); return SetPendingException (e.GetBaseException ());
} catch (Exception e) { } catch (Exception e) {
if (_Members.Length == 1) // Is the method overloaded? if (_Members.Length == 1) // Is the method overloaded?
...@@ -288,7 +289,8 @@ namespace NLua.Method ...@@ -288,7 +289,8 @@ namespace NLua.Method
else else
_Translator.Push (luaState, _LastCalledMethod.cachedMethod.Invoke (targetObject, _LastCalledMethod.args)); _Translator.Push (luaState, _LastCalledMethod.cachedMethod.Invoke (targetObject, _LastCalledMethod.args));
} }
} catch (TargetInvocationException e) { } catch (TargetInvocationException e) {
if (_Translator.interpreter.use_traceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback() as string;
return SetPendingException (e.GetBaseException ()); return SetPendingException (e.GetBaseException ());
} catch (Exception e) { } catch (Exception e) {
return SetPendingException (e); return SetPendingException (e);
......
...@@ -226,6 +226,7 @@ namespace NLua ...@@ -226,6 +226,7 @@ namespace NLua
*/ */
internal void ThrowError (LuaState luaState, object e) internal void ThrowError (LuaState luaState, object e)
{ {
// We use this to remove anything pushed by luaL_where // We use this to remove anything pushed by luaL_where
int oldTop = LuaLib.LuaGetTop (luaState); int oldTop = LuaLib.LuaGetTop (luaState);
...@@ -244,12 +245,14 @@ namespace NLua ...@@ -244,12 +245,14 @@ namespace NLua
if (message != null) { if (message != null) {
// Wrap Lua error (just a string) and store the error location // Wrap Lua error (just a string) and store the error location
if (interpreter.use_traceback) message += "\r\n"+interpreter.GetDebugTraceback() as string;
e = new LuaScriptException (message, errLocation); e = new LuaScriptException (message, errLocation);
} else { } else {
var ex = e as Exception; var ex = e as Exception;
if (ex != null) { if (ex != null) {
// Wrap generic .NET exception as an InnerException and store the error location // Wrap generic .NET exception as an InnerException and store the error location
if (interpreter.use_traceback) ex.Data["Traceback"] = interpreter.GetDebugTraceback() as string;
e = new LuaScriptException (ex, errLocation); e = new LuaScriptException (ex, errLocation);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment