lua-users home
lua-l archive

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


We're using tolua++ with an exception patch for the OCRopus project.  Tolua++ generates wrapper code that catches the exceptions and turns them into Lua errors.  This seems to work reliably.

Cheers,
Thomas.

On 10/19/07, Colin <share-lua@think42.com> wrote:
Hallo,

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