summaryrefslogtreecommitdiff
path: root/source/tests/02-luax/main.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-19 23:06:27 +0800
committerchai <chaifix@163.com>2019-03-19 23:06:27 +0800
commit1497dccd63a84b7ee2b229b1ad9c5c02718f2a78 (patch)
treef8d1bff50da13e126d08c7345653e002e293202d /source/tests/02-luax/main.cpp
parent5e2a973516e0729b225da9de0b03015dc5854ac4 (diff)
*rename
Diffstat (limited to 'source/tests/02-luax/main.cpp')
-rw-r--r--source/tests/02-luax/main.cpp255
1 files changed, 255 insertions, 0 deletions
diff --git a/source/tests/02-luax/main.cpp b/source/tests/02-luax/main.cpp
new file mode 100644
index 0000000..f87226e
--- /dev/null
+++ b/source/tests/02-luax/main.cpp
@@ -0,0 +1,255 @@
+///
+/// Scripting with Lua.
+///
+extern "C"{
+#include "Lua51/lua.h"
+#include "Lua51/lauxlib.h"
+}
+#include "Luax/luax.h"
+#include "header.h"
+
+#include <iostream>
+#include <string>
+
+using namespace std;
+using namespace Luax;
+
+//----------------------------------------------------------------------------------------------------------------
+
+class School
+ : public Singleton<School>
+ , public LuaxNativeClass<School>
+{
+public:
+
+ School() : mName("Koko") {}
+
+private:
+
+ const char* mName;
+
+public:
+
+ LUAX_DECL_SINGLETON(School);
+
+ LUAX_DECL_METHOD(l_GetName);
+
+};
+
+int School::l_GetName(lua_State* L)
+{
+ LUAX_STATE(L);
+
+ School* school = Get();
+
+ state.Push(school->mName);
+ return 1;
+}
+
+void School::RegisterLuaxClass(LuaxState& state)
+{
+ state.SetField(-1, "Region", "Block 1"); // 101
+
+ luaL_Reg regTable[] = {
+ { "GetName", l_GetName },
+ { NULL, NULL }
+ };
+
+ state.RegisterMethods(regTable);
+
+}
+
+void School::RegisterLuaxPostprocess(Luax::LuaxState&)
+{
+}
+
+//----------------------------------------------------------------------------------------------------------------
+
+class Boy : public LuaxNativeClass<Boy>
+{
+public:
+
+ Boy(int age, const char* name) : mAge(age), mName(name){}
+
+ int mAge;
+
+ const char* mName;
+
+private:
+
+public:
+
+ LUAX_DECL_FACTORY(SimBoy);
+
+ // member methods
+ LUAX_DECL_METHOD(l_GetAge);
+ LUAX_DECL_METHOD(l_GetName);
+
+ // class method
+ LUAX_DECL_METHOD(l_New);
+ LUAX_DECL_METHOD(l_GetGender);
+
+ LUAX_DECL_METHOD(l_Write);
+ LUAX_DECL_METHOD(l_Speak);
+
+private:
+
+ LuaxMemberRef mCallbak;
+
+};
+
+int Boy::l_New(lua_State* L)
+{
+ LUAX_STATE(L);
+
+ int age = state.GetValue(1, 0);
+ const char* name = state.GetValue(2, "");
+
+ Boy* boy = new Boy(age, name);
+ boy->PushLuaxUserdata(state);
+
+ return 1;
+}
+
+int Boy::l_GetAge(lua_State* L)
+{
+ LUAX_SETUP(L, "U");
+ Boy* self = state.GetLuaUserdata<Boy>(1);
+ state.Push(self->mAge);
+ return 1;
+}
+
+int Boy::l_GetName(lua_State* L)
+{
+ LUAX_SETUP(L, "U");
+ Boy* self = state.GetLuaUserdata<Boy>(1);
+ state.Push(self->mName);
+ return 1;
+}
+
+int Boy::l_GetGender(lua_State* L)
+{
+ LUAX_SETUP(L, "*");
+ state.Push("male student!");
+ return 1;
+}
+
+int Boy::l_Speak(lua_State* L)
+{
+ LUAX_STATE(L);
+
+ Boy* self = state.GetLuaUserdata<Boy>(1);
+ self->PushMemberRef(state, self->mCallbak);
+ state.Call(0, 1);
+ return 1;
+}
+
+int Boy::l_Write(lua_State* L)
+{
+ LUAX_STATE(L);
+ // self, func
+ Boy* self = state.GetLuaUserdata<Boy>(1);
+ self->SetMemberRef(state, self->mCallbak, 2);
+ return 0;
+}
+
+void Boy::RegisterLuaxClass(LuaxState& state)
+{
+ luaL_Reg regTable[] = {
+ // class functions
+ { "New", l_New },
+ { "GetGender", l_GetGender },
+ // members
+ { "GetAge", l_GetAge },
+ { "GetName", l_GetName },
+ { "Write", l_Write },
+ { "Speak", l_Speak },
+ { 0, 0}
+ };
+
+ state.RegisterMethods(regTable);
+
+ // boyİ
+ LuaxEnum EHabits[] = {
+ { "Computer", 1},
+ { "Buscketball", 2},
+ { "Baseball", 3},
+ { "Girls", 4},
+ {0, 0}
+ };
+
+ state.RegisterEnum("EHabits", EHabits);
+}
+
+void Boy::RegisterLuaxPostprocess(LuaxState& state)
+{
+ // boyİ
+ LuaxEnum EHabits[] = {
+ { "Computer", 1},
+ { "Buscketball", 2},
+ { "Baseball", 3},
+ { "Girls", 4},
+ {0, 0}
+ };
+
+ state.RegisterEnum("EHabits", EHabits);
+}
+
+//----------------------------------------------------------------------------------------------------------------
+
+class Girl : public LuaxNativeClass<Girl>
+{
+};
+
+//----------------------------------------------------------------------------------------------------------------
+
+#include "script.lua"
+
+//----------------------------------------------------------------------------------------------------------------
+
+int func(lua_State* L)
+{
+ LUAX_SETUP(L, "*");
+ state.Push("func ok");
+ return 1;
+}
+
+int main()
+{
+ LuaxRuntime& runtime = LuaxRuntime::Get();
+ lua_State* L = runtime.Open();
+
+ Luax::LuaxState& state = runtime[L].state;
+ state.OpenLibs();
+ state.PushGlobalNamespace();
+ state.PushNamespace("Asura");
+ state.RegisterPlainClassRegistry("Class"); // Asura.Class("Foo")
+ state.RegisterPlainEnumRegistry("Enum"); // Asura.Enum("EFoo", { ... });
+ //עenum
+ LuaxEnum EGender[] = {
+ { "BOY", 7 },
+ { "GIRL", 8 },
+ { "MID", 9 },
+ { 0, 0 },
+ };
+
+ state.RegisterEnum("EGender", EGender);
+
+ state.PushNamespace("author");
+ state.SetField(-1, "name", "chai");
+ state.SetField(-1, "func", func);
+ state.PopNamespace(); // Asura.author
+ state.SetField(-1, "version", 100);
+ cout << state.GetField<float>(-1, "version", 0);
+
+ state.RegisterSingleton<School>();
+ state.RegisterFactory<Boy>();
+
+ state.PopNamespace(); // Asura
+ state.PopNamespace(); // Global
+ state.DoString(script);
+
+ runtime.Close(L);
+
+ getchar();
+} \ No newline at end of file