diff options
author | chai <chaifix@163.com> | 2019-03-17 10:49:50 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-17 10:49:50 +0800 |
commit | ad2805aa839892589c13a615730da52f6e474709 (patch) | |
tree | f818bbacee347f8d64d9134ffe4fbda366ba87fe /Source/3rdParty/Luax/luax_class.cpp | |
parent | fb830270c5269a22819a0c0f1ee280f9aa3f1b39 (diff) |
*luax
Diffstat (limited to 'Source/3rdParty/Luax/luax_class.cpp')
-rw-r--r-- | Source/3rdParty/Luax/luax_class.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/Source/3rdParty/Luax/luax_class.cpp b/Source/3rdParty/Luax/luax_class.cpp new file mode 100644 index 0000000..bdff377 --- /dev/null +++ b/Source/3rdParty/Luax/luax_class.cpp @@ -0,0 +1,104 @@ +#include "luax_class.hpp" +#include "luax_runtime.h" +#include "luax_cfunctions.h" + +namespace Luax +{ + + int LuaxPlainClass::registry(lua_State* L) + { + LUAX_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"); + + // New() + lua_pushvalue(L, -1); // class table + lua_pushcclosure(L, l_New, 1); + lua_setfield(L, -2, "New"); + + // Extend() + + lua_pushvalue(L, -1); // class table + lua_setfield(L, -2, "__index"); + + return 1; + } + + /// + /// NewnԻȡCtorCtorʼʵ + /// + int LuaxPlainClass::l_New(lua_State* L) + { + LUAX_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); + + lua_getfield(L, classTable, "Ctor"); + if (!lua_isnil(L, -1)) + { + // stack: + // -1: Ctor() + // -2: instance + // -3~-n-2: params + + lua_insert(L, -2 - n); + // stack: + // -1: instance + // -2~-n-1: params + // -n-2: Ctor() + + lua_pushvalue(L, -1); + // stack: + // -1: instance + // -2: instance + // -3~-n-2: params + // -n-3: Ctor + + lua_insert(L, -3 - n); + // stack: + // -1: instance + // -2~-n-1: params + // -n-2: Ctor() + // -n-3: instance + + lua_insert(L, -1 - n); + // stack: + // -1~-n: params + // -n-1: instance + // -n-2: Ctor() + // -n-3: instance + + lua_pcall(L, n + 1, 0, 0); + } + else + { + state.Pop(); + } + + return 1; + } + +}
\ No newline at end of file |