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.cpp79
-rw-r--r--src/lua/common/je_lua.h54
-rw-r--r--src/lua/common/je_lua_error.h3
-rw-r--r--src/lua/common/je_lua_proxy.h11
-rw-r--r--src/lua/common/je_lua_reference.cpp18
-rw-r--r--src/lua/common/je_lua_reference.h3
6 files changed, 147 insertions, 21 deletions
diff --git a/src/lua/common/je_lua.cpp b/src/lua/common/je_lua.cpp
new file mode 100644
index 0000000..748606b
--- /dev/null
+++ b/src/lua/common/je_lua.cpp
@@ -0,0 +1,79 @@
+#include "je_lua.h"
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ ///
+ /// Lua objects table. Map object to proxy, like objects_table[object] = proxy.
+ ///
+ static const char* Jin_Lua_Objects_Table = "Jin_Objects_Table";
+
+ ///
+ /// Lua modules table. Map object to proxy, like modules_table[object] = module.
+ ///
+ static const char* Jin_Lua_Modules_Table = "Jin_Modules_Table";
+
+ ///
+ /// Lua references table. Map object to proxy, like references[object] = ref.
+ ///
+ static const char* Jin_Lua_Reference_Table = "Jin_Reference_Table";
+
+ Proxy* luax_newinstance(lua_State* L, const char* type, SharedBase* shared)
+ {
+ Proxy* proxy = static_cast<Proxy*>(luax_newinstance(L, type, sizeof(Proxy)));
+ if (shared) proxy->bind(shared);
+ luax_getobjectstable(L);
+ // Add to objects_table, like objects_table[shared] = proxy
+ luax_pushlightuserdata(L, shared);
+ luax_pushvalue(L, -3);
+ luax_settable(L, -3);
+ luax_pop(L, 1); // Pop objects table.
+ return proxy;
+ }
+
+ int luax_getobject(lua_State* L, SharedBase* shadred)
+ {
+ luax_getobjectstable(L);
+ luax_pushlightuserdata(L, shadred);
+ luax_gettable(L, -2);
+ luax_remove(L, -2); // Remove objects table on stack.
+ return 1;
+ }
+
+ int luax_getregistrytable(lua_State* L, const char* tbl)
+ {
+ luax_getfield(L, LUA_REGISTRYINDEX, tbl);
+ // If no such table, add one.
+ if (luax_isnil(L, -1) || !luax_istable(L, -1))
+ {
+ luax_pop(L, 1);
+ luax_newtable(L);
+ luax_pushstring(L, tbl);
+ luax_pushvalue(L, -2);
+ luax_settable(L, LUA_REGISTRYINDEX);
+ }
+ return 1;
+ }
+
+ int luax_getobjectstable(lua_State* L)
+ {
+ luax_getregistrytable(L, Jin_Lua_Objects_Table);
+ return 1;
+ }
+
+ int luax_getmodulestable(lua_State* L)
+ {
+ luax_getregistrytable(L, Jin_Lua_Modules_Table);
+ return 1;
+ }
+
+ int luax_getreferencestable(lua_State* L)
+ {
+ luax_getregistrytable(L, Jin_Lua_Reference_Table);
+ return 1;
+ }
+
+ }
+} \ No newline at end of file
diff --git a/src/lua/common/je_lua.h b/src/lua/common/je_lua.h
new file mode 100644
index 0000000..c345b2a
--- /dev/null
+++ b/src/lua/common/je_lua.h
@@ -0,0 +1,54 @@
+#ifndef __JE_LUA_H__
+#define __JE_LUA_H__
+#include "LuaJIT/lua.hpp"
+#include "libraries/luax/luax.h"
+
+#include "je_lua_proxy.h"
+#include "je_lua_reference.h"
+#include "je_lua_shared.hpp"
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ extern const char* Jin_Lua_Objects_Table;
+
+ extern const char* Jin_Lua_Modules_Table;
+
+ extern const char* Jin_Lua_Reference_Table;
+
+ ///
+ ///
+ ///
+ Proxy* luax_newinstance(lua_State* L, const char* type, SharedBase* shared);
+
+ ///
+ ///
+ ///
+ int luax_getobject(lua_State* L, SharedBase* shared);
+
+ ///
+ ///
+ ///
+ int luax_getobjectstable(lua_State* L);
+
+ ///
+ ///
+ ///
+ int luax_getmodulestable(lua_State* L);
+
+ ///
+ ///
+ ///
+ int luax_getreferencestable(lua_State* L);
+
+ ///
+ ///
+ ///
+ int luax_getregistrytable(lua_State* L, const char* tbl);
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/src/lua/common/je_lua_error.h b/src/lua/common/je_lua_error.h
index e0f7f35..bd5695d 100644
--- a/src/lua/common/je_lua_error.h
+++ b/src/lua/common/je_lua_error.h
@@ -3,8 +3,7 @@
#include <string.h>
-#include "LuaJIT/lua.hpp"
-#include "libraries/luax/luax.h"
+#include "common/je_lua.h"
namespace JinEngine
{
diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h
index 17e8da7..d6a6a0a 100644
--- a/src/lua/common/je_lua_proxy.h
+++ b/src/lua/common/je_lua_proxy.h
@@ -1,9 +1,6 @@
#ifndef __JIN_COMMON_PROXY_H
#define __JIN_COMMON_PROXY_H
-#include "LuaJIT/lua.hpp"
-#include "libraries/luax/luax.h"
-
#include "je_lua_shared.hpp"
namespace JinEngine
@@ -57,14 +54,6 @@ namespace JinEngine
};
- inline Proxy* luax_newinstance(lua_State* L, const char* type, SharedBase* shared = nullptr)
- {
- Proxy* proxy = static_cast<Proxy*>(luax_newinstance(L, type, sizeof(Proxy)));
- if(shared)
- proxy->bind(shared);
- return proxy;
- }
-
} // namespace Lua
} // namespace JinEngine
diff --git a/src/lua/common/je_lua_reference.cpp b/src/lua/common/je_lua_reference.cpp
index 05dc82b..37ed441 100644
--- a/src/lua/common/je_lua_reference.cpp
+++ b/src/lua/common/je_lua_reference.cpp
@@ -1,7 +1,6 @@
-#include "je_lua_reference.h"
+#include "common/je_lua.h"
-#include "LuaJIT/lua.hpp"
-#include "libraries/luax/luax.h"
+#include "je_lua_reference.h"
namespace JinEngine
{
@@ -12,7 +11,10 @@ namespace JinEngine
: mL(L)
{
luax_pushvalue(mL, i);
- mIndex = luax_ref(mL, LUA_REGISTRYINDEX);
+ luax_getreferencestable(L);
+ luax_pushvalue(mL, -2);
+ mIndex = luax_ref(mL, -2);
+ luax_pop(L, 3);
}
LuaRef::~LuaRef()
@@ -22,12 +24,16 @@ namespace JinEngine
void LuaRef::unref()
{
- luax_unref(mL, LUA_REGISTRYINDEX, mIndex);
+ luax_getreferencestable(mL);
+ luax_unref(mL, -1, mIndex);
+ luax_pop(mL, 1);
}
void LuaRef::push()
{
- luax_rawgeti(mL, LUA_REGISTRYINDEX, mIndex);
+ luax_getreferencestable(mL);
+ luax_rawgeti(mL, -1, mIndex);
+ luax_remove(mL, -2);
}
}
diff --git a/src/lua/common/je_lua_reference.h b/src/lua/common/je_lua_reference.h
index 84115a6..1a62dba 100644
--- a/src/lua/common/je_lua_reference.h
+++ b/src/lua/common/je_lua_reference.h
@@ -1,8 +1,7 @@
#ifndef __JIN_COMMON_REFERENCE_H
#define __JIN_COMMON_REFERENCE_H
-#include "LuaJIT/lua.hpp"
-#include "libraries/luax/luax.h"
+#include "common/je_lua.h"
namespace JinEngine
{