diff options
author | Mattes D <github@xoft.cz> | 2017-06-26 08:56:55 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2017-06-26 10:52:59 +0200 |
commit | e05ca494593d780c0ecf358bf66a94b224b35b5b (patch) | |
tree | c94b004d4c89aa118f889a2ae988cd354f69ace1 /src/Bindings/LuaState.cpp | |
parent | Fixed memory leak in cClientHandle. (#3794) (diff) | |
download | cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar.gz cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar.bz2 cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar.lz cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar.xz cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.tar.zst cuberite-e05ca494593d780c0ecf358bf66a94b224b35b5b.zip |
Diffstat (limited to 'src/Bindings/LuaState.cpp')
-rw-r--r-- | src/Bindings/LuaState.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
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 : "<unknown>", 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 : "<unknown>", 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 : "<unknown function>", 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)); |