diff options
author | chai <chaifix@163.com> | 2019-08-07 21:08:47 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-07 21:08:47 +0800 |
commit | 0c391fdbce5a079cf03e483eb6174dd47806163d (patch) | |
tree | b06cd7a9d0ae0d9bb9e82f3dcb786dfce11f8628 /Source/tests/02-luax/main.cpp | |
parent | 9686368e58e25cbd6dc37d686bdd2be3f80486d6 (diff) |
*misc
Diffstat (limited to 'Source/tests/02-luax/main.cpp')
-rw-r--r-- | Source/tests/02-luax/main.cpp | 305 |
1 files changed, 305 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..59a4bcb --- /dev/null +++ b/Source/tests/02-luax/main.cpp @@ -0,0 +1,305 @@ +/// +/// 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.CheckUserdata<Boy>(1); + state.Push(self->mAge); + return 1; +} + +int Boy::l_GetName(lua_State* L) +{ + LUAX_SETUP(L, "U"); + Boy* self = state.CheckUserdata<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.CheckUserdata<Boy>(1); + self->PushLuaxMemberRef(state, self->mCallbak); + state.Call(0, 1); + return 1; +} + +int Boy::l_Write(lua_State* L) +{ + LUAX_STATE(L); + // self, func + Boy* self = state.CheckUserdata<Boy>(1); + self->SetLuaxMemberRef(state, self->mCallbak, 2); + return 0; +} + +void Boy::RegisterLuaxClass(LuaxState& state) +{ + + LUAX_REGISTER_METHODS(state, + { "New", l_New },/**/ + { "GetGender", l_GetGender }, + { "GetAge", l_GetAge }, + { "GetName", l_GetName }, + { "Write", l_Write }, + { "Speak", l_Speak } + ); + + // boyİ + LUAX_REGISTER_ENUM(state, "EHabits", + { "Computer", 1 }, + { "Buscketball", 2 }, + { "Baseball", 3 }, + { "Girls", 4 } + ); +} + +void Boy::RegisterLuaxPostprocess(LuaxState& state) +{ + // boyİ + LUAX_REGISTER_ENUM(state, "EHabits", + { "Computer", 1 }, + { "Buscketball", 2 }, + { "Baseball", 3 }, + { "Girls", 4 } + ); +} + +//--------------------------------------------------------------------------------// +/// +/// Ӧsignalıհ +/// +class Slot +{ +public: + Slot(LuaxState& state, Boy* widget, int refID) + : mState(state) + , mRefID(refID) + , mWidget(widget) + { + } + + void operator()() + { + ASSERT(mState); + ASSERT(mWidget); + + mWidget->PushLuaxMemberRef(mState, mRefID); + if (lua_isfunction(mState, -1)) // callback + { + mState.Call(0, 0); + } + } + +private: + LuaxState & mState; // + Boy* mWidget; // ӦĿؼ + int mRefID; // ؼ + +}; +/// +/// ؼ¼ +/// +class Signal +{ +public: + Signal(); + + /// + /// Fire¼connectļߣãconnectĺ + /// + void operator()() + { + for (auto callback : mCallbacks) + callback(); + } + + /// + /// עص + /// + void Connect(const Slot& callback) + { + mCallbacks.push_back(callback); + } + + /// + /// + /// + void Disconnect(); + +private: + std::vector<Slot> mCallbacks; // + +}; + +//--------------------------------------------------------------------------------// + +#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); +}
\ No newline at end of file |