Commit 6b4d739e 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,20 @@ end
return errindex;
}
/// <summary>
/// <para>Return a debug.traceback() call result (a multi-line string, containing a full stack trace, including C calls.</para>
/// <para>Note: it won't return anything unless the interpreter is in the middle of execution - that is, it only makes sense to call it from a method called from Lua, or during a coroutine yield.</para>
/// </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>
/// Convert C# exceptions into Lua errors
/// </summary>
......
......@@ -193,6 +193,7 @@ namespace NLua.Method
failedCall = false;
} catch (TargetInvocationException e) {
// Failure of method invocation
if (_Translator.interpreter.use_traceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback() as string;
return SetPendingException (e.GetBaseException ());
} catch (Exception e) {
if (_Members.Length == 1) // Is the method overloaded?
......@@ -289,6 +290,7 @@ namespace NLua.Method
_Translator.Push (luaState, _LastCalledMethod.cachedMethod.Invoke (targetObject, _LastCalledMethod.args));
}
} catch (TargetInvocationException e) {
if (_Translator.interpreter.use_traceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback() as string;
return SetPendingException (e.GetBaseException ());
} catch (Exception e) {
return SetPendingException (e);
......
......@@ -244,12 +244,14 @@ namespace NLua
if (message != null) {
// 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);
} else {
var ex = e as Exception;
if (ex != null) {
// 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);
}
}
......
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