From 1a786066286e112d196b24e17c41b75215edafd1 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Sat, 10 Dec 2022 23:41:37 -0500 Subject: Trap lua errors and fail on error This likely could be updated to not be fatal errors and just log the errors to stderr. --- src/LuaApi.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/LuaApi.h') 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 +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; } -- cgit v1.2.3-54-g00ecf