From abbd5f4f98c24b6ffd3e5cf17a69f1c75dfc7d1b Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sat, 30 Jan 2021 15:34:27 -0600 Subject: [PATCH] Error handling in lua code --- core/Material.cpp | 4 +++- core/Mesh.cpp | 2 +- core/Mesh.h | 1 + core/Node.cpp | 8 ++++---- core/Scripting/Lua.cpp | 42 +++++++++++++++++++++--------------------- core/Scripting/Lua.h | 1 + core/Skybox.cpp | 4 +++- roadmap.md | 2 +- 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/core/Material.cpp b/core/Material.cpp index e922c86..b90fdb1 100644 --- a/core/Material.cpp +++ b/core/Material.cpp @@ -1,5 +1,7 @@ #include "Material.h" +#include + Texture::Texture() {} Texture Texture::FromFile(const char *filename) { @@ -18,7 +20,7 @@ Texture Texture::FromFile(const char *filename) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { - Util::Die("Error loading texture file: ", filename); + throw std::string() + "Error loading texture file: " + filename; } stbi_image_free(data); diff --git a/core/Mesh.cpp b/core/Mesh.cpp index e599cd4..4990ade 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -96,7 +96,7 @@ int Mesh::GetNumSubmeshes() { Material Mesh::GetMaterial(int submesh) { if (submesh >= GetNumSubmeshes()) { - Util::Die("Submesh index out of range"); + throw "Submesh index out of range"; } return submeshes[submesh]->material; } diff --git a/core/Mesh.h b/core/Mesh.h index efa3b6f..8e8e0bb 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -26,6 +26,7 @@ #define MESH_H #include +#include #include "types.h" #include "Spatial.h" diff --git a/core/Node.cpp b/core/Node.cpp index 068540b..c6208f9 100644 --- a/core/Node.cpp +++ b/core/Node.cpp @@ -33,10 +33,10 @@ NodeList::NodeList(bool isPrefabList) { void NodeList::Append(Node *node) { if (this->isPrefabList and not node->isPrefab) { - Util::Die("Attempt to add instanced node to prefab list!"); + throw "Attempt to add instanced node to prefab list!"; } if (node->isPrefab and not this->isPrefabList) { - Util::Die("Attempt to add prefab node to instanced list!"); + throw "Attempt to add prefab node to instanced list!"; } push_back(node); } @@ -83,7 +83,7 @@ void Node::QueueFree() { void Node::DoFree() { if (this != root) { - Util::Die("Tried to call DoFree from non-root node"); + throw "Tried to call DoFree from non-root node"; } freeList->FreeList(); } @@ -102,7 +102,7 @@ Node* Node::Duplicate() { Node* Node::Instance() { if (not isPrefab) { - Util::Die("Attempt to instance an instanced node!"); + throw "Attempt to instance an instanced node!"; } Node* instance = Duplicate(); instance->isPrefab = false; diff --git a/core/Scripting/Lua.cpp b/core/Scripting/Lua.cpp index d2b87ab..73422d1 100644 --- a/core/Scripting/Lua.cpp +++ b/core/Scripting/Lua.cpp @@ -1,4 +1,7 @@ #include "Lua.h" + +#include + #include "../Util.h" #ifdef LUA_SCRIPTING @@ -14,18 +17,14 @@ void Lua::Initialize() { // Initialize Lua luaL_openlibs(L); luaopen_couch(L); + lua_atpanic(L, &LuaExceptionHandler); + err = luaL_loadfile(L, "main.lua"); if (err == LUA_OK) { - err = lua_pcall(L, 0, 0, 0); - if (err != LUA_OK) { - Error(); - } + lua_call(L, 0, 0); if (HasHook("init")) { lua_getglobal(L, "init"); - err = lua_pcall(L, 0, 0, 0); - if (err != LUA_OK) { - Error(); - } + lua_call(L, 0, 0); } } else if (err == LUA_ERRFILE) { Util::Die("Could not find main.lua."); @@ -53,10 +52,7 @@ void Lua::Update(double delta) { if (HasHook("update")) { lua_getglobal(L, "update"); lua_pushnumber(L, delta); - int err = lua_pcall(L, 1, 0, 0); - if (err != LUA_OK) { - Error(); - } + lua_call(L, 1, 0); } #endif // LUA_SCRIPTING } @@ -88,16 +84,12 @@ bool Lua::HasHook(const char *name) { #ifdef LUA_SCRIPTING void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) { - // lua_State *L = (lua_State*) glfwGetWindowUserPointer(window); lua_getglobal(L, "onkey"); lua_pushinteger(L, key); lua_pushinteger(L, code); lua_pushinteger(L, action); lua_pushinteger(L, mods); - int err = lua_pcall(L, 4, 0, 0); - if (err != LUA_OK) { - Error(); - } + lua_call(L, 4, 0); } void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, double relx, double rely) { @@ -107,10 +99,18 @@ void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, doub lua_pushnumber(L, ypos); lua_pushnumber(L, relx); lua_pushnumber(L, rely); - int err = lua_pcall(L, 4, 0, 1); - if (err != LUA_OK) { - Error(); - } + lua_call(L, 4, 0); +} + +int Lua::LuaExceptionHandler(lua_State *L1) { + std::string err; + err += lua_tostring(L1, -1); + err += "\n"; + luaL_traceback(L1, L1, NULL, 1); + err += lua_tostring(L, -1); + Util::Die(err); + // Should never get here + return 1; } #endif // LUA_SCRIPTING diff --git a/core/Scripting/Lua.h b/core/Scripting/Lua.h index d19392f..ce37d33 100644 --- a/core/Scripting/Lua.h +++ b/core/Scripting/Lua.h @@ -27,6 +27,7 @@ private: static lua_State *L; static void LuaKeyHandler(Window *window, int key, int code, int action, int mods); static void LuaMousePositionHandler(Window *window, double xpos, double ypos, double xrel, double yrel); + static int LuaExceptionHandler(lua_State *L1); #endif // LUA_SCRIPTING }; diff --git a/core/Skybox.cpp b/core/Skybox.cpp index 9ad3621..77ec848 100644 --- a/core/Skybox.cpp +++ b/core/Skybox.cpp @@ -1,5 +1,7 @@ #include "Skybox.h" +#include + float vertices[] = { // positions -1.0f, 1.0f, -1.0f, @@ -91,7 +93,7 @@ Skybox *Skybox::FromFiles(const char *right, const char* left, const char* top, for (int i = 0; i < 6; i++) { data = stbi_load(files[i], &width, &height, &nrChannels, 3); if (!data) { - Util::Die("Could not load skybox image ", files[i]); + throw std::string() + "Could not load skybox image " + files[i]; } glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); diff --git a/roadmap.md b/roadmap.md index f6a7bd6..02e556f 100644 --- a/roadmap.md +++ b/roadmap.md @@ -79,7 +79,7 @@ Yeah sure let's do physics ## Great Refactor Things are starting to smell, here's what I need to do - [ ] Replace all public attributes with accessors (on classes) -- [ ] Seperate prefabs from instances +- [X] Seperate prefabs from instances - [ ] Combine related files (shaders) - [ ] Create a testing suite - [ ] Have exceptions thrown to Lua if generated from a user program