summaryrefslogtreecommitdiff
path: root/Source/Samples/LuaxTest/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Samples/LuaxTest/main.cpp')
-rw-r--r--Source/Samples/LuaxTest/main.cpp190
1 files changed, 190 insertions, 0 deletions
diff --git a/Source/Samples/LuaxTest/main.cpp b/Source/Samples/LuaxTest/main.cpp
new file mode 100644
index 0000000..828b427
--- /dev/null
+++ b/Source/Samples/LuaxTest/main.cpp
@@ -0,0 +1,190 @@
+///
+/// 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 : Singleton<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)
+{
+ LuaxState 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.Register(regTable);
+
+}
+
+//----------------------------------------------------------------------------------------------------------------
+
+class Boy : public LuaxClass
+{
+public:
+
+ Boy(int age) : mAge(age) {}
+
+private:
+
+ int mAge;
+
+public:
+
+ LUAX_DECL_FACTORY(SimBoy);
+
+ // member methods
+ LUAX_DECL_METHOD(l_New);
+ LUAX_DECL_METHOD(l_GetAge);
+
+ // class method
+ LUAX_DECL_METHOD(l_GetGender);
+
+};
+
+int Boy::l_New(lua_State* L)
+{
+ LuaxState state(L);
+
+ int age = state.GetValue(1, 0);
+
+ Boy* boy = new Boy(age);
+ boy->PushLuaUserdata(state);
+
+ return 1;
+}
+
+int Boy::l_GetAge(lua_State* L)
+{
+ LUAX_SETUP(L, "U");
+
+ return 1;
+}
+
+int Boy::l_GetGender(lua_State* L)
+{
+ LUAX_SETUP(L, "*");
+ state.Push("male student!");
+ return 1;
+}
+
+void Boy::RegisterLuaxClass(LuaxState& state)
+{
+ state.SetField(-1, "Class", 101); // 101
+ state.SetField(-1, "Gender", "Male"); // 101
+
+ luaL_Reg regTable[] = {
+ {"GetGender", l_GetGender},
+ {NULL, NULL}
+ };
+
+ state.Register(regTable);
+}
+
+void Boy::RegisterLuaxInterface(LuaxState& state)
+{
+ luaL_Reg regTable[] = {
+ {"New", l_New},
+ {NULL, NULL}
+ };
+
+ state.Register(regTable);
+}
+
+//----------------------------------------------------------------------------------------------------------------
+
+string script = R"(
+function main()
+ local a = 19
+ print(Asura.version)
+ print(Asura.author.name)
+ print("ok")
+ print(Asura.author.func())
+-- local boy = Asura.SimBoy.New("I am peter!", 19)
+-- boy:Say()
+-- print(Asura.SimSchool.GetName())
+ print(Asura.SimBoy.Class)
+ print(Asura.SimBoy.Gender)
+ print(Asura.SimBoy.GetGender())
+ print(Asura.SimBoy.GetClassName())
+ print(Asura.School.GetName())
+ print(Asura.School.Region)
+end
+function err(msg)
+ print(msg)
+end
+xpcall(main, err)
+)";
+
+//----------------------------------------------------------------------------------------------------------------
+
+int func(lua_State* L)
+{
+ LUAX_SETUP(L, "*");
+ state.Push("func ok");
+ return 1;
+}
+
+int main()
+{
+ lua_State* L = lua_open();
+ Luax::LuaxState state(L);
+ state.OpenLibs();
+ state.PushNamespace("Asura");
+ 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.DoString(script);
+
+ lua_close(L);
+
+ getchar();
+} \ No newline at end of file