aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/common')
-rw-r--r--src/lua/common/je_lua.h12
-rw-r--r--src/lua/common/je_lua_object.cpp28
-rw-r--r--src/lua/common/je_lua_object.h12
-rw-r--r--src/lua/common/je_lua_runtime.cpp42
-rw-r--r--src/lua/common/je_lua_shared.hpp15
5 files changed, 51 insertions, 58 deletions
diff --git a/src/lua/common/je_lua.h b/src/lua/common/je_lua.h
index cacec95..74f9819 100644
--- a/src/lua/common/je_lua.h
+++ b/src/lua/common/je_lua.h
@@ -35,32 +35,32 @@ namespace JinEngine
///
/// Access lua object by object pointer.
///
- int luax_getobject(lua_State* L, Shared* shared);
+ int luax_getobject(lua_State* L, LuaObject* obj);
///
/// Get object's reference table.
///
- void luax_getreference(lua_State* L, Shared* shared);
+ void luax_getreference(lua_State* L, LuaObject* obj);
///
///
///
- bool luax_addreference(lua_State* L, Shared* shared, Shared* dep);
+ bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep);
///
///
///
- void luax_removereference(lua_State* L, Shared* shared);
+ void luax_removereference(lua_State* L, LuaObject* obj);
///
///
///
- void luax_removereference(lua_State* L, Shared* shared, Shared* dep);
+ void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep);
///
///
///
- void luax_removeobject(lua_State* L, Shared* shared);
+ void luax_removeobject(lua_State* L, LuaObject* obj);
///
///
diff --git a/src/lua/common/je_lua_object.cpp b/src/lua/common/je_lua_object.cpp
index 9aabeb5..c35356e 100644
--- a/src/lua/common/je_lua_object.cpp
+++ b/src/lua/common/je_lua_object.cpp
@@ -12,7 +12,7 @@ namespace JinEngine
{
shared = obj;
shared->retain();
- dependencies = new std::map<uint, Shared*>();
+ dependencies = new std::map<uint, LuaObject*>();
}
}
@@ -35,7 +35,7 @@ namespace JinEngine
const char* LuaObject::getObjectType()
{
- return shared->getType();
+ return type;
}
Shared* LuaObject::getShared()
@@ -43,11 +43,11 @@ namespace JinEngine
return shared;
}
- void LuaObject::setDependency(uint key, Shared* dep)
+ void LuaObject::setDependency(uint key, LuaObject* dep)
{
removeDependency(key);
- dependencies->insert(std::pair<uint, Shared*>(key, dep));
- luax_addreference(state, shared, dep);
+ dependencies->insert(std::pair<uint, LuaObject*>(key, dep));
+ luax_addreference(state, this, dep);
}
void LuaObject::removeDependency(uint key)
@@ -55,19 +55,19 @@ namespace JinEngine
if (!isDependOn(key))
return;
DepsMap::iterator it = dependencies->find(key);
- Shared* dep = it->second;
- luax_removereference(state, shared, dep);
+ LuaObject* dep = it->second;
+ luax_removereference(state, this, dep);
dependencies->erase(it);
}
- void LuaObject::removeDependency(Shared* dependency)
+ void LuaObject::removeDependency(LuaObject* dependency)
{
for (DepsMap::iterator it = dependencies->begin(); it != dependencies->end();)
{
- Shared* dep = it->second;
+ LuaObject* dep = it->second;
if (dep == dependency)
{
- luax_removereference(state, shared, dep);
+ luax_removereference(state, this, dep);
dependencies->erase(it);
}
else
@@ -80,9 +80,9 @@ namespace JinEngine
return dependencies->find(key) != dependencies->end();
}
- bool LuaObject::isDependOn(Shared* shared)
+ bool LuaObject::isDependOn(LuaObject* shared)
{
- for (std::pair<uint, Shared*> dep : (*dependencies))
+ for (std::pair<uint, LuaObject*> dep : (*dependencies))
{
if (dep.second == shared)
return true;
@@ -92,11 +92,11 @@ namespace JinEngine
void LuaObject::clearDependencies()
{
- luax_removereference(state, shared);
+ luax_removereference(state, this);
dependencies->clear();
}
- Shared* LuaObject::getDependency(uint key)
+ LuaObject* LuaObject::getDependency(uint key)
{
if (!isDependOn(key))
return nullptr;
diff --git a/src/lua/common/je_lua_object.h b/src/lua/common/je_lua_object.h
index a81f57c..6e86508 100644
--- a/src/lua/common/je_lua_object.h
+++ b/src/lua/common/je_lua_object.h
@@ -31,19 +31,19 @@ namespace JinEngine
return shared->getObject<T>();
}
- void setDependency(uint key, Shared* shared);
+ void setDependency(uint key, LuaObject* dep);
void removeDependency(uint key);
- void removeDependency(Shared* dep);
+ void removeDependency(LuaObject* dep);
bool isDependOn(uint key);
- bool isDependOn(Shared* shared);
+ bool isDependOn(LuaObject* shared);
void clearDependencies();
- Shared* getDependency(uint key);
+ LuaObject* getDependency(uint key);
int getDependenciesCount();
@@ -51,12 +51,14 @@ namespace JinEngine
// Lua state object.
//////////////////////////////////////////////////////////////////////////////////////////////////////
- using DepsMap = std::map<uint, Shared*>;
+ using DepsMap = std::map<uint, LuaObject*>;
lua_State* state;
Shared* shared;
+ const char* type;
+
DepsMap* dependencies;
};
diff --git a/src/lua/common/je_lua_runtime.cpp b/src/lua/common/je_lua_runtime.cpp
index d7069b8..fb665f1 100644
--- a/src/lua/common/je_lua_runtime.cpp
+++ b/src/lua/common/je_lua_runtime.cpp
@@ -23,6 +23,7 @@ namespace JinEngine
{
LuaObject* obj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject)));
obj->state = L;
+ obj->type = type;
obj->bind(shared);
// Add to objects_table, objects_table[shared] = luaObj
luax_getobjectstable(L);
@@ -46,10 +47,11 @@ namespace JinEngine
DepsMap& srcDeps = *src->dependencies;
for (DepsMap::iterator it = srcDeps.begin(); it != srcDeps.end(); ++it)
{
- Shared* shr = it->second;
+ LuaObject* obj = it->second;
+ Shared* shr = obj->shared;
// Try get lua object.
- luax_getobject(src->state, shr);
- LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, shr->getType());
+ luax_getobject(src->state, obj);
+ LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, obj->getObjectType());
luax_pop(src->state, 1); // Pop lua object.
luax_copyinstance(to, luaObj);
luax_pop(to, 1); // Pop reference object.
@@ -68,25 +70,25 @@ namespace JinEngine
DepsMap::iterator it = deps.begin();
for (; it != deps.end(); ++it)
{
- Shared* dep = it->second;
- luax_addreference(to, shr, dep);
+ LuaObject* dep = it->second;
+ luax_addreference(to, src, dep);
}
return obj;
}
- int luax_getobject(lua_State* L, Shared* shared)
+ int luax_getobject(lua_State* L, LuaObject* obj)
{
luax_getobjectstable(L);
- luax_pushlightuserdata(L, shared);
+ luax_pushlightuserdata(L, obj->shared);
luax_gettable(L, -2);
luax_remove(L, -2); // Remove objects table on stack.
return 1;
}
- void luax_removeobject(lua_State* L, Shared* shared)
+ void luax_removeobject(lua_State* L, LuaObject* obj)
{
luax_getobjectstable(L);
- luax_pushlightuserdata(L, shared);
+ luax_pushlightuserdata(L, obj->shared);
luax_pushnil(L);
luax_settable(L, -3);
luax_pop(L, 1);
@@ -152,29 +154,29 @@ namespace JinEngine
return 1;
}
- void luax_getreference(lua_State* L, Shared* shared)
+ void luax_getreference(lua_State* L, LuaObject* obj)
{
luax_getreferencestable(L);
- luax_pushlightuserdata(L, shared);
+ luax_pushlightuserdata(L, obj->shared);
luax_gettable(L, -2);
luax_remove(L, -2);
}
- bool luax_addreference(lua_State* L, Shared* shared, Shared* dep)
+ bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep)
{
- luax_getreference(L, shared);
+ luax_getreference(L, obj);
// If no dependencies table, add one.
if (luax_isnil(L, -1))
{
luax_pop(L, 1);
luax_getreferencestable(L);
luax_newtable(L);
- luax_pushlightuserdata(L, shared);
+ luax_pushlightuserdata(L, obj->shared);
luax_pushvalue(L, -2);
luax_settable(L, -4);
luax_remove(L, -2); // Remove references table.
}
- luax_pushlightuserdata(L, dep);
+ luax_pushlightuserdata(L, dep->shared);
luax_getobject(L, dep);
if (luax_isnil(L, -1))
{
@@ -186,24 +188,24 @@ namespace JinEngine
return true;
}
- void luax_removereference(lua_State* L, Shared* shared)
+ void luax_removereference(lua_State* L, LuaObject* obj)
{
luax_getreferencestable(L);
- luax_pushlightuserdata(L, shared);
+ luax_pushlightuserdata(L, obj->shared);
luax_pushnil(L);
luax_settable(L, -3);
luax_pop(L, 1);
}
- void luax_removereference(lua_State* L, Shared* shared, Shared* dep)
+ void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep)
{
- luax_getreference(L, shared);
+ luax_getreference(L, obj);
if (luax_isnil(L, -1))
{
luax_pop(L, 1);
return;
}
- luax_pushlightuserdata(L, dep);
+ luax_pushlightuserdata(L, dep->shared);
luax_pushnil(L);
luax_settable(L, -3);
luax_pop(L, 1);
diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp
index 55434c9..6a58ab3 100644
--- a/src/lua/common/je_lua_shared.hpp
+++ b/src/lua/common/je_lua_shared.hpp
@@ -20,10 +20,9 @@ namespace JinEngine
class Shared
{
public:
- Shared(Object* obj, const char* type)
+ Shared(Object* obj)
: mCount(0)
, mObject(obj)
- , mType(type)
{
}
@@ -37,16 +36,6 @@ namespace JinEngine
return static_cast<Object*>(mObject);
}
- inline const char* getType()
- {
- return mType;
- }
-
- inline bool isType(const char* t)
- {
- return strcmp(mType, t) == 0;
- }
-
template<class T>
inline T* getObject()
{
@@ -86,7 +75,7 @@ namespace JinEngine
Object* mObject;
int mCount;
- const char* mType;
+
};
} // namespace Lua