Skip to content

Commit e9aaa38

Browse files
acoppesBenjamin-Dobell
authored andcommitted
fix: include original exception stacktrace when handling the script execution so if it fails on the c# part (if invoking a callback) the corresponding c# error line is captured.
1 parent be1bd13 commit e9aaa38

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/MoonSharp.Interpreter.Tests/EndToEnd/ErrorHandlingTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,32 @@ function d()
127127
Assert.AreEqual("!cba", res.String);
128128
}
129129

130+
private DynValue MethodWithError()
131+
{
132+
throw new NullReferenceException();
133+
}
134+
135+
[Test]
136+
public void Errors_Get_OriginalException()
137+
{
138+
string luaScript =
139+
@"
140+
callMethodWithError();
141+
";
142+
Script script = new Script(CoreModules.None);
143+
144+
script.Globals["callMethodWithError"] = DynValue.NewCallback((c, a) => MethodWithError());
145+
146+
try
147+
{
148+
DynValue res = script.DoString(luaScript);
149+
150+
}
151+
catch (Exception e)
152+
{
153+
Assert.IsTrue(e.StackTrace.Contains("MoonSharp.Interpreter.Tests.EndToEnd.ErrorHandlingTests.MethodWithError()"), "Exception should contain c# error line");
154+
}
155+
}
156+
130157
}
131158
}

src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_InstructionLoop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,9 @@ private int Internal_ExecCall(int argsCount, int instructionPtr, CallbackFunctio
729729
m_ValueStack.RemoveLast(argsCount + 1);
730730
m_ValueStack.Push(ret);
731731
}
732-
catch (Exception e)
732+
catch
733733
{
734-
throw e;
734+
throw;
735735
}
736736

737737
m_ExecutionStack.Pop();

0 commit comments

Comments
 (0)