Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the scriptengine back to the trunk

File size: 7.0 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{
100public:
101  objectOne () : Scriptable ()
102  {
103  }
104
105  int scriptCalling (LuaVirtualMachine& vm, std::string functionName)
106  {
107    //switch (iFunctionNumber) //- methodBase) uncomment this when methodBase gets correclty set in the Scriptable class
108    if(functionName == "printNameOne")
109    {
110      //case 0:
111      return printNameOne (vm);
112    }
113
114    return 0;
115  }
116
117
118  int printNameOne(LuaVirtualMachine& vm)
119  {
120    std::cout<<"Hi I'm Object 1 !"<<std::endl;
121    return 0;
122  }
123
124  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
125  {
126  }
127
128}
129;
130
131class objectTwo : public Scriptable
132{
133public:
134  objectTwo () : Scriptable ()
135  {
136    registerFunction(std::string("printNameTwo"));
137    // registerFunction(std::string("printHello"));
138
139  }
140
141  int scriptCalling (LuaVirtualMachine& vm, std::string functionName)
142  {
143    //switch (iFunctionNumber) //- m_iMethodBase) //TODO:find a way to determine the function number somehow: this works just incidentally
144    if(functionName == "printNameTwo")
145    {
146      //case 0:
147
148      return printNameTwo (vm);
149    }
150
151    return 0;
152  }
153
154
155  int printNameTwo(LuaVirtualMachine& vm)
156  {
157    std::cout<<"Hi I'm Object 2 !\n"<<std::endl;
158    return 0;
159  }
160
161  int printHello(LuaVirtualMachine& vm)
162  {
163    std::cout<<"Nice to meet you!\n"<<std::endl;
164    return 0;
165  }
166
167  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
168  {
169  }
170
171}
172;
173
174
175
176
177class CMyScript : public LuaScript
178{
179public:
180  CMyScript () : LuaScript ()
181  {
182    // m_iMethodBase = registerFunction ("hello1");
183    //registerFunction ("hello2");
184    //registerFunction ("hello3");
185  }
186
187  int scriptCalling (LuaVirtualMachine& vm, int iFunctionNumber)
188  {
189    /*switch (iFunctionNumber - m_iMethodBase)
190    {
191      case 0:
192        return Hello1 (vm);
193      case 1:
194        return Hello2 (vm);
195      case 2:
196        return Hello3 (vm);
197    }
198
199    return 0;*/
200  }
201
202  int Hello1 (LuaVirtualMachine& vm)
203  {
204    printf ("Hellow (1)\n");
205    return 0;
206  }
207
208  int Hello2 (LuaVirtualMachine& vm)
209  {
210    lua_State *state = (lua_State *) vm;
211
212    int iNumber = (int) lua_tonumber (state, -2);
213    int iNumber2 = (int) lua_tonumber (state, -1);
214    printf ("Hellow (2) -> %d\n", iNumber);
215    printf ("Second argument was %d\n",iNumber2);
216    return 0;
217  }
218
219  int Hello3 (LuaVirtualMachine& vm)
220  {
221    lua_State *state = (lua_State *) vm;
222
223    int iNumber = (int) lua_tonumber (state, -1);
224    printf ("Hello (3) -> %d\n", iNumber);
225    lua_pushnumber (state, iNumber + 2);
226    return 1;
227  }
228
229
230  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
231  {
232    if (strcmp (strFunc.c_str(), "divideMe") == 0)
233    {
234      // frames returns an answer of the stack
235      lua_State *state = (lua_State *) vm;
236      int itop = lua_gettop (state);
237      int iType = lua_type (state, -1);
238      const char *s = lua_typename (state, iType);
239      double fFrameCount = lua_tonumber (state, -1);
240
241      printf ("frame count = %f\n", fFrameCount);
242    }
243  }
244
245protected:
246  int m_iMethodBase;
247};
248
249//! main  test routine
250int main (int argc, const char** argv)
251{
252
253  std::cout << "== lua-testing environment ==\n" << std::endl;
254
255
256
257  CMyScript ms;
258
259  objectOne ob1;
260  objectTwo ob2;
261  //CMyScript ob1 (vm2);
262
263  //addObjectToScript(&ob1, "obOne", ms, "printNameOne");
264  int obTwoRef = ms.addScriptableToScript(&ob2, "object");
265  // ms.addFunctionToScriptable("printNameTwo",obTwoRef,-1);
266
267  ms.compileFile ("luascript1.lua");
268
269
270  int iTopS = lua_gettop ((lua_State *) ms.getVirtualMachine());
271
272
273  assert (ms.scriptHasFunction ("CountAndCall"));
274  assert (!ms.scriptHasFunction ("countAndCall"));
275  assert (!ms.scriptHasFunction ("test"));
276
277  ms.selectScriptFunction ("CountAndCall");
278  ms.addParam (2);
279  bool res = ms.run ();
280  printf("ms.run returned %b\n",res);
281
282  /* Debug Output
283  lua_State* state =  (lua_State *) (ms.vm());
284  lua_Debug debug;
285  //lua_getfield(state, LUA_GLOBALSINDEX, "CountAndCall" );
286  lua_getinfo( state, "Sl", &debug );
287  std::cout << debug.name<< std::endl;
288  */
289
290  /*ms.selectScriptFunction ("divideMe");
291  ms.addParam (33);
292  ms.run (1);
293
294  ms.selectScriptFunction ("returnToMe");
295  ms.addParam (10);
296  ms.run (0);
297  */
298  return 0 ;
299
300}
Note: See TracBrowser for help on using the repository browser.