|
First, it also compiles Lua with C++ linkage, which means that all the extensions also need to be compiled with C++ (since, otherwise, they don't see the Lua API functions). But many extensions simply won't compile with C++ at all. Second, it disables the setjmp/ longjmp mechanism, which means that existing extensions that use that would have to be hacked.
So, I think the cleanest overall solution for getting exception handling into Lua would be to change Lua itself so that when Lua gets compiled with C++, it catches exceptions, but its API functions would be surrounded by extern "C", and it continues to also provide the setjmp/longjmp mechanism.
However, patching Lua is not an option for us; we're creating an open source project, and we don't want to get into the business of maintaining a Lua fork. If we need to patch anything, it can only be the interface generator. Furthermore, any interface generator not designed to cope with exceptions (i.e., most of them) may need to be hacked anyway, even if Lua itself has exception handling, because there may need to be cleanup in the interface code (deallocation of temporaries, etc.).
So, I think it would be great if the base Lua distribution were updated to handle exceptions better. Basically, what would need to be done is:
* When compiled with C++, there need to be 'extern "C"' wrapped around all API function declarations. * When compiled with C++, there should be try...catch blocks wrapped around all calls to user functions. * By default, both exception mechanisms should be enabled (for compatibility), but users should be able to disable them with - DLUA_NO_EXCEPTIONS and -DLUA_NO_SETJMP
This would maintain compatibility with the existing C distribution and it would ensure that, no matter what exceptions get thrown, Lua remains in a consistent state.
Then, interface generators that cope with C++ need to be reviewed for whether they are exception-safe, and they can provide additional facilities to deal with exceptions in a more intelligent way, such as translating specific exceptions into specific Lua errors, etc.
BTW, note that many existing libraries use exception hierarchies that are not based on std::exception, so just catching std::exception is not sufficient. The std::exception class is simply the base class for exceptions in the standard library.
Cheers, Thomas. On Aug 16, 2007, at 7:54 PM, Erik Cassel wrote:
Perhaps this is relevant: http://lua-users.org/lists/lua-l/2006-03/msg00699.html "This lets me throw an std::exception-derived object from anywhere in C++ code. Lua will catch it and translate it appropriately in luaD_rawrunprotected(). Also, Lua uses an std::exception-derived class to throw Lua errors.This means I can catch errors thrown by Lua (as in lua_touserdata)."Sample source code is here: http://lua-users.org/lists/lua-l/2006-03/msg00707.html Greg Falcon found a bug in my solution, which he documents here:http://lua-users.org/lists/lua-l/2006-03/msg00735.html. Fortunately, the bug can be fixed by defining a copy constructor. If you're interested I can postthe code that we use in production. -Erik ________________________________________ From: lua-bounces@bazar2.conectiva.com.br[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of RJP ComputingSent: Thursday, August 16, 2007 11:01 AM To: Lua list Subject: Binding C++ functions that 'throw' exceptions Hi, I am using toLua++ to bind a bunch of functions and many of them throwexceptions. Lua / toLua doesn't allow the exception to come back to C++after it happens. Instead Lua / toLua reports "Cannot convert value tostring" which is not useful. I need to have the exception to go to my main application that knows how to deal with that type of exception. Or I need tohave the exception re-bundled as a lua_error. I am using Gcc for thecompiler on Windows. If I don't compile Lua as C++ I don't get anything except a close of my program. It appears the Gcc has a problem bridging the gap between C and C++ when exceptions are thrown. Are there any ideas on if this can be automated? I don't want to write all the bindings by hand and Ihave to deal with exceptions.Now I have created bindings by hand and I can properly catch the exceptioninside the binding function. All I do with the exception is get the exceptions description and call luaL_error( description ). This worksflawlessly. I basically am asking if there is a way to make the generated functions generate try/catch blocks in the generated functions. Or to push the error message from my exception on to the stack so that when lua_pcall() returns it's error number the message will be on the stack as it usuallydoes. I hope this made sense. Please ask if you need clarification. -- Regards, Ryan RJP Computing