[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Safe an quick way to determine context in a C-function
- From: TNHarris <telliamed@...>
- Date: Tue, 5 May 2009 19:05:15 -0400
On Tue, 5 May 2009 14:40:26 +0200
"Martijn van Buul (Ellips B.V.)" <martijn.van.buul@ellips.nl> wrote:
>
> The problem arises if for some reason this function could fail. If
> it's being called by Lua (from a script), the proper answer would be
> to raise an error using luaL_error. However, if PushNewPoint() is
> being called directly from the application, luaL_error will call
> exit(), causing an immediate and rather rude termination of the entire
> application. In this case, I'd much rather have PushNewPoint() to
> return a NULL pointer, so that the calling function can gracefully
> exit.
>
Of course the best thing is to use pcall, as Luiz said. With C++, I
installed a panic function that throws an exception instead of calling
exit().
static int panic_exception(lua_State* L) {
throw lua_exception(lua_tostring(L, -1));
}
lua_State* my_newstate(void) {
lua_State* L = luaL_newstate();
if (L) lua_atpanic(L, &panic_exception);
return L;
}
Or something like that.
-- tom
telliamed@whoopdedo.org