From 4a01879911c5673612693531c4f8970c1644947b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 19 Jan 2014 23:45:26 +0100 Subject: cLuaState can now check function params. --- src/Bindings/LuaState.cpp | 34 ++++++++++++++++++++++++++++++++++ src/Bindings/LuaState.h | 3 +++ 2 files changed, 37 insertions(+) (limited to 'src/Bindings') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 00e62fcf6..bfee1d037 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -914,6 +914,40 @@ bool cLuaState::CheckParamString(int a_StartParam, int a_EndParam) +bool cLuaState::CheckParamFunction(int a_StartParam, int a_EndParam) +{ + ASSERT(IsValid()); + + if (a_EndParam < 0) + { + a_EndParam = a_StartParam; + } + + for (int i = a_StartParam; i <= a_EndParam; i++) + { + if (lua_isfunction(m_LuaState, i)) + { + continue; + } + // 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' parameter #%d. Function expected, got %s", + (entry.name != NULL) ? entry.name : "?", i, GetTypeText(i).c_str() + ); + LogStackTrace(); + return false; + } // for i - Param + + // All params checked ok + return true; +} + + + + + bool cLuaState::CheckParamEnd(int a_Param) { tolua_Error tolua_err; diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 414e5e4b2..f8b67f5cd 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -815,6 +815,9 @@ public: /// Returns true if the specified parameters on the stack are strings; also logs warning if not bool CheckParamString(int a_StartParam, int a_EndParam = -1); + /// Returns true if the specified parameters on the stack are functions; also logs warning if not + bool CheckParamFunction(int a_StartParam, int a_EndParam = -1); + /// Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters) bool CheckParamEnd(int a_Param); -- cgit v1.2.3