lovr.errhand;

This commit is contained in:
bjorn 2016-08-08 12:22:33 -07:00
parent bb9a4d8db7
commit 9c3e4ddec9
3 changed files with 21 additions and 3 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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);