Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/OLD/main.cc @ 8065

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

merged the scriptengine back to the trunk

File size: 7.0 KB
RevLine 
[7645]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
[7653]15using namespace OrxScript;
[7645]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 ())
[7656]40  {
41    /*create object table*/
42    // Create a reference to the "object" table and set a name for the reference. Each reference is unique
[7645]43
[7656]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());
[7645]48
49
[7656]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);
[7645]55
56
57
[7656]58    return objRef;
59  }
[7645]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)
[7656]78{
79  lua_State *state = (lua_State *) (luaScript.getVirtualMachine());
80  LuaVirtualMachine& vm = luaScript.getVirtualMachine();
81  int iMethodIdx = -1;
82
83  if (vm.isOk ())
[7645]84  {
[7656]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  }
[7645]94
[7656]95  return iMethodIdx;
96}
[7645]97
[7656]98class objectOne : public Scriptable
99{
100public:
101  objectOne () : Scriptable ()
102  {
[7645]103  }
104
[7656]105  int scriptCalling (LuaVirtualMachine& vm, std::string functionName)
106  {
107    //switch (iFunctionNumber) //- methodBase) uncomment this when methodBase gets correclty set in the Scriptable class
[7657]108    if(functionName == "printNameOne")
[7645]109    {
[7656]110      //case 0:
111      return printNameOne (vm);
[7645]112    }
113
[7656]114    return 0;
115  }
[7645]116
117
[7656]118  int printNameOne(LuaVirtualMachine& vm)
119  {
[7645]120    std::cout<<"Hi I'm Object 1 !"<<std::endl;
121    return 0;
[7656]122  }
[7645]123
[7656]124  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
[7645]125  {
126  }
127
[7656]128}
129;
[7645]130
131class objectTwo : public Scriptable
132{
[7656]133public:
134  objectTwo () : Scriptable ()
135  {
136    registerFunction(std::string("printNameTwo"));
137    // registerFunction(std::string("printHello"));
[7645]138
[7656]139  }
[7645]140
[7656]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
[7657]144    if(functionName == "printNameTwo")
[7645]145    {
[7656]146      //case 0:
[7645]147
[7656]148      return printNameTwo (vm);
[7645]149    }
150
[7656]151    return 0;
152  }
[7645]153
154
[7656]155  int printNameTwo(LuaVirtualMachine& vm)
156  {
157    std::cout<<"Hi I'm Object 2 !\n"<<std::endl;
158    return 0;
159  }
[7645]160
[7656]161  int printHello(LuaVirtualMachine& vm)
162  {
163    std::cout<<"Nice to meet you!\n"<<std::endl;
164    return 0;
165  }
[7645]166
[7656]167  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
168  {
169  }
[7645]170
[7656]171}
172;
[7645]173
174
175
176
177class CMyScript : public LuaScript
178{
[7656]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)
[7645]190    {
[7656]191      case 0:
192        return Hello1 (vm);
193      case 1:
194        return Hello2 (vm);
195      case 2:
196        return Hello3 (vm);
[7645]197    }
198
[7656]199    return 0;*/
200  }
[7645]201
[7656]202  int Hello1 (LuaVirtualMachine& vm)
203  {
204    printf ("Hellow (1)\n");
205    return 0;
206  }
[7645]207
[7656]208  int Hello2 (LuaVirtualMachine& vm)
209  {
210    lua_State *state = (lua_State *) vm;
[7645]211
[7656]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  }
[7645]218
[7656]219  int Hello3 (LuaVirtualMachine& vm)
220  {
221    lua_State *state = (lua_State *) vm;
[7645]222
[7656]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  }
[7645]228
229
[7656]230  void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc)
231  {
232    if (strcmp (strFunc.c_str(), "divideMe") == 0)
[7645]233    {
[7656]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);
[7645]240
[7656]241      printf ("frame count = %f\n", fFrameCount);
[7645]242    }
[7656]243  }
[7645]244
[7656]245protected:
246  int m_iMethodBase;
[7645]247};
248
249//! main  test routine
250int main (int argc, const char** argv)
251{
252
[7656]253  std::cout << "== lua-testing environment ==\n" << std::endl;
[7645]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");
[7656]265  // ms.addFunctionToScriptable("printNameTwo",obTwoRef,-1);
[7645]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.