]>
git.r.bdr.sh - rbdr/pico-engine/blob - LuaVirtualMachine.cpp
1 // ---------------------------------------------------------------------------
2 // FILE NAME : LuaVirtualMachine.cpp
3 // ---------------------------------------------------------------------------
6 // Lua virtual machine implementation
8 // ---------------------------------------------------------------------------
11 // AUTHOR : Richard Shephard
12 // ---------------------------------------------------------------------------
13 // LIBRARY INCLUDE FILES
15 #include "LuaVirtualMachine.h"
19 // ---------------------------------------------------------------------------
22 //============================================================================
24 //---------------------------------------------------------------------------
25 // Prints a message to the console
27 // Parameter Dir Description
28 // --------- --- -----------
29 // lua IN State variable
33 // Number of return varaibles on the stack
35 //============================================================================
36 static int printMessage (lua_State
*lua
)
38 assert (lua_isstring (lua
,1));
40 const char *msg
= lua_tostring (lua
, 1);
44 memset (&ar
, 0, sizeof(ar
));
45 lua_getstack (lua
, 1, &ar
);
46 lua_getinfo (lua
, "Snl", &ar
);
49 const char *str
= ar
.source
;
50 printf ("script: %s -- at %s(%d)\n", msg
, str
, ar
.currentline
);
54 //============================================================================
55 // CLuaVirtualMachine::CLuaVirtualMachine
56 //---------------------------------------------------------------------------
57 // Constructor. Setups the default VM state
59 // Parameter Dir Description
60 // --------- --- -----------
67 //============================================================================
68 CLuaVirtualMachine::CLuaVirtualMachine (void) : m_pState (NULL
), m_pDbg (NULL
)
73 //============================================================================
74 // CLuaVirtualMachine::CLuaVirtualMachine
75 //---------------------------------------------------------------------------
76 // Destructor. Closes the VM
78 // Parameter Dir Description
79 // --------- --- -----------
86 //============================================================================
87 CLuaVirtualMachine::~CLuaVirtualMachine (void)
95 //============================================================================
96 // CLuaVirtualMachine::Panic
97 //---------------------------------------------------------------------------
98 // When things in Lua go wrong (ever called in protected mode??)
100 // Parameter Dir Description
101 // --------- --- -----------
102 // lua IN State variable
108 //============================================================================
109 void CLuaVirtualMachine::Panic (lua_State
*lua
)
113 //============================================================================
114 // bool CLuaVirtualMachine::InitialiseVM
115 //---------------------------------------------------------------------------
116 // Initialises the VM, open lua, makes sure things are OK
118 // Parameter Dir Description
119 // --------- --- -----------
126 //============================================================================
127 bool CLuaVirtualMachine::InitialiseVM (void)
130 if (Ok ()) DestroyVM ();
132 m_pState
= luaL_newstate ();
138 // Load util libs into lua
139 luaL_openlibs (m_pState
);
141 // setup global printing (trace)
142 lua_pushcclosure (m_pState
, printMessage
, 0);
143 lua_setglobal (m_pState
, "trace");
145 lua_atpanic (m_pState
, (lua_CFunction
) CLuaVirtualMachine::Panic
);
153 //============================================================================
154 // bool CLuaVirtualMachine::DestroyVM
155 //---------------------------------------------------------------------------
156 // Clears the current Lua state
158 // Parameter Dir Description
159 // --------- --- -----------
166 //============================================================================
167 bool CLuaVirtualMachine::DestroyVM (void)
171 lua_close (m_pState
);
179 //============================================================================
180 // bool CLuaVirtualMachine::RunFile
181 //---------------------------------------------------------------------------
182 // Compiles and runs a lua script file
184 // Parameter Dir Description
185 // --------- --- -----------
186 // strFilename IN Filename to compile and run
192 //============================================================================
193 bool CLuaVirtualMachine::RunFile (const char *strFilename
)
195 bool fSuccess
= false;
198 if ((iErr
= luaL_loadfile (m_pState
, strFilename
)) == 0)
201 if ((iErr
= lua_pcall (m_pState
, 0, LUA_MULTRET
, 0)) == 0)
207 if (fSuccess
== false)
209 if (m_pDbg
!= NULL
) m_pDbg
->ErrorRun (iErr
);
215 //============================================================================
216 // bool CLuaVirtualMachine::RunBuffer
217 //---------------------------------------------------------------------------
218 // Compiles and runs a pre-compiled data buffer
220 // Parameter Dir Description
221 // --------- --- -----------
222 // pbBuffer IN Buffer to run
223 // szLen IN Length of buffer
224 // strName IN Name of Buffer
230 //============================================================================
231 bool CLuaVirtualMachine::RunBuffer (const unsigned char *pbBuffer
, size_t szLen
, const char *strName
/* = NULL */)
233 bool fSuccess
= false;
241 if ((iErr
= luaL_loadbuffer (m_pState
, (const char *) pbBuffer
, szLen
, strName
)) == 0)
244 if ((iErr
= lua_pcall (m_pState
, 0, LUA_MULTRET
, 0)) == 0)
250 if (fSuccess
== false)
252 if (m_pDbg
!= NULL
) m_pDbg
->ErrorRun (iErr
);
259 //============================================================================
260 // CLuaVirtualMachine::CallFunction
261 //---------------------------------------------------------------------------
262 // Calls a function that is already on the stack
264 // Parameter Dir Description
265 // --------- --- -----------
266 // nArgs IN Args that are aleady on the stack
267 // nReturns IN Number of expected returns (will be on the stack)
273 //============================================================================
274 bool CLuaVirtualMachine::CallFunction (int nArgs
, int nReturns
/* = 0 */)
276 bool fSuccess
= false;
278 if (lua_isfunction (m_pState
, -nArgs
-1))
281 iErr
= lua_pcall (m_pState
, nArgs
, nReturns
, 0);
289 if (m_pDbg
!= NULL
) m_pDbg
->ErrorRun (iErr
);