summaryrefslogtreecommitdiff
path: root/source/3rd-party/Luax/luax_state.inl
diff options
context:
space:
mode:
Diffstat (limited to 'source/3rd-party/Luax/luax_state.inl')
-rw-r--r--source/3rd-party/Luax/luax_state.inl140
1 files changed, 140 insertions, 0 deletions
diff --git a/source/3rd-party/Luax/luax_state.inl b/source/3rd-party/Luax/luax_state.inl
new file mode 100644
index 0000000..06d9350
--- /dev/null
+++ b/source/3rd-party/Luax/luax_state.inl
@@ -0,0 +1,140 @@
+namespace Luax
+{
+
+ ///
+ /// עṤעclass tabletype nameΪƿռϡע׶βԪȵNewõʱŻᡣ
+ ///
+ template<typename T>
+ void LuaxState::RegisterFactory()
+ {
+ lua_State* L = mState;
+ LuaxState& state = *this;
+
+ int top = lua_gettop(L); // namespace table
+ assert(lua_istable(L, top));
+
+ // class table
+ lua_newtable(L);
+ LuaxNativeClass<T>::RegisterLuaxClass(state);
+ LuaxNativeClass<T>::RegisterLuaxFactoryClass(state);
+ T::RegisterLuaxClass(state);
+
+ // TǷûעķ
+#define _assertmethod(I, NAME) \
+ GetField(I, NAME); \
+ assert(IsType(-1, LUA_TFUNCTION)); \
+ Pop();
+
+ _assertmethod(-1, "New");
+ //_assertmethod(-1, "__gc");
+
+#undef _assertmethod
+
+#if LUAX_ENABLE_NATIVE_EXTEND
+ // .Extend()
+ lua_pushvalue(state, -1); // class table
+ lua_pushcclosure(state, LuaxNativeClass<T>::l_ExtendFactory, 1);
+ lua_setfield(state, -2, "Extend");
+#endif
+
+ // class["__index"] = class
+ lua_pushvalue(state, -1); // class table
+ lua_setfield(state, -2, "__index");
+
+ LuaxNativeClass<T>::SetLuaxClassTableRef(state, -1);
+
+ cc8* type = T::GetLuaxFactoryName();
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+ //
+ T::RegisterLuaxPostprocess(state);
+ }
+
+ ///
+ /// Singleton
+ ///
+ template<typename T>
+ void LuaxState::RegisterSingleton()
+ {
+ lua_State* L = mState;
+ LuaxState& state = *this;
+
+ int top = lua_gettop(L); // namespace table
+ assert(lua_istable(L, top));
+
+ // class table.
+ lua_newtable(L);
+ LuaxNativeClass<T>::RegisterLuaxClass(state);
+ LuaxNativeClass<T>::RegisterLuaxSingletonClass(state);
+ T::RegisterLuaxClass(state);
+
+ LuaxNativeClass<T>::SetLuaxClassTableRef(state, -1);
+
+ lua_pushvalue(state, -1);
+ lua_setfield(state, -2, "__index");
+
+#if LUAX_ENABLE_NATIVE_EXTEND
+ // .Extend()
+ lua_pushvalue(state, -1); // class table
+ lua_pushcclosure(state, LuaxNativeClass<T>::l_ExtendSingleton, 1);
+ lua_setfield(state, -2, "Extend");
+#endif
+
+ cc8* type = T::GetLuaxSingletonName();
+ SetField(top, type);
+
+ // reset top
+ lua_settop(L, top);
+
+ //
+ T::RegisterLuaxPostprocess(state);
+ }
+
+ template<typename T>
+ void LuaxState::SetField(int idx, cc8* key, T value)
+ {
+ if (IsTableOrUserdata(idx))
+ {
+ idx = AbsIndex(idx);
+ this->Push(value);
+ lua_setfield(mState, idx, key);
+ }
+ }
+
+ template<typename T>
+ T LuaxState::GetField(int idx, cc8* key, T value)
+ {
+ GetField(idx, key);
+ T result = GetValue < T >(-1, value);
+ this->Pop();
+
+ return result;
+ }
+
+ template<typename T>
+ T LuaxState::GetField(int idx, int key, T value)
+ {
+ GetField(idx, key);
+ T result = GetValue < T >(-1, value);
+ Pop();
+
+ return result;
+ }
+
+ template<typename T>
+ T* LuaxState::GetLuaUserdata(int idx)
+ {
+ void* p = nullptr;
+
+ if (IsType(idx, LUA_TUSERDATA))
+ {
+ p = *(void**)lua_touserdata(mState, idx);
+ }
+
+ return static_cast<T*>(p);
+ }
+
+} \ No newline at end of file