diff options
-rw-r--r-- | src/Bindings/LuaState.cpp | 28 | ||||
-rw-r--r-- | src/Bindings/LuaState.h | 2 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 1dd2b4dd8..149c304ed 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -228,6 +228,9 @@ bool cLuaState::PushFunction(const char * a_FunctionName) return false; } + // Push the error handler for lua_pcall() + lua_pushcfunction(m_LuaState, &ReportFnCallErrors); + lua_getglobal(m_LuaState, a_FunctionName); if (!lua_isfunction(m_LuaState, -1)) { @@ -249,6 +252,9 @@ bool cLuaState::PushFunction(int a_FnRef) ASSERT(IsValid()); ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack + // Push the error handler for lua_pcall() + lua_pushcfunction(m_LuaState, &ReportFnCallErrors); + lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef); // same as lua_getref() if (!lua_isfunction(m_LuaState, -1)) { @@ -269,6 +275,9 @@ bool cLuaState::PushFunction(const cTableRef & a_TableRef) ASSERT(IsValid()); ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack + // Push the error handler for lua_pcall() + lua_pushcfunction(m_LuaState, &ReportFnCallErrors); + lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_TableRef.GetTableRef()); // Get the table ref if (!lua_istable(m_LuaState, -1)) { @@ -731,11 +740,13 @@ void cLuaState::GetReturn(int a_StackPos, double & a_ReturnedVal) bool cLuaState::CallFunction(int a_NumResults) { ASSERT (m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first - ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 1)); + ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 1)); // The function to call + ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 2)); // The error handler - int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, 0); - if (ReportErrors(s)) + int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, -m_NumCurrentFunctionArgs - 2); + if (s != 0) { + // The error has already been printed together with the stacktrace LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName.c_str()); m_NumCurrentFunctionArgs = -1; m_CurrentFunctionName.clear(); @@ -1013,6 +1024,17 @@ AString cLuaState::GetTypeText(int a_StackPos) +int cLuaState::ReportFnCallErrors(lua_State * a_LuaState) +{ + LOGWARNING("LUA: %s", lua_tostring(a_LuaState, -1)); + LogStackTrace(a_LuaState); + return 1; // We left the error message on the stack as the return value +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cLuaState::cRef: diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index c77dbeb7f..a43d39732 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -889,6 +889,8 @@ protected: */ bool CallFunction(int a_NumReturnValues); + /** Used as the error reporting function for function calls */ + static int ReportFnCallErrors(lua_State * a_LuaState); } ; |