LuaBridge
LuaBridge copied to clipboard
Catch cast exceptions
LuaExceptions thrown during a cast
call can't be caught, which is a problem for some applications, e.g. casts to std::map
- I can cast to std::map<LuaRef, LuaRef>
, but then I have to manually check each key and value whether it is of a certain type.
What do you mean by "can't be caught"? There is an exception and a catch
operator skips it?
Could you please post the minimal code example that reproduces the issue?
I've already figured it out for myself, forgot to update this issue, sorry :/
The problem is that failed casts generate a Lua error, which isn't re-raised as LuaException
, but can only be caught by lua_pcall
. The solution I used was to wrap the cast in a call to lua_pcall
and return an empty std::optional
on a failed cast.
Actually if you had at least one luagridge::getGlobalNamespace (L)
call, Lua errors would be translated to C++ exceptions. Such behavior was introduced in the version 2.3.1.
Also I'm going to provide an explicit luabridge::enableExceptions (L)
function (which will still be implicitly invoked by luagridge::getGlobalNamespace (L)
). The implementation is pretty simple: set atpanic handler (via lua_atpanic
) which will get the error message from the stack and throw an exception with the what
message.
So my questions: what LuaBridge version did you use and did you do any class/function/data registration?
P.S. My personal opinion about heterogeneous Lua tables is that they should be cast to std::map <LuaRef, LuaRef>
- exactly as you do (though it seems to be a rather special use case).