summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.c37
-rw-r--r--src/util/util.h1
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