Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/main.cc @ 7653

Last change on this file since 7653 was 7653, checked in by bensch, 18 years ago

orxonox/script_engine: namespace OrxScript introduced

File size: 7.2 KB
Line 
1#include <iostream>
2#include <string>
3#include <assert.h>
4#include <stdio.h>
5#include <string.h>
6
7#include "luaincl.h"
8#include "VirtualMachine.h"
9#include "Script.h"
10#include "LuaCallback.h"
11#include "RestoreStack.h"
12#include "scriptable.h"
13
14
15using namespace OrxScript;
16
17//HACK !!!
18/**
19 * @brief This function adds an object to a script so that the object is accessable from within the luascript. this function is later included in LuaScript as a method
20 *
21 * @param scriptable the scriptable object to add.
22 * @param strObjName the name that the object goes by in the script.
23 * @param luaScript  the script to which the scrtiptable is added
24 *
25 * @return a reference to the added object
26 *
27 *
28 * @todo add this function as a method to LuaScript
29 */
30
31
32int addObjectToScript(Scriptable* scriptable, const std::string& strObjName, LuaScript& luaScript)
33{
34
35  lua_State *state = (lua_State *) (luaScript.getVirtualMachine());
36  LuaVirtualMachine& virtualMachine = luaScript.getVirtualMachine();
37
38  //if() TODO: check if strObjName isn't "this"
39  if (virtualMachine.isOk ())
40     {
41       /*create object table*/
42      // Create a reference to the "object" table and set a name for the reference. Each reference is unique
43
44      lua_newtable (state);
45      int  objRef = luaL_ref (state, LUA_REGISTRYINDEX);
46      lua_rawgeti (state, LUA_REGISTRYINDEX, objRef);
47      lua_setglobal (state, strObjName.c_str());
48
49
50      // Save the "object" table to index 0 of the "object" table
51      LuaRestoreStack rs(virtualMachine);
52      lua_rawgeti (state, LUA_REGISTRYINDEX, objRef);
53      lua_pushlightuserdata (state, scriptable);
54      lua_rawseti (state, -2, 0);
55
56
57
58      return objRef;
59     }
60
61}
62
63
64/**
65 * @brief This function adds a function to a scriptable object in the lua state so that the function is accessable from within the luascript. this function is later included in LuaScript as a method
66           *
67           * @param scriptableRef a reference to the scriptable object
68           * @param strFuncName the name that the function goes by in the script.
69           * @param luaScript  the script to which the function is added
70           *
71           * @returns the pseudoindex of the function (The first function added has index 0, the 2nd 1 and so on)
72           *
73           * @todo add this function as a method to LuaScript
74 */
75
76
77int addFunctionToScriptable(int scriptableRef, const std::string& strFuncName, LuaScript& luaScript)
78  {
79    lua_State *state = (lua_State *) (luaScript.getVirtualMachine());
80    LuaVirtualMachine& vm = luaScript.getVirtualMachine();
81    int iMethodIdx = -1;
82
83    if (vm.isOk ())
84    {
85     /* Add function */
86     LuaRestoreStack rs (vm);
87     iMethodIdx = 0;
88     lua_rawgeti (state, LUA_REGISTRYINDEX, scriptableRef);
89     lua_pushstring (state, strFuncName.c_str());
90     lua_pushnumber (state, (lua_Number) iMethodIdx);
91     lua_pushcclosure (state, luaCallback, 1);
92     lua_settable (state, -3);
93    }
94
95    return iMethodIdx;
96  }
97
98class objectOne : public Scriptable
99{
100  public:
101    objectOne () : Scriptable ()
102    {
103
104    }
105
106    int scriptCalling (LuaVirtualMachine& vm, std::string functionName)
107    {
108      //switch (iFunctionNumber) //- methodBase) uncomment this when methodBase gets correclty set in the Scriptable class
109      if(functionName.compare("printNameOne")==0)
110      {
111        //case 0:
112          return printNameOne (vm);
113      }
114
115      return 0;
116    }
117
118
119    int printNameOne(LuaVirtualMachine& vm)
120    {
121    std::cout<<"Hi I'm Object 1 !"<<std::endl;
122    return 0;
123    }
124
125    void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
126  {
127
128  }
129
130};
131
132class objectTwo : public Scriptable
133{
134  public:
135    objectTwo () : Scriptable ()
136    {
137      registerFunction(std::string("printNameTwo"));
138     // registerFunction(std::string("printHello"));
139
140    }
141
142    int scriptCalling (LuaVirtualMachine& vm, std::string functionName)
143    {
144      //switch (iFunctionNumber) //- m_iMethodBase) //TODO:find a way to determine the function number somehow: this works just incidentally
145      if(functionName.compare("printNameTwo")==0)
146      {
147        //case 0:
148
149          return printNameTwo (vm);
150      }
151
152      return 0;
153    }
154
155
156    int printNameTwo(LuaVirtualMachine& vm)
157    {
158      std::cout<<"Hi I'm Object 2 !\n"<<std::endl;
159      return 0;
160    }
161
162    int printHello(LuaVirtualMachine& vm)
163    {
164      std::cout<<"Nice to meet you!\n"<<std::endl;
165      return 0;
166    }
167
168    void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
169    {
170
171    }
172
173};
174
175
176
177
178class CMyScript : public LuaScript
179{
180  public:
181    CMyScript () : LuaScript ()
182    {
183     // m_iMethodBase = registerFunction ("hello1");
184      //registerFunction ("hello2");
185      //registerFunction ("hello3");
186    }
187
188    int scriptCalling (LuaVirtualMachine& vm, int iFunctionNumber)
189    {
190      /*switch (iFunctionNumber - m_iMethodBase)
191      {
192        case 0:
193          return Hello1 (vm);
194        case 1:
195          return Hello2 (vm);
196        case 2:
197          return Hello3 (vm);
198      }
199
200      return 0;*/
201    }
202
203    int Hello1 (LuaVirtualMachine& vm)
204    {
205      printf ("Hellow (1)\n");
206      return 0;
207    }
208
209    int Hello2 (LuaVirtualMachine& vm)
210    {
211      lua_State *state = (lua_State *) vm;
212
213      int iNumber = (int) lua_tonumber (state, -2);
214      int iNumber2 = (int) lua_tonumber (state, -1);
215      printf ("Hellow (2) -> %d\n", iNumber);
216      printf ("Second argument was %d\n",iNumber2);
217      return 0;
218    }
219
220    int Hello3 (LuaVirtualMachine& vm)
221    {
222      lua_State *state = (lua_State *) vm;
223
224      int iNumber = (int) lua_tonumber (state, -1);
225      printf ("Hello (3) -> %d\n", iNumber);
226      lua_pushnumber (state, iNumber + 2);
227      return 1;
228    }
229
230
231    void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
232    {
233      if (strcmp (strFunc.c_str(), "divideMe") == 0)
234      {
235         // frames returns an answer of the stack
236        lua_State *state = (lua_State *) vm;
237        int itop = lua_gettop (state);
238        int iType = lua_type (state, -1);
239        const char *s = lua_typename (state, iType);
240        double fFrameCount = lua_tonumber (state, -1);
241
242        printf ("frame count = %f\n", fFrameCount);
243      }
244    }
245
246  protected:
247    int m_iMethodBase;
248};
249
250//! main  test routine
251int main (int argc, const char** argv)
252{
253
254  std::cout << "lua-testing environment\n" << std::endl;
255
256
257
258  CMyScript ms;
259
260  objectOne ob1;
261  objectTwo ob2;
262  //CMyScript ob1 (vm2);
263
264  //addObjectToScript(&ob1, "obOne", ms, "printNameOne");
265  int obTwoRef = ms.addScriptableToScript(&ob2, "object");
266 // ms.addFunctionToScriptable("printNameTwo",obTwoRef,-1);
267
268  ms.compileFile ("luascript1.lua");
269
270
271  int iTopS = lua_gettop ((lua_State *) ms.getVirtualMachine());
272
273
274  assert (ms.scriptHasFunction ("CountAndCall"));
275  assert (!ms.scriptHasFunction ("countAndCall"));
276  assert (!ms.scriptHasFunction ("test"));
277
278  ms.selectScriptFunction ("CountAndCall");
279  ms.addParam (2);
280  bool res = ms.run ();
281  printf("ms.run returned %b\n",res);
282
283  /* Debug Output
284  lua_State* state =  (lua_State *) (ms.vm());
285  lua_Debug debug;
286  //lua_getfield(state, LUA_GLOBALSINDEX, "CountAndCall" );
287  lua_getinfo( state, "Sl", &debug );
288  std::cout << debug.name<< std::endl;
289  */
290
291  /*ms.selectScriptFunction ("divideMe");
292  ms.addParam (33);
293  ms.run (1);
294
295  ms.selectScriptFunction ("returnToMe");
296  ms.addParam (10);
297  ms.run (0);
298  */
299  return 0 ;
300
301}
Note: See TracBrowser for help on using the repository browser.