summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/LuaBindState.inc
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Scripting/LuaBindState.inc')
-rw-r--r--Runtime/Scripting/LuaBindState.inc180
1 files changed, 180 insertions, 0 deletions
diff --git a/Runtime/Scripting/LuaBindState.inc b/Runtime/Scripting/LuaBindState.inc
new file mode 100644
index 0000000..b2692cb
--- /dev/null
+++ b/Runtime/Scripting/LuaBindState.inc
@@ -0,0 +1,180 @@
+namespace LuaBind
+{
+
+ ///
+ /// 注册工厂,注册class table,以type name为键设置在名称空间上。在注册阶段不会设置元表,等到New方法调用的时候才会。
+ ///
+ template<class TYPE>
+ void LuaBindState::RegisterFactory()
+ {
+ cc8* type = TYPE::GetLuaBindFactoryName();
+
+ lua_State* L = mState;
+ LuaBindState& state = *this;
+
+ int top = lua_gettop(L); // namespace table
+ assert(lua_istable(L, top));
+
+ // class table
+ lua_newtable(L);
+ TYPE::RegisterLuaBindClassShared(state);
+ TYPE::RegisterLuaBindFactoryClass(state);
+ TYPE::RegisterLuaBindClass(state);
+
+ // 检测TYPE里面是否没有注册必须的方法
+#define _assertmethod(I, NAME) \
+ GetField(I, NAME); \
+ assert(IsType(-1, LUA_TFUNCTION)); \
+ Pop();
+
+ //_assertmethod(-1, "New");
+
+#undef _assertmethod
+
+#if LUA_BIND_ENABLE_NATIVE_EXTEND
+ // .Extend()
+ lua_pushvalue(state, -1); // class table
+ lua_pushcclosure(state, TYPE::_ExtendFactory, 1);
+ lua_setfield(state, -2, "Extend");
+#endif
+
+ // class["__index"] = class
+ lua_pushvalue(state, -1); // class table
+ lua_setfield(state, -2, "__index");
+
+ TYPE::SetLuaBindClassTableRef(state, -1);
+
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+ // 后处理
+ TYPE::RegisterLuaBindPostprocess(state);
+ }
+
+ ///
+ /// Singleton
+ ///
+ template<typename TYPE>
+ void LuaBindState::RegisterSingleton()
+ {
+ lua_State* L = mState;
+ LuaBindState& state = *this;
+
+ int top = lua_gettop(L); // namespace table
+ assert(lua_istable(L, top));
+
+ // class table.
+ lua_newtable(L);
+ TYPE::RegisterLuaBindClassShared(state);
+ TYPE::RegisterLuaBindSingletonClass(state);
+ TYPE::RegisterLuaBindClass(state);
+
+ TYPE::SetLuaBindClassTableRef(state, -1);
+
+ lua_pushvalue(state, -1);
+ lua_setfield(state, -2, "__index");
+
+#if LUA_BIND_ENABLE_NATIVE_EXTEND
+ // .Extend()
+ lua_pushvalue(state, -1); // class table
+ lua_pushcclosure(state, TYPE::_ExtendSingleton, 1);
+ lua_setfield(state, -2, "Extend");
+#endif
+
+ cc8* type = TYPE::GetLuaBindSingletonName();
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+ // 后处理
+ TYPE::RegisterLuaBindPostprocess(state);
+ }
+
+ template<typename TYPE>
+ void LuaBindState::SetField(int idx, cc8* key, TYPE value)
+ {
+ if (IsTableOrUserdata(idx))
+ {
+ idx = AbsIndex(idx);
+ this->Push(value);
+ lua_setfield(mState, idx, key);
+ }
+ }
+
+ template<typename TYPE>
+ void LuaBindState::SetFieldByIndex(int idx, int key, TYPE value)
+ {
+ if (IsTableOrUserdata(idx))
+ {
+ idx = AbsIndex(idx);
+ this->Push(value);
+ lua_rawseti(mState, idx, key);
+ }
+ }
+
+ template<typename TYPE>
+ TYPE LuaBindState::GetField(int idx, cc8* key, TYPE value)
+ {
+ GetField(idx, key);
+ TYPE result = GetValue < TYPE >(-1, value);
+ this->Pop();
+
+ return result;
+ }
+
+ template<typename TYPE>
+ TYPE LuaBindState::GetField(int idx, int key, TYPE value)
+ {
+ GetField(idx, key);
+ TYPE result = GetValue < TYPE >(-1, value);
+ Pop();
+
+ return result;
+ }
+
+ template<typename TYPE>
+ TYPE* LuaBindState::GetUserdata(int idx)
+ {
+ void* p = nullptr;
+
+ if (IsType(idx, LUA_TUSERDATA))
+ {
+ p = *(void**)lua_touserdata(mState, idx);
+ }
+
+ return static_cast<TYPE*>(p);
+ }
+
+ template<typename TYPE>
+ TYPE* LuaBindState::CheckUserdata(int idx)
+ {
+ if (IsType(idx, LUA_TUSERDATA))
+ {
+ if (lua_getmetatable(mState, idx)) // ref table
+ {
+ if (lua_getmetatable(mState, -1)) // member table
+ {
+ if (lua_getmetatable(mState, -1)) // class table
+ {
+ TYPE::PushLuaBindClassTable(*this); // target class table
+ if (lua_rawequal(mState, -1, -2))
+ {
+ Pop(4); // ref\member\class\target class
+ TYPE* udata = GetUserdata<TYPE>(idx);
+ return udata; // userdata
+ }
+ Pop(2); // target class table\class table
+ }
+ Pop(1); // member table
+ }
+ Pop(1); // ref table
+ }
+ }
+ luaL_typerror(mState, idx, TYPE::GetLuaBindClassName());
+ return nullptr;
+ }
+
+} \ No newline at end of file