diff options
author | Steph Enders <smenders@gmail.com> | 2022-12-10 23:41:37 -0500 |
---|---|---|
committer | Steph Enders <smenders@gmail.com> | 2022-12-10 23:41:37 -0500 |
commit | 1a786066286e112d196b24e17c41b75215edafd1 (patch) | |
tree | aecaaf9e202a16da42be233642c2295ebeb83b00 | |
parent | aec2f478ecce57953c320eccd34d2f60139de04e (diff) |
Trap lua errors and fail on error
This likely could be updated to not be fatal errors and just log the
errors to stderr.
-rw-r--r-- | src/LuaApi.h | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/LuaApi.h b/src/LuaApi.h index 9da5c0a..3a46bc8 100644 --- a/src/LuaApi.h +++ b/src/LuaApi.h @@ -30,6 +30,11 @@ #include "lua.hpp" #include <iostream> +void trap(const char *message) { + std::cerr << message << std::endl; + exit(EXIT_FAILURE); +} + struct LState { lua_State *onkeypress; lua_State *onupdate; @@ -85,7 +90,8 @@ bool lua_dofn(lua_State *L, const char *fn) { std::cout << "[C] Error " << fn << " not function | not found" << std::endl; return false; } - lua_pcall(L, 0, 1, 0); + if (lua_pcall(L, 0, 1, 0) != LUA_OK) + trap(lua_tostring(L, -1)); return true; } @@ -96,7 +102,8 @@ bool lua_dofn_with_number(lua_State *L, const char *fn, lua_Number num) { return false; } lua_pushnumber(L, num); - lua_pcall(L, 1, 1, 0); + if (lua_pcall(L, 1, 1, 0) != LUA_OK) + trap(lua_tostring(L, -1)); return true; } |