Initial commit
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/* - | 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_EXCEPTION_H
|
||||
#define CI2_EXCEPTION_H
|
||||
|
||||
#include "../ci2_platform.h"
|
||||
#include <setjmp.h>
|
||||
|
||||
struct Exception
|
||||
{
|
||||
const char* msg;
|
||||
};
|
||||
typedef struct Exception Exception;
|
||||
|
||||
struct Exception_Frame
|
||||
{
|
||||
struct Exception_Frame* prev;
|
||||
jmp_buf env;
|
||||
const char* file;
|
||||
const char* func;
|
||||
int line;
|
||||
const struct Exception* exception;
|
||||
};
|
||||
typedef struct Exception_Frame Exception_Frame;
|
||||
|
||||
enum
|
||||
{
|
||||
EXCEPTION_ENTERED = 0,
|
||||
EXCEPTION_RAISED,
|
||||
EXCEPTION_HANDLED,
|
||||
EXCEPTION_FINALIZED
|
||||
};
|
||||
|
||||
CI2_API const struct Exception assertion_failed;
|
||||
CI2_API void
|
||||
ci2_try_assert(int cond);
|
||||
|
||||
CI2_API void
|
||||
ci2_raise_exception(const struct Exception* e,
|
||||
const char* file,
|
||||
const char* func,
|
||||
int line);
|
||||
|
||||
#if defined(CI2_WINDOWS)
|
||||
CI2_DEF DWORD exception_stack;
|
||||
CI2_DEF void
|
||||
ci2_exception_init(void);
|
||||
CI2_DEF void
|
||||
ci2_exception_push(struct Exception_Frame* fp);
|
||||
CI2_DEF void
|
||||
ci2_exception_pop(void);
|
||||
|
||||
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, CI2_FUNC, __LINE__)
|
||||
|
||||
#define CI2_RERAISE \
|
||||
ci2_raise_exception(exception_frame.exception, \
|
||||
exception_frame.file, \
|
||||
exception_frame.func, \
|
||||
exception_frame.line)
|
||||
|
||||
#define CI2_RETURN \
|
||||
switch (ci2_exception_pop(), 0) \
|
||||
default: \
|
||||
return
|
||||
|
||||
#define CI2_TRY \
|
||||
do { \
|
||||
volatile int exception_flag; \
|
||||
Exception_Frame exception_frame; \
|
||||
if (exception_stack == TLS_OUT_OF_INDEXES) \
|
||||
ci2_exception_init(); \
|
||||
ci2_exception_push(&exception_frame); \
|
||||
exception_flag = setjmp(exception_frame.env); \
|
||||
if (exception_flag == EXCEPTION_ENTERED) {
|
||||
|
||||
#define CI2_EXCEPT(e) \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
ci2_exception_pop(); \
|
||||
} \
|
||||
else if (exception_frame.exception == &(e)) \
|
||||
{ \
|
||||
exception_flag = EXCEPTION_HANDLED;
|
||||
|
||||
#define CI2_ELSE \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
ci2_exception_pop(); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
exception_flag = EXCEPTION_HANDLED;
|
||||
|
||||
#define CI2_FINALLY \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
ci2_exception_pop(); \
|
||||
} \
|
||||
{ \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_flag = EXCEPTION_FINALIZED;
|
||||
|
||||
#define CI2_END_TRY \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
ci2_exception_pop(); \
|
||||
} \
|
||||
if (exception_flag == EXCEPTION_RAISED) \
|
||||
CI2_RERAISE; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#else // UNIX
|
||||
CI2_DEF struct Exception_Frame* exception_stack;
|
||||
|
||||
#define CI2_RAISE(e) ci2_raise_exception(&(e), __FILE__, CI2_FUNC, __LINE__)
|
||||
|
||||
#define CI2_RERAISE \
|
||||
ci2_raise_exception(exception_frame.exception, \
|
||||
exception_frame.file, \
|
||||
exception_frame.func, \
|
||||
exception_frame.line)
|
||||
|
||||
#define CI2_RETURN \
|
||||
switch (exception_stack = exception_stack->prev, 0) \
|
||||
default: \
|
||||
return
|
||||
|
||||
#define CI2_TRY \
|
||||
do { \
|
||||
volatile int exception_flag; \
|
||||
Exception_Frame exception_frame; \
|
||||
exception_frame.prev = exception_stack; \
|
||||
exception_stack = &exception_frame; \
|
||||
exception_flag = setjmp(exception_frame.env); \
|
||||
if (exception_flag == EXCEPTION_ENTERED) {
|
||||
|
||||
#define CI2_EXCEPT(e) \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_stack = exception_stack->prev; \
|
||||
} \
|
||||
else if (exception_frame.exception == &(e)) \
|
||||
{ \
|
||||
exception_flag = EXCEPTION_HANDLED;
|
||||
|
||||
#define CI2_ELSE \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_stack = exception_stack->prev; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
exception_flag = EXCEPTION_HANDLED;
|
||||
|
||||
#define CI2_FINALLY \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_stack = exception_stack->prev; \
|
||||
} \
|
||||
{ \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_flag = EXCEPTION_FINALIZED;
|
||||
|
||||
#define CI2_END_TRY \
|
||||
if (exception_flag == EXCEPTION_ENTERED) \
|
||||
exception_stack = exception_stack->prev; \
|
||||
} \
|
||||
if (exception_flag == EXCEPTION_RAISED) \
|
||||
CI2_RERAISE; \
|
||||
} \
|
||||
while (0)
|
||||
#endif
|
||||
|
||||
#endif // ci2_exception.h
|
||||
Reference in New Issue
Block a user