summaryrefslogtreecommitdiff
path: root/Runtime/Lua/LuaBind/LuaBindClass.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-23 00:31:25 +0800
committerchai <chaifix@163.com>2021-10-23 00:31:25 +0800
commitdf0444f85f9bf623cc886e1632e624ef20cb0f1b (patch)
tree4aff1454312540f137bb318349b48e58d9b06d80 /Runtime/Lua/LuaBind/LuaBindClass.cpp
parent4dafefe46a72ba47468b13d011f8299055081b0f (diff)
-member table
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindClass.cpp')
-rw-r--r--Runtime/Lua/LuaBind/LuaBindClass.cpp235
1 files changed, 0 insertions, 235 deletions
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