Fixed a few bugs, and improved the exception macros (based on

suggestions by Sergei Turchanov).
This commit is contained in:
Alexandre Julliard 1999-05-14 18:21:55 +00:00
parent 9fe7a2545a
commit 0691998e45
2 changed files with 98 additions and 42 deletions

View File

@ -15,7 +15,7 @@
* *
* They should be used like this: * They should be used like this:
* *
* __TRY() * __TRY
* { * {
* do some stuff that can raise an exception * do some stuff that can raise an exception
* } * }
@ -23,11 +23,11 @@
* { * {
* handle the exception here * handle the exception here
* } * }
* __ENDTRY() * __ENDTRY
* *
* or * or
* *
* __TRY() * __TRY
* { * {
* do some stuff that can raise an exception * do some stuff that can raise an exception
* } * }
@ -40,52 +40,86 @@
* *
* The finally_func must be defined with the WINE_FINALLY_FUNC macro. * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
* *
* Warning: you cannot use return, break, or continue inside a __TRY * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
* or __EXCEPT block. * break out of the current block. 'return' causes disaster and must
* never be used.
* *
* -- AJ * -- AJ
*/ */
#define __TRY() \ /* Define this if you want to use your compiler built-in __try/__except support.
do { __WINE_FRAME __f; int __state = -1; \ * This is only useful when compiling to a native Windows binary, as the built-in
while (__state != 2) switch(__state) \ * compiler exceptions will most certainly not work under Winelib.
{ case 0: /* try */ */
#ifdef USE_COMPILER_EXCEPTIONS
#define __TRY __try
#define __EXCEPT(func,arg) __except((func)(GetExceptionInformation(),GetExceptionCode(),(arg)))
#define __FINALLY(func,arg) __finally { (func)(!AbnormalTermination(),(arg)); }
#define __ENDTRY /*nothing*/
#else /* USE_COMPILER_EXCEPTIONS */
#define __TRY \
do { __WINE_FRAME __f; \
int __first = 1; \
for (;;) if (!__first) \
{ \
do {
#define __EXCEPT(func,arg) \ #define __EXCEPT(func,arg) \
__state = 2; break; \ } while(0); \
case -1: /* init */ \ EXC_pop_frame( &__f.frame ); \
__f.frame.Handler = (PEXCEPTION_HANDLER)WINE_exception_handler; \ break; \
__f.u.e.filter = (func); \ } else { \
__f.u.e.param = (LPVOID)(arg); \ __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_exception_handler; \
EXC_push_frame( &__f.frame ); \ __f.u.e.filter = (func); \
__state = setjmp( __f.u.e.jmp); \ __f.u.e.param = (LPVOID)(arg); \
break; \ EXC_push_frame( &__f.frame ); \
case 1: /* except */ if (setjmp( __f.u.e.jmp)) { \
EXC_pop_frame( &__f.frame ); \
do {
#define __ENDTRY() \ #define __ENDTRY \
__state++; break; \ } while (0); \
} \ break; \
EXC_pop_frame( &__f.frame ); \ } \
__first = 0; \
} \
} while (0); } while (0);
#define __FINALLY(func,arg) \ #define __FINALLY(func,arg) \
__state = 2; break; \ } while(0); \
case -1: /* init */ \ EXC_pop_frame( &__f.frame ); \
__f.frame.Handler = (PEXCEPTION_HANDLER)WINE_finally_handler; \ (func)( TRUE, (LPVOID)(arg) ); \
__f.u.f.finally_func = (func); \ break; \
__f.u.f.param = (LPVOID)(arg); \ } else { \
EXC_push_frame( &__f.frame ); \ __f.frame.Handler = (PEXCEPTION_HANDLER)WINE_finally_handler; \
__ENDTRY() __f.u.f.finally_func = (func); \
__f.u.f.param = (LPVOID)(arg); \
EXC_push_frame( &__f.frame ); \
__first = 0; \
} \
} while (0);
#define WINE_EXCEPTION_FILTER(func,arg) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr, LPVOID arg )
#define WINE_FINALLY_FUNC(func,arg) void WINAPI func( LPVOID arg ) /* Dummy structure for using GetExceptionCode in filter as well as in handler */
typedef struct
{
union { struct { DWORD code; } e; } u;
} __WINE_DUMMY_FRAME;
typedef DWORD (*CALLBACK __WINE_FILTER)(PEXCEPTION_POINTERS,__WINE_DUMMY_FRAME,LPVOID);
typedef void (*CALLBACK __WINE_FINALLY)(BOOL,LPVOID);
#define WINE_EXCEPTION_FILTER(func,arg) \
DWORD WINAPI func( EXCEPTION_POINTERS *__eptr, __WINE_DUMMY_FRAME __f, LPVOID arg )
#define WINE_FINALLY_FUNC(func,arg) \
void WINAPI func( BOOL __normal, LPVOID arg )
#define GetExceptionInformation() (__eptr) #define GetExceptionInformation() (__eptr)
#define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode) #define GetExceptionCode() (__f.u.e.code)
#define AbnormalTermination() (!__normal)
typedef DWORD (*CALLBACK __WINE_FILTER)(PEXCEPTION_POINTERS,LPVOID);
typedef void (*CALLBACK __WINE_FINALLY)(LPVOID);
typedef struct typedef struct
{ {
@ -96,6 +130,7 @@ typedef struct
{ {
__WINE_FILTER filter; __WINE_FILTER filter;
LPVOID param; LPVOID param;
DWORD code;
jmp_buf jmp; jmp_buf jmp;
} e; } e;
struct /* finally data */ struct /* finally data */
@ -111,18 +146,36 @@ extern DWORD WINAPI WINE_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_
extern DWORD WINAPI WINE_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame, extern DWORD WINAPI WINE_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
CONTEXT *context, LPVOID pdispatcher ); CONTEXT *context, LPVOID pdispatcher );
#endif /* USE_COMPILER_EXCEPTIONS */
static inline EXCEPTION_FRAME *EXC_push_frame( EXCEPTION_FRAME *frame ) static inline EXCEPTION_FRAME *EXC_push_frame( EXCEPTION_FRAME *frame )
{ {
#if defined(__GNUC__) && defined(__i386__)
EXCEPTION_FRAME *prev;
__asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
"\n\tmovl %0,(%1)"
"\n\t.byte 0x64\n\tmovl %1,(0)"
: "=&r" (prev) : "r" (frame) : "memory" );
return prev;
#else
TEB * teb = NtCurrentTeb(); TEB * teb = NtCurrentTeb();
frame->Prev = teb->except; frame->Prev = teb->except;
teb->except = frame; teb->except = frame;
return frame; return frame->Prev;
#endif
} }
static inline EXCEPTION_FRAME *EXC_pop_frame( EXCEPTION_FRAME *frame ) static inline EXCEPTION_FRAME *EXC_pop_frame( EXCEPTION_FRAME *frame )
{ {
#if defined(__GNUC__) && defined(__i386__)
__asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
: : "r" (frame->Prev) : "memory" );
return frame->Prev;
#else
NtCurrentTeb()->except = frame->Prev; NtCurrentTeb()->except = frame->Prev;
return frame->Prev; return frame->Prev;
#endif
} }
#endif /* __WINE_WINE_EXCEPTION_H */ #endif /* __WINE_WINE_EXCEPTION_H */

