From de46b91a524c5f2c8e72b379f2900afe34ccb815 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 11 Nov 2021 10:29:17 +0800 Subject: *misc --- Runtime/Lua/LuaBind/LuaBind.h | 1 + Runtime/Lua/LuaBind/LuaBindInvoker.cpp | 4 +- Runtime/Lua/LuaBind/LuaBindLFunction.cpp | 63 ++++++++++++++++++++++++++++++++ Runtime/Lua/LuaBind/LuaBindLFunction.h | 26 +++++++++---- Runtime/Lua/LuaBind/LuaBindState.cpp | 30 +++++++++++++++ Runtime/Lua/LuaBind/LuaBindState.h | 2 + 6 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 Runtime/Lua/LuaBind/LuaBindLFunction.cpp (limited to 'Runtime/Lua/LuaBind') diff --git a/Runtime/Lua/LuaBind/LuaBind.h b/Runtime/Lua/LuaBind/LuaBind.h index 2a73c98..8e8c6b1 100644 --- a/Runtime/Lua/LuaBind/LuaBind.h +++ b/Runtime/Lua/LuaBind/LuaBind.h @@ -13,5 +13,6 @@ #include "LuaBindInvoker.h" #include "LuaBindTable.h" #include "LuaBindLClass.h" +#include "LuaBindLFunction.h" #endif \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp index bd12e5c..d6780d7 100644 --- a/Runtime/Lua/LuaBind/LuaBindInvoker.cpp +++ b/Runtime/Lua/LuaBind/LuaBindInvoker.cpp @@ -105,7 +105,7 @@ namespace LuaBind } lua_replace(state, -2); lua_insert(state, -1 - argc); - state.Call(argc, nReturns, onErrorOccured); + state.Call(argc, nReturns); } -} +} \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindLFunction.cpp b/Runtime/Lua/LuaBind/LuaBindLFunction.cpp new file mode 100644 index 0000000..b468897 --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindLFunction.cpp @@ -0,0 +1,63 @@ +#include "LuaBindLFunction.h" +#include "LuaBindHelper.h" + +namespace LuaBind +{ + + LuaFunction::LuaFunction(const char* func) + { + method = func; + } + + void LuaFunction::operator = (const char* func) + { + method = func; + } + + void LuaFunction::AddInt(State& state, int n) + { + state.Push(n); + ++argc; + } + + void LuaFunction::AddFloat(State& state, float n) + { + state.Push(n); + ++argc; + } + + void LuaFunction::AddNil(State& state) + { + state.PushNil(); + ++argc; + } + + void LuaFunction::AddBool(State& state, bool b) + { + state.Push(b); + ++argc; + } + + void LuaFunction::AddString(State& state, const char* str) + { + state.Push(str); + ++argc; + } + + void LuaFunction::AddTable(State& state, INativeTable& tb) + { + tb.CastToTable(state); + ++argc; + } + + void LuaFunction::Invoke(State& state, int nReturns) + { + if (state.GetGlobalField(method)) + { + int funcIdx = -1 - argc; + lua_replace(state, funcIdx); + state.Call(argc, nReturns); + } + } + +} \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindLFunction.h b/Runtime/Lua/LuaBind/LuaBindLFunction.h index 1ad6fa0..5c4346b 100644 --- a/Runtime/Lua/LuaBind/LuaBindLFunction.h +++ b/Runtime/Lua/LuaBind/LuaBindLFunction.h @@ -3,19 +3,29 @@ namespace LuaBind { - struct LuaFunction : public GlobalInvoker + struct LuaFunction { const char* method; // full name - LuaFunction(lua_State* L, const char* func) - : GlobalInvoker(L) - , method(func) - {} + LuaFunction(const char* func=NULL); + void operator = (const char* func); - void Invoke(int nReturns) override - { + void AddInt(State& state, int n); + void AddFloat(State& state, float n); + void AddNil(State& state); + void AddBool(State& state, bool b); + void AddString(State& state, const char* str); + void AddTable(State& state, INativeTable& tb); + template + void AddUserdata(State& state, NativeClass& udata) { + udata.PushUserdata(state); + ++argc; + } - } + void Invoke(State& state, int nReturns); + + private: + int argc; }; } \ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index 92e46cd..3fefa52 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -7,6 +7,8 @@ #include #include +using namespace std; + namespace LuaBind { OnRegisterClassHandler onRegisterNativeClass = NULL; @@ -415,6 +417,34 @@ namespace LuaBind lua_gettable(mState, idx); } + bool State::GetGlobalField(const char* fullName) + { + if (fullName == NULL || strlen(fullName) == 0) + return false; + int top = GetTop(); + lua_pushvalue(*this, LUA_GLOBALSINDEX); + string name = fullName; + while (name.size() > 0) + { + int dot = name.find('.'); + dot = dot != string::npos ? dot : name.size(); + string pkg = name.substr(0, dot); + if (dot < name.size()) + name = name.substr(dot + 1, name.size() - dot - 1); + else + name = ""; + lua_getfield(*this, -1, pkg.c_str()); + if (lua_isnil(*this, -1)) + { + lua_settop(*this, top); + return false; + } + } + lua_replace(*this, top + 1); + lua_settop(*this, top + 1); + return true; + } + std::string State::GetField(int idx, cc8* key, cc8* default_value) { std::string str; diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index aaad952..e8ac42d 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -74,6 +74,8 @@ namespace LuaBind //------------------------------------------------------------------------------// + bool GetGlobalField(const char* fullName); + void GetField(int idx, cc8* name); void GetField(int idx, int key); std::string GetField(int idx, cc8* key, cc8* value); -- cgit v1.1-26-g67d0