Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/VirtualMachine.cc @ 7645

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

added script_engine

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