Exceptions
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/* - | 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.
|
||||
|
||||
* --------------------------------------------------------------------------*/
|
||||
#include "../include/platform/error/ci2_exception.h"
|
||||
|
||||
struct Exception_Frame* exception_stack = NULL;
|
||||
|
||||
const struct Exception assertion_failed = { "CI2 Assertion Failed" };
|
||||
|
||||
void
|
||||
ci2_rt_assert(int cond)
|
||||
{
|
||||
if (!cond) {
|
||||
CI2_RAISE(assertion_failed);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ci2_raise_exception(const struct Exception* e,
|
||||
const char* file,
|
||||
const char* func,
|
||||
int line)
|
||||
{
|
||||
|
||||
struct Exception_Frame* p = exception_stack;
|
||||
ci2_rt_assert(e != NULL);
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "Uncaught exception");
|
||||
if (e->msg)
|
||||
fprintf(stderr, " %s", e->msg);
|
||||
else
|
||||
fprintf(stderr, " at 0x%p", (void*)e);
|
||||
if (file && line > 0)
|
||||
fprintf(stderr, " raised at %s:%d", file, line);
|
||||
if (func != NULL)
|
||||
fprintf(stderr, ":%s", func);
|
||||
fprintf(stderr, "\ninitiating debug trap...\n");
|
||||
fflush(stderr);
|
||||
CI2_DEBUG_TRAP();
|
||||
}
|
||||
p->exception = e;
|
||||
p->file = file;
|
||||
p->func = func;
|
||||
p->line = line;
|
||||
|
||||
exception_stack = exception_stack->prev;
|
||||
longjmp(p->env, EXCEPTION_RAISED);
|
||||
}
|
||||
Reference in New Issue
Block a user