From 2fa0047f4600ef95d32e864dd7c54e99e27265fe Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 24 Feb 2023 04:06:36 -0600 Subject: refactor: bind (almost) all ode functions --- src/util/util.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/util.h | 1 + 2 files changed, 38 insertions(+) (limited to 'src/util') 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 -- cgit v1.2.1