Luabind, objects, and exceptions

Jason McKesson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
I've discovered something that may constitute a bug. Or, at least, a 
design flaw.

Pure Lua prefers to communicate useful error messages via the Lua stack. 
Luabind prefers to communicate with exceptions. Both of these are 
servicable methods of doing so, but the current implementation clashes.

It clashes because the Luabind exception doesn't actually communicate 
anything. Any actually useful error messages are still supposed to be 
passed through the Lua stack. The problem is that exceptions cause the 
stack to be unwound up until the appropriate catch clause.

This is a problem because, unless you catch every exception at the 
moment you make Luabind calls (which makes exceptions just a verbose 
return flag), there's a good chance that the C++ stack unwinding is 
going to start poking at the Lua stack. Like if you have a 
luabind::object on the stack. Possibly as a temporary that you used to 
make the actual call. Something as simple as this:

luabind::call_function<void>(someObject[anIndex], ...);

can fail. The [] operator creates a luabind::object temporary, and stack 
unwinding, no matter where you put the try/catch block, is going to 
collect this. That action is going to do highly unpleasant things to the 
Lua stack.

This wouldn't be a problem if luabind::error worked like most exceptions 
and actually carried error data. I generally do this by having the 
exception object store a large char[] array. It's not heap-allocated, so 
copying into/out of this buffer is perfectly fine. Sure, the exception 
object is huge, but that's better than having information destroyed.

------------------------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.