summaryrefslogtreecommitdiff
path: root/Runtime/Lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-23 18:19:18 +0800
committerchai <chaifix@163.com>2021-10-23 18:19:18 +0800
commit7bf672fd0c6211909d94078b448032b1bd0916ef (patch)
tree655b875b81ec8d0559ca905fb7d37a1d88e6a80d /Runtime/Lua
parentdf0444f85f9bf623cc886e1632e624ef20cb0f1b (diff)
*misc
Diffstat (limited to 'Runtime/Lua')
-rw-r--r--Runtime/Lua/LuaBind/LuaBindClass.hpp25
-rw-r--r--Runtime/Lua/LuaBind/LuaBindHelper.h1
-rw-r--r--Runtime/Lua/LuaBind/LuaBindRef.h3
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.cpp5
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.h3
-rw-r--r--Runtime/Lua/LuaBind/LuaBindVM.cpp10
-rw-r--r--Runtime/Lua/LuaBind/LuaBindVM.h7
-rw-r--r--Runtime/Lua/LuaHelper.cpp5
-rw-r--r--Runtime/Lua/LuaHelper.h3
9 files changed, 52 insertions, 10 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindClass.hpp b/Runtime/Lua/LuaBind/LuaBindClass.hpp
index c112c03..eb028c2 100644
--- a/Runtime/Lua/LuaBind/LuaBindClass.hpp
+++ b/Runtime/Lua/LuaBind/LuaBindClass.hpp
@@ -44,11 +44,6 @@ namespace LuaBind
};
- // TODO: 将公共部分提取出来,不要重复生成代码
- //class NativeClassBase
- //{
- //}
-
// 需要暴露给lua的native class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在多个线程中需要确
// 定不会误释放。
template<class TYPE, class BASE = Object>
@@ -90,11 +85,16 @@ namespace LuaBind
NativeClass(LuaBind::VM* vm);
virtual ~NativeClass();
+ VM* GetVM() { return mOwner; }
+
// 成员引用管理,在实例的ref table里。设置、取、清除
void SetMemberRef(State& state, MemberRef& memRef, int idx);
bool PushMemberRef(State& state, MemberRef& memRef);
void ClearMemberRef(State& state, MemberRef& memRef);
+ // 调用回调函数
+ void InvokeLuaCallback(LuaBind::MemberRef script, cc8* method);
+
private:
friend class State;
@@ -348,7 +348,6 @@ namespace LuaBind
state.PushPtrUserdata(p);
lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期
- //lua_newtable(state); // member table,lua中创建的对象成员都保存在这里
PushClassTable(state); // class table
// stack:
@@ -405,6 +404,20 @@ namespace LuaBind
}
}
+ template<class TYPE, class BASE>
+ void NativeClass<TYPE, BASE>::InvokeLuaCallback(LuaBind::MemberRef script, cc8* method)
+ {
+ if (!script)
+ return;
+ LuaBind::State state = GetVM()->GetCurThread();
+ int top = state.GetTop();
+ PushMemberRef(state, script);
+ state.GetField(-1, method);
+ PushMemberRef(state, script);
+ state.Call(1, 0);
+ state.SetTop(top);
+ }
+
template<class TYPE, class BASE>
bool NativeClass<TYPE, BASE>::PushMemberRef(State& state, MemberRef& memRef)
{
diff --git a/Runtime/Lua/LuaBind/LuaBindHelper.h b/Runtime/Lua/LuaBind/LuaBindHelper.h
index f095add..86fcb13 100644
--- a/Runtime/Lua/LuaBind/LuaBindHelper.h
+++ b/Runtime/Lua/LuaBind/LuaBindHelper.h
@@ -13,4 +13,5 @@ namespace LuaBind
int m_Cur;
};
};
+
#endif \ No newline at end of file
diff --git a/Runtime/Lua/LuaBind/LuaBindRef.h b/Runtime/Lua/LuaBind/LuaBindRef.h
index cd2fad1..c7b7ab3 100644
--- a/Runtime/Lua/LuaBind/LuaBindRef.h
+++ b/Runtime/Lua/LuaBind/LuaBindRef.h
@@ -7,7 +7,8 @@
namespace LuaBind
{
- // 全局引用,保存在LUA_REGISTRYINDEX下面的两个表里
+ // 全局引用,保存在LUA_REGISTRYINDEX下面的两个表里,生命周期手动控制
+ // 如果要用局部引用,用MemberRef,会保存在UserData的RefTable里,生命周期和UserData一致
class Ref
{
public:
diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp
index 3d26dc3..c9183ff 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindState.cpp
@@ -323,6 +323,11 @@ namespace LuaBind
return lua_gettop(mState);
}
+ void State::SetTop(int top)
+ {
+ lua_settop(mState, top);
+ }
+
bool State::HasField(int idx, cc8* name) {
lua_getfield(mState, idx, name);
diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h
index aa77c26..60e05be 100644
--- a/Runtime/Lua/LuaBind/LuaBindState.h
+++ b/Runtime/Lua/LuaBind/LuaBindState.h
@@ -222,7 +222,8 @@ namespace LuaBind
if(!state.CheckParams(1, params)) return 0
#define LUA_BIND_STATE(L) \
- LuaBind::State state(L)
+ LuaBind::State state(L);\
+ state.GetVM()->SetCurThread(L)
//--------------------------------------------------------------------------------//
diff --git a/Runtime/Lua/LuaBind/LuaBindVM.cpp b/Runtime/Lua/LuaBind/LuaBindVM.cpp
index 6b39288..e454929 100644
--- a/Runtime/Lua/LuaBind/LuaBindVM.cpp
+++ b/Runtime/Lua/LuaBind/LuaBindVM.cpp
@@ -60,6 +60,16 @@ namespace LuaBind
return mMainThread;
}
+ void VM::SetCurThread(lua_State* cur)
+ {
+ mCurThread = cur;
+ }
+
+ lua_State* VM::GetCurThread()
+ {
+ return mCurThread;
+ }
+
State VM::GetMainState()
{
return mMainThread;
diff --git a/Runtime/Lua/LuaBind/LuaBindVM.h b/Runtime/Lua/LuaBind/LuaBindVM.h
index a53a707..f3a9a30 100644
--- a/Runtime/Lua/LuaBind/LuaBindVM.h
+++ b/Runtime/Lua/LuaBind/LuaBindVM.h
@@ -30,6 +30,8 @@ namespace LuaBind
void OpenLibs();
lua_State* GetMainThread();
+ void SetCurThread(lua_State* cur);
+ lua_State* GetCurThread();
lua_State* CreateThread();
State GetMainState();
@@ -42,8 +44,9 @@ namespace LuaBind
private:
static std::map<global_State*, VM*> VMs; // 通过global_State索引虚拟机,为了方便
- global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享,保存注册表、管理GC
- lua_State* mMainThread; // 主线程
+ global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享,保存注册表、管理GC
+ lua_State* mMainThread; // 主线程
+ lua_State* mCurThread; // 当前线程,并不是绝对准确的,在调用C函数时会设置,见LUA_BIND_STATE宏
RefTable mStrongRefTable; // 全局强引用表
RefTable mWeakRefTable; // 全局弱引用表
diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp
index 289f0f6..53bea3f 100644
--- a/Runtime/Lua/LuaHelper.cpp
+++ b/Runtime/Lua/LuaHelper.cpp
@@ -21,3 +21,8 @@ Vector2f State::GetValue < Vector2f >(int idx, const Vector2f value)
v2.y = GetField<float>(idx, 2, 0);
return v2;
}
+
+int LuaHelper::Call(const char* func, const char* params, ...)
+{
+ return 1;
+} \ No newline at end of file
diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h
index 5e9bf90..4ab0d6a 100644
--- a/Runtime/Lua/LuaHelper.h
+++ b/Runtime/Lua/LuaHelper.h
@@ -6,5 +6,8 @@
class LuaHelper
{
public:
+ static int Call(const char* func, const char* params, ...);
};
+
+