summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2021-09-04 01:58:39 -0500
committersanine <sanine.not@pm.me>2021-09-04 01:58:39 -0500
commit1f0be0e482147faa479818a9a2a179e302b9fec6 (patch)
treec2669963c612b43699ceb7cee4994c8e5bac075c
parent16c30cbbeb77cf5607a3212b4e7039169b9b268e (diff)
add hs_traceback.c
-rw-r--r--src/hs_traceback.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/hs_traceback.c b/src/hs_traceback.c
new file mode 100644
index 0000000..0fc8266
--- /dev/null
+++ b/src/hs_traceback.c
@@ -0,0 +1,37 @@
+#include "honeysuckle.h"
+
+int hs_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 hs_call(lua_State *L, int nargs, int nret)
+{
+ int insert_pos = lua_gettop(L) - nargs;
+ lua_pushcfunction(L, hs_traceback);
+ lua_insert(L, traceback_pos);
+
+ int result = lua_pcall(L, nargs, nret, traceback_pos);
+ lua_remove(L, traceback_pos);
+ return result;
+}