rework assert, wasm_alert, wasm_clear, expect

This commit is contained in:
krzosa
2024-12-30 19:48:40 +01:00
parent 578fa33605
commit 46084a3412
16 changed files with 150 additions and 95 deletions

View File

@@ -18,8 +18,9 @@
#endif
#if PLATFORM_WASM
double strtod(const char *str, char **end_unused);
void puts(const char *str);
fn double strtod(const char *str, char **end_unused);
fn void puts(const char *str);
fn void alert(s8_t string);
#endif
#if PLATFORM_CL

View File

@@ -83,6 +83,7 @@ fn void *ma_push_size_ex(ma_arena_t *arena, usize size) {
}
}
if (new_len > arena->commit) {
invalid_codepath;
return NULL;
}
}
@@ -98,12 +99,16 @@ fn void *ma_push_size(ma_arena_t *arena, usize size) {
return result;
}
fn ma_arena_t *ma_push_arena(ma_arena_t *allocator, usize size) {
ma_arena_t *result = ma_push_type(allocator, ma_arena_t);
fn void ma_push_arena_ex(ma_arena_t *allocator, ma_arena_t *result, usize size) {
result->data = (u8 *)ma_push_size(allocator, size);
result->reserve = size;
result->commit = size;
result->align = ma_default_alignment;
}
fn ma_arena_t *ma_push_arena(ma_arena_t *allocator, usize size) {
ma_arena_t *result = ma_push_type(allocator, ma_arena_t);
ma_push_arena_ex(allocator, result, size);
return result;
}
@@ -151,7 +156,7 @@ fn ma_temp_t ma_begin_scratch_ex(ma_arena_t **conflicts, int conflict_count) {
}
}
assertf(unoccupied, "Failed to get free scratch memory, this is a fatal error, this shouldnt happen");
assert(unoccupied); // failed to get free scratch memory, this is a fatal error, this shouldnt happen
ma_temp_t result = ma_begin_temp(unoccupied);
return result;
}

View File

@@ -26,9 +26,10 @@ fn void ma_destroy(ma_arena_t *arena);
//
// push
fn void *ma_push_size_ex(ma_arena_t *arena, usize size);
fn void *ma_push_size(ma_arena_t *arena, usize size);
fn void *ma_push_size_ex(ma_arena_t *arena, usize size);
fn ma_arena_t *ma_push_arena(ma_arena_t *allocator, usize size);
fn void ma_push_arena_ex(ma_arena_t *allocator, ma_arena_t *result, usize size);
#define ma_push_type(arena, Type) (Type *)ma_push_size((arena), sizeof(Type))
#define ma_push_array(arena, Type, count) (Type *)ma_push_size((arena), sizeof(Type) * (count))

View File

@@ -46,6 +46,7 @@ typedef double f64;
#ifndef offsetof
#define offsetof(st, m) ((usize)&(((st *)0)->m))
#endif
#define expect(x) if (!(x))
#define kib(x) (1024ULL * (x##ULL))
#define mib(x) (1024ULL * kib(x))

View File

@@ -87,16 +87,6 @@ fn f64 f64_from_s8(s8_t string) {
return result;
}
fn void core_print_message(s8_t string) {
ma_temp_t scratch = ma_begin_scratch();
s8_t copy = s8_printf(scratch.arena, "%S\n", string);
#if PLATFORM_WINDOWS
OutputDebugStringA(copy.str);
#endif
puts(copy.str);
ma_end_scratch(scratch);
}
fn date_t date_now(void) {
date_t result = {0};
#if PLATFORM_WASM
@@ -121,4 +111,4 @@ fn date_t date_now(void) {
result.year = lt->tm_year;
#endif
return result;
}
}

View File

@@ -35,6 +35,6 @@ THREAD_LOCAL core_desc_t core_desc = {
.log = {
.break_on_fatal = true,
.log_proc = default_log_proc,
.flags = log_flag_all,
.flags = 0,
}
};

View File

