Changeset 7653 in orxonox.OLD for branches/script_engine/src/lib/script_engine/VirtualMachine.cc
- Timestamp:
- May 17, 2006, 7:55:59 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/VirtualMachine.cc
r7645 r7653 5 5 #include "luaincl.h" 6 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 //============================================================================ 24 static int printMessage (lua_State *lua) 7 namespace OrxScript 25 8 { 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; 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 } 40 291 } 41 42 //============================================================================43 // LuaVirtualMachine::LuaVirtualMachine44 //---------------------------------------------------------------------------45 // Constructor. Setups the default VM state46 //47 // Parameter Dir Description48 // --------- --- -----------49 //50 //51 // Return52 // ------53 // None.54 //55 //============================================================================56 LuaVirtualMachine::LuaVirtualMachine (void) : luaState (NULL)57 {58 machineIsOk = false;59 }60 61 //============================================================================62 // LuaVirtualMachine::LuaVirtualMachine63 //---------------------------------------------------------------------------64 // Destructor. Closes the VM65 //66 // Parameter Dir Description67 // --------- --- -----------68 //69 //70 // Return71 // ------72 // None.73 //74 //============================================================================75 LuaVirtualMachine::~LuaVirtualMachine (void)76 {77 if (luaState != NULL)78 {79 lua_close (luaState);80 }81 }82 83 //============================================================================84 // LuaVirtualMachine::Panic85 //---------------------------------------------------------------------------86 // When things in Lua go wrong (ever called in protected mode??)87 //88 // Parameter Dir Description89 // --------- --- -----------90 // lua IN State variable91 //92 // Return93 // ------94 // None.95 //96 //============================================================================97 void LuaVirtualMachine::panic (lua_State *lua)98 {99 }100 101 //============================================================================102 // bool LuaVirtualMachine::init103 //---------------------------------------------------------------------------104 // Initialises the VM, open lua, makes sure things are OK105 //106 // Parameter Dir Description107 // --------- --- -----------108 // None.109 //110 // Return111 // ------112 // Success.113 //114 //============================================================================115 bool 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 lua127 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::destroy149 //---------------------------------------------------------------------------150 // Clears the current Lua state151 //152 // Parameter Dir Description153 // --------- --- -----------154 // None.155 //156 // Return157 // ------158 // Success.159 //160 //============================================================================161 bool 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::runFile175 //---------------------------------------------------------------------------176 // Compiles and runs a lua script file177 //178 // Parameter Dir Description179 // --------- --- -----------180 // strFilename IN Filename to compile and run181 //182 // Return183 // ------184 // Success.185 //186 //============================================================================187 bool 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::runBuffer211 //---------------------------------------------------------------------------212 // Compiles and runs a pre-compiled data buffer213 //214 // Parameter Dir Description215 // --------- --- -----------216 // pbBuffer IN Buffer to run217 // szLen IN Length of buffer218 // strName IN Name of Buffer219 //220 // Return221 // ------222 // Success.223 //224 //============================================================================225 bool 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::callFunction255 //---------------------------------------------------------------------------256 // Calls a function that is already on the stack257 //258 // Parameter Dir Description259 // --------- --- -----------260 // nArgs IN Args that are aleady on the stack261 // nReturns IN Number of expected returns (will be on the stack)262 //263 // Return264 // ------265 // Success.266 //267 //============================================================================268 bool 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 /*else282 {283 if (m_pDbg != NULL) m_pDbg->ErrorRun (iErr);284 }285 */286 }287 288 return fSuccess;289 }
Note: See TracChangeset
for help on using the changeset viewer.