Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/OLD/VirtualMachine.cc @ 8061

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

merged the scriptengine back to the trunk

File size: 7.6 KB
RevLine 
[7645]1#include <assert.h>
2#include <string.h>
3#include <stdio.h>
4
5#include "luaincl.h"
6#include "VirtualMachine.h"
[7653]7namespace OrxScript
8{
9  // ---------------------------------------------------------------------------
[7645]10
11
[7653]12  //============================================================================
13  // int printMessage
14  //---------------------------------------------------------------------------
15  // Prints a message to the console
16  //
17  // Parameter   Dir      Description
18  // ---------   ---      -----------
19  // lua         IN       State variable
20  //
21  // Return
22  // ------
23  // Number of return varaibles on the stack
24  //
25  //============================================================================
26  static int printMessage (lua_State *lua)
27  {
28    assert (lua_isstring (lua,1));
[7645]29
[7653]30    const char *msg = lua_tostring (lua, 1);
[7645]31
[7653]32    // get caller
33    lua_Debug ar;
34    memset (&ar, 0, sizeof(ar));
35    lua_getstack (lua, 1, &ar);
36    lua_getinfo (lua, "Snl", &ar);
[7645]37
[7653]38    // debug output
39    const char *str = ar.source;
40    printf ("script: %s -- at %s(%d)\n", msg, str, ar.currentline);
41    return 0;
42  }
[7645]43
[7653]44  //============================================================================
45  // LuaVirtualMachine::LuaVirtualMachine
46  //---------------------------------------------------------------------------
47  // Constructor. Setups the default VM state
48  //
49  // Parameter   Dir      Description
50  // ---------   ---      -----------
51  //
52  //
53  // Return
54  // ------
55  // None.
56  //
57  //============================================================================
58  LuaVirtualMachine::LuaVirtualMachine (void) : luaState (NULL)
59  {
60    machineIsOk = false;
61  }
[7645]62
[7653]63  //============================================================================
64  // LuaVirtualMachine::LuaVirtualMachine
65  //---------------------------------------------------------------------------
66  // Destructor. Closes the VM
67  //
68  // Parameter   Dir      Description
69  // ---------   ---      -----------
70  //
71  //
72  // Return
73  // ------
74  // None.
75  //
76  //============================================================================
77  LuaVirtualMachine::~LuaVirtualMachine (void)
[7645]78  {
[7653]79    if (luaState != NULL)
80    {
81      lua_close (luaState);
82    }
[7645]83  }
84
[7653]85  //============================================================================
86  // LuaVirtualMachine::Panic
87  //---------------------------------------------------------------------------
88  // When things in Lua go wrong (ever called in protected mode??)
89  //
90  // Parameter   Dir      Description
91  // ---------   ---      -----------
92  // lua         IN       State variable
93  //
94  // Return
95  // ------
96  // None.
97  //
98  //============================================================================
99  void LuaVirtualMachine::panic (lua_State *lua)
100  {}
[7645]101
[7653]102  //============================================================================
103  // bool LuaVirtualMachine::init
104  //---------------------------------------------------------------------------
105  // Initialises the VM, open lua, makes sure things are OK
106  //
107  // Parameter   Dir      Description
108  // ---------   ---      -----------
109  // None.
110  //
111  // Return
112  // ------
113  // Success.
114  //
115  //============================================================================
116  bool LuaVirtualMachine::init (void)
117  {
118    // Open Lua!
119    if (isOk ()) destroy ();
[7645]120
[7653]121    luaState = lua_open ();
[7645]122
[7653]123    if (luaState)
124    {
125      machineIsOk = true;
[7645]126
127      // Load util libs into lua
[7653]128      luaopen_base (luaState);
129      luaopen_table (luaState);
130      luaopen_string (luaState);
131      luaopen_math (luaState);
132      luaopen_debug (luaState);
133      luaopen_io (luaState);
134      luaopen_loadlib (luaState);
[7645]135
136      // setup global printing (trace)
[7653]137      lua_pushcclosure (luaState, printMessage, 0);
138      lua_setglobal (luaState, "trace");
[7645]139
[7653]140      lua_atpanic (luaState, (lua_CFunction) LuaVirtualMachine::panic);
[7645]141
[7653]142      return true;
143    }
[7645]144
[7653]145    return false;
[7645]146  }
147
[7653]148  //============================================================================
149  // bool LuaVirtualMachine::destroy
150  //---------------------------------------------------------------------------
151  // Clears the current Lua state
152  //
153  // Parameter   Dir      Description
154  // ---------   ---      -----------
155  // None.
156  //
157  // Return
158  // ------
159  // Success.
160  //
161  //============================================================================
162  bool LuaVirtualMachine::destroy (void)
[7645]163  {
[7653]164    if (luaState)
[7645]165    {
[7653]166      lua_close (luaState);
167      luaState = NULL;
168      machineIsOk = false;
[7645]169    }
[7653]170    return true;
[7645]171  }
172
173
[7653]174  //============================================================================
175  // bool LuaVirtualMachine::runFile
176  //---------------------------------------------------------------------------
177  // Compiles and runs a lua script file
178  //
179  // Parameter   Dir      Description
180  // ---------   ---      -----------
181  // strFilename IN       Filename to compile and run
182  //
183  // Return
184  // ------
185  // Success.
186  //
187  //============================================================================
188  bool LuaVirtualMachine::runFile (const std::string& strFilename)
[7645]189  {
[7653]190    bool fSuccess = false;
191    int iErr = 0;
[7645]192
[7653]193    if ((iErr = luaL_loadfile (luaState, strFilename.c_str())) == 0)
194    {
[7645]195      // Call main...
[7653]196      if ((iErr = lua_pcall (luaState, 0, LUA_MULTRET, 0)) == 0)
197      {
198        fSuccess = true;
199      }
[7645]200    }
201
[7653]202    /* if (fSuccess == false)
203     {
204       if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);
205     }
206    */
207    return fSuccess;
[7645]208  }
209
[7653]210  //============================================================================
211  // bool LuaVirtualMachine::runBuffer
212  //---------------------------------------------------------------------------
213  // Compiles and runs a pre-compiled data buffer
214  //
215  // Parameter   Dir      Description
216  // ---------   ---      -----------
217  // pbBuffer    IN       Buffer to run
218  // szLen       IN       Length of buffer
219  // strName     IN       Name of Buffer
220  //
221  // Return
222  // ------
223  // Success.
224  //
225  //============================================================================
226  bool LuaVirtualMachine::runBuffer (const unsigned char *pbBuffer, size_t szLen, const char *strName /* = NULL */)
[7645]227  {
[7653]228    bool fSuccess = false;
[7645]229    int iErr = 0;
230
[7653]231    if (strName == NULL)
[7645]232    {
[7653]233      strName = "Temp";
[7645]234    }
[7653]235
236    if ((iErr = luaL_loadbuffer (luaState, (const char *) pbBuffer, szLen, strName)) == 0)
[7645]237    {
[7653]238      // Call main...
239      if ((iErr = lua_pcall (luaState, 0, LUA_MULTRET, 0)) == 0)
240      {
241        fSuccess = true;
242      }
[7645]243    }
[7653]244
245    /* if (fSuccess == false)
246     {
247       if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);
248     }
[7645]249    */
[7653]250    return fSuccess;
251
[7645]252  }
253
[7653]254  //============================================================================
255  // LuaVirtualMachine::callFunction
256  //---------------------------------------------------------------------------
257  // Calls a function that is already on the stack
258  //
259  // Parameter   Dir      Description
260  // ---------   ---      -----------
261  // nArgs       IN       Args that are aleady on the stack
262  // nReturns    IN       Number of expected returns (will be on the stack)
263  //
264  // Return
265  // ------
266  // Success.
267  //
268  //============================================================================
269  bool LuaVirtualMachine::callFunction (int nArgs, int nReturns /* = 0 */)
270  {
271    bool fSuccess = false;
272
273    if (lua_isfunction (luaState, -nArgs-1))
274    {
275      int iErr = 0;
276      iErr = lua_pcall (luaState, nArgs, nReturns, 0);
277
278      if (iErr == 0)
279      {
280        fSuccess = true;
281      }
282      /*else
283      {
284        if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);
285      }
286      */
287    }
288
289    return fSuccess;
290  }
[7645]291}
Note: See TracBrowser for help on using the repository browser.