-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
When returning the result of a C# method in a Lua script and the C# method is throwing an exception, an unexpected IndexOutOfRangeException occurs:
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Lua.Runtime.Traceback.get_LastPosition()
at Lua.LuaRuntimeException.get_Message()
at LuaCSharpBug.Program.Main(String[] args) in C:\LuaCSharpBug\LuaCSharpBug\Program.cs:line 28
I am using LuaCSharp NuGet package version 0.4.2 in .NET 8.0
My minimal example code to demonstrate this is here:
using Lua;
namespace LuaCSharpBug
{
internal class Program
{
static void Main(string[] args)
{
var scriptCodeOk = """
local s = Greeter:Greet("XY")
return s
""";
var scriptCodeIdxOutOfBounds = """
return Greeter:Greet("XY")
""";
var _scriptRuntime = LuaState.Create();
var greeter = new Greeter();
_scriptRuntime.Environment["Greeter"] = greeter;
try {
//var result = _scriptRuntime.DoStringAsync(scriptCodeOk).Result; // This produces the expected error
var result = _scriptRuntime.DoStringAsync(scriptCodeIdxOutOfBounds).Result; // This produces an IndexOutOfRangeException instead of the expected error
Console.WriteLine(result[0]);
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex.Message}");
}
}
}
[LuaObject]
public partial class Greeter
{
[LuaMember]
public string Greet(string lang = "EN")
{
string res = lang.ToUpperInvariant() switch
{
"EN" => "Hello",
"DE" => "Hallo",
_ => throw new ArgumentException($"Invalid language"),
};
return res;
}
}
}
Metadata
Metadata
Assignees
Labels
No labels