Line | |
---|
1 | #include "luaincl.h" |
---|
2 | |
---|
3 | #include "lunar.h" |
---|
4 | |
---|
5 | class Account { |
---|
6 | lua_Number m_balance; |
---|
7 | public: |
---|
8 | static const char className[]; |
---|
9 | static Lunar<Account>::RegType methods[]; |
---|
10 | |
---|
11 | Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } |
---|
12 | int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } |
---|
13 | int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } |
---|
14 | int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } |
---|
15 | ~Account() { printf("deleted Account (%p)\n", this); } |
---|
16 | }; |
---|
17 | |
---|
18 | const char Account::className[] = "Account"; |
---|
19 | |
---|
20 | |
---|
21 | #define method(class, name) {#name, &class::name} |
---|
22 | |
---|
23 | Lunar<Account>::RegType Account::methods[] = { |
---|
24 | method(Account, deposit), |
---|
25 | method(Account, withdraw), |
---|
26 | method(Account, balance), |
---|
27 | {0,0} |
---|
28 | }; |
---|
29 | |
---|
30 | |
---|
31 | int main(int argc, char *argv[]) |
---|
32 | { |
---|
33 | lua_State *L = lua_open(); |
---|
34 | |
---|
35 | luaopen_base(L); |
---|
36 | luaopen_table(L); |
---|
37 | luaopen_io(L); |
---|
38 | luaopen_string(L); |
---|
39 | luaopen_math(L); |
---|
40 | luaopen_debug(L); |
---|
41 | |
---|
42 | Lunar<Account>::Register(L); |
---|
43 | |
---|
44 | if(argc>1) lua_dofile(L, argv[1]); |
---|
45 | |
---|
46 | lua_setgcthreshold(L, 0); // collected garbage |
---|
47 | lua_close(L); |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
Note: See
TracBrowser
for help on using the repository browser.