Lua/LuaClass: Don't remove instance from the Lua stack

The instance Lua entry now remains as the first index, shifting all the
parameters indexes by 1
This commit is contained in:
Lynix
2016-10-27 11:22:35 +02:00
parent c58ec94e2d
commit 5ffc5a8784
11 changed files with 171 additions and 155 deletions

View File

@@ -26,7 +26,7 @@ namespace Nz
friend class LuaClass;
public:
using ClassFunc = std::function<int(LuaInstance& lua, T& instance)>;
using ClassFunc = std::function<int(LuaInstance& lua, T& instance, std::size_t argumentCount)>;
using ClassIndexFunc = std::function<bool(LuaInstance& lua, T& instance)>;
using ConstructorFunc = std::function<bool(LuaInstance& lua, T* instance, std::size_t argumentCount)>;
template<typename P> using ConvertToParent = std::function<P*(T*)>;

View File

@@ -85,7 +85,7 @@ namespace Nz
// Let's create the metatable which will be associated with every instance.
SetupMetatable(lua);
if (m_info->constructor || m_info->staticGetter || m_info->staticSetter || !m_info->staticMethods.empty())
if (m_info->constructor || m_info->staticGetter || m_info->staticSetter || !m_staticMethods.empty())
SetupGlobalTable(lua);
lua.Pop(); // Pop our ClassInfo, which is now referenced by all our functions
@@ -127,7 +127,7 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArgs(lua);
@@ -141,7 +141,7 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArgs(lua);
@@ -155,7 +155,7 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArgs(lua);
@@ -169,7 +169,7 @@ namespace Nz
{
typename LuaImplMethodProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
BindMethod(name, [func, handler] (LuaInstance& lua, T& object) -> int
BindMethod(name, [func, handler] (LuaInstance& lua, T& object, std::size_t /*argumentCount*/) -> int
{
handler.ProcessArgs(lua);
@@ -449,7 +449,7 @@ namespace Nz
{
// Query from the metatable
lua.GetMetatable(info->name); //< Metatable
lua.PushValue(1); //< Field
lua.PushValue(2); //< Field
lua.GetTable(); // Metatable[Field]
lua.Remove(-2); // Remove Metatable
@@ -476,7 +476,6 @@ namespace Nz
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
T* instance = static_cast<T*>(lua.CheckUserdata(1, info->name));
lua.Remove(1); //< Remove the instance from the Lua stack
Get(info, lua, instance);
return 1;
@@ -501,8 +500,6 @@ namespace Nz
instance = it->second(lua);
}
lua.Pop(2);
lua.Remove(1); //< Remove the instance from the Lua stack
}
if (!instance)
@@ -511,9 +508,11 @@ namespace Nz
return 0;
}
std::size_t argCount = lua.GetStackTop() - 1U;
unsigned int index = static_cast<unsigned int>(lua.ToInteger(lua.GetIndexOfUpValue(2)));
const ClassFunc& method = info->methods[index];
return method(lua, *instance);
return method(lua, *instance, argCount);
}
template<class T>
@@ -525,7 +524,6 @@ namespace Nz
const ClassIndexFunc& setter = info->setter;
T& instance = *static_cast<T*>(lua.CheckUserdata(1, info->name));
lua.Remove(1); //< Remove the instance from the Lua stack
if (!setter(lua, instance))
{

View File

@@ -209,6 +209,24 @@ namespace Nz
return LuaImplReplyVal(instance, val, TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
std::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag<const T&>)
{
return LuaImplReplyVal(instance, std::move(val), TypeTag<T>());
}
template<typename T>
int LuaImplReplyVal(const LuaInstance& instance, T&& val, TypeTag<T&&>)
{
return LuaImplReplyVal(instance, std::forward<T>(val), TypeTag<T>());
}
inline int LuaImplReplyVal(const LuaInstance& instance, std::string&& val, TypeTag<std::string>)
{
instance.PushString(val.c_str(), val.size());
@@ -375,7 +393,7 @@ namespace Nz
void ProcessArgs(const LuaInstance& instance) const
{
m_index = 1;
m_index = 2; //< 1 being the instance
ProcessArgs<0, Args...>(instance);
}