libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moLuaBase.h
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moLuaBase.h
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright (C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30  Description:
31  Classes for Lua scripting. Based in the Lua article and VC project by Richard
32  Shephard, available online at:
33  http://www.codeproject.com/cpp/luaincpp.asp?msg=1741798
34 
35 *******************************************************************************/
36 
37 #ifndef __MO_LUA_BASE_H__
38 #define __MO_LUA_BASE_H__
39 
40 #include "moTypes.h"
41 #include "moAbstract.h"
42 #include "moText.h"
43 
44 // LUA includes.
45 extern "C"
46 {
47  #include <lua.h>
48  #include <lauxlib.h>
49  #include <lualib.h>
50 }
51 
52 enum
53 {
54  DBG_MASK_CALL = LUA_MASKCALL,
55  DBG_MASK_RET = LUA_MASKRET,
56  DBG_MASK_LINE = LUA_MASKLINE,
57  DBG_MASK_COUNT = LUA_MASKCOUNT
58 };
59 
60 class moLuaDebugger;
61 
67 {
68 public:
72  moLuaVirtualMachine (void);
76  virtual ~moLuaVirtualMachine (void);
77 
82  bool InitialiseVM (void);
87  bool FinaliseVM (void);
88 
94  bool RunFile (const char *strFilename);
95 
103  bool RunBuffer (const unsigned char *pbBuffer, size_t szLen, const char *strName = NULL);
104 
111  bool CallFunction (int nArgs, int nReturns = 0);
112 
117  operator lua_State *(void) { return m_pState; }
118 
123  static void Panic (lua_State *lua);
124 
129  virtual bool Ok (void) { return m_fIsOk; }
130 
135  void AttachDebugger (moLuaDebugger *dbg) { m_pDbg = dbg; }
136 protected:
137  lua_State *m_pState;
138  bool m_fIsOk;
140 };
141 
147 {
148 public:
157  virtual ~moLuaDebugger (void);
158 
163  void InitaliseDBG();
167  void FinaliseDBG (void);
168 
173  void SetHooksFlag (int iMask);
178  void SetCount (int iCount) { m_iCountMask = iCount; }
179 
184  void ErrorRun (int iErrorCode);
185 protected:
188 };
189 
195 
200 {
201 public:
206  moLuaRestoreStack (moLuaVirtualMachine& vm) : m_pState (NULL)
207  {
208  m_pState = (lua_State *) vm;
209  if (vm.Ok ())
210  {
211  if (m_pState)
212  m_iTop = lua_gettop (m_pState);
213  }
214  }
215 
219  virtual ~moLuaRestoreStack (void)
220  {
221  if (m_pState)
222  lua_settop (m_pState, m_iTop);
223  }
224 protected:
225  lua_State *m_pState;
226  int m_iTop;
227 };
228 
233 {
234 public:
240  moLuaThis (moLuaVirtualMachine& vm, int iRef) : m_iOldRef (0), m_vm (vm)
241  {
242  lua_State *state = (lua_State *) vm;
243  if (vm.Ok ())
244  {
245  // Save the old "this" table
246  lua_getglobal (state, "this");
247  m_iOldRef = luaL_ref (state, LUA_REGISTRYINDEX);
248 
249  // replace it with our new one
250  lua_rawgeti(state, LUA_REGISTRYINDEX, iRef);
251  lua_setglobal (state, "this");
252  }
253  }
254 
258  virtual ~moLuaThis (void)
259  {
260  lua_State *state = (lua_State *) m_vm;
261  if (m_iOldRef > 0 && m_vm.Ok ())
262  {
263  // Replace the old "this" table
264  lua_rawgeti(state, LUA_REGISTRYINDEX, m_iOldRef);
265  lua_setglobal (state, "this");
266  luaL_unref (state, LUA_REGISTRYINDEX, m_iOldRef);
267  }
268  }
269 protected:
272 };
273 
274 #endif
275 
virtual bool Ok(void)
Definition: moLuaBase.h:129
moLuaVirtualMachine & m_vm
Definition: moLuaBase.h:187
lua_State * m_pState
Definition: moLuaBase.h:137
virtual ~moLuaRestoreStack(void)
Definition: moLuaBase.h:219
Clase base abstracta de donde deben derivar los objetos [virtual pura].
Definition: moAbstract.h:191
#define LIBMOLDEO_API
Definition: moTypes.h:180
moLuaVirtualMachine & m_vm
Definition: moLuaBase.h:271
moLuaDebugger * m_pDbg
Definition: moLuaBase.h:139
void SetCount(int iCount)
Definition: moLuaBase.h:178
moLuaRestoreStack(moLuaVirtualMachine &vm)
Definition: moLuaBase.h:206
moLuaThis(moLuaVirtualMachine &vm, int iRef)
Definition: moLuaBase.h:240
int m_iCountMask
Definition: moLuaBase.h:186
virtual ~moLuaThis(void)
Definition: moLuaBase.h:258
void AttachDebugger(moLuaDebugger *dbg)
Definition: moLuaBase.h:135
restaurador del stack de LUA
Definition: moLuaBase.h:199
lua_State * m_pState
Definition: moLuaBase.h:225
int m_iOldRef
Definition: moLuaBase.h:270