- Timestamp:
- Mar 31, 2010, 12:04:30 AM (15 years ago)
- Location:
- code/branches/gamestate
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gamestate/data/lua/Debugger.lua
r6626 r6660 433 433 434 434 -- Transform line endings to \n 435 text 435 text:gsub("\r\n", "\n") -- Windows to Unix 436 436 text:gsub("\r", "\n") -- Mac to Unix 437 437 if text[-1] ~= "\n" then -
code/branches/gamestate/data/lua/LuaStateInit.lua
r6629 r6660 17 17 dofile = function(filename) 18 18 luaState:doFile(filename) 19 -- Required because the C++ function cannot return whatever might be on the stack19 -- Required because if the file returns a table, it cannot be passed through the C++ function 20 20 return LuaStateReturnValue -- C-injected global variable 21 21 end … … 26 26 include = function(filename) 27 27 luaState:includeFile(filename) 28 -- Required because the C++ function cannot return whatever might be on the stack28 -- Required because if the file returns a table, it cannot be passed through the C++ function 29 29 return LuaStateReturnValue -- C-injected global variable 30 30 end … … 44 44 _LOADED = {} 45 45 end 46 if not _LOADED[moduleName]then46 if _LOADED[moduleName] == nil then 47 47 -- save old value 48 48 local _REQUIREDNAME_OLD = _REQUIREDNAME 49 49 _REQUIREDNAME = moduleName 50 50 luaState:doFile(moduleName .. ".lua") 51 _LOADED[moduleName] = LuaStateReturnValue 51 -- LuaStateReturnValue is required because if the file returns a table, 52 -- it cannot be passed through the C++ function 53 if LuaStateReturnValue == nil then -- C-injected global variable 54 LuaStateReturnValue = true 55 end 56 _LOADED[moduleName] = LuaStateReturnValue -- This entry must never be nil 52 57 -- restore old value 53 58 _REQUIREDNAME = _REQUIREDNAME_OLD … … 55 60 return _LOADED[moduleName] 56 61 end 62 63 64 -- Include command line debugger for lua 5.1 65 -- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning 66 if _VERSION ~= "Lua 5.0" and not luaState:usingIOConsole() then 67 require("Debugger") 68 else 69 -- Fallback pause function 70 pause = function() 71 logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active. 72 Do you have the IOConsole disabled and are you using Lua version 5.1?"]]) 73 end 74 end 75 76 -- General error handler that gets called whenever an error happens at runtime 77 errorHandler = function(err) 78 -- Display the error message 79 if type(err) == "string" then 80 logMessage(1, "Lua runtime error: "..err) 81 end 82 83 -- Start debugger if possible 84 if _LOADED and _LOADED["Debugger"] ~= nil then 85 pause() 86 else 87 -- Fallback: print stack trace 88 debug.traceback(nil, "", 2) 89 end 90 return err -- Hello Lua debugger user! Please type 'set 2' to get to the 91 -- actual position in the stack where the error occurred 92 end 93 57 94 58 95 -- Convenience function for console commands … … 71 108 return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value) 72 109 end 73 74 -- Include command line debugger for lua 5.175 if _VERSION ~= "Lua 5.0" then76 require("Debugger")77 end -
code/branches/gamestate/src/libraries/core/LuaState.cc
r6655 r6660 37 37 38 38 #include "util/Debug.h" 39 #include "util/Exception.h" 40 #include "util/ScopeGuard.h" 41 #include "IOConsole.h" 39 42 #include "Resource.h" 40 43 #include "ToluaBindCore.h" … … 54 57 // Create new lua state and configure it 55 58 luaState_ = lua_open(); 59 Loki::ScopeGuard luaStateGuard = Loki::MakeGuard(&lua_close, luaState_); 56 60 #if LUA_VERSION_NUM == 501 57 61 luaL_openlibs(luaState_); … … 78 82 79 83 // Parse init script 80 this->doFile("LuaStateInit.lua"); 84 if (!this->doFile("LuaStateInit.lua")) 85 ThrowException(InitialisationFailed, "Running LuaStateInit.lua failed"); 86 87 luaStateGuard.Dismiss(); 81 88 } 82 89 … … 96 103 } 97 104 98 voidLuaState::includeFile(const std::string& filename)105 bool LuaState::includeFile(const std::string& filename) 99 106 { 100 107 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); 101 108 if (sourceInfo != NULL) 102 this->includeString(Resource::open(sourceInfo)->getAsString(), sourceInfo); 103 else 104 COUT(2) << "LuaState: Cannot include file '" << filename << "'." << std::endl; 105 } 106 107 void LuaState::includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) 109 return this->includeString(Resource::open(sourceInfo)->getAsString(), sourceInfo); 110 else 111 { 112 COUT(2) << "LuaState: Cannot include file '" << filename << "' (not found)." << std::endl; 113 return false; 114 } 115 } 116 117 bool LuaState::includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) 108 118 { 109 119 // Parse string with provided include parser (otherwise don't preparse at all) … … 121 131 } 122 132 123 this->doString(luaInput, sourceFileInfo);133 bool returnValue = this->doString(luaInput, sourceFileInfo); 124 134 125 135 if (sourceFileInfo != NULL) … … 129 139 this->sourceCodeMap_.erase(sourceFileInfo->filename); 130 140 } 131 } 132 133 void LuaState::doFile(const std::string& filename) 141 142 return returnValue; 143 } 144 145 bool LuaState::doFile(const std::string& filename) 134 146 { 135 147 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); 136 148 if (sourceInfo != NULL) 137 this->doString(Resource::open(sourceInfo)->getAsString(), sourceInfo); 138 else 139 COUT(2) << "LuaState: Cannot do file '" << filename << "'." << std::endl; 140 } 141 142 void LuaState::doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) 149 return this->doString(Resource::open(sourceInfo)->getAsString(), sourceInfo); 150 else 151 { 152 COUT(2) << "LuaState: Cannot do file '" << filename << "' (not found)." << std::endl; 153 return false; 154 } 155 } 156 157 bool LuaState::doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) 143 158 { 144 159 // Save the old source file info … … 161 176 } 162 177 163 int error = 0; 178 // Push custom error handler that uses the debugger 179 int errorHandler = 1; 180 lua_getglobal(this->luaState_, "errorHandler"); 181 if (lua_isnil(this->luaState_, -1)) 182 { 183 lua_pop(this->luaState_, 1); 184 errorHandler = 0; 185 } 186 164 187 #if LUA_VERSION_NUM != 501 165 188 LoadS ls; 166 189 ls.s = code.c_str(); 167 190 ls.size = code.size(); 168 error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, chunkname.c_str());191 int error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, chunkname.c_str()); 169 192 #else 170 error = luaL_loadbuffer(luaState_, code.c_str(), code.size(), chunkname.c_str());193 int error = luaL_loadbuffer(luaState_, code.c_str(), code.size(), chunkname.c_str()); 171 194 #endif 172 195 173 // execute the chunk 196 switch (error) 197 { 198 case LUA_ERRSYNTAX: // Syntax error 199 COUT(1) << "Lua syntax error: " << lua_tostring(luaState_, -1) << std::endl; 200 break; 201 case LUA_ERRMEM: // Memory allocation error 202 COUT(1) << "Lua memory allocation error: Consult your dentist immediately!" << std::endl; 203 lua_pop(luaState_, 1); 204 break; 205 } 206 174 207 if (error == 0) 175 error = lua_pcall(luaState_, 0, 1, 0); 208 { 209 // Execute the chunk in protected mode with an error handler function (stack index) 210 error = lua_pcall(luaState_, 0, 1, errorHandler); 211 212 switch (error) 213 { 214 case LUA_ERRRUN: // Runtime error 215 // Remove error string from stack (we already display the error in the 216 // 'errorHandler' Lua function in LuaStateInit.lua) 217 lua_pop(luaState_, 1); 218 break; 219 case LUA_ERRERR: // Error in the error handler 220 COUT(1) << "Lua error in error handler: " << lua_tostring(luaState_, -1) << std::endl; 221 break; 222 case LUA_ERRMEM: // Memory allocation error 223 COUT(1) << "Lua memory allocation error: Consult your dentist immediately!" << std::endl; 224 lua_pop(luaState_, 1); 225 break; 226 } 227 } 228 176 229 if (error != 0) 177 230 { 178 std::string origin; 179 if (sourceFileInfo != NULL) 180 origin = " originating from " + sourceFileInfo_->filename; 181 COUT(1) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl; 182 // return value is true (not nil!) 183 lua_pushboolean(luaState_, 1); 184 } 185 if (lua_isnil(luaState_, -1)) 186 { 187 // Nil return values cause problems 188 lua_pop(luaState_, 1); 189 lua_pushboolean(luaState_, 1); 190 } 191 // Push return value because it will get lost since the return value of this function is void 231 // Push a nil return value 232 lua_pushnil(luaState_); 233 } 234 235 // Set return value to a global variable because we cannot return a table in this function 236 // here. It would work for numbers, pointers and strings, but certainly not for Lua tables. 192 237 lua_setglobal(luaState_, "LuaStateReturnValue"); 193 238 194 239 // Load the old info again 195 240 sourceFileInfo_ = oldSourceFileInfo; 241 242 return (error == 0); 196 243 } 197 244 … … 230 277 } 231 278 279 bool LuaState::usingIOConsole() const 280 { 281 return IOConsole::exists(); 282 } 283 232 284 #if LUA_VERSION_NUM != 501 233 285 const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size) -
code/branches/gamestate/src/libraries/core/LuaState.h
r6627 r6660 71 71 ~LuaState(); 72 72 73 voiddoFile(const std::string& filename); // tolua_export74 voiddoString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>());73 bool doFile(const std::string& filename); // tolua_export 74 bool doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); 75 75 76 voidincludeFile(const std::string& filename); // tolua_export77 voidincludeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>());76 bool includeFile(const std::string& filename); // tolua_export 77 bool includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); 78 78 79 79 void luaPrint(const std::string& str); // tolua_export … … 92 92 93 93 Functor* createLuaFunctor(const std::string& code) { return new LuaFunctor(code, this); } // tolua_export 94 //! Tells about whether IOConsole was activated. The Lua debugger only works with a normal console. 95 bool usingIOConsole() const; // tolua_export 94 96 95 97 static bool addToluaInterface(int (*function)(lua_State*), const std::string& name);
Note: See TracChangeset
for help on using the changeset viewer.