This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtestlua.cpp
241 lines (194 loc) · 5.18 KB
/
testlua.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <cstdlib>
#include <iostream>
extern "C" {
#include "lua/lualib.h"
}
#include "lualite/lualite.hpp"
struct point
{
int x;
int y;
};
inline int set(lua_State* const L, point p)
{
using namespace ::lualite;
lua_createtable(L, 2, 0);
lua_pushliteral(L, "x");
set(L, const_cast<decltype(p.x) const&>(p.x));
assert(lua_istable(L, -3));
lua_rawset(L, -3);
lua_pushliteral(L, "y");
set(L, const_cast<decltype(p.y) const&>(p.y));
assert(lua_istable(L, -3));
lua_rawset(L, -3);
return 1;
}
template <std::size_t I, typename T>
inline std::enable_if_t<
::std::is_same<T, point>{}, point
>
get(lua_State* const L)
{
using namespace ::lualite;
assert(lua_istable(L, I));
struct point p;
lua_pushliteral(L, "x");
lua_rawget(L, -2);
p.x = lua_tointeger(L, -1);
lua_pushliteral(L, "y");
lua_rawget(L, -2);
p.y = lua_tointeger(L, -1);
return p;
}
point testfunc(int i, int j, int k)
{
std::cout << "testfunc(): " << i << " " << j << " " << k << std::endl;
return {-1, -222};
}
void testpair(std::pair<char const*, char const*> const& p)
{
std::cout << "first: " << p.first
<< " second: " << p.second << std::endl;
}
void testtuple(std::tuple<char const*, char const*, int> const& p)
{
std::cout << "first: " << std::get<0>(p)
<< " second: " << std::get<1>(p)
<< " third: " << std::get<2>(p) << std::endl;
}
struct testbase
{
std::string dummy(std::string msg)
{
return { "dummy() called: " + msg };
}
};
struct testclass : testbase
{
testclass()
: a_(777)
{
std::cout << "testclass::testclass()" << std::endl;
}
testclass(int i)
: a_(777)
{
std::cout << "testclass::testclass(int):" << i << std::endl;
}
std::vector<std::string> print(std::string msg) const
{
std::cout << "hello world!: " << msg << std::endl;
return { 10, "bla!!!" };
}
std::tuple<int, std::string, char const*> print(int i)
{
std::cout << i << std::endl;
return std::make_tuple(9, "huh?", "tralala");
}
int const& a()
{
std::cout << "getter called: " << a_ << std::endl;
return a_;
}
void set_a(int i)
{
std::cout << "setter called: " << i << std::endl;
a_ = i;
}
std::string const& test_array(std::array<int, 10> const& a)
{
std::cout << a[a.size() - 1] << std::endl;
s_ = "blablabla";
return s_;
}
testclass* pointer()
{
return this;
}
testclass& reference()
{
return *this;
}
private:
int a_;
std::string s_;
};
int main(int argc, char* argv[])
{
lua_State* L(luaL_newstate());
luaL_openlibs(L);
lualite::module{L,
lualite::class_<testbase>("testbase")
.constant("__classname", "testbase")
.constant("__b", true)
.constant("__pi", 3.1459)
.def<decltype(&testbase::dummy), &testbase::dummy>("dummy"),
lualite::class_<testclass>("testclass")
.constructor("defaultNew")
.constructor<int>()
.inherits<testbase>() // you can add more classes to inherit from
.enum_("smell", 9)
.def<std::tuple<int, std::string, char const*> (testclass::*)(int), &testclass::print>("print")
.def<std::vector<std::string> (testclass::*)(std::string) const, &testclass::print>("print_")
.def<LLFUNC(testclass::pointer)>("pointer")
.def<LLFUNC(testclass::reference)>("reference")
.property<LLFUNC(testclass::a), LLFUNC(testclass::set_a)>("a")
.def<LLFUNC(testclass::test_array)>("test_array"),
lualite::scope("subscope",
lualite::class_<testclass>("testclass")
.constructor<>("defaultNew")
.constructor<int>()
.enum_("smell", 10)
.def<LLFUNC(testfunc)>("testfunc")
.def<std::tuple<int, std::string, char const*> (testclass::*)(int), &testclass::print>("print")
.def<std::vector<std::string> (testclass::*)(std::string) const, &testclass::print>("print_")
)
}
.enum_("apple", 1)
.def<LLFUNC(testfunc)>("testfunc")
.def<LLFUNC(testpair)>("testpair")
.def<LLFUNC(testtuple)>("testtuple");
luaL_dostring(
L,
"local a = testfunc(3, 2, 1)\n"
"r = { \"my\", \"pair\" }\n"
"testpair(r)\n"
"r[3] = 3\n"
"testtuple(r)\n"
"print(a.y)\n"
"print(apple)\n"
"print(testclass.smell)\n"
"print(testbase.__classname)\n"
"print(testbase.__b)\n"
"print(testbase.__pi)\n"
"local b = testclass.defaultNew()\n"
"print(\"---\")\n"
"print(b.a)\n"
"b:reference().a = 888\n"
"print(b.dummy)\n"
"print(b.a .. \" \" .. b:dummy(\"test\"))\n"
"local tmp1, tmp2, tmp3 = b:pointer():print(100)\n"
"print(tmp1 .. \" \" .. tmp2 .. \" \" .. tmp3)\n"
"b:reference():print_(\"msg1\")\n"
"local a = subscope.testclass.new(1111)\n"
"print(subscope.testclass.smell)\n"
"subscope.testclass.testfunc(200, 0, 1)\n"
"local c = a:reference():print_(\"msg2\")\n"
"print(c[10])\n"
"r = {}"
"for i = 1, 10 do\n"
" r[i] = 7\n"
"end\n"
"print(a:test_array(r))\n"
);
::std::cout << ::lualite::class_<testclass>::inherits("testbase") <<
::std::endl;
lua_getglobal(L, "print");
::lualite::call(L, 0, "--test--");
::std::cout << ::lualite::hash("test") <<
" " <<
::lualite::hash("test") <<
::std::endl;
lua_close(L);
return EXIT_SUCCESS;
}