Optimize lua object pushing
This commit is contained in:
		
							parent
							
								
									3b345cf868
								
							
						
					
					
						commit
						773d58da01
					
				| 
						 | 
				
			
			@ -23,11 +23,11 @@
 | 
			
		|||
#include "luaobject.h"
 | 
			
		||||
#include "luainterface.h"
 | 
			
		||||
 | 
			
		||||
#include <typeinfo>
 | 
			
		||||
#include <framework/core/application.h>
 | 
			
		||||
 | 
			
		||||
LuaObject::LuaObject() :
 | 
			
		||||
    m_fieldsTableRef(-1),
 | 
			
		||||
    m_metatableRef(-1)
 | 
			
		||||
    m_fieldsTableRef(-1)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -37,11 +37,6 @@ LuaObject::~LuaObject()
 | 
			
		|||
    assert(!g_app.isTerminated());
 | 
			
		||||
#endif
 | 
			
		||||
    releaseLuaFieldsTable();
 | 
			
		||||
 | 
			
		||||
    if(m_metatableRef != -1) {
 | 
			
		||||
        g_lua.unref(m_metatableRef);
 | 
			
		||||
        m_metatableRef = -1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool LuaObject::hasLuaField(const std::string& field)
 | 
			
		||||
| 
						 | 
				
			
			@ -91,13 +86,19 @@ void LuaObject::luaGetField(const std::string& key)
 | 
			
		|||
 | 
			
		||||
void LuaObject::luaGetMetatable()
 | 
			
		||||
{
 | 
			
		||||
    if(m_metatableRef == -1) {
 | 
			
		||||
        // set the userdata metatable
 | 
			
		||||
        g_lua.getGlobal(stdext::format("%s_mt", getClassName()));
 | 
			
		||||
        m_metatableRef = g_lua.ref();
 | 
			
		||||
    }
 | 
			
		||||
    static std::unordered_map<const std::type_info*, int> metatableMap;
 | 
			
		||||
    const std::type_info& tinfo = typeid(*this);
 | 
			
		||||
    auto it = metatableMap.find(&tinfo);
 | 
			
		||||
 | 
			
		||||
    g_lua.getRef(m_metatableRef);
 | 
			
		||||
    int metatableRef;
 | 
			
		||||
    if(it == metatableMap.end()) {
 | 
			
		||||
        g_lua.getGlobal(getClassName() + "_mt");
 | 
			
		||||
        metatableRef = g_lua.ref();
 | 
			
		||||
        metatableMap[&tinfo] = metatableRef;
 | 
			
		||||
    } else
 | 
			
		||||
        metatableRef = it->second;
 | 
			
		||||
 | 
			
		||||
    g_lua.getRef(metatableRef);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LuaObject::luaGetFieldsTable()
 | 
			
		||||
| 
						 | 
				
			
			@ -112,3 +113,9 @@ int LuaObject::getUseCount()
 | 
			
		|||
{
 | 
			
		||||
    return ref_count();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string LuaObject::getClassName()
 | 
			
		||||
{
 | 
			
		||||
    // TODO: this could be cached for more performance
 | 
			
		||||
    return stdext::demangle_name(typeid(*this).name());
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,7 +24,6 @@
 | 
			
		|||
#define LUAOBJECT_H
 | 
			
		||||
 | 
			
		||||
#include "declarations.h"
 | 
			
		||||
#include <typeinfo>
 | 
			
		||||
 | 
			
		||||
/// LuaObject, all script-able classes have it as base
 | 
			
		||||
// @bindclass
 | 
			
		||||
| 
						 | 
				
			
			@ -76,10 +75,7 @@ public:
 | 
			
		|||
    int getUseCount();
 | 
			
		||||
 | 
			
		||||
    /// Returns the derived class name, its the same name used in Lua
 | 
			
		||||
    std::string getClassName() {
 | 
			
		||||
        // TODO: this could be cached for more performance
 | 
			
		||||
        return stdext::demangle_name(typeid(*this).name());
 | 
			
		||||
    }
 | 
			
		||||
    std::string getClassName();
 | 
			
		||||
 | 
			
		||||
    LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -87,7 +83,6 @@ public:
 | 
			
		|||
 | 
			
		||||
private:
 | 
			
		||||
    int m_fieldsTableRef;
 | 
			
		||||
    int m_metatableRef;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#include "luainterface.h"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,6 +25,9 @@
 | 
			
		|||
#include <framework/luaengine/luainterface.h>
 | 
			
		||||
#include <otclient/otclient.h>
 | 
			
		||||
 | 
			
		||||
#include "otclient/item.h"
 | 
			
		||||
#include "otclient/tile.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc, const char* argv[])
 | 
			
		||||
{
 | 
			
		||||
    std::vector<std::string> args(argv, argv + argc);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -127,7 +127,7 @@ private:
 | 
			
		|||
    uint8 m_countOrSubType;
 | 
			
		||||
    stdext::packed_storage<uint8> m_attribs;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#pragma pack(pop)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue