summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-17 11:41:14 +0800
committerchai <chaifix@163.com>2019-03-17 11:41:14 +0800
commit5a9bbd481f94119488d845f5b8272734dfda82cc (patch)
tree15a340153d6a32fcaeee840906685519aaa59a46 /Source
parentad2805aa839892589c13a615730da52f6e474709 (diff)
*enum
Diffstat (limited to 'Source')
-rw-r--r--Source/3rdParty/Luax/luax_class.cpp58
-rw-r--r--Source/3rdParty/Luax/luax_enum.cpp30
-rw-r--r--Source/3rdParty/Luax/luax_enum.h3
-rw-r--r--Source/3rdParty/Luax/luax_state.cpp7
-rw-r--r--Source/Samples/LuaxTest/script.lua28
5 files changed, 126 insertions, 0 deletions
diff --git a/Source/3rdParty/Luax/luax_class.cpp b/Source/3rdParty/Luax/luax_class.cpp
index bdff377..3840d00 100644
--- a/Source/3rdParty/Luax/luax_class.cpp
+++ b/Source/3rdParty/Luax/luax_class.cpp
@@ -21,12 +21,20 @@ namespace Luax
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, l_New, 1);
lua_setfield(L, -2, "New");
// Extend()
+ lua_pushvalue(L, -1); // class table
+ lua_pushcclosure(L, l_Extend, 1);
+ lua_setfield(L, -2, "Extend");
lua_pushvalue(L, -1); // class table
lua_setfield(L, -2, "__index");
@@ -101,4 +109,54 @@ namespace Luax
return 1;
}
+ int LuaxPlainClass::l_Extend(lua_State* L)
+ {
+ LUAX_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, l_New, 1);
+ lua_setfield(L, -2, "New");
+
+ // Extend()
+ lua_pushvalue(L, -1); // class table
+ lua_pushcclosure(L, l_Extend, 1);
+ lua_setfield(L, -2, "Extend");
+
+ // .base
+ lua_pushvalue(L, baseClass);
+ lua_setfield(L, -2, "base");
+
+ lua_pushvalue(L, -1); // class table
+ lua_setfield(L, -2, "__index");
+
+ // classmetatableΪbaseClass
+ lua_pushvalue(L, baseClass);
+ lua_setmetatable(L, -2);
+
+ return 1;
+ }
+
} \ No newline at end of file
diff --git a/Source/3rdParty/Luax/luax_enum.cpp b/Source/3rdParty/Luax/luax_enum.cpp
index a58593a..af832d1 100644
--- a/Source/3rdParty/Luax/luax_enum.cpp
+++ b/Source/3rdParty/Luax/luax_enum.cpp
@@ -34,4 +34,34 @@ namespace Luax
return luaL_error(L, "Enum called \"%s\" is readonly.", name);
}
+ //--------------------------------------------------------------------------------------------------------------
+
+ int LuaxPlainEnum::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, 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;
+ }
+
} \ No newline at end of file
diff --git a/Source/3rdParty/Luax/luax_enum.h b/Source/3rdParty/Luax/luax_enum.h
index 17fcda4..19b2b83 100644
--- a/Source/3rdParty/Luax/luax_enum.h
+++ b/Source/3rdParty/Luax/luax_enum.h
@@ -26,6 +26,9 @@ namespace Luax
///
class LuaxPlainEnum
{
+ public:
+
+ static int registry(lua_State* L);
};
diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp
index 0913ee3..94ca148 100644
--- a/Source/3rdParty/Luax/luax_state.cpp
+++ b/Source/3rdParty/Luax/luax_state.cpp
@@ -707,4 +707,11 @@ namespace Luax
lua_setfield(mState, -2, name);
}
+ void LuaxState::RegisterPlainEnumRegistry(cc8* name)
+ {
+ assert(lua_istable(mState, -1));
+ lua_pushcfunction(mState, LuaxPlainEnum::registry);
+ lua_setfield(mState, -2, name);
+ }
+
} \ No newline at end of file
diff --git a/Source/Samples/LuaxTest/script.lua b/Source/Samples/LuaxTest/script.lua
index 97890c0..7cea38d 100644
--- a/Source/Samples/LuaxTest/script.lua
+++ b/Source/Samples/LuaxTest/script.lua
@@ -80,7 +80,35 @@ function main()
local foo = Foo.New(10, "lee", kid)
print(foo:GetName())
print(Foo.GetClassName())
+ print(foo.GetClassName())
print(foo.boy:GetAge())
+--------------------inherits test
+ local Coo = Foo.Extend("Coo")
+ print(Coo.GetClassName())
+ local coo = Coo.New(20, "Wang", kid)
+ print(coo:GetAge())
+ Coo.Ctor = function(self, age, name, boy)
+ self.age = age - 1
+ self.name = boy:GetName()
+ self.boy = boy
+ self.base.Ctor(self, age, name, boy)
+ end
+ Coo.GetName = function(self)
+ local name = self.base.GetName(self)
+ return "his name is " .. name
+ end
+ local coo2 = Coo.New(20, "Wang", kid)
+ print(coo2:GetAge())
+ print(coo2.GetClassName())
+ print(coo2:GetName())
+---------------------plain enum test
+ local ERace = Asura.Enum("ERace", {
+ ["White"] = 1,
+ ["Asian"] = 2,
+ ["Black"] = 3,
+ })
+ print(ERace.White)
+ print(ERace.Asian)
end
function err(msg)
print(msg)