summaryrefslogtreecommitdiff
path: root/src/honey_lua.c
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-10-24 19:54:18 -0500
committersanine-a <sanine.not@pm.me>2020-10-24 19:54:18 -0500
commit27292f0d9ed90340792d4d065fefd2e4e248e4bd (patch)
tree52fd2a6743a0bc9c392de83583cd31c6411e3da9 /src/honey_lua.c
parent2cf300b870659a44d4e73ab65c035f6f443c3d25 (diff)
add honey_lua_pcall with traceback ability
Diffstat (limited to 'src/honey_lua.c')
-rw-r--r--src/honey_lua.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/honey_lua.c b/src/honey_lua.c
index f397872..ded2a91 100644
--- a/src/honey_lua.c
+++ b/src/honey_lua.c
@@ -190,3 +190,43 @@ void honey_lua_push_element(lua_State* L, honey_lua_element element)
break;
}
}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+int honey_lua_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 honey_lua_pcall(lua_State* L, int nargs, int nret)
+{
+ int traceback_pos = lua_gettop(L) - nargs;
+ lua_pushcfunction(L, honey_lua_traceback);
+ lua_insert(L, traceback_pos);
+
+ int result = lua_pcall(L, nargs, nret, traceback_pos);
+ lua_remove(L, traceback_pos);
+ return result;
+}
+