[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Exceptions
- From: Mark Hamburg <mhamburg@...>
- Date: Thu, 01 Jan 2004 16:21:13 -0800
I modified the 5.0 distribution to allow luaD_throw and luaD_rawrunprotected
to be overriden. In my case it was to work with MacOS X's exception system
in Cocoa, but the principle is the same. You basically need to throw
exceptions of whatever type you are using in the luaD_throw equivalent --
most of the work is actually in translating between exception types -- and
implement an appropriate catching context in luaD_rawrunprotected.
I remember being favorably impressed at the time at how easy this was to do.
Summary:
void my_throw( lua_State *L, int errcode ) {
lua_unlock( L );
// Unlock because we're about to go into non Lua VM C code
// translate the error code and possibly the top of the stack into
// an exception
// raise/throw the exception
}
int my_rawrunprotected(
lua_State *L,
void (*f)( lua_State *L, void *ud ),
void *ud ) {
volatile int status = 0;
NS_DURING // e.g., try
(*f)( L, ud );
NS_HANDLER // e.g., catch
status = exceptionToLuaError( L, localException );
// Translate back to Lua. Note that if the exception
// did not originate with Lua, then we need to push
// something onto the stack to match what Lua would
// have done.
lua_lock( L );
// Restore the lock
NS_ENDHANDLER
return status;
}
The unlock and the lock are important for matching Lua's lock state when
executing non-Lua code.
Implementing this allows exception handlers in C code to execute (or C++
destructors if you do the C++ equivalent) and allows for exceptions to be
thrown from C code and caught by Lua provided that you can do the
appropriate translation back.
Mark
on 1/1/04 1:50 PM, D Burgess at db@werx4.com wrote:
> I note that lhf in
> http://lua-users.org/lists/lua-l/2003-06/msg00164.html
>
> lhf says: The next version will contain macros that make this even
> easier.
>
> Any chance of a preview of this code?
> DB
> +++++++++++++++++++++++++++++++++
>> Here's one example:
>>
>> http://www.fensende.com/~mcuddy/ltn/luapp.html
>>
>> Although it's lua 4.x, it shouldn't be much trouble to modify it for lua
>> 5.x.
>>
>> -Kevin
>>
>>> -----Original Message-----
>>> From: lua-bounces@bazar2.conectiva.com.br
>>> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of D Burgess
>>> Sent: Friday, January 02, 2004 12:00 AM
>>> To: Lua list
>>> Subject: Exceptions
>>>
>>>
>>> Would anyone like to share the code for replacing
>>> setjmp/longjmp with C++ exceptions or Win32 structured
>>> exception handling?
>>>
>>> David B
>>>
>>>
>>>
>
>
>
>
>