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 --- docs/common.md | 22 ++++++++++++++++++++++ docs/net/listen.md | 4 ++++ library/lullaby/net.lua | 1 + 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 ++++ tests/kill.lua | 4 +++- tests/units/net-nested.lua | 1 + 11 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 docs/common.md create mode 100644 src/lullaby.c create mode 100644 src/lullaby.h diff --git a/docs/common.md b/docs/common.md new file mode 100644 index 0000000..c0d5be7 --- /dev/null +++ b/docs/common.md @@ -0,0 +1,22 @@ +## errors + +errors will typically be created and propogated using luaI_error (in c) but will always retain a common style (unless mentioned otherwise) + +it will return 3 values, in order + +* nil (just always a nil value first, useful to do a quick check for errors on functions with a return value) +* string (an error message) +* integer (an error code) + +similarily, when luaI_assert is called, the string will be the expression and the integer will be -1 + +## stream + +this is a generic function used in many places. + +stream:read(?bytes) +stream:file(filename, ?bytes, ?mode) + +bytes is an optional value allowing you to select how many bytes at maximum to read. this value can be ignored or adjusted by the function, and if so, it will be noted in the docs (default is nil, and will read as much as possible) + +mode is the mode the file will be opened with (defaults to "w") diff --git a/docs/net/listen.md b/docs/net/listen.md index f64c6f2..cc464b3 100644 --- a/docs/net/listen.md +++ b/docs/net/listen.md @@ -146,6 +146,8 @@ res.header["test"] = "wowa" res:sendfile(filepath, options?) +this can return an error if the file is not found or if the user does not have read permissions + res.header["Content-Type"] is set automatically (unless already set) depending on the file extention, using /etc/mime.types, or whatever option was supplied to listen (see listen options) options table @@ -184,6 +186,8 @@ these can, of course be used with wildcards however you want req:roll(bytes?) +> this will be changed to be a stream internally, see common.md + when bytes is null it will read as much as possible (may not be the full request) will update req according to how the bytes needed to be parsed, returns the number of bytes read (not necessarily parsed), 0 if there diff --git a/library/lullaby/net.lua b/library/lullaby/net.lua index 799e7d2..080c73d 100644 --- a/library/lullaby/net.lua +++ b/library/lullaby/net.lua @@ -19,6 +19,7 @@ function res_table.send(T, value) end ---autosets Content-Type and sends contents of file to client and closes socket ---@param T res-table ---@param value string +---@return error | nil error function res_table.sendfile(T, value) end ---sends value to client and doesn't close the socket 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; } diff --git a/tests/kill.lua b/tests/kill.lua index 53344dc..e4010f7 100644 --- a/tests/kill.lua +++ b/tests/kill.lua @@ -1,12 +1,14 @@ local llby = require"lullaby" -local t = llby.thread.async(function(res) +local t, e = llby.thread.async(function(res) for i = 1, 50 do print(i) os.execute("sleep 1") end end) +print(t, e) + os.execute("sleep 5") print("killing") diff --git a/tests/units/net-nested.lua b/tests/units/net-nested.lua index a7892d6..a9bbcb3 100644 --- a/tests/units/net-nested.lua +++ b/tests/units/net-nested.lua @@ -9,6 +9,7 @@ local net = llby.thread.async(function(tres) _G.server:GET("/test", function(res, req) res:send("555221") end) + res:send("") end) end, PORT) end) -- cgit v1.2.3