From 45328cbadd8a946c19a77301f218efbf650e2f28 Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Mon, 18 Oct 2021 19:56:41 +0800
Subject: *misc

---
 Runtime/Lua/LuaBind/LuaBindState.h | 266 +++++++++++++++++++++++++++++++++++++
 1 file changed, 266 insertions(+)
 create mode 100644 Runtime/Lua/LuaBind/LuaBindState.h

(limited to 'Runtime/Lua/LuaBind/LuaBindState.h')

diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h
new file mode 100644
index 0000000..776b23d
--- /dev/null
+++ b/Runtime/Lua/LuaBind/LuaBindState.h
@@ -0,0 +1,266 @@
+#ifndef __LUA_BIND_STATE_H__
+#define __LUA_BIND_STATE_H__
+
+#include <string>
+
+#include "LuaBindConfig.h"
+#include "LuaBindRefTable.h"
+#include "LuaBindGlobalState.h"
+
+namespace LuaBind
+{
+
+	class VM;
+	class Enum;
+	class StrongRef;
+	class WeakRef;
+
+	// ��lua_State�Ĵ��������˱���һ��lua_State�����ò������������ݡ�һ��ʵ����metatable���£�
+	//     class table
+	//     member table
+	//     ref table
+	//     userdata
+	// ��userdataͨ��getmetatable��ȡ�ϼ�metatable������֮�⻹��һ��class tableע���ڶ�Ӧ
+	// �����ƿռ��
+	LUA_BIND_API class State 
+	{
+	public:
+
+		State(lua_State* state);
+		State(const State& state);
+		virtual ~State();
+
+		inline lua_State* operator ->() { return mState; };
+		inline lua_State& operator *() { return *mState; };
+		inline operator lua_State*() { return mState; }
+		inline operator bool() { return mState != nullptr; };
+
+		// ��ȡ�󶨵�lua_State
+		inline lua_State* GetHandle() { return mState; };
+
+		global_State* GetGlobalState();
+
+		VM* GetVM();
+
+		//------------------------------------------------------------------------------//
+
+		void OpenLibs();
+
+		//------------------------------------------------------------------------------//
+		// ���ƿռ���������ƿռ����һ������_G��������ı�
+
+		void PushGlobalNamespace();
+		void PushNamespace(cc8* name);
+		void PopNamespace();
+		bool IsNamespace(int idx);
+		
+		//------------------------------------------------------------------------------//
+
+		void SetTop(int top);
+		int GetTop();
+		bool CheckParams(int idx, cc8* format);
+		int AbsIndex(int idx);
+		void Call(int nArgs, int nResults);
+
+		//------------------------------------------------------------------------------//
+
+		void GetField(int idx, cc8* name);
+		void GetField(int idx, int key);
+		std::string	GetField(int idx, cc8* key, cc8* value);
+		std::string GetField(int idx, int key, cc8* value);
+		std::string	GetField(int idx, cc8* key, const std::string& value);
+		std::string GetField(int idx, int key, const std::string& value);
+		bool GetFieldWithType(int idx, cc8* name, int type);
+		bool GetFieldWithType(int idx, int key, int type);
+		bool GetSubfieldWithType(int idx, cc8* format, int type, ...);
+		static cc8* GetLuaTypeName(int type);
+
+		void SetField(int idx, cc8* key);
+
+		bool IsNil(int idx);
+		bool IsNilOrNone(int idx);
+		bool IsTableOrUserdata(int idx);
+		bool IsTrueOrNotNil(int idx);
+		bool IsType(int idx, int type);
+		bool IsType(int idx, cc8* name, int type);
+		bool IsValid();
+
+		bool HasField(int idx, cc8* name);
+		bool HasField(int idx, int key);
+		bool HasField(int idx, cc8* name, int type);
+		bool HasField(int idx, int name, int type);
+		bool HasKeys(int idx);
+
+		void PushNil();
+		void Push(bool value);
+		void Push(cc8* value);
+		void Push(double value);
+		void Push(float value);
+		void Push(int value);
+		void Push(u16 value);
+		void Push(u32 value);
+		void Push(u64 value);
+		void Push(s64 value);
+		void Push(uintptr value);
+		void Push(lua_CFunction value);
+		void Push(void* data, size_t size);
+		void Push(const void* value);
+		void Push(std::string value);
+		
+		// ��idx��ʼ��n��push��ջ����idx�ᱻȡ����n����������
+		void PushValues(int idx, int n);
+
+		// ��void** ����ʽ����userdata������ֵ����Ϊptr
+		void PushPtrUserdata(void* ptr);
+
+		void Pop(int n = 1);
+
+		void Settop(int idx);
+
+		template<typename T> T* GetUserdata(int idx = 1);
+
+		//------------------------------------------------------------------------------//
+
+		int ErrorType(int idx, cc8* hint);
+
+		//------------------------------------------------------------------------------//
+
+		template<typename T> T GetValue(int idx, T default_value);
+		template<typename T> T GetField(int idx, int key, T value);
+		template<typename T> T GetField(int idx, cc8* key, T value);
+		template<typename T> void SetField(int idx, cc8* key, T value);
+		template<typename T> void SetFieldByIndex(int idx, int key, T value);
+
+		template<typename T> T* CheckUserdata(int idx);
+		template<typename T> T CheckValue(int idx);
+
+		//------------------------------------------------------------------------------//
+
+		void DoString(const std::string& code);
+		void DoFile(const std::string& file);
+
+		//------------------------------------------------------------------------------//
+		// ע�᷽��
+
+		// ע�Ṥ������������ͨ�࣬��New����
+		template<class TYPE> void RegisterFactory();
+
+		// ע�ᵥ����û��New����
+		template<class TYPE> void RegisterSingleton();
+
+		// ע��ö��
+		void RegisterEnum(cc8* name, Enum* enums);
+
+		// ע��C������ע������һ��{0�� 0}
+		void RegisterMethods(const luaL_Reg *l);
+
+		// ע�ᵥ��C����
+		void RegisterMethod(cc8* fname, lua_CFunction func);
+
+		// ��preloader�ӵ�package.preload���require"libname"ʱlua��loader_preload����
+		// libname�ҵ�preloaderֱ�Ӽ��ء�����ʵ����Ҫrequire��ʱ��ż��أ����Ҽ��ع�һ�κ�
+		// package.loaded��¼�������´β����ټ��ء�ͨ��require��������preloader��
+		void RegisterPreloader(cc8* libname, lua_CFunction preloader);
+
+		// ����luaL_Reg����lib table������_G��package.loaded������libname��������ָ��lib table��
+		void RegisterLib(cc8* libname, const luaL_Reg* l);
+
+#if LUA_BIND_ENABLE_PLAIN_CLASS
+		// ע�ᴿlua���ע�ắ��������������lua�ࡣ
+		void RegisterPlainClassRegistry(cc8* name);
+#endif
+
+#if LUA_BIND_ENABLE_PLAIN_ENUM
+		// ע�ᴿlua��ö�٣��Է�ֹ�޸�ö��ֵ��
+		void RegisterPlainEnumRegistry(cc8* name);
+#endif
+
+	protected:
+
+		friend class VM;
+
+		// ���ζ�State�ĵ�ַ��ز���
+		void* operator &();
+		void* operator new(size_t size);
+
+		lua_State* const mState;
+	};
+
+	//--------------------------------------------------------------------------------//
+	// GetValue()ģ���ػ�
+
+	template <> bool State::GetValue < bool >(int idx, const bool value);
+	template <> cc8* State::GetValue < cc8* >(int idx, const cc8* value);
+	template <> double State::GetValue < double >(int idx, const double value);
+	template <> float State::GetValue < float >(int idx, const float value);
+	template <> s8 State::GetValue < s8 >(int idx, const s8 value);
+	template <> s16 State::GetValue < s16 >(int idx, const s16 value);
+	template <> s32 State::GetValue < s32 >(int idx, const s32 value);
+	template <> s64 State::GetValue < s64 >(int idx, const s64 value);
+	template <> u8 State::GetValue < u8 >(int idx, const u8 value);
+	template <> u16 State::GetValue < u16 >(int idx, const u16 value);
+	template <> u32 State::GetValue < u32 >(int idx, const u32 value);
+	template <> u64	 State::GetValue < u64 >(int idx, const u64 value);
+	template <> std::string State::GetValue < std::string >(int idx, const std::string value);
+	template <> const void* State::GetValue < const void* >(int idx, const void* value);
+
+	//--------------------------------------------------------------------------------//
+	// CheckValueģ���ػ�
+
+	template <> bool State::CheckValue < bool >(int idx);
+	template <> cc8* State::CheckValue < cc8* >(int idx);
+	template <> double State::CheckValue < double >(int idx);
+	template <> float State::CheckValue < float >(int idx);
+	template <> s8 State::CheckValue < s8 >(int idx);
+	template <> s16 State::CheckValue < s16 >(int idx);
+	template <> s32 State::CheckValue < s32 >(int idx);
+	template <> s64 State::CheckValue < s64 >(int idx);
+	template <> u8 State::CheckValue < u8 >(int idx);
+	template <> u16 State::CheckValue < u16 >(int idx);
+	template <> u32 State::CheckValue < u32 >(int idx);
+	template <> u64	 State::CheckValue < u64 >(int idx);
+	template <> std::string State::CheckValue < std::string >(int idx);
+	template <> const void* State::CheckValue < const void* >(int idx);
+
+	// �ڳ�Ա�����ﴴ��State���Բ������м�顣
+#define LUA_BIND_SETUP(L, params) \
+	LuaBind::State state(L); \
+	if(!state.CheckParams(1, params)) return 0
+
+#define LUA_BIND_STATE(L) \
+	LuaBind::State state(L)
+
+	//--------------------------------------------------------------------------------//
+
+	// ȷ������ȫ��lua�����ܹ��ڵ���֮�󷵻ص����stack״̬��
+	class ScopedState
+		: public State
+	{
+	public:
+		ScopedState(lua_State* state)
+			: State(state)
+		{
+			mRestoreTop = lua_gettop(mState);
+		}
+		ScopedState(const State& state)
+			: State(state)
+		{
+			mRestoreTop = lua_gettop(mState);
+		}
+		~ScopedState()
+		{
+			if (mState) {
+				if (lua_gettop(mState) != mRestoreTop) {
+					lua_settop(mState, mRestoreTop);
+				}
+			}
+		}
+	private: 
+		void* operator new(size_t);
+		int mRestoreTop;
+
+	};
+
+}
+
+#endif
\ No newline at end of file
-- 
cgit v1.1-26-g67d0