@@ -1,11 +1,11 @@
fn s8_t log_level_str(log_level_t level) {
fn char *log_level_str(log_level_t level) {
switch(level) {
case log_level_debug: return s8_lit("DEBUG");
case log_level_info: return s8_lit("INFO");
case log_level_warning: return s8_lit("WARN");
case log_level_error: return s8_lit("ERROR");
case log_level_fatal: return s8_lit("FATAL");
default: return s8_lit("INVALID");
case log_level_debug: return "DEBUG";
case log_level_info: return "INFO";
case log_level_warning: return "WARN";
case log_level_error: return "ERROR";
case log_level_fatal: return "FATAL";
default: return "INVALID";
}
}
@@ -20,6 +20,17 @@ fn void log_basef(i32 module, log_level_t level, s8_t file_and_line, const char
ma_end_scratch(scratch);
}
fn void core_error_message(char *str) {
#if PLATFORM_WINDOWS
MessageBoxA(NULL, str, "fatal error", MB_OK);
fprintf(stderr, "%s", str);
#elif PLATFORM_WASM
alert(s8_from_char(str));
#else
fprintf(stderr, "%s", str);
#endif
}
fn void default_log_proc(log_event_t ev) {
ma_temp_t scratch = ma_begin_scratch();
sb8_t *sb = sb8_serial_begin(scratch.arena);
@@ -27,7 +38,7 @@ fn void default_log_proc(log_event_t ev) {
sb8_printf(sb, "[%d] ", ev.module);
}
if (core_desc.log.flags & log_flag_level) {
sb8_printf(sb, "%-5S ", log_level_str(ev.level));
sb8_printf(sb, "%-5s ", log_level_str(ev.level));
}
date_t date = {0};
{
@@ -47,23 +58,25 @@ fn void default_log_proc(log_event_t ev) {
sb8_printf(sb, "%-40S ", ev.file_and_line);
}
sb8_printf(sb, " %S", ev.string);
sb8_printf(sb, " %S\n", ev.string);
s8_t result = sb8_serial_end(sb);
core_print_message(result);
ma_end_scratch(scratch);
if (ev.level == log_level_fatal) {
if (core_desc.log.break_on_fatal) {
#if PLATFORM_WINDOWS
if (IsDebuggerPresent())
#endif
if (ev.level != log_level_fatal) {
IF_PLATFORM_WINDOWS(OutputDebugStringA(result.str);)
puts(result.str);
} else {
core_error_message(result.str);
#if PLATFORM_WASM
debug_break();
} else {
SWITCH_PLATFORM_WINDOWS_WASM_ELSE(
ExitProcess(1),
debug_break(),
exit(1)
);
}
#else
if (core_desc.log.break_on_fatal) {
debug_break();
} else {
exit(1);
}
#endif
}
ma_end_scratch(scratch);
}

View File

@@ -52,23 +52,27 @@ struct logger_t {
b32 break_on_fatal;
};
fn void log_base(i32 module, log_level_t level, s8_t file_and_line, s8_t string);
fn void log_basef(i32 module, log_level_t level, s8_t file_and_line, const char *str, ...);
fn s8_t log_level_str(log_level_t level);
///////////////////////////////
// main api
#define debugf(...) log_basef(module_null, log_level_debug, S8_FILE_AND_LINE, __VA_ARGS__)
#define errorf(MODULE, ...) log_basef(MODULE, log_level_error, S8_FILE_AND_LINE, __VA_ARGS__)
#define fatalf(...) (log_basef(module_null, log_level_fatal, S8_FILE_AND_LINE, __VA_ARGS__), 0)
#define assert(x) (!(x) && fatalf("condition doesn't hold: " #x))
#define assertf(x, ...) (!(x) && fatalf("condition doesn't hold: " __VA_ARGS__))
#if PLATFORM_CL
#define debug__break() __debugbreak()
fn void core_error_message(char *str);
#define program_version "---"
#if PLATFORM_DEBUG_ASSERT
#define assert(x) (!(x) && (core_error_message(FILE_AND_LINE ": internal program error! assertion failed, program version: " program_version), debug_break()))
#else
#define debug__break() __builtin_trap()
#define assert(x) (void)(x)
#endif
#define debug_break() (debug__break(), 0)
#define not_implemented assert(!"not implemented!")
#define invalid_codepath assert(!"invalid code path!")
fn void log_base(i32 module, log_level_t level, s8_t file_and_line, s8_t string);
fn void log_basef(i32 module, log_level_t level, s8_t file_and_line, const char *str, ...);
/*

View File

@@ -64,6 +64,10 @@
#define PLATFORM_ADDRESS_SANITIZER 0
#endif
#ifndef PLATFORM_DEBUG_ASSERT
#define PLATFORM_DEBUG_ASSERT 1
#endif
#if PLATFORM_WINDOWS
#define SWITCH_PLATFORM_WINDOWS_WASM_ELSE(WINDOWS, WASM, ELSE) WINDOWS
#elif PLATFORM_WASM
@@ -72,7 +76,19 @@
#define SWITCH_PLATFORM_WINDOWS_WASM_ELSE(WINDOWS, WASM, ELSE) ELSE
#endif
#if PLATFORM_CL
#pragma warning(disable: 4116)
#endif
#if PLATFORM_CL
#define debug__break() __debugbreak()
#else
#define debug__break() __builtin_trap()
#endif
#define debug_break() (debug__break(), 0)
#if PLATFORM_WINDOWS
#define IF_PLATFORM_WINDOWS(x) x
#else
#define IF_PLATFORM_WINDOWS(x)
#endif

View File

@@ -69,8 +69,8 @@ fn s8_t s8_from_char(char *string) {
fn s16_t s16_from_s8(ma_arena_t *ma, s8_t string) {
u16 *buffer = ma_push_array(ma, u16, string.len + 1);
i64 len = wstr_from_str(buffer, string.len + 1, string.str, string.len);
assert(len < string.len);
return (s16_t){buffer,len};
assert(len <= string.len); // @todo: verify
return (s16_t){buffer, len};
}
fn s8_t s8_from_s16(ma_arena_t *ma, s16_t string) {
@@ -78,18 +78,15 @@ fn s8_t s8_from_s16(ma_arena_t *ma, s16_t string) {
char *buffer = ma_push_array(ma, char, buffer_size);
i64 len = str_from_wstr(buffer, buffer_size, string.str, string.len);
assert(len < buffer_size);
return (s8_t){buffer,len};
return (s8_t){buffer, len};
}
fn s8_t s8_copy(ma_arena_t *ma, s8_t string) {
char *copy = (char *)ma_push_size(ma, sizeof(char) * (string.len + 1));
if (copy) {
memory_copy(copy, string.str, string.len);
copy[string.len] = 0;
s8_t result = s8(copy, string.len);
return result;
}
return (s8_t){0};
memory_copy(copy, string.str, string.len);
copy[string.len] = 0;
s8_t result = s8(copy, string.len);
return result;
}
s8_t s8_copy_char(ma_arena_t *ma, char *s) {
@@ -207,8 +204,8 @@ s8_t s8_skip_past(s8_t string, s8_t a) {
fn s8_t s8_slice(s8_t string, int64_t first_index, int64_t one_past_last_index) {
if (one_past_last_index < 0) one_past_last_index = string.len + one_past_last_index + 1;
if (first_index < 0) first_index = string.len + first_index;
assert(first_index < one_past_last_index && "s8_slice, first_index is bigger then one_past_last_index");
assert(string.len > 0 && "Slicing string of length 0! Might be an error!");
assert(first_index < one_past_last_index);
assert(string.len > 0);
s8_t result = string;
if (string.len > 0) {
if (one_past_last_index > first_index) {
@@ -476,10 +473,7 @@ fn u64 u64_from_s8(s8_t s, u64 base) {
u64 acc = 0;
for (i64 i = 0; i < s.len; i++) {
u64 num = u64_from_hexchar(s.str[i]);
if (num >= base) {
fatalf("failed to convert string into number, didn't expect: %c", s.str[i]);
break;
}
assert(num < base);
acc *= base;
acc += num;
}

View File

@@ -368,7 +368,8 @@ fn void ti__deserial_data_ex(ma_arena_t *arena, parser_t *par, void *p, type_t *
lex_t *ident = parser_expect(par, lex_kind_ident);
parser_expect(par, lex_kind_colon);
assert(s8_equal(ident->string, mem->name));
expect (s8_equal(ident->string, mem->name)) fatalf("expected identifier: %S, got instead %S", mem->name, ident->string);
ti__deserial_data_ex(arena, par, mem_p, mem->type);
parser_expect(par, lex_kind_comma);