View File

@ -110,14 +110,17 @@ DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *
{ {
__WINE_FRAME *wine_frame = (__WINE_FRAME *)frame; __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)) if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
return ExceptionContinueSearch; return ExceptionContinueSearch;
if (wine_frame->u.e.filter) if (wine_frame->u.e.filter)
{ {
EXCEPTION_POINTERS ptrs; EXCEPTION_POINTERS ptrs;
__WINE_DUMMY_FRAME dummy;
ptrs.ExceptionRecord = record; ptrs.ExceptionRecord = record;
ptrs.ContextRecord = context; ptrs.ContextRecord = context;
switch(wine_frame->u.e.filter( &ptrs, wine_frame->u.e.param )) dummy.u.e.code = record->ExceptionCode;
switch(wine_frame->u.e.filter( &ptrs, dummy, wine_frame->u.e.param ))
{ {
case EXCEPTION_CONTINUE_SEARCH: case EXCEPTION_CONTINUE_SEARCH:
return ExceptionContinueSearch; return ExceptionContinueSearch;
@ -126,8 +129,8 @@ DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *
case EXCEPTION_EXECUTE_HANDLER: case EXCEPTION_EXECUTE_HANDLER:
break; break;
default: default:
/* FIXME: should probably raise a nested exception here */ MESSAGE( "Invalid return value from exception filter\n" );
return ExceptionContinueSearch; assert( FALSE );
} }
} }
RtlUnwind( frame, 0, record, 0 ); RtlUnwind( frame, 0, record, 0 );
@ -147,6 +150,6 @@ DWORD WINAPI WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *fr
if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))) if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
return ExceptionContinueSearch; return ExceptionContinueSearch;
wine_frame->u.f.finally_func( wine_frame->u.f.param ); wine_frame->u.f.finally_func( FALSE, wine_frame->u.f.param );
return ExceptionContinueSearch; return ExceptionContinueSearch;
} }