diff options
| author | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 | 
|---|---|---|
| committer | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 | 
| commit | 45328cbadd8a946c19a77301f218efbf650e2f28 (patch) | |
| tree | 8ec4f3a9737b2cbb9744f8347a56783743be2a4c /Runtime/LuaBind/LuaBindState.cpp | |
| parent | b5702ece4c2cf751c252e76fb885a7ec41ccabe8 (diff) | |
*misc
Diffstat (limited to 'Runtime/LuaBind/LuaBindState.cpp')
| -rw-r--r-- | Runtime/LuaBind/LuaBindState.cpp | 893 | 
1 files changed, 0 insertions, 893 deletions
| diff --git a/Runtime/LuaBind/LuaBindState.cpp b/Runtime/LuaBind/LuaBindState.cpp deleted file mode 100644 index 4ee87f4..0000000 --- a/Runtime/LuaBind/LuaBindState.cpp +++ /dev/null @@ -1,893 +0,0 @@ -#include "LuaBindEnum.h" -#include "LuaBindState.h" -#include "LuaBindVM.h" -#include "LuaBindClass.hpp" -#include "LuaBindInternal.h" - -#include <string> - -namespace LuaBind -{ -	 -	State::State(lua_State* state) -		: mState(state)  -	{ -		assert(state); -	} - -	State::State(const State& state) -		: mState(state.mState) -	{ -		assert(state.mState); -	} - -	State::~State() -	{ -	} - -	void State::OpenLibs() -	{ -		luaL_openlibs(mState); -	} - -	global_State* State::GetGlobalState() -	{ -		return G(mState); -	} - -	VM* State::GetVM() -	{ -		return VM::TryGetVM(G(mState)); -	} - -	void State::PushGlobalNamespace() -	{ -#if false -		int top = GetTop(); - -		lua_newtable(mState); // pseudo namespace table  -		int pnt = GetTop(); - -		lua_newtable(mState); // metatable  -		int mt = GetTop(); - -		// __index = _G -		// __newindex = _G -		lua_pushvalue(mState, LUA_GLOBALSINDEX); -		lua_pushvalue(mState, LUA_GLOBALSINDEX); -		lua_setfield(mState, mt, "__index"); -		lua_setfield(mState, mt, "__newindex"); - -		lua_setmetatable(mState, pnt); - -		// stack:  -		// -1 pseudo global namespace -#else  -		lua_pushvalue(mState, LUA_GLOBALSINDEX); -#endif -	} - -	void State::PushNamespace(cc8* name) -	{ -		assert(IsNamespace(-1)); - -		int top = GetTop(); - -		lua_getfield(mState, -1, name); -		if (lua_isnil(mState, -1)) -		{ -			lua_pop(mState, 1); - -			lua_newtable(mState); -			lua_pushvalue(mState, -1); -			lua_setfield(mState, top, name); -		} - -		// stack: -		// -1 namespace -	} - -	void State::PopNamespace() -	{ -		assert(lua_istable(mState, -1)); -		lua_pop(mState, 1); -	} - -	bool State::IsNamespace(int idx) -	{ -		return lua_istable(mState, idx); -	} - -	void State::DoString(const std::string& code) -	{ -		luaL_dostring(mState, code.c_str()); -	} - -	void State::DoFile(const std::string & path) -	{ -		luaL_dofile(mState, path.c_str()); -	} - -	int State::AbsIndex(int idx) -	{ -/* -#define abs_index(mState, i)		((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ -					lua_gettop(mState) + (i) + 1) -*/ -		if (idx < 0) { -			//return lua_gettop(mState) + idx + 1; -			return 	((idx) > 0 || (idx) <= LUA_REGISTRYINDEX ? (idx) : \ -				lua_gettop(mState) + (idx)+1); -		} -		return idx; -	} - -	void State::Call(int nArgs, int nResults) -	{ -		lua_pcall(mState, nArgs, nResults, 0); -	} - -	void State::PushNil()  -	{ -		lua_pushnil(mState); -	} - -	void State::Push(bool value)  -	{ -		lua_pushboolean(mState, value ? 1 : 0); -	} - -	void State::Push(cc8* value)  -	{ -		lua_pushstring(mState, value); -	} - -	void State::Push(double value)  -	{ -		lua_pushnumber(mState, value); -	} - -	void State::Push(float value)  -	{ -		lua_pushnumber(mState, value); -	} - -	void State::Push(int value)  -	{ -		lua_pushnumber(mState, value); -	} - -	void State::Push(u16 value)  -	{ -		lua_pushnumber(mState, value); -	} - -	void State::Push(u32 value)  -	{ -		lua_pushnumber(mState, value); -	} - -	void State::Push(u64 value)  -	{ -		lua_pushnumber(mState, (double)value); -	} - -	void State::Push(s64 value) -	{ -		lua_pushinteger(mState, value); -	} - -	void State::Push(uintptr value)  -	{ -		lua_pushlightuserdata(mState, (void*)value); -	} - -	void State::Push(lua_CFunction value)  -	{ -		lua_pushcfunction(mState, value); -	} - -	void State::Push(void* data, size_t size) -	{ -		lua_pushlstring(mState, (cc8*)data, size); -	} - -	void State::Push(const void* value) -	{ -		lua_pushlightuserdata(mState, (void*)value); -	} - -	void State::Push(std::string value) -	{ -		Push(value.c_str()); -	} - -	void State::PushValues(int idx, int n) -	{ -		idx = AbsIndex(idx); -		for (int i = idx; i < idx + n; ++i) -			lua_pushvalue(mState, i); -	} - -	void State::Pop(int n /* = 1 */) -	{ -		lua_pop(mState, n); -	} - -	bool State::IsNil(int idx)  -	{ -		return lua_isnil(mState, idx); -	} - -	bool State::IsNilOrNone(int idx)  -	{ -		int t = lua_type(mState, idx); -		return ((t == LUA_TNONE) || (t == LUA_TNIL)); -	} - -	bool State::IsTableOrUserdata(int idx)  -	{ -		int check = lua_type(mState, idx); -		return ((check == LUA_TTABLE) || (check == LUA_TUSERDATA)); -	} - -	bool State::IsTrueOrNotNil(int idx)  -	{ -		if (lua_isboolean(mState, idx)) { -			return lua_toboolean(mState, idx) ? true : false; -		} -		return !lua_isnil(mState, idx); -	} - -	bool State::IsType(int idx, int type)  -	{ -		return (lua_type(mState, idx) == type); -	} - -	bool State::IsType(int idx, cc8* name, int type)  -	{ -		return this->HasField(idx, name, type); -	} - -	bool State::IsValid()  -	{ -		return (mState != 0); -	} - -	void State::Settop(int idx) -	{ -		lua_settop(mState, idx); -	} - -	int State::GetTop() -	{ -		return lua_gettop(mState); -	} - -	bool State::HasField(int idx, cc8* name) { - -		lua_getfield(mState, idx, name); -		bool hasField = (lua_isnil(mState, -1) == false); -		lua_pop(mState, 1); - -		return hasField; -	} - -	bool State::HasField(int idx, int key) { - -		this->GetField(idx, key); -		bool hasField = (lua_isnil(mState, -1) == false); -		lua_pop(mState, 1); - -		return hasField; -	} - -	bool State::HasField(int idx, cc8* name, int type) { - -		lua_getfield(mState, idx, name); -		bool hasField = (lua_type(mState, -1) == type); -		lua_pop(mState, 1); - -		return hasField; -	} - -	bool State::HasField(int idx, int key, int type) { - -		this->GetField(idx, key); -		bool hasField = (lua_type(mState, -1) == type); -		lua_pop(mState, 1); - -		return hasField; -	} - -	bool State::HasKeys(int idx) { - -		idx = this->AbsIndex(idx); - -		lua_pushnil(mState);  /* first key */ -		if (lua_next(mState, idx) != 0) { -			lua_pop(mState, 2); -			return true; -		} -		return false; -	} - -	void State::GetField(int idx, cc8* name)  -	{ -		lua_getfield(mState, idx, name); -	} - -	void State::GetField(int idx, int key)  -	{ -		idx = this->AbsIndex(idx); - -		lua_pushinteger(mState, key); -		lua_gettable(mState, idx); -	} - -	std::string State::GetField(int idx, cc8* key, cc8* default_value)  -	{ -		std::string str; -		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) { -			str = lua_tostring(mState, -1); -			lua_pop(mState, 1); -		} -		else { -			str = default_value; -		} -		return str; -	} - -	std::string State::GetField(int idx, int key, cc8* default_value) -	{ -		std::string str; -		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) { -			str = lua_tostring(mState, -1); -			lua_pop(mState, 1); -		} -		else { -			str = default_value; -		} -		return str; -	} - -	std::string State::GetField(int idx, cc8* key, const std::string& value) -	{ -		std::string str; -		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) { -			str = lua_tostring(mState, -1); -			lua_pop(mState, 1); -		} -		else { -			str = value; -		} -		return str; -	} - -	std::string State::GetField(int idx, int key, const std::string& value)  -	{ -		std::string str; -		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) { -			str = lua_tostring(mState, -1); -			lua_pop(mState, 1); -		} -		else { -			str = value; -		} -		return str; -	} - -	bool State::GetFieldWithType(int idx, cc8* name, int type)  -	{ -		lua_getfield(mState, idx, name); -		if (lua_type(mState, -1) != type) { -			lua_pop(mState, 1); -			return false; -		} -		return true; -	} - -	bool State::GetFieldWithType(int idx, int key, int type)  -	{ -		this->GetField(idx, key); -		if (lua_type(mState, -1) != type) { -			lua_pop(mState, 1); -			return false; -		} -		return true; -	} - -	void State::SetField(int idx, cc8* key) -	{ -		if (IsTableOrUserdata(idx)) -		{ -			idx = AbsIndex(idx); -			lua_setfield(mState, idx, key); -		} -	} - -	cc8* State::GetLuaTypeName(int type)  -	{ -		switch (type) { -			case LUA_TNONE:				return "none"; -			case LUA_TNIL:				return "nil"; -			case LUA_TBOOLEAN:			return "boolean"; -			case LUA_TLIGHTUSERDATA:	return "lightuserdata"; -			case LUA_TNUMBER:			return "number"; -			case LUA_TSTRING:			return "string"; -			case LUA_TTABLE:			return "table"; -			case LUA_TFUNCTION:			return "function"; -			case LUA_TUSERDATA:			return "userdata"; -			case LUA_TTHREAD:			return "thread"; -		} -		return "unknown"; -	} - -	 -	bool State::GetSubfieldWithType(int idx, cc8* format, int type, ...)  -	{ -		va_list args; -		va_start(args, type); - -		idx = this->AbsIndex(idx); -		lua_pushvalue(this->mState, idx); - -		for (cc8* c = format; *c; ++c) { -			switch (*c) { -				// number -			case 'N': -				lua_pushnumber(this->mState, va_arg(args, int)); -				lua_gettable(this->mState, -1); -				break; - -				// string -			case 'S': -				lua_getfield(this->mState, -1, va_arg(args, char*)); -				break; - -			default: -				lua_pushnil(this->mState); -			} - -			if (lua_isnil(this->mState, -1)) break; -			lua_replace(this->mState, -2); -		} -		va_end(args); -		if (lua_type(this->mState, -1) != type) { -			lua_pop(this->mState, 1); -			return false; -		} -		return true; -	} - -	bool State::CheckParams(int idx, cc8* format) -	{ -		idx = AbsIndex(idx); - -		for (int i = 0; format[i]; ++i) { - -			int pos = idx + i; -			int type = LUA_TNIL; -			int expected = LUA_TNONE; - -			if (pos <= GetTop()) { -				type = lua_type(mState, pos); -			} - -			switch (format[i]) { - -				// boolean -			case 'B': -				if (type != LUA_TBOOLEAN) expected = LUA_TBOOLEAN; -				break; - -				// coroutine -			case 'C': -				if (type != LUA_TTHREAD) expected = LUA_TTHREAD; -				break; - -				// function -			case 'F': -				if (type != LUA_TFUNCTION) expected = LUA_TFUNCTION; -				break; - -				// light userdata -			case 'L': -				if (type != LUA_TLIGHTUSERDATA) expected = LUA_TLIGHTUSERDATA; -				break; - -				// number -			case 'N': -				if (type != LUA_TNUMBER) expected = LUA_TNUMBER; -				break; - -				// string -			case 'S': -				if (type != LUA_TSTRING) expected = LUA_TSTRING; -				break; - -				// table -			case 'T': -				if (type != LUA_TTABLE) expected = LUA_TTABLE; -				break; - -				// userdata -			case 'U': -				if (type != LUA_TUSERDATA) expected = LUA_TUSERDATA; -				break; - -				// any type -			case '*': -			case '.': -				break; -			} - -			if (expected != LUA_TNONE) { -				return false; -			} -		} - -		return true; -	} -	 -	template <> -	bool State::GetValue < bool >(int idx, const bool value) { - -		if (this->IsType(idx, LUA_TBOOLEAN)) { -			return (lua_toboolean(this->mState, idx) != 0); -		} -		return value; -	} - -	static std::string s_str; -	template <> -	cc8* State::GetValue < cc8* >(int idx, const cc8* value) { - -		if (this->IsType(idx, LUA_TSTRING)) { -			return lua_tostring(this->mState, idx); -		} - -		if (this->IsType(idx, LUA_TNUMBER)) { -			if (luax_isinteger(mState, -1)) { -				int num = lua_tointeger(this->mState, idx); -				s_str = std::to_string(num); -				cc8* str = s_str.c_str(); -				return str; -			} -			else { -				float num = lua_tonumber(this->mState, idx); -				s_str = std::to_string(num); -				cc8* str = s_str.c_str(); -				return str; -			} -		} - -		if (this->IsType(idx, LUA_TBOOLEAN)) { -			bool b = lua_toboolean(this->mState, idx); -			return b ? "true" : "false"; -		} - -		if (this->IsType(idx, LUA_TNIL)) { -			return "NIL"; -		} - -		return value; -	} - -	template <> -	std::string State::GetValue<std::string>(int idx, const std::string value)  -	{ -		std::string str; -		if (lua_type(this->mState, idx) == LUA_TSTRING) { -			str = lua_tostring(this->mState, idx); -		} -		else { -			str = value; -		} -		return str; -	} -	 -	template <> -	double State::GetValue < double >(int idx, const double value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	float State::GetValue < float >(int idx, const float value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (float)lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	s8 State::GetValue < s8 >(int idx, const s8 value) -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (s8)lua_tonumber(this->mState, idx); -		} -		return value; -	} - -	 -	template <> -	s16 State::GetValue < s16 >(int idx, const s16 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (s16)lua_tonumber(this->mState, idx); -		} -		return value; -	} - -	 -	template <> -	s32 State::GetValue < s32 >(int idx, const s32 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (s32)lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	s64 State::GetValue < s64 >(int idx, const s64 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (s64)lua_tonumber(this->mState, idx); -		} -		return value; -	} - -	template <> -	u8 State::GetValue < u8 >(int idx, const u8 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (u8)lua_tonumber(this->mState, idx); -		} -		return value; -	} - -	template <> -	u16 State::GetValue < u16 >(int idx, const u16 value) -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (u16)lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	u32 State::GetValue < u32 >(int idx, const u32 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (u32)lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	u64 State::GetValue < u64 >(int idx, const u64 value)  -	{ -		if (this->IsType(idx, LUA_TNUMBER)) { -			return (u64)lua_tonumber(this->mState, idx); -		} -		return value; -	} -	 -	template <> -	const void* State::GetValue < const void* >(int idx, const void* value) -	{ -		if (this->IsType(idx, LUA_TLIGHTUSERDATA)) { -			return (void*)lua_touserdata(this->mState, idx); -		} -		return value; -	} - -	void State::PushPtrUserdata(void* ptr)  -	{ -		void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*)); -		assert(handle); -		(*handle) = ptr; -	} - -	void State::RegisterEnum(cc8* name, Enum* en) -	{ -		assert(name); -		assert(en); - -		// short name -		lua_State* L = mState; - -		int top = GetTop(); - -		lua_newtable(L); // enum table  - -		int et = GetTop(); - -		lua_newtable(L); // matatable - -		// 所有枚举都存在metatable下,修改时触发__newindex报错 -		for (; en->name; ++en) -		{ -			lua_pushinteger(L, en->value); -			lua_setfield(L, -2, en->name); -		} - -		// __index  -		//lua_pushvalue(L, -1); // metatable -		//lua_pushcclosure(L, _rmt__index, 1); -		lua_pushvalue(L, -1); -		lua_setfield(L, -2, "__index"); - -		// __newinedx -		lua_pushstring(L, name); // enum name -		lua_pushcclosure(L, _rmt__newindex, 1); -		lua_setfield(L, -2, "__newindex"); - -		lua_setmetatable(L, et); - -		lua_setfield(L, top, name); -	} - - -	void State::RegisterMethods(const luaL_Reg *l) -	{ -		assert(lua_istable(mState, -1)); -		// luaL_register第二个参数为空,则向-1位置注册luaL_Reg中这些函数 -		luaL_register(mState, 0, l); -	} - -	void State::RegisterMethod(cc8* fname, lua_CFunction func) -	{ -		assert(lua_istable(mState, -1)); -		lua_pushcfunction(mState, func); -		lua_setfield(mState, -2, fname); -	} - -	void State::RegisterPreloader(cc8* libname, lua_CFunction preloader) -	{ -		lua_getglobal(mState, "package"); -		lua_getfield(mState, -1, "preload"); -		lua_pushcfunction(mState, preloader); -		lua_setfield(mState, -2, libname); -		lua_pop(mState, 2); -	} - -	void State::RegisterLib(cc8* libname, const luaL_Reg* l) -	{ -		luaL_register(mState, libname, l); -	} - -#if LUA_BIND_ENABLE_PLAIN_CLASS -	void State::RegisterPlainClassRegistry(cc8* name) -	{ -		assert(lua_istable(mState, -1)); -		lua_pushcfunction(mState, PlainClass::registry); -		lua_setfield(mState, -2, name); -	} -#endif - -#if LUA_BIND_ENABLE_PLAIN_ENUM -	void State::RegisterPlainEnumRegistry(cc8* name) -	{ -		assert(lua_istable(mState, -1)); -		lua_pushcfunction(mState, PlainEnum::registry); -		lua_setfield(mState, -2, name); -	} -#endif - -	int State::ErrorType(int idx, cc8* hint) -	{ -		return luaL_typerror(mState, idx, hint); -	} - -	template <>  -	bool State::CheckValue < bool >(int idx) -	{ -		bool b = false; -		if (lua_type(mState, idx) == LUA_TBOOLEAN) -		{ -			b = lua_toboolean(mState, idx); -		} -		else -		{ -			luaL_typerror(mState, idx, lua_typename(mState, LUA_TBOOLEAN)); -		} -		return b; -	} - -	template <>  -	cc8* State::CheckValue < cc8* >(int idx) -	{ -		return luaL_checkstring(mState, idx); -	} - -	template <>  -	double State::CheckValue < double >(int idx) -	{ -		return luaL_checknumber(mState, idx); -	} - -	template <>  -	float State::CheckValue < float >(int idx) -	{ -		return luaL_checknumber(mState, idx); -	} - -	template <>  -	s8 State::CheckValue < s8 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	s16 State::CheckValue < s16 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	s32 State::CheckValue < s32 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	s64 State::CheckValue < s64 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	u8 State::CheckValue < u8 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	u16 State::CheckValue < u16 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	u32 State::CheckValue < u32 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	u64	 State::CheckValue < u64 >(int idx) -	{ -		return luaL_checkinteger(mState, idx); -	} - -	template <>  -	std::string State::CheckValue < std::string >(int idx) -	{ -		return luaL_checkstring(mState, idx); -	} - -	// -	// check light userdata -	// -	template <> -	const void* State::CheckValue < const void* >(int idx) -	{ -		if (IsType(idx, LUA_TLIGHTUSERDATA)) -		{ -			return GetValue<const void*>(idx, nullptr); -		} -		else -		{ -			luaL_typerror(mState, idx, "light userdata"); -			return nullptr; -		} -	} - -}
\ No newline at end of file | 
