Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/VirtualMachine.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.6 KB
Line 
1#include <assert.h>
2#include <string.h>
3#include <stdio.h>
4
5#include "luaincl.h"
6#include "VirtualMachine.h"
7namespace OrxScript
8{
9  // ---------------------------------------------------------------------------
10
11
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));
29
30    const char *msg = lua_tostring (lua, 1);
31
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);
37
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  }
43
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  }
62
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)
78  {
79    if (luaState != NULL)
80    {
81      lua_close (luaState);
82    }
83  }
84
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  {}
101
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 ();
120
121    luaState = lua_open ();
122
123    if (luaState)
124    {
125      machineIsOk = true;
126
127      // Load util libs into lua
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);
135
136      // setup global printing (trace)
137      lua_pushcclosure (luaState, printMessage, 0);
138      lua_setglobal (luaState, "trace");
139
140      lua_atpanic (luaState, (lua_CFunction) LuaVirtualMachine::panic);
141
142      return true;
143    }
144
145    return false;
146  }
147
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)
163  {
164    if (luaState)
165    {
166      lua_close (luaState);
167      luaState = NULL;
168      machineIsOk = false;
169    }
170    return true;
171  }
172
173
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)
189  {
190    bool fSuccess = false;
191    int iErr = 0;
192
193    if ((iErr = luaL_loadfile (luaState, strFilename.c_str())) == 0)
194    {
195      // Call main...
196      if ((iErr = lua_pcall (luaState, 0, LUA_MULTRET, 0)) == 0)
197      {
198        fSuccess = true;
199      }
200    }
201
202    /* if (fSuccess == false)
203     {
204       if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);
205     }
206    */
207    return fSuccess;
208  }
209
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 */)
227  {
228    bool fSuccess = false;
229    int iErr = 0;
230
231    if (strName == NULL)
232    {
233      strName = "Temp";
234    }
235
236    if ((iErr = luaL_loadbuffer (luaState, (const char *) pbBuffer, szLen, strName)) == 0)
237    {
238      // Call main...
239      if ((iErr = lua_pcall (luaState, 0, LUA_MULTRET, 0)) == 0)
240      {
241        fSuccess = true;
242      }
243    }
244
245    /* if (fSuccess == false)
246     {
247       if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);
248     }
249    */
250    return fSuccess;
251
252  }
253
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  }
291}
Note: See TracBrowser for help on using the repository browser.