From b94f6d148193f91cab50d16e2873095827b89b1b Mon Sep 17 00:00:00 2001 From: amelia squires Date: Wed, 15 Oct 2025 16:50:46 -0500 Subject: better errors and docs --- src/lua.h | 12 +++++++++++- src/lullaby.c | 3 +++ src/lullaby.h | 13 +++++++++++++ src/net.c | 3 ++- src/net/lua.c | 8 ++------ src/reg.c | 4 ++++ 6 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/lullaby.c create mode 100644 src/lullaby.h (limited to 'src') diff --git a/src/lua.h b/src/lua.h index 1d5bea1..8fc934c 100644 --- a/src/lua.h +++ b/src/lua.h @@ -7,6 +7,7 @@ #ifndef __lua_h #define __lua_h + enum deep_copy_flags { SKIP_META = (1 << 0), SKIP_GC = (1 << 1), @@ -81,12 +82,21 @@ void luaI_jointable(lua_State* L); lua_settable(L, Tidx);\ lua_pop(L, 1);} +//in lullaby.h +extern int _print_errors; + #define luaI_error(L, en, str){\ lua_pushnil(L);\ lua_pushstring(L, str);\ + if(_print_errors) printf("%s\n",str);\ lua_pushinteger(L, en);\ return 3;} - +#define luaI_assert(L, eq){_helperluaI_assert(L, eq, __FILE__, __LINE__);} +#define _helperluaI_assert(L, eq, file, line){\ + if(!(eq)){\ + char err[1024] = {0};\ + sprintf(err, "(%s:%i) %s assertion failed", file, line, #eq);\ + luaI_error(L, -1, err);}} int writer(lua_State*, const void*, size_t, void*); diff --git a/src/lullaby.c b/src/lullaby.c new file mode 100644 index 0000000..9033297 --- /dev/null +++ b/src/lullaby.c @@ -0,0 +1,3 @@ +#include "lullaby.h" + +int _print_errors = 0; diff --git a/src/lullaby.h b/src/lullaby.h new file mode 100644 index 0000000..9474a08 --- /dev/null +++ b/src/lullaby.h @@ -0,0 +1,13 @@ +#ifndef LULLABY_H +#define LULLABY_H +#pragma once +#include "config.h" + +extern int _print_errors; + +static struct config lullaby_config[] = { + {.name = "print_errors", .type = c_int, .value = {.c_int = &_print_errors}}, + {.type = c_none} +}; + +#endif diff --git a/src/net.c b/src/net.c index 71168c0..36b37b9 100644 --- a/src/net.c +++ b/src/net.c @@ -1057,7 +1057,8 @@ net_end: } parray_lclear(paths); - luaI_error(L, 0, "close eventfd signal"); + lua_pushnil(L); + return 1; } //TODO reformat all of this code and the structs (use more common/generic ones) diff --git a/src/net/lua.c b/src/net/lua.c index ab112fe..829c2f7 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -221,12 +221,8 @@ int l_sendfile(lua_State* L){ lua_gettable(L, -2); int header = lua_gettop(L); - if(access(path, F_OK)) { - p_fatal("file not found"); //TODO: use diff errors here - } - if(access(path, R_OK)){ - p_fatal("missing permissions"); - } + luaI_assert(L, !access(path, F_OK) /*file not found*/); + luaI_assert(L, !access(path, R_OK) /*missing permissions*/); lua_pushstring(L, "Content-Type"); lua_gettable(L, header); diff --git a/src/reg.c b/src/reg.c index 440a6f0..58acc40 100644 --- a/src/reg.c +++ b/src/reg.c @@ -7,6 +7,7 @@ #include "thread.h" #include "test.h" #include "config.h" +#include "lullaby.h" #define open_common(name, config)\ int luaopen_lullaby_##name (lua_State* L){\ @@ -49,6 +50,9 @@ int luaopen_lullaby(lua_State* L) { push(top, test); luaI_tsets(L, top, "version", GIT_COMMIT); + lua_pushvalue(L, top); + int idx = i_config_metatable(L, lullaby_config); + lua_settop(L, top); return 1; } -- cgit v1.2.3