aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-11-22 12:23:12 +0800
committerchai <chaifix@163.com>2018-11-22 12:23:12 +0800
commit7a20483f06624e82feb129c9e4c8fa13881a6a9f (patch)
tree82707e7546fe14d1b781a2e15f65f6ef976ac322 /src/lua/common/je_lua.cpp
parent7256d216ecf154d6418c1f3b36dd58a2b69b7827 (diff)
*修改lua bind
Diffstat (limited to 'src/lua/common/je_lua.cpp')
-rw-r--r--src/lua/common/je_lua.cpp79
1 files changed, 79 insertions, 0 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