From fd43a8568844f0d2a730bea425d8178584dcfe79 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 14 Aug 2018 22:56:11 +0200 Subject: [PATCH] [DEV] function call work --- luaWrapper/luaWrapper.hpp | 53 +++++++++++++++++-------- test/testCCallLuaFunction.cpp | 74 ++++++++++++++++++++++++++++++----- 2 files changed, 102 insertions(+), 25 deletions(-) diff --git a/luaWrapper/luaWrapper.hpp b/luaWrapper/luaWrapper.hpp index f139c02..3ad19cb 100644 --- a/luaWrapper/luaWrapper.hpp +++ b/luaWrapper/luaWrapper.hpp @@ -60,14 +60,19 @@ namespace luaWrapper { template LUAW_TYPE to(lua_State* _luaState, int _index); template void push(lua_State* _luaState, const LUAW_TYPE& _value); } - template - void setCallParameters(lua_State* _luaState) { + // end the recursive template... + inline void setCallParameters(lua_State* _luaState) { // nothing to do... } + template + void setCallParameters(lua_State* _luaState, const char* _value, LUAW_ARGS&&... _args) { + luaWrapper::utils::push(_luaState, _value); + setCallParameters(_luaState, etk::forward(_args)...); + } template void setCallParameters(lua_State* _luaState, LUAW_ARG&& _value, LUAW_ARGS&&... _args) { luaWrapper::utils::push(_luaState, _value); - setCallParameters(_luaState, _args...); + setCallParameters(_luaState, etk::forward(_args)...); } /** * @brief main interface of Lua engine. @@ -102,28 +107,44 @@ namespace luaWrapper { lua_State* getState() { return m_luaState; } - template - LUAW_RETURN_TYPE call(const char* _functionName, LUAW_ARGS&&... _args) { - LUAW_RETURN_TYPE returnValue; - + private: + template + void callGeneric(int32_t _numberReturn, const char* _functionName, LUAW_ARGS&&... _args) { /* push functions and arguments */ lua_getglobal(m_luaState, _functionName); /* function to be called */ - setCallParameters(m_luaState, _args ...); + setCallParameters(m_luaState, etk::forward(_args)...); /* do the call (n arguments, 1 result) */ - if (lua_pcall(m_luaState, int32_t(sizeof...(LUAW_ARGS)), 1, 0) != 0) { + if (lua_pcall(m_luaState, int32_t(sizeof...(LUAW_ARGS)), _numberReturn, 0) != 0) { ETK_THROW_EXCEPTION(etk::exception::RuntimeError(etk::String("error running function `") + _functionName +": " + lua_tostring(m_luaState, -1))); } - /* retrieve result */ - if (!lua_isnumber(m_luaState, -1)) { - ETK_THROW_EXCEPTION(etk::exception::RuntimeError(etk::String("function `") + _functionName +"`: must return a number")); - } - returnValue = lua_tonumber(m_luaState, -1); - lua_pop(m_luaState, 1); /* pop returned value */ + } + public: + /** + * Call a lua function with some generic parameters (with return value). + * @param[in] _functionName Funtion to call. + * @param[in] _args... Multiple argument (what you want). + * @return The specified type. + */ + template + LUAW_RETURN_TYPE call(const char* _functionName, LUAW_ARGS&&... _args) { + callGeneric(1, _functionName, etk::forward(_args)...); + // retrieve result + LUAW_RETURN_TYPE returnValue = luaWrapper::utils::check(m_luaState, -1); + // pop returned value + lua_pop(m_luaState, 1); return returnValue; } + /** + * Call a lua function with some generic parameters (WITHOUT return value). + * @param[in] _functionName Funtion to call. + * @param[in] _args... Multiple argument (what you want). + */ + template + void callVoid(const char* _functionName, LUAW_ARGS&&... _args) { + callGeneric(0, _functionName, etk::forward(_args)...); + } }; - /** * A simple utility function to adjust a given index * Useful for when a parameter index needs to be adjusted diff --git a/test/testCCallLuaFunction.cpp b/test/testCCallLuaFunction.cpp index 3fa0af9..959b7ff 100644 --- a/test/testCCallLuaFunction.cpp +++ b/test/testCCallLuaFunction.cpp @@ -8,18 +8,74 @@ #include -static etk::String refOutputBoolean1("{\n\t\"tmpElement\": true\n}\n"); - -TEST(TestCCallLuaFunctionn, basicCall) { +TEST(TestCCallLuaFunctionn, basicCallReturnDouble) { luaWrapper::Lua lua; lua.executeString(R"#( function MyFunctionName(x, y) return x + y*2 end )#"); - - float ret = lua.call("MyFunctionName", 43.9f, 43.6f); - - EXPECT_EQ(ret, 43.9f + 43.6f * 2.0f); - -} \ No newline at end of file + double ret = lua.call("MyFunctionName", 43.9, 143.6); + EXPECT_EQ(ret, float(43.9 + 143.6 * 2.0)); +} + +TEST(TestCCallLuaFunctionn, basicCallReturnFloat) { + luaWrapper::Lua lua; + lua.executeString(R"#( + function MyFunctionName(x, y) + return x + y*2 + end + )#"); + float ret = lua.call("MyFunctionName", 43.9f, 143.6f); + EXPECT_EQ(ret, 43.9f + 143.6f * 2.0f); +} + +TEST(TestCCallLuaFunctionn, basicCallReturnInt) { + luaWrapper::Lua lua; + lua.executeString(R"#( + function MyFunctionName(x, y) + return x + y*2 + end + )#"); + int ret = lua.call("MyFunctionName", 43, 76); + EXPECT_EQ(ret, 43 + 76 * 2); +} + +TEST(TestCCallLuaFunctionn, basicCallReturnBool) { + luaWrapper::Lua lua; + lua.executeString(R"#( + function MyFunctionName(x, y) + return x or y + end + )#"); + bool ret = lua.call("MyFunctionName", true, false); + EXPECT_EQ(ret, true || false); + ret = lua.call("MyFunctionName", false, false); + EXPECT_EQ(ret, false || false); + ret = lua.call("MyFunctionName", true, true); + EXPECT_EQ(ret, true || true); + ret = lua.call("MyFunctionName", false, true); + EXPECT_EQ(ret, false || true); +} + +TEST(TestCCallLuaFunctionn, basicCallReturnVoid) { + luaWrapper::Lua lua; + lua.executeString(R"#( + function MyFunctionName(x, y) + print("x or y") + end + )#"); + lua.callVoid("MyFunctionName", true, "TRESDF"); +} + +TEST(TestCCallLuaFunctionn, basicCallNotExist) { + luaWrapper::Lua lua; + lua.executeString(R"#( + function MyFunctionName(x, y) + print("x or y") + end + )#"); + lua.callVoid("MyFunctionNameThatDoesNotExit", true, "TRESDF"); +} + +