From 9c3e4ddec94bbc8ee2eb6d54217b752cc6aca40a Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 8 Aug 2016 12:22:33 -0700 Subject: [PATCH] lovr.errhand; --- src/lovr.c | 19 ++++++++++++++++++- src/lovr.h | 3 ++- src/main.c | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/lovr.c b/src/lovr.c index 887eaf0f..650ceaf5 100644 --- a/src/lovr.c +++ b/src/lovr.c @@ -65,6 +65,8 @@ void lovrInit(lua_State* L) { const char* message = luaL_checkstring(L, 1); error("Unable to bootstrap LOVR: %s", message); } + + lua_atpanic(L, lovrOnLuaError); } void lovrDestroy() { @@ -87,7 +89,22 @@ void lovrRun(lua_State* L) { lua_call(L, 0, 0); } -void lovrOnError(int code, const char* description) { +int lovrOnLuaError(lua_State* L) { + const char* message = luaL_checkstring(L, -1); + lua_getglobal(L, "lovr"); + lua_getfield(L, -1, "errhand"); + + if (lua_isfunction(L, -1)) { + lua_pushstring(L, message); + lua_call(L, 1, 0); + } else { + error(message); + } + + return 0; +} + +void lovrOnGlfwError(int code, const char* description) { error(description); } diff --git a/src/lovr.h b/src/lovr.h index 2b4e3362..a0b09951 100644 --- a/src/lovr.h +++ b/src/lovr.h @@ -6,5 +6,6 @@ void lovrInit(lua_State* L); void lovrDestroy(); void lovrRun(lua_State* L); -void lovrOnError(int code, const char* description); +int lovrOnLuaError(lua_State* L); +void lovrOnGlfwError(int code, const char* description); void lovrOnClose(GLFWwindow* window); diff --git a/src/main.c b/src/main.c index 88c379f6..9033a897 100644 --- a/src/main.c +++ b/src/main.c @@ -7,7 +7,7 @@ int main(int argc, char* argv[]) { L = luaL_newstate(); luaL_openlibs(L); - initGlfw(lovrOnError, lovrOnClose); + initGlfw(lovrOnGlfwError, lovrOnClose); lovrInit(L); lovrRun(L);