From e05ca494593d780c0ecf358bf66a94b224b35b5b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 26 Jun 2017 08:56:55 +0200 Subject: Moved ApiParamError into cLuaState. --- src/Bindings/LuaState.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index d18b6efcd..e30d0ed5f 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -1775,7 +1775,33 @@ bool cLuaState::CheckParamSelf(const char * a_SelfClassName) VERIFY(lua_getstack(m_LuaState, 0, &entry)); VERIFY(lua_getinfo (m_LuaState, "n", &entry)); AString ErrMsg = Printf( - "Error in function '%s'. The 'self' parameter is not of the expected type, \"instance of %s\". Make sure you're using the correct calling convention (obj:fn() instead of obj.fn()).", + "Error in function '%s'. The 'self' parameter is not of the expected type, \"instance of %s\". " \ + "Make sure you're using the correct calling convention (obj:fn() instead of obj.fn()).", + (entry.name != nullptr) ? entry.name : "", a_SelfClassName + ); + tolua_error(m_LuaState, ErrMsg.c_str(), &tolua_err); + return false; +} + + + + + +bool cLuaState::CheckParamStaticSelf(const char * a_SelfClassName) +{ + tolua_Error tolua_err; + if (tolua_isusertable(m_LuaState, 1, a_SelfClassName, 0, &tolua_err) && !lua_isnil(m_LuaState, 1)) + { + return true; + } + + // Not the correct parameter + lua_Debug entry; + VERIFY(lua_getstack(m_LuaState, 0, &entry)); + VERIFY(lua_getinfo (m_LuaState, "n", &entry)); + AString ErrMsg = Printf( + "Error in function '%s'. The 'self' parameter is not of the expected type, \"class %s\". " \ + "Make sure you're using the correct calling convention (cClassName:fn() instead of cClassName.fn() or obj:fn()).", (entry.name != nullptr) ? entry.name : "", a_SelfClassName ); tolua_error(m_LuaState, ErrMsg.c_str(), &tolua_err); @@ -1863,6 +1889,46 @@ void cLuaState::LogStackTrace(lua_State * a_LuaState, int a_StartingDepth) +int cLuaState::ApiParamError(const char * a_MsgFormat, ...) +{ + // Retrieve current function name + lua_Debug entry; + VERIFY(lua_getstack(m_LuaState, 0, &entry)); + VERIFY(lua_getinfo(m_LuaState, "n", &entry)); + + // Compose the error message: + va_list argp; + va_start(argp, a_MsgFormat); + AString msg; + + #ifdef __clang__ + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wformat-nonliteral" + #endif + + AppendVPrintf(msg, a_MsgFormat, argp); + + #ifdef __clang__ + #pragma clang diagnostic pop + #endif + + va_end(argp); + AString errorMsg = Printf("%s: %s", (entry.name != nullptr) ? entry.name : "", msg.c_str()); + + // Log everything into the console: + LOGWARNING("%s", errorMsg.c_str()); + // cLuaState::LogStackTrace(a_LuaState); // Do NOT log stack trace, it is already output as part of the Lua error handling + LogStackValues(m_LuaState, "Parameters on the stack"); + + // Raise Lua error: + lua_pushstring(m_LuaState, errorMsg.c_str()); + return lua_error(m_LuaState); +} + + + + + AString cLuaState::GetTypeText(int a_StackPos) { return lua_typename(m_LuaState, lua_type(m_LuaState, a_StackPos)); -- cgit v1.2.3