diff options
author | chai <chaifix@163.com> | 2021-10-23 00:31:25 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-23 00:31:25 +0800 |
commit | df0444f85f9bf623cc886e1632e624ef20cb0f1b (patch) | |
tree | 4aff1454312540f137bb318349b48e58d9b06d80 | |
parent | 4dafefe46a72ba47468b13d011f8299055081b0f (diff) |
-member table
-rw-r--r-- | Editor/EditorApplication.h | 2 | ||||
-rw-r--r-- | Editor/EditorMain.cpp | 4 | ||||
-rw-r--r-- | Editor/GUI/EditorWindows.h | 7 | ||||
-rw-r--r-- | Editor/GUI/GUIWindow.cpp | 2 | ||||
-rw-r--r-- | Editor/Scripting/Editor/Editor.bind.cpp | 2 | ||||
-rw-r--r-- | Editor/Scripting/EditorGUI/EditorGUI.bind.cpp | 4 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindClass.cpp | 235 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindClass.hpp | 348 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindConfig.h | 4 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindEnum.cpp | 30 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindEnum.h | 15 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.cpp | 22 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.h | 31 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindUtility.h | 12 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindVM.h | 2 |
15 files changed, 83 insertions, 637 deletions
diff --git a/Editor/EditorApplication.h b/Editor/EditorApplication.h index 25661a1..1b464e8 100644 --- a/Editor/EditorApplication.h +++ b/Editor/EditorApplication.h @@ -20,7 +20,7 @@ public: private :
- LUA_BIND_DECL_FACTORY(EditorApplication);
+ LUA_BIND_DECL_CLASS(EditorApplication);
LUA_BIND_DECL_METHOD(_New);
LUA_BIND_DECL_METHOD(_SetMainWindow);
diff --git a/Editor/EditorMain.cpp b/Editor/EditorMain.cpp index 95937a7..7ca90ad 100644 --- a/Editor/EditorMain.cpp +++ b/Editor/EditorMain.cpp @@ -15,7 +15,7 @@ void ErrorHandle(cc8* msg) log_error(std::string("[Lua] ") + msg);
}
-void OnRegisterFactoryClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
+void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
{
// 填充类型的元数据
lua_newtable(state);
@@ -35,7 +35,7 @@ void InitLuaState() vm.Setup();
vm.OpenLibs();
- LuaBind::onRegisterFactoryClass = OnRegisterFactoryClass;
+ LuaBind::onRegisterNativeClass = OnRegisterNativeClass;
if (!SetupGameLabEditorScripting(vm.GetMainThread()))
{
diff --git a/Editor/GUI/EditorWindows.h b/Editor/GUI/EditorWindows.h index d9805a0..a25b612 100644 --- a/Editor/GUI/EditorWindows.h +++ b/Editor/GUI/EditorWindows.h @@ -89,7 +89,7 @@ private: POINT m_MinSize; POINT m_MaxSize; - LUA_BIND_DECL_FACTORY(ContainerWindow); + LUA_BIND_DECL_CLASS(ContainerWindow); LUA_BIND_DECL_METHOD(_New); LUA_BIND_DECL_METHOD(_SetTitle); @@ -161,12 +161,11 @@ private: HDC m_DC; HGLRC m_RC; - LuaBind::Ref m_Instance; // EditorWindow脚本 + LuaBind::MemberRef m_Instance; // EditorWindow脚本 - LUA_BIND_DECL_FACTORY(GUIWindow); + LUA_BIND_DECL_CLASS(GUIWindow); LUA_BIND_DECL_METHOD(_New); - LUA_BIND_DECL_METHOD(_GC); LUA_BIND_DECL_METHOD(_DoPaint); LUA_BIND_DECL_METHOD(_Focus); LUA_BIND_DECL_METHOD(_SetContainerWindow); diff --git a/Editor/GUI/GUIWindow.cpp b/Editor/GUI/GUIWindow.cpp index 8918ad3..2127232 100644 --- a/Editor/GUI/GUIWindow.cpp +++ b/Editor/GUI/GUIWindow.cpp @@ -176,7 +176,7 @@ void GUIWindow::RepaintAll() GUIWindow::GUIWindow(LuaBind::VM* vm) : LuaBind::NativeClass<GUIWindow>(vm) - , m_Instance(vm, Ref::STRONG_REF) + , m_Instance() { } diff --git a/Editor/Scripting/Editor/Editor.bind.cpp b/Editor/Scripting/Editor/Editor.bind.cpp index 2300d2f..9808141 100644 --- a/Editor/Scripting/Editor/Editor.bind.cpp +++ b/Editor/Scripting/Editor/Editor.bind.cpp @@ -11,7 +11,7 @@ int luaopen_GameLab_Editor(lua_State* L) state.PushNamespace("GameLab"); state.PushNamespace("Editor"); - state.RegisterFactory<EditorApplication>(); + state.RegisterNativeClass<EditorApplication>(); return 1; }
\ No newline at end of file diff --git a/Editor/Scripting/EditorGUI/EditorGUI.bind.cpp b/Editor/Scripting/EditorGUI/EditorGUI.bind.cpp index 0f30276..2fea6be 100644 --- a/Editor/Scripting/EditorGUI/EditorGUI.bind.cpp +++ b/Editor/Scripting/EditorGUI/EditorGUI.bind.cpp @@ -12,8 +12,8 @@ int luaopen_GameLab_Editor_GUI(lua_State* L) state.PushNamespace("Editor"); state.PushNamespace("GUI"); - state.RegisterFactory<ContainerWindow>(); - state.RegisterFactory<GUIWindow>(); + state.RegisterNativeClass<ContainerWindow>(); + state.RegisterNativeClass<GUIWindow>(); return 1; }
\ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindClass.cpp b/Runtime/Lua/LuaBind/LuaBindClass.cpp index 9c67475..3e2dec5 100644 --- a/Runtime/Lua/LuaBind/LuaBindClass.cpp +++ b/Runtime/Lua/LuaBind/LuaBindClass.cpp @@ -7,239 +7,4 @@ namespace LuaBind UIDGenerator gClassIDGenerator; -#if LUA_BIND_ENABLE_PLAIN_CLASS - - int PlainClass::registry(lua_State* L) - { - LUA_BIND_STATE(L); - - // params: - // 1: class name - - cc8* type = state.GetValue<cc8*>(1, ""); - - lua_newtable(L); // class table - - // GetClassName() - lua_pushstring(L, type); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClassName"); - - // GetClass() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClass"); - - // TypeOf() - lua_pushcfunction(L, _TypeOf); - lua_setfield(L, -2, "TypeOf"); - - // New() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, _New, 1); - lua_setfield(L, -2, "New"); - - // Extend() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, _Extend, 1); - lua_setfield(L, -2, "Extend"); - - lua_pushvalue(L, -1); // class table - lua_setfield(L, -2, "__index"); - - lua_pushstring(L, type); - lua_pushcclosure(L, __tostring, 1); - lua_setfield(L, -2, "__tostring"); - - return 1; - } - - int PlainClass::__tostring(lua_State* L) - { - // upvalues: - // 1: class name - - // params: - // 1: instance - - if (!lua_istable(L, 1)) - { - return luaL_typerror(L, 1, lua_typename(L, LUA_TTABLE)); - } - - cc8* type = lua_tostring(L, lua_upvalueindex(1)); - - lua_pushfstring(L, "%s: %p", type, lua_topointer(L, 1)); - - return 1; - } - - // - // New函数接受n个参数,并尝试获取__init,将参数传给__init初始化实例。 - // - int PlainClass::_New(lua_State* L) - { - LUA_BIND_STATE(L); - - // upvalues: - // 1: class table - - // params: - // n: params - int n = lua_gettop(L); - - int classTable = lua_upvalueindex(1); - - lua_newtable(L); // instance table - - // instance 的 metatable 设置为 class - lua_pushvalue(L, classTable); - lua_setmetatable(L, -2); - - // 找到构造函数,会触发metatable.__index,根据继承链向上找。 - lua_getfield(L, classTable, "__init"); - if (state.IsType(-1, LUA_TFUNCTION)) - { - // stack: - // -1: __init() - // -2: instance - // -3~-n-2: params - - lua_insert(L, -2 - n); - // stack: - // -1: instance - // -2~-n-1: params - // -n-2: __init() - - lua_pushvalue(L, -1); - // stack: - // -1: instance - // -2: instance - // -3~-n-2: params - // -n-3: __init - - lua_insert(L, -3 - n); - // stack: - // -1: instance - // -2~-n-1: params - // -n-2: __init() - // -n-3: instance - - lua_insert(L, -1 - n); - // stack: - // -1~-n: params - // -n-1: instance - // -n-2: __init() - // -n-3: instance - - lua_pcall(L, n + 1, 0, 0); - } - else - { - state.Pop(); - } - - return 1; - } - - int PlainClass::_Extend(lua_State* L) - { - LUA_BIND_STATE(L); - - // upvalues: - // 1: base class - - // params: - // 1: class name - - cc8* type = state.GetValue<cc8*>(1, ""); - - int baseClass = lua_upvalueindex(1); - - lua_newtable(L); // class table - - // GetClassName() - lua_pushstring(L, type); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClassName"); - - // GetClass() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClass"); - - // New() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, _New, 1); - lua_setfield(L, -2, "New"); - - // Extend() - lua_pushvalue(L, -1); // class table - lua_pushcclosure(L, _Extend, 1); - lua_setfield(L, -2, "Extend"); - - // .__base 用来索引到基类 - lua_pushvalue(L, baseClass); // base class - lua_setfield(L, -2, "__base"); - - lua_pushvalue(L, -1); // class table - lua_setfield(L, -2, "__index"); - - lua_pushstring(L, type); - lua_pushcclosure(L, __tostring, 1); - lua_setfield(L, -2, "__tostring"); - - // class的metatable设置为baseClass - lua_pushvalue(L, baseClass); - lua_setmetatable(L, -2); - - return 1; - } - - int PlainClass::_TypeOf(lua_State* L) - { - // params: - // 1: lua instance - // 2: type string - - LUA_BIND_STATE(L); - - cc8* type = state.GetValue<cc8*>(2, ""); - - if (!lua_istable(L, 1)) - { - return luaL_typerror(L, 1, "Object"); - } - - lua_pushvalue(L, 1); // lua instance - - while (lua_getmetatable(L, -1)) - { - lua_getfield(L, -1, "GetClassName"); - if (lua_isfunction(L, -1)) - { - state.Call(0, 1); - cc8* name = state.GetValue<cc8*>(-1, ""); - if (strcmp(name, type) == 0) - { - lua_pushboolean(L, true); - return 1; - } - else - { - state.Pop(); // name - } - } - else - { - state.Pop(); - } - } - - lua_pushboolean(L, false); - return 1; - } - -#endif /*LUA_BIND_ENABLE_PLAIN_CLASS*/ - }
\ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindClass.hpp b/Runtime/Lua/LuaBind/LuaBindClass.hpp index f0fdf43..c112c03 100644 --- a/Runtime/Lua/LuaBind/LuaBindClass.hpp +++ b/Runtime/Lua/LuaBind/LuaBindClass.hpp @@ -36,7 +36,6 @@ namespace LuaBind // 成员引用管理,在实例的ref table里。设置、取、清除。 virtual bool PushMemberRef(State& state, int refID) = 0; virtual bool PushUserdata(State& state) = 0; - virtual bool PushMemberTable(State& state) = 0; virtual bool PushRefTable(State& state) = 0; // 被NativeClass实现。保持和释放native资源。 @@ -56,7 +55,7 @@ namespace LuaBind class NativeClass : public BASE { public: - enum NativeClassTableIndex { kRefTable = 1, kMemberTable = 2, kClassTable = 3 }; + enum NativeClassTableIndex { kRefTable = 1, kClassTable = 2 }; // 将userdata作为key,在ref table里对userdata添加一个引用,以维持userdata的生命周期。 // 相比较member ref,这个用在实体会被多次被不同其他实体引用的情况,并频繁销毁这些实体, @@ -70,7 +69,6 @@ namespace LuaBind // userdata留在栈顶。并添加一个引用。这是一个将native对象所有权移交给lua控制的方法。 bool PushUserdata(State& state) override; bool PushMemberRef(State& state, int refID) override; - bool PushMemberTable(State& state) override; bool PushRefTable(State& state) override; // WatchDog添加一个native引用。luaVM引用不会提供外部接口。继承此类的派生类不能直接使用 @@ -102,7 +100,7 @@ namespace LuaBind friend class State; static void RegisterClassShared(State& state); - static void RegisterFactoryClass(State& state); + static void RegisterNativeClass(State& state); static void SetClassTableRef(State& state, int idx); static void PushClassTable(State& state); @@ -121,13 +119,6 @@ namespace LuaBind static int __gc (lua_State*); static int _GetRefTable (lua_State*); -#if LUA_BIND_ENABLE_NATIVE_EXTEND - static int _New(lua_State*); - - static int _ExtendFactory (lua_State*); - static int _ExtendSingleton (lua_State*); -#endif - //--------------------------------------------------------------------------------// // 类型的全局UID,在不同的虚拟机中相同 @@ -138,8 +129,7 @@ namespace LuaBind // 通过userdata可以拿到: // 1: ref table - // 2: member table - // 3: class table + // 2: class table WeakRef mUserdata; // 通过后才能删除 @@ -154,27 +144,6 @@ namespace LuaBind }; -#if LUA_BIND_ENABLE_PLAIN_CLASS - // - // 纯lua类 - // - class PlainClass - { - public: - - // - // 用来注册类的入口函数。可以通过registry(类名)注册类。 - // - static int registry(lua_State* L); - - LUA_BIND_DECL_METHOD(__tostring); - LUA_BIND_DECL_METHOD(_Extend); - LUA_BIND_DECL_METHOD(_New); - LUA_BIND_DECL_METHOD(_TypeOf); - - }; -#endif - //--------------------------------------------------------------------------------// template<class TYPE, class BASE> int NativeClass<TYPE, BASE>::sClassID = CLASS_NONE_ID; @@ -207,7 +176,7 @@ namespace LuaBind // 工厂类的成员,注册在class table template<class TYPE, class BASE> - void NativeClass<TYPE, BASE>::RegisterFactoryClass(State& state) + void NativeClass<TYPE, BASE>::RegisterNativeClass(State& state) { luaL_Reg regTable[] = { { "GetRefTable", _GetRefTable }, @@ -348,27 +317,6 @@ namespace LuaBind } template<class TYPE, class BASE> - bool NativeClass<TYPE, BASE>::PushMemberTable(State& state) - { - int top = state.GetTop(); - if (this->PushUserdata(state)) - { - if (lua_getmetatable(state, -1)) // ref table - { - lua_replace(state, -2); - if (lua_getmetatable(state, -1)) // member table - { - lua_replace(state, -2); - return true; - } - } - } - lua_settop(state, top); - lua_pushnil(state); - return false; - } - - template<class TYPE, class BASE> bool NativeClass<TYPE, BASE>::PushRefTable(State& state) { if (this->PushUserdata(state)) @@ -383,8 +331,7 @@ namespace LuaBind } // 创建userdata,并以此添加ref table,member table和class table。 - // ref table 是kv强引用table,保存对其他userdata的引用计数(通过userdata作为key, - // 计数为value),以及成员引用 + // ref table 是kv强引用table,保存对其他GCObject的引用 // member table 保存lua创建的实例的成员 // class table 所有本类型的实例共有的函数表 // @@ -392,70 +339,67 @@ namespace LuaBind template<class TYPE, class BASE> void NativeClass<TYPE, BASE>::BindToLua(State& state) { - assert(!mUserdata); - - // 创建userdata并留在栈顶,注意地址要转换为TYPE*,直接用this可能会导致多重继承的类丧失多态。 - // 如果直接传this进去,在多重继承情况下,是拿不到另一头的虚函数表的。所以这里需要将this - // 转换为整个对象的低地址,这样可以拿到另一个基类的虚函数表,通过另一个基类实现多态。 - TYPE* p = static_cast<TYPE*>(this); - state.PushPtrUserdata(p); - - lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期 - lua_newtable(state); // member table,lua中创建的对象成员都保存在这里 - PushClassTable(state); // class table - - // stack: - // -1 class table - // -2 member table - // -3 ref table - // -4 userdata - - int top = state.GetTop(); - int memberTable = top - 1; - int refTable = top - 2; - - // ref table 注册 __tostring 和 __gc - lua_pushcfunction(state, __tostring); - lua_setfield(state, refTable, "__tostring"); - - lua_pushcfunction(state, __gc); - lua_setfield(state, refTable, "__gc"); - - // ref table 的 __index 和 __newindex 设为 member table - lua_pushvalue(state, memberTable); - lua_setfield(state, refTable, "__index"); - - lua_pushvalue(state, memberTable); - lua_setfield(state, refTable, "__newindex"); - - // 设置元表 userdata -> refTable -> memberTable -> classTable - lua_setmetatable(state, -2); // class is metatable of member - lua_setmetatable(state, -2); // member is metatable of ref - lua_setmetatable(state, -2); // ref is metatable of userdata - - // 设置一个userdata的弱引用,方便通过PushLuaUserdata方法返回lua对象 - mUserdata.SetRef(state, -1); - assert(mUserdata); - - // 增加一个虚拟机引用,在GC时-1 - ++mWatchDog.mVMRef; -#if LUA_BIND_PROFILER - mRefVMs.insert(state.GetVM()); + assert(!mUserdata);
+
+ // 创建userdata并留在栈顶,注意地址要转换为TYPE*,直接用this可能会导致多重继承的类丧失多态。
+ // 如果直接传this进去,在多重继承情况下,是拿不到另一头的虚函数表的。所以这里需要将this
+ // 转换为整个对象的低地址,这样可以拿到另一个基类的虚函数表,通过另一个基类实现多态。
+ TYPE* p = static_cast<TYPE*>(this);
+ state.PushPtrUserdata(p);
+
+ lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期
+ //lua_newtable(state); // member table,lua中创建的对象成员都保存在这里
+ PushClassTable(state); // class table
+
+ // stack:
+ // -1 class table
+ // -2 ref table
+ // -3 userdata
+
+ int clsTable = state.GetTop();
+ int refTable = clsTable - 1;
+
+ // ref table 注册 __tostring 和 __gc
+ lua_pushcfunction(state, __tostring);
+ lua_setfield(state, refTable, "__tostring");
+
+ lua_pushcfunction(state, __gc);
+ lua_setfield(state, refTable, "__gc");
+
+ // ref table 的 __index 和 __newindex 设为 member table
+ lua_pushvalue(state, clsTable);
+ lua_setfield(state, refTable, "__index");
+
+ lua_pushvalue(state, clsTable);
+ lua_setfield(state, refTable, "__newindex");
+
+ // 设置元表 userdata -> refTable -> classTable
+ lua_setmetatable(state, -2); // class is metatable of ref
+ lua_setmetatable(state, -2); // ref is metatable of userdata
+
+ // 设置一个userdata的弱引用,方便通过PushLuaUserdata方法返回lua对象
+ mUserdata.SetRef(state, -1);
+ assert(mUserdata);
+
+ // 增加一个虚拟机引用,在GC时-1
+ ++mWatchDog.mVMRef;
+#if LUA_BIND_PROFILER
+ mRefVMs.insert(state.GetVM());
#endif } // 成员引用管理 template<class TYPE, class BASE> - void NativeClass<TYPE, BASE>::SetMemberRef(State& state, MemberRef& memRef, int idx) + void NativeClass<TYPE, BASE>::SetMemberRef(State& state, MemberRef& outRef, int idx) { - ClearMemberRef(state, memRef); + ClearMemberRef(state, outRef); if (!lua_isnil(state, idx)) { idx = state.AbsIndex(idx); - if (PushRefTable(state)) + if (PushRefTable(state)) // RefTable { lua_pushvalue(state, idx); - memRef.refID = luaL_ref(state, -2); + outRef.refID = luaL_ref(state, -2); state.Pop(); // ref table } } @@ -466,7 +410,7 @@ namespace LuaBind { if (memRef) { - if (PushRefTable(state)) + if (PushRefTable(state)) // RefTable { lua_rawgeti(state, -1, memRef.refID); lua_replace(state, -2); // ref table @@ -484,7 +428,7 @@ namespace LuaBind template<class TYPE, class BASE> bool NativeClass<TYPE, BASE>::PushMemberRef(State& state, int refID) { - if (PushRefTable(state)) + if (PushRefTable(state)) // RefTable { lua_rawgeti(state, -1, refID); lua_replace(state, -2); // ref table @@ -502,7 +446,7 @@ namespace LuaBind { if (memRef) { - if (PushRefTable(state)) + if (PushRefTable(state)) // RefTable { luaL_unref(state, -1, memRef.refID); state.Pop(); // ref table @@ -568,182 +512,6 @@ namespace LuaBind return 0; } -#if LUA_BIND_ENABLE_NATIVE_EXTEND - // 派生出子类,在lua里对派生类的成员和行为进行重新设计,但是保证了userdata的统一。Native class的派生提供__init支持,在 - // native实体创建后可以使用__init进行初始化,派生类拥有和基类一样的New参数列表,且native对象是一样的类型。 - template<class TYPE, class BASE> - int NativeClass<TYPE, BASE>::_ExtendFactory(lua_State* L) - { - // upvalues: - // 1: base class - - // params: - // 1: class name - - int baseClass = lua_upvalueindex(1); - - lua_newtable(L); // class table - - int inheritClass = lua_gettop(L); - - // .GetClassName() - cc8* type = lua_tostring(L, 1); - lua_pushstring(L, type); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClassName"); - - // .GetClass() - lua_pushvalue(L, inheritClass); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClass"); - - // .Extend() - lua_pushvalue(L, inheritClass); - lua_pushcclosure(L, _ExtendFactory, 1); - lua_setfield(L, -2, "Extend"); - - // .New() - lua_pushvalue(L, inheritClass); - lua_getfield(L, baseClass, "New"); - lua_pushcclosure(L, _New, 2); - lua_setfield(L, -2, "New"); - - // __base = baseClass - lua_pushvalue(L, baseClass); - lua_setfield(L, -2, "__base"); - - // __index = inheritClass - lua_pushvalue(L, inheritClass); - lua_setfield(L, -2, "__index"); - - // metatable is baseClass - lua_pushvalue(L, baseClass); - lua_setmetatable(L, inheritClass); - - return 1; - } - - template<class TYPE, class BASE> - int NativeClass<TYPE, BASE>::_ExtendSingleton(lua_State* L) - { - // upvalues: - // 1: base class - - // params: - // 1: class name - - int baseClass = lua_upvalueindex(1); - - lua_newtable(L); // class name - - int inheritClass = lua_gettop(L); - - // .GetClassName() - cc8* type = lua_tostring(L, 1); - lua_pushstring(L, type); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClassName"); - - // .GetClass() - lua_pushvalue(L, inheritClass); - lua_pushcclosure(L, luax_c_getupvalue, 1); - lua_setfield(L, -2, "GetClass"); - - // .Extend() - lua_pushvalue(L, inheritClass); - lua_pushcclosure(L, _ExtendFactory, 1); - lua_setfield(L, -2, "Extend"); - - // __base = baseClass - lua_pushvalue(L, baseClass); - lua_setfield(L, -2, "__base"); - - // __index = inheritClass - lua_pushvalue(L, inheritClass); - lua_setfield(L, -2, "__index"); - - // metatable is baseClass - lua_pushvalue(L, baseClass); - lua_setmetatable(L, inheritClass); - - return 1; - } - - template<class TYPE, class BASE> - int NativeClass<TYPE, BASE>::_New(lua_State* L) - { - LUA_BIND_STATE(L); - - // upvalues: - // 1: class table - // 2: original New() - - // stack: - // -1~-n: args - - int n = lua_gettop(L); // n args - - lua_pushvalue(L, lua_upvalueindex(2)); - if (state.IsType(-1, LUA_TFUNCTION)) - { - // stack: - // -1: New - // -2~-1-n: args - - state.PushValues(-1 - n, n); - - // stack: - // -1~-n: args - // -n-1: New - // -n-2~-1-2n: args - - state.Call(n, 1); - - // stack: - // -1: userdata - // -2~-1-n: args - - // reset member table's metatable to class table - if (state.IsType(-1, LUA_TUSERDATA)) - { - if (lua_getmetatable(L, -1)) // ref table - { - if (lua_getmetatable(L, -1)) // member table - { - lua_pushvalue(L, lua_upvalueindex(1)); // class table - lua_setmetatable(L, -2); - state.Pop(); // member table - } - state.Pop(); // ref table - } - - // stack: - // -1: userdata - // -2~-1-n: args - - int args = state.AbsIndex(-1 - n); - - // 尝试调用__init函数 - lua_getfield(L, lua_upvalueindex(1), "__init"); - - if (state.IsType(-1, LUA_TFUNCTION)) - { - lua_pushvalue(L, -2); // userdata - state.PushValues(args, n); - state.Call(n + 1, 0); - } - else - state.Pop(); - - } - - return 1; - } - return 0; - } - -#endif /*LUA_BIND_ENABLE_NATIVE_EXTEND*/ - template<class TYPE, class BASE> int NativeClass<TYPE, BASE>::_GetClass(lua_State* L) { diff --git a/Runtime/Lua/LuaBind/LuaBindConfig.h b/Runtime/Lua/LuaBind/LuaBindConfig.h index 829c049..d2cbff7 100644 --- a/Runtime/Lua/LuaBind/LuaBindConfig.h +++ b/Runtime/Lua/LuaBind/LuaBindConfig.h @@ -50,10 +50,6 @@ namespace LuaBind #define LUA_BIND_API LUA_BIND_LIBRARY_EXPORT #endif -#define LUA_BIND_ENABLE_NATIVE_EXTEND 0 -#define LUA_BIND_ENABLE_PLAIN_CLASS 0 -#define LUA_BIND_ENABLE_PLAIN_ENUM 0 - #define LUA_BIND_PROFILER 0 } diff --git a/Runtime/Lua/LuaBind/LuaBindEnum.cpp b/Runtime/Lua/LuaBind/LuaBindEnum.cpp index 63e2567..849fb8a 100644 --- a/Runtime/Lua/LuaBind/LuaBindEnum.cpp +++ b/Runtime/Lua/LuaBind/LuaBindEnum.cpp @@ -34,34 +34,4 @@ namespace LuaBind return luaL_error(L, "Enum called \"%s\" is readonly.", name); } - //--------------------------------------------------------------------------------// -#if LUA_BIND_ENABLE_PLAIN_ENUM - int PlainEnum::registry(lua_State* L) - { - // params: - // 1: enum name - // 2: metatable - - cc8* name = luaL_checkstring(L, 1); - - if (!lua_istable(L, 2)) - { - return luaL_error(L, "Create plain enum failed. Require table, but get %s", luaL_typename(L, 2)); - } - - lua_pushvalue(L, -1); - lua_setfield(L, -2, "__index"); - - lua_pushstring(L, name); - lua_pushcclosure(L, _rmt__newindex, 1); - lua_setfield(L, -2, "__newindex"); - - lua_newtable(L); // enum table - - lua_pushvalue(L, -2); // metatable - lua_setmetatable(L, -2); - - return 1; - } -#endif }
\ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindEnum.h b/Runtime/Lua/LuaBind/LuaBindEnum.h index 122e845..336a117 100644 --- a/Runtime/Lua/LuaBind/LuaBindEnum.h +++ b/Runtime/Lua/LuaBind/LuaBindEnum.h @@ -19,21 +19,6 @@ namespace LuaBind extern int _rmt__newindex(lua_State* L); - //--------------------------------------------------------------------------------// - -#if LUA_BIND_ENABLE_PLAIN_ENUM - // - // 纯lua的枚举,创建不可修改的table - // - class PlainEnum - { - public: - - static int registry(lua_State* L); - - }; -#endif - } #endif
\ No newline at end of file diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index ecb8196..3d26dc3 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -8,7 +8,7 @@ namespace LuaBind { - OnRegisterClassHandler onRegisterFactoryClass; + OnRegisterClassHandler onRegisterNativeClass; std::string g_NameSpace = ""; @@ -822,24 +822,6 @@ namespace LuaBind 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); @@ -932,9 +914,7 @@ namespace LuaBind return luaL_checkstring(mState, idx); } - // // check light userdata - // template <> const void* State::CheckValue < const void* >(int idx) { diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index e5b8f6c..aa77c26 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -17,7 +17,7 @@ namespace LuaBind typedef void (*ErrorHandler) (cc8 * msg); typedef void(*OnRegisterClassHandler)(State&, int clsIdx, std::string clsName, std::string pkgName); - extern OnRegisterClassHandler onRegisterFactoryClass; + extern OnRegisterClassHandler onRegisterNativeClass; extern std::string g_NameSpace; // 对lua_State的代理,除了保存一个lua_State的引用不保存其他内容。一个实例的metatable如下: @@ -150,7 +150,7 @@ namespace LuaBind // 注册方法 // 注册工厂,适用于普通类,有New方法 - template<class TYPE> void RegisterFactory(); + template<class TYPE> void RegisterNativeClass(); // 注册枚举 void RegisterEnum(cc8* name, Enum* enums); @@ -169,16 +169,6 @@ namespace LuaBind // 根据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; @@ -267,9 +257,9 @@ namespace LuaBind // 注册工厂,注册class table,以type name为键设置在名称空间上。在注册阶段不会设置元表,等到New方法调用的时候才会。 template<class TYPE> - void State::RegisterFactory() + void State::RegisterNativeClass() { - cc8* type = TYPE::GetFactoryName(); + cc8* type = TYPE::GetNativeClassName(); lua_State* L = mState; State& state = *this; @@ -281,12 +271,12 @@ namespace LuaBind lua_newtable(L); int clsIdx = lua_gettop(L); TYPE::RegisterClassShared(state); - TYPE::RegisterFactoryClass(state); + TYPE::RegisterNativeClass(state); TYPE::RegisterClass(state); // 自定义注册内容 - if (onRegisterFactoryClass) - onRegisterFactoryClass(state, clsIdx, type, g_NameSpace); + if (onRegisterNativeClass) + onRegisterNativeClass(state, clsIdx, type, g_NameSpace); // 清理栈 lua_settop(state, clsIdx); @@ -300,13 +290,6 @@ namespace LuaBind //_assertmethod(-1, "New"); #undef _assertmethod -#if LUA_BIND_ENABLE_NATIVE_EXTEND - // .Extend() - lua_pushvalue(state, -1); // class table - lua_pushcclosure(state, TYPE::_ExtendFactory, 1); - lua_setfield(state, -2, "Extend"); -#endif - // class["__index"] = class lua_pushvalue(state, -1); // class table lua_setfield(state, -2, "__index"); diff --git a/Runtime/Lua/LuaBind/LuaBindUtility.h b/Runtime/Lua/LuaBind/LuaBindUtility.h index fd39f47..94f2e9c 100644 --- a/Runtime/Lua/LuaBind/LuaBindUtility.h +++ b/Runtime/Lua/LuaBind/LuaBindUtility.h @@ -3,18 +3,18 @@ // 导出native接口 -// RegisterClass 注册类的方法和成员,比如枚举、常量等到class table GetFactoryName 获得工厂的类名, +// RegisterClass 注册类的方法和成员,比如枚举、常量等到class table GetNativeClassName 获得工厂的类名, // 同时用来避免注册时错误注册为了singleton,通过编译时报错避免 -#define LUA_BIND_DECL_FACTORY(type, ...) \ +#define LUA_BIND_DECL_CLASS(type, ...) \ friend class ::State; \ friend class ::NativeClass<type,##__VA_ARGS__>; \ static void RegisterClass(::State&); \ static void RegisterPostprocess(::State&); \ - static const char* GetFactoryName() { return #type; };\ + static const char* GetNativeClassName() { return #type; };\ static const char* GetClassName() { return #type; }; // 作为基类的抽象工厂类可以使用此宏,注册一个入口,在派生类的注册函数中调用,注册基类的这些方法。 -#define LUA_BIND_DECL_ABSTRACT_FACTORY() \ +#define LUA_BIND_DECL_ABSTRACT_CLASS() \ static void RegisterClass(::State&);\ static void RegisterPostprocess(::State&) @@ -30,8 +30,8 @@ #define LUA_BIND_POSTPROCESS(type) void type::RegisterPostprocess(::State& state) // 用来注册的宏。之前这里忘了用可变宏,导致没有luaclastable ref没有注册对。 -#define LUA_BIND_REGISTER_FACTORY(state, param) state.RegisterFactory<param>() -#define LUA_BIND_REGISTER_ABSTRACT_FACTORY(state, type) type::RegisterPostprocess(state) +#define LUA_BIND_REGISTER_CLASS(state, param) state.RegisterNativeClass<param>() +#define LUA_BIND_REGISTER_ABSTRACT_CLASS(state, type) type::RegisterPostprocess(state) #define LUA_BIND_REGISTER_METHODS(state, ...) \ do{ \ luaL_Reg __m[] = {__VA_ARGS__,{0, 0}}; \ diff --git a/Runtime/Lua/LuaBind/LuaBindVM.h b/Runtime/Lua/LuaBind/LuaBindVM.h index 37810b9..a53a707 100644 --- a/Runtime/Lua/LuaBind/LuaBindVM.h +++ b/Runtime/Lua/LuaBind/LuaBindVM.h @@ -48,7 +48,7 @@ namespace LuaBind RefTable mStrongRefTable; // 全局强引用表 RefTable mWeakRefTable; // 全局弱引用表 - std::unordered_map<int/*ClassID*/, StrongRef> mClasses; // 类型 + std::unordered_map<int, StrongRef> mClasses; #if LUA_BIND_PROFILER size_t mObjectCount; // 统计所有在此虚拟机中创建的实例 |