summaryrefslogtreecommitdiff
path: root/src/util/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/util.c')
-rw-r--r--src/util/util.c37
1 files changed, 37 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;
+}