summaryrefslogtreecommitdiff
path: root/Source/Samples/LuaxTest/main.cpp
blob: 8dc728169a36ac1d28b15735dd2dc5eafad10458 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///
/// 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)
{
	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.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)
{
	LUAX_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)
--[[
	local Kid = Asura.SimBoy.Extend() 
	Asura.Kid = Kid
	Kid.New = function(self, height, age) 
		self.base(age)
		self.height = height
	end
	Kid.GetHeight = function(self) 
		print(self.height)
	end	
	local kid = Kid.New(110, 12)
	kid:GetHeight()
]]
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()
{
	LuaxRuntime& runtime = LuaxRuntime::Get();
	lua_State* L = runtime.Open();

	Luax::LuaxState& state = runtime[L].state;
	LuaxRefTable& strong = runtime[L].strongRefTable;
	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);
	
	runtime.Close(L);

	getchar();
}