Files
ci2/include/ci2_exception.h

129 lines
5.1 KiB
C
Raw Normal View History

2026-05-11 20:29:59 -05:00
/* - | Copyright | --------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#ifndef CI2_EXCEPTIONS_H
#define CI2_EXCEPTIONS_H
#include "./base/ci2_base.h" // CURRENT_FUNC macro
#include <setjmp.h>
#include <errno.h>
extern int errno;
struct CI2_Exception {
int num;
const char *msg;
};
typedef struct CI2_Exception CI2_Exception;
struct CI2_Exception_Frame {
struct CI2_Exception_Frame *prev;
jmp_buf env;
const char *func;
const char *file;
int line;
const struct CI2_Exception *exception;
};
typedef struct CI2_Exception_Frame CI2_Exception_Frame;
enum { CI2_EXCEPT_STATE_ENTERED= 0,
CI2_EXCEPT_STATE_RAISED,
CI2_EXCEPT_STATE_HANDLED,
CI2_EXCEPT_STATE_FINALIZED,
CI2_EXCEPT_STATE_COUNT
};
extern const struct CI2_Exception runtime_assertion_failed;
extern struct CI2_Exception_Frame *ci2_exception_stack;
extern void ci2_raise_exception(const struct CI2_Exception *e, const char *file, int line, const char *func);
extern void ci2_rt_assert(int e);
#ifdef CI2_WINDOWS
extern DWORD ci2_exception_index;
extern void ci2_exception_init(void);
extern void ci2_exception_push(struct CI2_Exception_Frame *fp);
extern void ci2_exception_pop(void);
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, __LINE__, CURRENT_FUNC)
#define CI2_RERAISE ci2_raise_exception(exception_frame.exception, \
exception_frame.file, exception_frame.line, CURRENT_FUNC)
#define CI2_RETURN switch (ci2_exception_pop(),0) default: return
#define CI2_TRY do { \
volatile int ci2_exception_flag; \
CI2_Exception_Frame exception_frame; \
if (exception_index == TLS_OUT_OF_INDEXES) \
ci2_exception_init(); \
ci2_exception_push(&exception_frame); \
ci2_exception_flag = setjmp(exception_frame.env); \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) {
#define CI2_EXCEPT(e) \
if (ci2_except_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_pop(); \
} else if (exception_frame.exception == &(e)) { \
ci2_exception_flag = CI2_EXCEPT_STATE_HANDLED;
#define CI2_ELSE \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_pop(); \
} else { \
ci2_exception_flag = CI2_EXCEPT_STATE_HANDLED;
#define CI2_FINALLY \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_pop(); \
} { \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) \
ci2_exception_flag = CI2_EXCEPT_STATE_FINALIZED;
#define CI2_END_TRY \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_pop(); \
} if (ci2_exception_flag == CI2_EXCEPT_STATE_RAISED) CI2_RERAISE; \
} while (0)
#else // UNIX
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, __LINE__, CURRENT_FUNC)
#define CI2_RERAISE ci2_raise_exception(exception_frame.exception, \
exception_frame.file, exception_frame.line, CURRENT_FUNC)
#define CI2_RETURN switch (ci2_exception_stack = ci2_exception_stack->prev,0) default: return
#define CI2_TRY do { \
volatile int ci2_exception_flag; \
CI2_Exception_Frame exception_frame; \
exception_frame.prev = ci2_exception_stack; \
ci2_exception_stack = &exception_frame; \
ci2_exception_flag = setjmp(exception_frame.env); \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) {
#define CI2_EXCEPT(e) \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_stack = ci2_exception_stack->prev; \
} else if (exception_frame.exception == &(e)) { \
ci2_exception_flag = CI2_EXCEPT_STATE_HANDLED;
#define CI2_ELSE \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_stack = ci2_exception_stack->prev; \
} else { \
ci2_exception_flag = CI2_EXCEPT_STATE_HANDLED;
#define CI2_FINALLY \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_stack = ci2_exception_stack->prev; \
} { \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) \
ci2_exception_flag = CI2_EXCEPT_STATE_FINALIZED;
#define CI2_END_TRY \
if (ci2_exception_flag == CI2_EXCEPT_STATE_ENTERED) ci2_exception_stack = ci2_exception_stack->prev; \
} if (ci2_exception_flag == CI2_EXCEPT_STATE_RAISED) CI2_RERAISE; \
} while (0)
#endif
#endif // ci2_exceptions.h