lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On 13 Mar 2015, at 12:43, Nagaev Boris <bnagaev@gmail.com> wrote:
> 
> On Fri, Mar 13, 2015 at 9:23 AM, Colin <lua@colin-hirsch.net> wrote:
>> Hi,
>> 
>> following the announcement of Lua 5.3 with the greatly appreciated addition of native integer support we are planning to update a bunch of C++ projects from Lua 5.1 and were wondering how the compatibility with C++ exceptions has progressed.
>> 
>> 
>> The scenario that we are looking at is
>> 
>> C++ application
>> calls
>> Lua function
>> calls
>> C++ function
>> 
>> when the inner C++ function throws an exception.
>> 
>> 
>> Currently we handle this situation at the Lua-to-C++ call point by wrapping the C++ function in a try-catch block that logs an exception and calls the Lua error function; at the C++-to-Lua transition we detect the Lua error and then throw a C++ exception.
>> 
>> (With C++11 we could improve this approach by extending the Lua state by a std::exception_ptr, storing the exception caught at the Lua-to-C++ transition there, and re-throwing it at the C++-to-Lua point.)
>> 
>> 
>> The question is, can this be simplified?
>> 
>> Ideally the C++ exception would simply “fly through” the Lua interpreter from the inner C++ function to the outside C++ application, but as far as I understand, that is not, or at least: was not possible because Lua obviously can’t use RAII and requires an explicit call to its error handling facilities to keep everything consistent.
>> 
>> If this “flying through” does not work unmodified, is it possible to somehow “patch up” things, i.e. let the exception fly through and then afterwards, in a try-catch at the C++-to-Lua point, call some Lua function to put things back into a consistent state? (This would allow leaving out the try-catch around the inner C++ function and not require adding the exception_ptr to the Lua state to recover the original exception.)
>> 
>> Or what else is the recommended way of handling this interoperability question, what is the current “best practice”?
>> 
>> Thanks, Colin
> 
> Hello, Colin,
> 
> LuaJIT [1] can handle C++ exceptions transparently in some setups. C++
> exceptions can be caught on the Lua side with pcall(), lua_pcall()
> etc. It's safe to throw C++ exceptions across non-protected Lua frames
> on the C stack. The contents of the C++ exception object pass through
> unmodified. Throwing Lua errors across C++ frames is safe. C++
> destructors will be called [2]. LuaJIT is compatible with Lua 5.1.
> 
> [1] http://luajit.org/luajit.html
> [2] http://luajit.org/extensions.html#exceptions

That does look interesting, although we’d have to check whether it is possible for us to switch to LuaJIT. The last time I looked at it that was not possible for lack of 64bit integer support (unfortunately Lua’s default numeric type “double” is not at all appropriate to our applications, and our Lua 5.1 is configured to use int64_t instead, something that Lua is well designed to handle). However it seems that in the meantime LuaJIT has gained support for some boxed 64bit integers, which might be good enough if well integrated…

Thanks again, Colin