From 1882f8b72ac222a5f9df727fc32ffed49cd12dba Mon Sep 17 00:00:00 2001 From: skejeton Date: Sun, 25 Aug 2024 21:03:41 -0300 Subject: [PATCH] Handle errors in destroy callback --- src/main.c | 15 ++++++++++++--- src/misc.c | 38 +++++++++++++++++++++++++++++++++++++- src/window.c | 36 ------------------------------------ 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/main.c b/src/main.c index 9ba2b25..f890970 100644 --- a/src/main.c +++ b/src/main.c @@ -193,8 +193,13 @@ th_deinit() } } - if (umkaAlive(thg->umka)) - umkaCall(thg->umka, &thg->umka_destroy); + if (umkaAlive(thg->umka)) { + int code = umkaCall(thg->umka, &thg->umka_destroy); + if (!umkaAlive(thg->umka) || code != 0) { + th_print_umka_error_and_quit(code); + } + } + if (umkaAlive(thg->umka)) umkaRun(thg->umka); @@ -217,8 +222,12 @@ int run_playground(const char *src) { if (thg->umka) { - if (umkaAlive(thg->umka)) + if (umkaAlive(thg->umka)) { umkaCall(thg->umka, &thg->umka_destroy); + if (!umkaAlive(thg->umka) || code != 0) { + th_print_umka_error_and_quit(code); + } + } if (umkaAlive(thg->umka)) umkaRun(thg->umka); umkaFree(thg->umka); diff --git a/src/misc.c b/src/misc.c index 4ccef87..eac6de0 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,9 +1,10 @@ +#include "tophat.h" #include #include #include #include -#include "tophat.h" +#define MSG_LEN 1024 extern th_global *thg; @@ -164,3 +165,38 @@ th_regularize_path(const char *path, const char *cur_folder, char *regularized_p th_error("Path too long: %s%s", cur_folder, path); exit(-1); } + +void +th_print_umka_error_and_quit(int code) +{ + UmkaError *error = umkaGetError(thg->umka); + + if (error->code != 0 && error->msg[0]) { + th_error("%s (%d): %s\n", error->fileName, error->line, error->msg); + + fprintf(stderr, "\tStack trace:\n"); + + for (int depth = 0; depth < 10; depth++) { + char fnName[MSG_LEN + 1]; + char file[MSG_LEN + 1]; + int line, offset; + + if (!umkaGetCallStack( + thg->umka, depth, MSG_LEN + 1, &offset, file, fnName, &line)) { + fprintf(stderr, "\t\t...\n"); + break; + } +#ifndef _WIN32 + fprintf(stderr, "\033[34m"); +#endif + fprintf(stderr, "\t\t%s:%06d: ", file, line); +#ifndef _WIN32 + fprintf(stderr, "\033[0m"); +#endif + fprintf(stderr, "%s\n", fnName); + } + } + + th_deinit(); + exit(code); +} diff --git a/src/window.c b/src/window.c index c2199e2..9e8886e 100644 --- a/src/window.c +++ b/src/window.c @@ -10,45 +10,9 @@ #include #include -#define MSG_LEN 1024 extern th_global *thg; -void -th_print_umka_error_and_quit(int code) -{ - UmkaError *error = umkaGetError(thg->umka); - - if (error->code != 0 && error->msg[0]) { - th_error("%s (%d): %s\n", error->fileName, error->line, error->msg); - - fprintf(stderr, "\tStack trace:\n"); - - for (int depth = 0; depth < 10; depth++) { - char fnName[MSG_LEN + 1]; - char file[MSG_LEN + 1]; - int line, offset; - - if (!umkaGetCallStack( - thg->umka, depth, MSG_LEN + 1, &offset, file, fnName, &line)) { - fprintf(stderr, "\t\t...\n"); - break; - } -#ifndef _WIN32 - fprintf(stderr, "\033[34m"); -#endif - fprintf(stderr, "\t\t%s:%06d: ", file, line); -#ifndef _WIN32 - fprintf(stderr, "\033[0m"); -#endif - fprintf(stderr, "%s\n", fnName); - } - } - - th_deinit(); - exit(code); -} - static void init(void) {