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