From cc920ea9297953e4f5e07b93587454516de7b34b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 8 Aug 2013 16:02:07 +0200 Subject: cPlugin_NewLua is now completely rewritten to use templated LuaState calls. --- source/LuaState.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) (limited to 'source/LuaState.cpp') diff --git a/source/LuaState.cpp b/source/LuaState.cpp index baa20fc07..83581d19a 100644 --- a/source/LuaState.cpp +++ b/source/LuaState.cpp @@ -200,7 +200,25 @@ bool cLuaState::LoadFile(const AString & a_FileName) -bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailure /* = true */) +bool cLuaState::HasFunction(const char * a_FunctionName) +{ + if (!IsValid()) + { + // This happens if cPlugin::Initialize() fails with an error + return false; + } + + lua_getglobal(m_LuaState, a_FunctionName); + bool res = (!lua_isnil(m_LuaState, -1) && lua_isfunction(m_LuaState, -1)); + lua_pop(m_LuaState, 1); + return res; +} + + + + + +bool cLuaState::PushFunction(const char * a_FunctionName) { ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack @@ -213,10 +231,7 @@ bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailur lua_getglobal(m_LuaState, a_FunctionName); if (!lua_isfunction(m_LuaState, -1)) { - if (a_ShouldLogFailure) - { - LOGWARNING("Error in %s: Could not find function %s()", m_SubsystemName.c_str(), a_FunctionName); - } + LOGWARNING("Error in %s: Could not find function %s()", m_SubsystemName.c_str(), a_FunctionName); lua_pop(m_LuaState, 1); return false; } @@ -229,7 +244,7 @@ bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailur -bool cLuaState::PushFunctionFromRegistry(int a_FnRef) +bool cLuaState::PushFunction(int a_FnRef) { ASSERT(IsValid()); ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack @@ -545,6 +560,45 @@ void cLuaState::Push(TakeDamageInfo * a_TDI) +void cLuaState::Push(cWindow * a_Window) +{ + ASSERT(IsValid()); + ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first + + tolua_pushusertype(m_LuaState, a_Window, "cWindow"); + m_NumCurrentFunctionArgs += 1; +} + + + + + +void cLuaState::Push(cPlugin_NewLua * a_Plugin) +{ + ASSERT(IsValid()); + ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first + + tolua_pushusertype(m_LuaState, a_Plugin, "cPlugin_NewLua"); + m_NumCurrentFunctionArgs += 1; +} + + + + + +void cLuaState::Push(const HTTPRequest * a_Request) +{ + ASSERT(IsValid()); + ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first + + tolua_pushusertype(m_LuaState, (void *)a_Request, "HTTPRequest"); + m_NumCurrentFunctionArgs += 1; +} + + + + + void cLuaState::GetReturn(int a_StackPos, bool & a_ReturnedVal) { a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0); -- cgit v1.2.3