Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 31, 2010, 12:04:30 AM (14 years ago)
Author:
rgrieder
Message:

Improved Lua error handling: Clearer messages and debugger hook.
So, if the IOConsole is not running and a runtime error occurs, the lua debugger automatically gets started.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestate/data/lua/LuaStateInit.lua

    r6629 r6660  
    1717dofile = function(filename)
    1818  luaState:doFile(filename)
    19   -- Required because the C++ function cannot return whatever might be on the stack
     19  -- Required because if the file returns a table, it cannot be passed through the C++ function
    2020  return LuaStateReturnValue -- C-injected global variable
    2121end
     
    2626include = function(filename)
    2727  luaState:includeFile(filename)
    28   -- Required because the C++ function cannot return whatever might be on the stack
     28  -- Required because if the file returns a table, it cannot be passed through the C++ function
    2929  return LuaStateReturnValue -- C-injected global variable
    3030end
     
    4444    _LOADED = {}
    4545  end
    46   if not _LOADED[moduleName] then
     46  if _LOADED[moduleName] == nil then
    4747    -- save old value
    4848    local _REQUIREDNAME_OLD = _REQUIREDNAME
    4949    _REQUIREDNAME = moduleName
    5050    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
    5257    -- restore old value
    5358    _REQUIREDNAME = _REQUIREDNAME_OLD
     
    5560  return _LOADED[moduleName]
    5661end
     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
     66if _VERSION ~= "Lua 5.0"  and not luaState:usingIOConsole() then
     67  require("Debugger")
     68else
     69  -- Fallback pause function
     70  pause = function()
     71    logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active.
     72Do you have the IOConsole disabled and are you using Lua version 5.1?"]])
     73  end
     74end
     75
     76-- General error handler that gets called whenever an error happens at runtime
     77errorHandler = 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
     92end
     93
    5794
    5895-- Convenience function for console commands
     
    71108  return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
    72109end
    73 
    74 -- Include command line debugger for lua 5.1
    75 if _VERSION ~= "Lua 5.0" then
    76   require("Debugger")
    77 end
Note: See TracChangeset for help on using the changeset viewer.