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