1 | |
---|
2 | #include <iostream> |
---|
3 | |
---|
4 | #include "luaincl.h" |
---|
5 | #include "lunar.h" |
---|
6 | #include "script.h" |
---|
7 | |
---|
8 | |
---|
9 | class Account : public BaseObject { |
---|
10 | lua_Number m_balance; |
---|
11 | public: |
---|
12 | static const char className[]; |
---|
13 | static Lunar<Account>::RegType methods[]; |
---|
14 | |
---|
15 | Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } |
---|
16 | Account(double balance=0) : m_balance(balance) { } |
---|
17 | |
---|
18 | void deposit (float value) { m_balance += value; }; |
---|
19 | void withdraw(float value) { m_balance -= value; }; |
---|
20 | float balance() { return m_balance; }; |
---|
21 | |
---|
22 | int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } |
---|
23 | int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } |
---|
24 | int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } |
---|
25 | ~Account() { printf("deleted Account (%p)\n", this); } |
---|
26 | }; |
---|
27 | |
---|
28 | const char Account::className[] = "Account"; |
---|
29 | |
---|
30 | Lunar<Account>::RegType Account::methods[] = { |
---|
31 | {"deposit", new ExecutorLua1<Account, float>(&Account::deposit)}, |
---|
32 | {"withdraw", new ExecutorLua1<Account, float>(&Account::withdraw)}, |
---|
33 | {"balance", new ExecutorLua0ret<Account, float>(&Account::balance)}, |
---|
34 | {0,NULL} |
---|
35 | }; |
---|
36 | |
---|
37 | |
---|
38 | class Object : public BaseObject { |
---|
39 | public: |
---|
40 | static const char className[]; |
---|
41 | static Lunar<Object>::RegType methods[]; |
---|
42 | |
---|
43 | Object(lua_State* L) {callCount = 0; } |
---|
44 | Object(){callCount = 0;}; |
---|
45 | ~Object() { printf("deleted Object (%p)\n", this); } |
---|
46 | |
---|
47 | //meber functions |
---|
48 | void takeParam(int i) |
---|
49 | { |
---|
50 | printf("Lua passed %i to c++\n",i); |
---|
51 | } |
---|
52 | |
---|
53 | int printName(lua_State* L) |
---|
54 | { |
---|
55 | this->printName(); |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | void printName() |
---|
60 | { |
---|
61 | callCount ++; |
---|
62 | printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); |
---|
63 | } |
---|
64 | |
---|
65 | int getCallCount(){ return callCount; } |
---|
66 | |
---|
67 | private: |
---|
68 | int callCount; |
---|
69 | |
---|
70 | }; |
---|
71 | |
---|
72 | const char Object::className[] = "Object"; |
---|
73 | |
---|
74 | Lunar<Object>::RegType Object::methods[] = { |
---|
75 | {"printName", new ExecutorLua0<Object>(&Object::printName)}, |
---|
76 | {"getCallCount", new ExecutorLua0ret<Object, int>(&Object::getCallCount)}, |
---|
77 | {"takeParam", new ExecutorLua1<Object, int>(&Object::takeParam)}, |
---|
78 | {0,0} |
---|
79 | }; |
---|
80 | |
---|
81 | int main(int argc, char *argv[]) |
---|
82 | { |
---|
83 | Script script; |
---|
84 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
85 | |
---|
86 | // Register classes with the script |
---|
87 | Lunar<Account>::Register(&script); |
---|
88 | Lunar<Object>::Register(&script); |
---|
89 | |
---|
90 | Object* obj= new Object(); |
---|
91 | Account a; |
---|
92 | Account *b = new Account(30); |
---|
93 | |
---|
94 | // Add instances to the script |
---|
95 | Lunar<Object>::insertObject(&script,obj,"Obj",true); |
---|
96 | Lunar<Account>::insertObject(&script,&a,"a",false); |
---|
97 | Lunar<Account>::insertObject(&script,b,"b",true); |
---|
98 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
99 | |
---|
100 | //Load a file |
---|
101 | std::string file(argv[1]); |
---|
102 | |
---|
103 | if(script.loadFile(file)) |
---|
104 | printf("File %s succefully loaded\n", file.c_str()); |
---|
105 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
106 | |
---|
107 | //execute a function |
---|
108 | printf("----------- main -----------\n"); |
---|
109 | std::string main("main"); |
---|
110 | if( script.selectFunction(main,3)) |
---|
111 | printf("function %s selected\n",main.c_str()); |
---|
112 | |
---|
113 | script.pushParam(3.14159,main); |
---|
114 | script.executeFunction(); |
---|
115 | |
---|
116 | float retf = script.getReturnedFloat(); |
---|
117 | printf("main returned %f\n",retf); |
---|
118 | |
---|
119 | |
---|
120 | if(script.getReturnedBool()) |
---|
121 | printf("main returned true\n"); |
---|
122 | else |
---|
123 | printf("main returned false\n"); |
---|
124 | |
---|
125 | int ret = script.getReturnedInt(); |
---|
126 | printf("main returned %i\n",ret); |
---|
127 | |
---|
128 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
129 | //execute a 2nd function |
---|
130 | printf("----------- test -----------\n"); |
---|
131 | std::string test("test"); |
---|
132 | if( script.selectFunction(test,0)) |
---|
133 | printf("function %s selected\n",test.c_str()); |
---|
134 | |
---|
135 | script.executeFunction(); |
---|
136 | |
---|
137 | |
---|
138 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
---|
139 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|