Lua/LuaClass: Add a default ToString implementation
Former-commit-id: ff6c9cf1d18d34ad8fd7b03c970eec2da5ba0947
This commit is contained in:
parent
9390ce35b3
commit
b6ea30f9c1
|
|
@ -86,6 +86,7 @@ namespace Nz
|
||||||
static int StaticGetterProxy(lua_State* state);
|
static int StaticGetterProxy(lua_State* state);
|
||||||
static int StaticMethodProxy(lua_State* state);
|
static int StaticMethodProxy(lua_State* state);
|
||||||
static int StaticSetterProxy(lua_State* state);
|
static int StaticSetterProxy(lua_State* state);
|
||||||
|
static int ToStringProxy(lua_State* state);
|
||||||
|
|
||||||
std::map<String, ClassFunc> m_methods;
|
std::map<String, ClassFunc> m_methods;
|
||||||
std::map<String, StaticFunc> m_staticMethods;
|
std::map<String, StaticFunc> m_staticMethods;
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,15 @@ namespace Nz
|
||||||
lua.PushString(m_info->name);
|
lua.PushString(m_info->name);
|
||||||
lua.SetField("__type");
|
lua.SetField("__type");
|
||||||
|
|
||||||
|
// In case a __tostring method is missing, add a default implementation returning the type
|
||||||
|
if (m_methods.find("__tostring") == m_methods.end())
|
||||||
|
{
|
||||||
|
// Define the Finalizer
|
||||||
|
lua.PushValue(1); // shared_ptr on UserData
|
||||||
|
lua.PushCFunction(ToStringProxy, 1);
|
||||||
|
lua.SetField("__tostring");
|
||||||
|
}
|
||||||
|
|
||||||
// Define the Finalizer
|
// Define the Finalizer
|
||||||
lua.PushValue(1);
|
lua.PushValue(1);
|
||||||
lua.PushCFunction(FinalizerProxy, 1);
|
lua.PushCFunction(FinalizerProxy, 1);
|
||||||
|
|
@ -486,6 +495,17 @@ namespace Nz
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
int LuaClass<T>::ToStringProxy(lua_State* state)
|
||||||
|
{
|
||||||
|
LuaInstance& lua = *LuaInstance::GetInstance(state);
|
||||||
|
|
||||||
|
std::shared_ptr<ClassInfo>& info = *static_cast<std::shared_ptr<ClassInfo>*>(lua.ToUserdata(lua.GetIndexOfUpValue(1)));
|
||||||
|
|
||||||
|
lua.PushString(info->name);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Lua/DebugOff.hpp>
|
#include <Nazara/Lua/DebugOff.hpp>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue