lua-users home
lua-l archive

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

The problem you said is much like what I asked before. Here is my solution
for it. It is based on one point: you can always use lua_error() if you get
an error in a C function.

First, I modify luaconf.h:

namespace lua
  class lua_exception

#define LUAI_THROW(L,c) throw lua::lua_exception()
And, in my application codes, I use a wrapper function for the function that
can be called from Lua:

int LuaFunctionWrapper(lua_State *s)
    return real_lua_function( s );
  catch(const lua::lua_exception &e)
    // real_lua_function() called lua_error(), the error handler was called
    // So spread the exception to the lua_pcall's exception
    throw e;
    // real_lua_function() throw a C++ exception directly, the error handler
was not called.
    // Call lua_error() for spread the exception. lua_error() will call
error handler and throw a C++ exception lua::lua_exception.
    lua_pushstring( s, "an error message" )
    lua_error( s );
  return 0; // For compiler warning, never executed it

Following are some info from Roberto:
Me: Now, my question is whether it is safe to throw an C++ exception> in
such context. Does it miss anything about cleanup or state> settings? And,
does it place the lua_State instance in an inconsistent> state?
Roberto: If you compile Lua using try-catch internally, then it should be
safeto throw an exception in your library. Lua would catch it and do
allnecessary cleanup.

周惟迪 | Zhou Wei Di |

[] 代表 Colin
发送时间: 2007年10月20日 2:48
主题: C++ exceptions not really supported?


I am currently, for the first time, in the process of adding Lua scripting
to a set of C++ applications and have reached the point of digging into
error handling; after reading the following statement in the documentation:

"You can also choose to use exceptions if you use C++"

I was hoping that this would be easy, however after digging into the Lua
source it seems that exceptions aren't really supported, and that adding
support for them would require a significant effort.

What do I mean by "support exceptions", and why do I think that it's hard?

My, perhaps exaggerated, expecation was that if I "choose to use" C++
exceptions in luaconf.h then

- Lua would use exceptions to report errors (something derived from
  std::exception with a meaningful "what()" message), and

- exceptions thrown by lua_CFunctions (i.e., C++-functions) that are
  called from Lua "sail through" the Lua interpreter cleaning up
  behind themselves (either the cumbersome, C-style with a lot of
  try-catch blocks, or the C++-style where all cleanup is performed
  in stack-local objects' destructors, i.e. generalised RAII).

As far as I can see, Lua is far from both, and, in practice, enabling
exceptions does not really change much: a lua_CFunction should never
throw an exception because the missing automatic cleanup means that it
will leave the lua_State in a strange state when not invoked within a
protected call.

I just spent half an hour to see how hard a conversion would be, i.e.
I removed all longjmp related stuff, and changed rawrunprotected to
just call the function, and started to convert the callers of this
function to be exception safe ... but it seems that somebody with 
more intimate knowledge of the Lua internals than mine is required
to successfully convert _everything_.

Is this analysis correct? Has anybody else worked on making Lua play
well with C++ and its exceptions?

Thanks you, Colin