forked from sniperHW/luawrapper
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.cpp
306 lines (277 loc) · 5.8 KB
/
test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "luaWrapper.h"
#include <stdio.h>
typedef luacpp::luatable luatable;
typedef luacpp::luaObject luaObject;
#define any_cast luacpp::any_cast
//测试直接调用C函数
static int showmsg(const char *msg)
{
printf("c-showmsg:%s\n",msg);
return 0;
}
void test_call_c_function(lua_State *L)
{
printf("-------call c function---------\n");
luacpp::reg_cfun(L,"cshow",showmsg);
try{
luacpp::call<void>(L,"test1");
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
class test_class_A
{
public:
test_class_A():memb_a(0)
{
printf("test_class_A void\n");
}
test_class_A(const test_class_A &other):memb_a(other.memb_a)
{
printf("test_class_A copy constructor %d \n",memb_a);
}
~test_class_A()
{
printf("~test_class_A\n");
}
int memb_a;
void show()
{
printf("test_class_A::show\n");
}
};
void test_class1(lua_State *L)
{
printf("\n-----pass a c++ pointer to lua-----\n");
luacpp::reg_cclass<test_class_A>::_reg(L,"test_class_A")
.constructor<void>()//无参构造
.constructor<const test_class_A&>()//一个参数构造
.property("memb_a",&test_class_A::memb_a)
.function("show",&test_class_A::show);
test_class_A a;
a.memb_a = 100;
try{
luacpp::call<void>(L,"test2",&a);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
printf("after call test2,a.memb_a:%d\n",a.memb_a);
}
void test_pass_luatable(lua_State *L)
{
printf("\n-----pass luatable to lua-----\n");
try{
luatable lt;
for(int i = 0; i < 10; ++i)
lt.push_back(i);
luacpp::call<void>(L,"test3",lt);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
luatable c_return_luatable()
{
luatable lt;
for(int i = 0; i < 10; ++i)
lt.push_back(i);
return lt;
}
void test_c_return_luatable(lua_State *L)
{
printf("\n-----C++ return a luatable to lua-----\n");
luacpp::reg_cfun(L,"c_return_luatable",c_return_luatable);
try{
luacpp::call<void>(L,"test4");
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_lua_return_luatable(lua_State *L)
{
printf("\n-----lua return a luatable to C++-----\n");
try{
luatable lt = luacpp::call<luatable>(L,"test5");
for(int i = 0 ;i < (int)lt.size(); ++i)
printf("%s\n",any_cast<std::string>(lt[i]).c_str());
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_op_lua_obj(lua_State *L)
{
printf("\n----- operate lua object -----\n");
try{
luaObject lo = luacpp::call<luaObject>(L,"test6");
lo.call<void>("show");
printf("balance:%d\n",lo.Get<int>("balance"));
lo.Set("balance",1000);
printf("balance:%d\n",lo.Get<int>("balance"));
printf("1:%d\n",lo.Get<int>(1));
printf("2:%f\n",lo.Get<double>(2.2));
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
class base
{
public:
virtual void show()
{
printf("this base show\n");
}
void show2()
{
printf("this is not virtual function\n");
}
};
class child:public base
{
public:
void show()
{
printf("this child show\n");
}
};
void test_call_virtual_function(lua_State *L)
{
printf("\n----- lua call C++ virtual function -----\n");
luacpp::reg_cclass<base>::_reg(L,"base")
.function("show",&base::show)
.function("show2",&base::show2);
luacpp::reg_cclass<child,base>::_reg(L,"child");
child c;
base *b = &c;
try{
luacpp::call<void>(L,"test7",b,&c);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_int64(lua_State *L)
{
printf("\n-----use 64 integer in lua-----\n");
try{
int64_t a = 4294967295;
int64_t b = 4294967296;
luacpp::call<void>(L,"test8",a,b);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_pass_c_object(lua_State *L)
{
printf("\n-----pass a C++ Object to lua-----\n");
test_class_A a;
a.memb_a = 100;
try{
luacpp::call<void>(L,"test9",a);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
printf("after test9,a.memb_a:%d\n",a.memb_a);
}
void arg_c_object(test_class_A a)
{
printf("%d\n",a.memb_a);
a.memb_a = 101;
}
void test_call_c_pass_obj(lua_State *L)
{
printf("\n-----pass a C++ reference to lua-----\n");
test_class_A a;
a.memb_a = 100;
luacpp::reg_cfun(L,"arg_c_object",arg_c_object);
try{
luacpp::call<void>(L,"test10",a);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
printf("after test10,a.memb_a:%d\n",a.memb_a);
}
void arg_c_object_ref(test_class_A &a)
{
printf("%d\n",a.memb_a);
a.memb_a = 101;
}
void test_call_c_pass_obj_ref(lua_State *L)
{
printf("\n-----pass a C++ reference to lua-----\n");
test_class_A a;
a.memb_a = 100;
luacpp::reg_cfun(L,"arg_c_object_ref",arg_c_object_ref);
try{
luacpp::call<void>(L,"test11",a);
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
printf("after test11,a.memb_a:%d\n",a.memb_a);
}
void test_create_c_obj(lua_State *L)
{
printf("\n-----construct C++ object in lua-----\n");
try{
luacpp::call<void>(L,"test12");
}catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_getglobal(lua_State *L)
{
printf("\n-----lua global objcet-----\n");
try{
luatable lt = luacpp::Get<luatable>(L,"t_table");
for(int i = 0; i < (int)lt.size();++i)
{
printf("%d\n",any_cast<int>(lt[i]));
}
}
catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
void test_setglobal(lua_State *L)
{
printf("\n-----lua global object-----\n");
try{
luacpp::Set(L,"TEST_GLOBAL","this is test global");
luacpp::call<void>(L,"test13");
}
catch(std::string &err)
{
printf("%s\n",err.c_str());
}
}
int main()
{
luacpp::luaWrapper lw;
lw.dofile("start.lua");
test_call_c_function(lw);
test_class1(lw);
test_pass_luatable(lw);
test_c_return_luatable(lw);
test_lua_return_luatable(lw);
test_op_lua_obj(lw);
test_call_virtual_function(lw);
test_int64(lw);
test_pass_c_object(lw);
test_create_c_obj(lw);
test_getglobal(lw);
test_setglobal(lw);
test_call_c_pass_obj(lw);
test_call_c_pass_obj_ref(lw);
getchar();
return 0;
}