1 | #include "luaincl.h" |
---|
2 | #include <iostream> |
---|
3 | |
---|
4 | #include "lunar.h" |
---|
5 | |
---|
6 | class Account { |
---|
7 | lua_Number m_balance; |
---|
8 | public: |
---|
9 | static const char className[]; |
---|
10 | static Lunar<Account>::RegType methods[]; |
---|
11 | |
---|
12 | Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } |
---|
13 | int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } |
---|
14 | int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } |
---|
15 | int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } |
---|
16 | ~Account() { printf("deleted Account (%p)\n", this); } |
---|
17 | }; |
---|
18 | |
---|
19 | const char Account::className[] = "Account"; |
---|
20 | |
---|
21 | |
---|
22 | #define method(class, name) {#name, &class::name} |
---|
23 | |
---|
24 | Lunar<Account>::RegType Account::methods[] = { |
---|
25 | method(Account, deposit), |
---|
26 | method(Account, withdraw), |
---|
27 | method(Account, balance), |
---|
28 | {0,0} |
---|
29 | }; |
---|
30 | |
---|
31 | |
---|
32 | class Object { |
---|
33 | public: |
---|
34 | static const char className[]; |
---|
35 | static Lunar<Object>::RegType methods[]; |
---|
36 | |
---|
37 | Object(lua_State* L) { } |
---|
38 | ~Object() { printf("deleted Object (%p)\n", this); } |
---|
39 | |
---|
40 | int printName(lua_State* L){std::cout<<"Hi i'm object!"<<std::endl; return 0;} |
---|
41 | }; |
---|
42 | |
---|
43 | const char Object::className[] = "Object"; |
---|
44 | |
---|
45 | Lunar<Object>::RegType Object::methods[] = { |
---|
46 | method(Object, printName), |
---|
47 | {0,0} |
---|
48 | }; |
---|
49 | |
---|
50 | int main(int argc, char *argv[]) |
---|
51 | { |
---|
52 | lua_State *L = lua_open(); |
---|
53 | |
---|
54 | luaopen_base(L); |
---|
55 | luaopen_table(L); |
---|
56 | luaopen_io(L); |
---|
57 | luaopen_string(L); |
---|
58 | luaopen_math(L); |
---|
59 | luaopen_debug(L); |
---|
60 | |
---|
61 | Lunar<Account>::Register(L); |
---|
62 | Lunar<Object>::Register(L); |
---|
63 | Object obj(L); |
---|
64 | Lunar<Object>::insertObject(L,&obj,"Object",false); |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | if(argc>1) lua_dofile(L, argv[1]); |
---|
70 | |
---|
71 | lua_setgcthreshold(L, 0); // collected garbage |
---|
72 | lua_close(L); |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|