diff options
author | sanine <sanine.not@pm.me> | 2023-02-24 04:06:36 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-24 04:06:36 -0600 |
commit | 2fa0047f4600ef95d32e864dd7c54e99e27265fe (patch) | |
tree | e592d63a38c087f8da7360a6df7e5904ca831dc8 /src/util | |
parent | 98c6064fc679586251793ae47c3c6f57dd4d1cb1 (diff) |
refactor: bind (almost) all ode functions
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/util.c | 37 | ||||
-rw-r--r-- | src/util/util.h | 1 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c index 6f1ee44..564ffd4 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -76,3 +76,40 @@ int gc_canary_collect(lua_State *L) luaL_unref(L, LUA_REGISTRYINDEX, *canary); return 0; } + + +static int h_traceback(lua_State *L) +{ + if (!lua_isstring(L, 1)) + /* 'message' is not a string, keep intact */ + return 1; + + lua_getglobal(L, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + return 1; +} + + +int h_call(lua_State *L, int nargs, int nret) +{ + int traceback_pos = lua_gettop(L) - nargs; + lua_pushcfunction(L, h_traceback); + lua_insert(L, traceback_pos); + + int result = lua_pcall(L, nargs, nret, traceback_pos); + lua_remove(L, traceback_pos); + return result; +} diff --git a/src/util/util.h b/src/util/util.h index 6092d11..ab68330 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -22,5 +22,6 @@ struct honey_tbl_t { void setup_util(lua_State *L, int honey_tbl); void create_table(lua_State *L, struct honey_tbl_t *tbl); void append_table(lua_State *L, int tbl_a, int tbl_b); +int h_call(lua_State *L, int nargs, int nret); #endif |