diff --git a/luaWrapper/luaWrapper.hpp b/luaWrapper/luaWrapper.hpp index f5eb652..f139c02 100644 --- a/luaWrapper/luaWrapper.hpp +++ b/luaWrapper/luaWrapper.hpp @@ -42,6 +42,7 @@ #include #include #include +#include #include @@ -54,6 +55,20 @@ #define LUAW_WRAPPER_KEY "LuaWrapper" namespace luaWrapper { + namespace utils { + template LUAW_TYPE check(lua_State* _luaState, int _index); + 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) { + // nothing to do... + } + template + void setCallParameters(lua_State* _luaState, LUAW_ARG&& _value, LUAW_ARGS&&... _args) { + luaWrapper::utils::push(_luaState, _value); + setCallParameters(_luaState, _args...); + } /** * @brief main interface of Lua engine. */ @@ -87,6 +102,26 @@ namespace luaWrapper { lua_State* getState() { return m_luaState; } + template + LUAW_RETURN_TYPE call(const char* _functionName, LUAW_ARGS&&... _args) { + LUAW_RETURN_TYPE returnValue; + + /* push functions and arguments */ + lua_getglobal(m_luaState, _functionName); /* function to be called */ + setCallParameters(m_luaState, _args ...); + + /* do the call (n arguments, 1 result) */ + if (lua_pcall(m_luaState, int32_t(sizeof...(LUAW_ARGS)), 1, 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 */ + return returnValue; + } }; /** @@ -514,13 +549,6 @@ namespace luaWrapper { lua_pop( _luaState, 1 ); return 0; } - /* - static int gc( lua_State* const L ) - { - - return 0; - } - */ /** * Thakes two tables and registers them with Lua to the table on the top of the * stack. @@ -529,21 +557,12 @@ namespace luaWrapper { */ inline void registerfuncs(lua_State* _luaState, const luaL_Reg _defaulttable[], const luaL_Reg _table[]) { // ... T - #if LUA_VERSION_NUM == 502 if (_defaulttable) { luaL_setfuncs(_luaState, _defaulttable, 0); // ... T } if (_table) { luaL_setfuncs(_luaState, _table, 0); // ... T } - #else - if (_defaulttable) { - luaL_register(_luaState, NULL, _defaulttable); // ... T - } - if (_table) { - luaL_register(_luaState, NULL, _table); // ... T - } - #endif } /** @@ -668,36 +687,36 @@ namespace luaWrapper { } template - void registerElement(lua_State* _luaState, + void registerElement(Lua& _lua, const char* _classname, const luaL_Reg* _table, const luaL_Reg* _metatable, ememory::SharedPtr (*_allocator)(lua_State*), void (*_identifier)(lua_State*, ememory::SharedPtr)) { - setfuncs(_luaState, _classname, _table, _metatable, _allocator, _identifier); // ... T - lua_pushvalue(_luaState, -1); // ... LUAW_TYPE LUAW_TYPE - lua_setglobal(_luaState, _classname); // ... LUAW_TYPE + setfuncs(_lua.getState(), _classname, _table, _metatable, _allocator, _identifier); // ... T + lua_pushvalue(_lua.getState(), -1); // ... LUAW_TYPE LUAW_TYPE + lua_setglobal(_lua.getState(), _classname); // ... LUAW_TYPE } template - void registerElement(lua_State* _luaState, + void registerElement(Lua& _lua, const char* _classname, const luaL_Reg* _table, const luaL_Reg* _metatable, ememory::SharedPtr (*_allocator)(lua_State*)) { - setfuncs(_luaState, _classname, _table, _metatable, _allocator, defaultidentifier); - lua_pushvalue(_luaState, -1); // ... LUAW_TYPE LUAW_TYPE - lua_setglobal(_luaState, _classname); // ... LUAW_TYPE + setfuncs(_lua.getState(), _classname, _table, _metatable, _allocator, defaultidentifier); + lua_pushvalue(_lua.getState(), -1); // ... LUAW_TYPE LUAW_TYPE + lua_setglobal(_lua.getState(), _classname); // ... LUAW_TYPE } template - void registerElement(lua_State* _luaState, + void registerElement(Lua& _lua, const char* _classname, const luaL_Reg* _table, const luaL_Reg* _metatable) { - setfuncs(_luaState, _classname, _table, _metatable, defaultallocator, defaultidentifier); // ... T - lua_pushvalue(_luaState, -1); // ... T T - lua_setglobal(_luaState, _classname); // ... T + setfuncs(_lua.getState(), _classname, _table, _metatable, defaultallocator, defaultidentifier); // ... T + lua_pushvalue(_lua.getState(), -1); // ... T T + lua_setglobal(_lua.getState(), _classname); // ... T } /** diff --git a/luaWrapper/luaWrapperEtk.cpp b/luaWrapper/luaWrapperEtk.cpp index e59d10e..f9a44e5 100644 --- a/luaWrapper/luaWrapperEtk.cpp +++ b/luaWrapper/luaWrapperEtk.cpp @@ -29,104 +29,104 @@ namespace luaWrapper { * This example uses etk::String, but if you have other custom string types it * should be easy to write versions of those functions too */ - template<> etk::String check(lua_State* _L, int _index) { - return etk::String(luaL_checkstring(_L, _index)); + template<> etk::String check(lua_State* _luaState, int _index) { + return etk::String(luaL_checkstring(_luaState, _index)); } - template<> etk::String to(lua_State* _L, int _index) { - return etk::String(lua_tostring(_L, _index)); + template<> etk::String to(lua_State* _luaState, int _index) { + return etk::String(lua_tostring(_luaState, _index)); } - template<> void push(lua_State* _L, const etk::String& _val) { - lua_pushstring(_L, _val.c_str()); + template<> void push(lua_State* _luaState, const etk::String& _val) { + lua_pushstring(_luaState, _val.c_str()); } - template<> bool check(lua_State* _L, int _index) { - return lua_toboolean(_L, _index) != 0; + template<> bool check(lua_State* _luaState, int _index) { + return lua_toboolean(_luaState, _index) != 0; } - template<> bool to(lua_State* _L, int _index) { - return lua_toboolean(_L, _index) != 0; + template<> bool to(lua_State* _luaState, int _index) { + return lua_toboolean(_luaState, _index) != 0; } - template<> void push(lua_State* _L, const bool& _value) { - lua_pushboolean(_L, _value); + template<> void push(lua_State* _luaState, const bool& _value) { + lua_pushboolean(_luaState, _value); } - template<> const char* check(lua_State* _L, int _index) { - return luaL_checkstring(_L, _index); + template<> const char* check(lua_State* _luaState, int _index) { + return luaL_checkstring(_luaState, _index); } - template<> const char* to(lua_State* _L, int _index) { - return lua_tostring(_L, _index); + template<> const char* to(lua_State* _luaState, int _index) { + return lua_tostring(_luaState, _index); } - template<> void push(lua_State* _L, const char* const& _value) { - lua_pushstring(_L, _value); + template<> void push(lua_State* _luaState, const char* const& _value) { + lua_pushstring(_luaState, _value); } - template<> const char* const check(lua_State* _L, int _index) { - return luaL_checkstring(_L, _index); + template<> const char* const check(lua_State* _luaState, int _index) { + return luaL_checkstring(_luaState, _index); } - template<> const char* const to(lua_State* _L, int _index) { - return lua_tostring(_L, _index); + template<> const char* const to(lua_State* _luaState, int _index) { + return lua_tostring(_luaState, _index); } - template<> void push(lua_State* _L, const char* const& _value) { - lua_pushstring(_L, _value); + template<> void push(lua_State* _luaState, const char* const& _value) { + lua_pushstring(_luaState, _value); } - template<> unsigned int check(lua_State* _L, int _index) { - return static_cast(luaL_checkinteger(_L, _index)); + template<> unsigned int check(lua_State* _luaState, int _index) { + return static_cast(luaL_checkinteger(_luaState, _index)); } - template<> unsigned int to(lua_State* _L, int _index) { - return static_cast(lua_tointeger(_L, _index)); + template<> unsigned int to(lua_State* _luaState, int _index) { + return static_cast(lua_tointeger(_luaState, _index)); } - template<> void push(lua_State* _L, const unsigned int& _value) { - lua_pushinteger(_L, _value); + template<> void push(lua_State* _luaState, const unsigned int& _value) { + lua_pushinteger(_luaState, _value); } - template<> int check(lua_State* _L, int _index) { - return static_cast(luaL_checkinteger(_L, _index)); + template<> int check(lua_State* _luaState, int _index) { + return static_cast(luaL_checkinteger(_luaState, _index)); } - template<> int to(lua_State* _L, int _index) { - return static_cast(lua_tointeger(_L, _index)); + template<> int to(lua_State* _luaState, int _index) { + return static_cast(lua_tointeger(_luaState, _index)); } - template<> void push(lua_State* _L, const int& _value) { - lua_pushinteger(_L, _value); + template<> void push(lua_State* _luaState, const int& _value) { + lua_pushinteger(_luaState, _value); } - template<> unsigned char check(lua_State* _L, int _index) { - return static_cast(luaL_checkinteger(_L, _index)); + template<> unsigned char check(lua_State* _luaState, int _index) { + return static_cast(luaL_checkinteger(_luaState, _index)); } - template<> unsigned char to(lua_State* _L, int _index) { - return static_cast(lua_tointeger(_L, _index)); + template<> unsigned char to(lua_State* _luaState, int _index) { + return static_cast(lua_tointeger(_luaState, _index)); } - template<> void push(lua_State* _L, const unsigned char& _value) { - lua_pushinteger(_L, _value); + template<> void push(lua_State* _luaState, const unsigned char& _value) { + lua_pushinteger(_luaState, _value); } - template<> char check(lua_State* _L, int _index) { - return static_cast(luaL_checkinteger(_L, _index)); + template<> char check(lua_State* _luaState, int _index) { + return static_cast(luaL_checkinteger(_luaState, _index)); } - template<> char to(lua_State* _L, int _index) { - return static_cast(lua_tointeger(_L, _index)); + template<> char to(lua_State* _luaState, int _index) { + return static_cast(lua_tointeger(_luaState, _index)); } - template<> void push(lua_State* _L, const char& _value) { - lua_pushinteger(_L, _value); + template<> void push(lua_State* _luaState, const char& _value) { + lua_pushinteger(_luaState, _value); } - template<> float check(lua_State* _L, int _index) { - return static_cast(luaL_checknumber(_L, _index)); + template<> float check(lua_State* _luaState, int _index) { + return static_cast(luaL_checknumber(_luaState, _index)); } - template<> float to(lua_State* _L, int _index) { - return static_cast(lua_tonumber(_L, _index)); + template<> float to(lua_State* _luaState, int _index) { + return static_cast(lua_tonumber(_luaState, _index)); } - template<> void push(lua_State* _L, const float& _value) { - lua_pushnumber(_L, _value); + template<> void push(lua_State* _luaState, const float& _value) { + lua_pushnumber(_luaState, _value); } - template<> double check(lua_State* _L, int _index) { - return static_cast(luaL_checknumber(_L, _index)); + template<> double check(lua_State* _luaState, int _index) { + return static_cast(luaL_checknumber(_luaState, _index)); } - template<> double to(lua_State* _L, int _index) { - return static_cast(lua_tonumber(_L, _index)); + template<> double to(lua_State* _luaState, int _index) { + return static_cast(lua_tonumber(_luaState, _index)); } - template<> void push(lua_State* _L, const double& _value) { - lua_pushnumber(_L, _value); + template<> void push(lua_State* _luaState, const double& _value) { + lua_pushnumber(_luaState, _value); } } } diff --git a/luaWrapper/luaWrapperUtil.hpp b/luaWrapper/luaWrapperUtil.hpp index bec5372..0509a60 100644 --- a/luaWrapper/luaWrapperUtil.hpp +++ b/luaWrapper/luaWrapperUtil.hpp @@ -29,69 +29,57 @@ namespace luaWrapper { /** * @brief This template removes reference and const qualifier from the type */ - template struct remove_cr { - typedef typename std::remove_const::type>::type type; + template struct remove_cr { + typedef typename std::remove_const::type>::type type; }; - /** - * A set of templated luaL_check and lua_push functions for use in the getters - * and setters below - * - * It is often useful to override luaWrapper::utils::check, luaWrapper::utils::to and/or luaWrapper::utils::push to - * operate on your own simple types rather than register your type with - * LuaWrapper, especially with small objects. - */ - - template U check(lua_State* _L, int _index); - template U to(lua_State* _L, int _index); - template void push(lua_State* _L, const U& _value); /** * This is slightly different than the previous three functions in that you * shouldn't need to write your own version of it, since it uses check * automatically. */ - template U opt(lua_State* _L, int _index, const U& _fallback = U()) { - if (lua_isnil(_L, _index)) { + template U opt(lua_State* _luaState, int _index, const U& _fallback = U()) { + if (lua_isnil(_luaState, _index)) { return _fallback; } else { - return luaWrapper::utils::check(_L, _index); + return luaWrapper::utils::check(_luaState, _index); } } /** * These are just some functions I've always felt should exist */ - template inline U getfield(lua_State* _L, int _index, const char* _field) { + template inline U getfield(lua_State* _luaState, int _index, const char* _field) { static_assert(!LUAW_STD::is_same::value, "luaWrapper::utils::getfield is not safe to use on const char*'s. (The string will be popped from the stack.)"); - lua_getfield(_L, _index, _field); - U val = luaWrapper::utils::to(_L, -1); - lua_pop(_L, 1); + lua_getfield(_luaState, _index, _field); + U val = luaWrapper::utils::to(_luaState, -1); + lua_pop(_luaState, 1); return val; } - template inline U checkfield(lua_State* _L, int _index, const char* _field) { + template inline U checkfield(lua_State* _luaState, int _index, const char* _field) { static_assert(!LUAW_STD::is_same::value, "luaWrapper::utils::checkfield is not safe to use on const char*'s. (The string will be popped from the stack.)"); - lua_getfield(_L, _index, _field); - U val = luaWrapper::utils::check(_L, -1); - lua_pop(_L, 1); + lua_getfield(_luaState, _index, _field); + U val = luaWrapper::utils::check(_luaState, -1); + lua_pop(_luaState, 1); return val; } - template inline U optfield(lua_State* _L, int _index, const char* _field, const U& _fallback = U()) { + template inline U optfield(lua_State* _luaState, int _index, const char* _field, const U& _fallback = U()) { static_assert(!LUAW_STD::is_same::value, "luaWrapper::utils::getfield is not safe to use on const char*'s. (The string will be popped from the stack.)"); - lua_getfield(_L, _index, _field); - U val = luaWrapper::utils::opt(_L, -1, _fallback); - lua_pop(_L, 1); + lua_getfield(_luaState, _index, _field); + U val = luaWrapper::utils::opt(_luaState, -1, _fallback); + lua_pop(_luaState, 1); return val; } - template inline void setfield(lua_State* _L, int _index, const char* _field, U _val) { - luaWrapper::utils::push(_L, _val); - lua_setfield(_L, luaWrapper::correctindex(_L, _index, 1), _field); + template inline void setfield(lua_State* _luaState, int _index, const char* _field, U _val) { + luaWrapper::utils::push(_luaState, _val); + lua_setfield(_luaState, luaWrapper::correctindex(_luaState, _index, 1), _field); } /** A set of trivial getter and setter templates. These templates are designed @@ -121,9 +109,9 @@ namespace luaWrapper { * } * * Getters and setters must have one of the following signatures: - * void T::Setter(U _value); - * void T::Setter(U* _value); - * void T::Setter(const U& _value); + * void LUAW_TYPE::Setter(U _value); + * void LUAW_TYPE::Setter(U* _value); + * void LUAW_TYPE::Setter(const U& _value); * U Getter() const; * U* Getter() const; * @@ -141,211 +129,211 @@ namespace luaWrapper { * In a Lua script, you can now use foo:GetBar(), foo:SetBar() and foo:Bar() */ - template int get(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); - luaWrapper::utils::push(_L, obj.get()->*Member); + template int get(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); + luaWrapper::utils::push(_luaState, obj.get()->*Member); return 1; } - template int get(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); - luaWrapper::push(_L, obj.get()->*Member); + template int get(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); + luaWrapper::push(_luaState, obj.get()->*Member); return 1; } - template int get(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); - luaWrapper::utils::push(_L, (obj.get()->*Getter)()); + template int get(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); + luaWrapper::utils::push(_luaState, (obj.get()->*Getter)()); return 1; } - template int get(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); - luaWrapper::utils::push(_L, (obj.get()->*Getter)()); + template int get(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); + luaWrapper::utils::push(_luaState, (obj.get()->*Getter)()); return 1; } - template int get(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); - luaWrapper::push(_L, (obj.get()->*Getter)()); + template int get(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); + luaWrapper::push(_luaState, (obj.get()->*Getter)()); return 1; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - obj.get()->*Member = luaWrapper::utils::check(_L, 2); + obj.get()->*Member = luaWrapper::utils::check(_luaState, 2); } return 0; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); obj.get()->*Member = member.get(); } return 0; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); obj.get()->*Member = member.get(); } return 0; } - template int setAndRelease(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int setAndRelease(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); obj.get()->*Member = member.get(); if (member) { - luaWrapper::release(_L, member); + luaWrapper::release(_luaState, member); } } return 0; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - (obj.get()->*Setter)(luaWrapper::utils::check(_L, 2)); + (obj.get()->*Setter)(luaWrapper::utils::check(_luaState, 2)); } return 0; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - (obj.get()->*Setter)(luaWrapper::utils::check(_L, 2)); + (obj.get()->*Setter)(luaWrapper::utils::check(_luaState, 2)); } return 0; } - template int set(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int set(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); (obj.get()->*Setter)(member.get()); } return 0; } - template int setAndRelease(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int setAndRelease(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if (obj != null) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); (obj.get()->*Setter)(member); if (member) { - luaWrapper::release(_L, member); + luaWrapper::release(_luaState, member); } } return 0; } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - obj.get()->*Member = luaWrapper::utils::check(_L, 2); + && lua_gettop(_luaState) >= 2) { + obj.get()->*Member = luaWrapper::utils::check(_luaState, 2); return 0; } else { - luaWrapper::utils::push(_L, obj.get()->*Member); + luaWrapper::utils::push(_luaState, obj.get()->*Member); return 1; } } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + && lua_gettop(_luaState) >= 2) { + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); obj.get()->*Member = member.get(); return 0; } else { - luaWrapper::push(_L, obj.get()->*Member); + luaWrapper::push(_luaState, obj.get()->*Member); return 1; } } - template int getSetAndRelease(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSetAndRelease(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + && lua_gettop(_luaState) >= 2) { + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); obj.get()->*Member = member.get(); if (member) - luaWrapper::release(_L, member); + luaWrapper::release(_luaState, member); return 0; } else { - luaWrapper::push(_L, obj.get()->*Member); + luaWrapper::push(_luaState, obj.get()->*Member); return 1; } } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - (obj.get()->*Setter)(luaWrapper::utils::check(_L, 2)); + && lua_gettop(_luaState) >= 2) { + (obj.get()->*Setter)(luaWrapper::utils::check(_luaState, 2)); return 0; } else { - luaWrapper::utils::push(_L, (obj.get()->*Getter)()); + luaWrapper::utils::push(_luaState, (obj.get()->*Getter)()); return 1; } } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - (obj.get()->*Setter)(luaWrapper::utils::check(_L, 2)); + && lua_gettop(_luaState) >= 2) { + (obj.get()->*Setter)(luaWrapper::utils::check(_luaState, 2)); return 0; } else { - luaWrapper::utils::push(_L, (obj.get()->*Getter)()); + luaWrapper::utils::push(_luaState, (obj.get()->*Getter)()); return 1; } } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - (obj.get()->*Setter)(luaWrapper::utils::check(_L, 2)); + && lua_gettop(_luaState) >= 2) { + (obj.get()->*Setter)(luaWrapper::utils::check(_luaState, 2)); return 0; } else { - luaWrapper::utils::push(_L, (obj.get()->*Getter)()); + luaWrapper::utils::push(_luaState, (obj.get()->*Getter)()); return 1; } } - template int getSet(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSet(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + && lua_gettop(_luaState) >= 2) { + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); (obj.get()->*Setter)(member.get()); return 0; } else { - luaWrapper::push(_L, (obj.get()->*Getter)()); + luaWrapper::push(_luaState, (obj.get()->*Getter)()); return 1; } } - template int getSetAndRelease(lua_State* _L) { - ememory::SharedPtr obj = luaWrapper::check(_L, 1); + template int getSetAndRelease(lua_State* _luaState) { + ememory::SharedPtr obj = luaWrapper::check(_luaState, 1); if ( obj != null - && lua_gettop(_L) >= 2) { - ememory::SharedPtr member = luaWrapper::opt(_L, 2); + && lua_gettop(_luaState) >= 2) { + ememory::SharedPtr member = luaWrapper::opt(_luaState, 2); (obj.get()->*Setter)(member); if (member) - luaWrapper::release(_L, member); + luaWrapper::release(_luaState, member); return 0; } else { - luaWrapper::push(_L, (obj.get()->*Getter)()); + luaWrapper::push(_luaState, (obj.get()->*Getter)()); return 1; } } @@ -461,28 +449,28 @@ namespace luaWrapper { struct MemberFuncWrapper; template - struct MemberFuncWrapper { + struct MemberFuncWrapper { public: - static int call(lua_State* _L) { - return callImpl(_L, makeIntRange<2,sizeof...(Args)>()); + static int call(lua_State* _luaState) { + return callImpl(_luaState, makeIntRange<2,sizeof...(Args)>()); } private: - template static int callImpl(lua_State* _L, IntPack) { - luaWrapper::utils::push(_L, ((*luaWrapper::check(_L, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_L, indices)...)); + template static int callImpl(lua_State* _luaState, IntPack) { + luaWrapper::utils::push(_luaState, ((*luaWrapper::check(_luaState, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_luaState, indices)...)); return 1; } }; template - struct MemberFuncWrapper { + struct MemberFuncWrapper { public: - static int call(lua_State* _L) { - return callImpl(_L, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); + static int call(lua_State* _luaState) { + return callImpl(_luaState, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); } private: template - static int callImpl(lua_State* _L, IntPack) { - ((*luaWrapper::check(_L, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_L, indices)...); + static int callImpl(lua_State* _luaState, IntPack) { + ((*luaWrapper::check(_luaState, 1)).*MemberFunc)(luaWrapper::utils::check::type>(_luaState, indices)...); return 0; } }; @@ -498,12 +486,12 @@ namespace luaWrapper { template struct StaticFuncWrapper { public: - static int call(lua_State* _L) { - return callImpl(_L, luaWrapper::utils::makeIntRange<2,sizeof...(Args)>()); + static int call(lua_State* _luaState) { + return callImpl(_luaState, luaWrapper::utils::makeIntRange<2,sizeof...(Args)>()); } private: - template static int callImpl(lua_State* _L, IntPack) { - luaWrapper::utils::push(_L, (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_L, indices)...)); + template static int callImpl(lua_State* _luaState, IntPack) { + luaWrapper::utils::push(_luaState, (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_luaState, indices)...)); return 1; } }; @@ -511,13 +499,13 @@ namespace luaWrapper { template struct StaticFuncWrapper { public: - static int call(lua_State* _L) { - return callImpl(_L, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); + static int call(lua_State* _luaState) { + return callImpl(_luaState, luaWrapper::utils::makeIntRange<2, sizeof...(Args)>()); } private: template - static int callImpl(lua_State* _L, luaWrapper::utils::IntPack) { - (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_L, indices)...); + static int callImpl(lua_State* _luaState, luaWrapper::utils::IntPack) { + (*LUAW_FUNCTION)(luaWrapper::utils::check::type>(_luaState, indices)...); return 0; } }; @@ -539,13 +527,13 @@ namespace luaWrapper { * foo = Foo.new() * foo2 = foo:clone() */ - template int clone(lua_State* _L) { - T* obj = new T(*luaWrapper::check(_L, 1)); - lua_remove(_L, 1); // ... - int numargs = lua_gettop(_L); - luaWrapper::push(_L, obj); // ... clone - luaWrapper::hold(_L, obj); - luaWrapper::postconstructor(_L, numargs); + template int clone(lua_State* _luaState) { + LUAW_TYPE* obj = new LUAW_TYPE(*luaWrapper::check(_luaState, 1)); + lua_remove(_luaState, 1); // ... + int numargs = lua_gettop(_luaState); + luaWrapper::push(_luaState, obj); // ... clone + luaWrapper::hold(_luaState, obj); + luaWrapper::postconstructor(_luaState, numargs); return 1; } @@ -567,17 +555,17 @@ namespace luaWrapper { * After the object is constructed, luaWrapper::utils::build will do the equivalent of * calling f:X(10) and f:Y(20). */ - template int build(lua_State* _L) { + template int build(lua_State* _luaState) { // obj {} - lua_insert(_L, -2); // {} obj - if (lua_type(_L, 1) == LUA_TTABLE) { - for (lua_pushnil(_L); lua_next(_L, 1); lua_pop(_L, 1)) { + lua_insert(_luaState, -2); // {} obj + if (lua_type(_luaState, 1) == LUA_TTABLE) { + for (lua_pushnil(_luaState); lua_next(_luaState, 1); lua_pop(_luaState, 1)) { // {} obj k v - lua_pushvalue(_L, -2); // {} obj k v k - lua_gettable(_L, -4); // {} obj k v ud[k] - lua_pushvalue(_L, -4); // {} obj k v ud[k] ud - lua_pushvalue(_L, -3); // {} obj k v ud[k] ud v - lua_call(_L, 2, 0); // {} obj k v + lua_pushvalue(_luaState, -2); // {} obj k v k + lua_gettable(_luaState, -4); // {} obj k v ud[k] + lua_pushvalue(_luaState, -4); // {} obj k v ud[k] ud + lua_pushvalue(_luaState, -3); // {} obj k v ud[k] ud v + lua_call(_luaState, 2, 0); // {} obj k v } // {} ud } @@ -595,30 +583,30 @@ namespace luaWrapper { * * e.g. * - * Foo* parent = luaWrapper::check(_L, 1); - * Bar* child = luaWrapper::check(_L, 2); + * Foo* parent = luaWrapper::check(_luaState, 1); + * Bar* child = luaWrapper::check(_luaState, 2); * if (parent && child) * { - * luaWrapper::utils::store(_L, 1, "Children"); + * luaWrapper::utils::store(_luaState, 1, "Children"); * parent->AddChild(child); * } */ - template void store(lua_State* _L, int _index, const char* _storagetable, const char* _key = NULL) { + template void store(lua_State* _luaState, int _index, const char* _storagetable, const char* _key = NULL) { // ... store ... obj store.storagetable - lua_getfield(_L, _index, _storagetable); + lua_getfield(_luaState, _index, _storagetable); if (_key) { // ... store ... obj store.storagetable key - lua_pushstring(_L, _key); + lua_pushstring(_luaState, _key); } else { // ... store ... obj store.storagetable key - LuaWrapper::identifier(_L, luaWrapper::to(_L, -2)); + LuaWrapper::identifier(_luaState, luaWrapper::to(_luaState, -2)); } // ... store ... obj store.storagetable key obj - lua_pushvalue(_L, -3); + lua_pushvalue(_luaState, -3); // ... store ... obj store.storagetable - lua_settable(_L, -3); + lua_settable(_luaState, -3); // ... store ... obj - lua_pop(_L, 1); + lua_pop(_luaState, 1); } } } diff --git a/lutin_luaWrapper-test.py b/lutin_luaWrapper-test.py new file mode 100644 index 0000000..1079100 --- /dev/null +++ b/lutin_luaWrapper-test.py @@ -0,0 +1,41 @@ +#!/usr/bin/python +import lutin.debug as debug +import lutin.tools as tools + + +def get_type(): + return "BINARY" + +def get_sub_type(): + return "TEST" + +def get_name(): + return "lua-wrapper-test" + +def get_desc(): + return "lua wrapper test application" + +def get_licence(): + return "MPL-2" + +def get_compagny_type(): + return "com" + +def get_compagny_name(): + return "atria-soft" + +def get_maintainer(): + return ["Mr DUPIN Edouard "] + +def configure(target, my_module): + my_module.add_src_file([ + 'test/test.cpp', + 'test/testCCallLuaFunction.cpp', + ]) + my_module.add_depend([ + 'luaWrapper', + 'etest', + 'test-debug' + ]) + return True + diff --git a/sample/sample_1/LuaBankAccount.cpp b/sample/sample_1/LuaBankAccount.cpp index e5a7742..ab422d3 100644 --- a/sample/sample_1/LuaBankAccount.cpp +++ b/sample/sample_1/LuaBankAccount.cpp @@ -82,11 +82,12 @@ static luaL_Reg BankAccount_metatable[] = { }; int luaopen_BankAccount(luaWrapper:Lua& _lua) { - _lua.registerElement("BankAccount", - BankAccount_table, - BankAccount_metatable, - BankAccount_new // If your class has a default constructor you can omit this argument, LuaWrapper will generate a default allocator for you. - ); + luaWrapper::registerElement(_lua, + "BankAccount", + BankAccount_table, + BankAccount_metatable, + BankAccount_new + ); return 1; } diff --git a/sample/sample_2/LuaExample.cpp b/sample/sample_2/LuaExample.cpp index 5d3472f..5663110 100644 --- a/sample/sample_2/LuaExample.cpp +++ b/sample/sample_2/LuaExample.cpp @@ -133,7 +133,7 @@ static luaL_Reg Example_metatable[] = { }; int luaopen_Example(luaWrapper:Lua& _lua) { - _lua.registerElement("Example", NULL, Example_metatable); + luaWrapper::registerElement(_lua, "Example", NULL, Example_metatable); return 1; } diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..8d99692 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,24 @@ +/** + * @author Edouard DUPIN + * @copyright 2014, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ + +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc, const char *argv[]) { + // init test engine: + etest::init(argc, argv); + // init etk log system and file interface: + etk::init(argc, argv); + // Run all test with etest + return RUN_ALL_TESTS(); +} + diff --git a/test/testCCallLuaFunction.cpp b/test/testCCallLuaFunction.cpp new file mode 100644 index 0000000..3fa0af9 --- /dev/null +++ b/test/testCCallLuaFunction.cpp @@ -0,0 +1,25 @@ +/** + * @author Edouard DUPIN + * @copyright 2014, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ + +#include +#include + + +static etk::String refOutputBoolean1("{\n\t\"tmpElement\": true\n}\n"); + +TEST(TestCCallLuaFunctionn, basicCall) { + 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