Changeset 6746 for code/trunk/data/lua
- Timestamp:
- Apr 16, 2010, 2:50:16 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/data/lua/LuaStateInit.lua
r6536 r6746 1 1 -- Note: luaState is a pointer to the LuaState instance that created this lua state 2 2 3 -- Save original print function in debug4 debug = print5 6 3 -- Redirect print to the C++ print function 4 original_print = print 7 5 print = function(s) 8 6 luaState:luaPrint(s) … … 13 11 luaState:luaLog(level, message) 14 12 end 13 cout = logMessage 15 14 16 15 -- Redirect dofile in order to load with the resource manager 17 doFile = function(filename) 18 luaState:doFile(filename) 19 -- Required because the C++ function cannot return whatever might be on the stack 16 original_dofile = dofile 17 dofile = function(filename) 18 if not luaState:doFile(filename) then 19 error("Error propagation. Do not display") 20 end 21 -- Required because if the file returns a table, it cannot be passed through the C++ function 20 22 return LuaStateReturnValue -- C-injected global variable 21 23 end 22 original_dofile = dofile 23 dofile = doFile 24 doFile = dofile 24 25 25 26 -- Create includeFile function that preparses the file according 26 27 -- to a function provided to the LuaState constructor (in C++) 27 28 include = function(filename) 28 luaState:includeFile(filename) 29 -- Required because the C++ function cannot return whatever might be on the stack 29 if not luaState:includeFile(filename) then 30 error("Error propagation. Do not display") 31 end 32 -- Required because if the file returns a table, it cannot be passed through the C++ function 30 33 return LuaStateReturnValue -- C-injected global variable 31 34 end … … 34 37 -- The loaded modules are then stored with their names (where name has no .lua extension) 35 38 -- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file 36 old_require = require 39 original_require = require 40 _REQUIREDNAME = "" 41 LuaStateReturnValue = true 37 42 require = function(moduleName) 38 43 if not luaState:fileExists(moduleName .. ".lua") then 44 logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ") 39 45 return nil 40 46 end 47 41 48 if not _LOADED then 42 49 _LOADED = {} 43 50 end 51 if not _LOADED_RETURN_VALUES then 52 _LOADED_RETURN_VALUES = {} 53 end 54 44 55 if not _LOADED[moduleName] then 45 -- save old value 46 _REQUIREDNAME_OLD = _REQUIREDNAME56 -- save old value for the required name 57 local _REQUIREDNAME_OLD = _REQUIREDNAME 47 58 _REQUIREDNAME = moduleName 48 luaState:doFile(moduleName .. ".lua") 49 _LOADED[moduleName] = LuaStateReturnValue or true 59 60 if not luaState:doFile(moduleName .. ".lua") then 61 error("Error propagation. Do not display") 62 end 63 -- LuaStateReturnValue is required because if the file returns a table, 64 -- it cannot be passed through the C++ function 65 _LOADED_RETURN_VALUES[moduleName] = LuaStateReturnValue 66 _LOADED[moduleName] = true 67 50 68 -- restore old value 51 69 _REQUIREDNAME = _REQUIREDNAME_OLD 52 70 end 53 return _LOADED[moduleName] 71 local asdf = _LOADED_RETURN_VALUES[moduleName] 72 return asdf 54 73 end 74 75 76 -- Load useful tool functions (like handleDefaultArgument) 77 require("Tools") 78 79 80 -- Include command line debugger for lua 5.1 81 -- Note: It doesn't work if the IOConsole was started. Then we replace pause() with a warning 82 if _VERSION ~= "Lua 5.0" and not luaState:usingIOConsole() then 83 require("Debugger") 84 else 85 -- Fallback pause function 86 pause = function() 87 logMessage(2, [["Warning: debug() called in Lua, but Debugger is not active. 88 Do you have the IOConsole disabled and are you using Lua version 5.1?"]]) 89 end 90 end 91 92 -- General error handler that gets called whenever an error happens at runtime 93 errorHandler = function(err) 94 if type(err) == "string" then 95 -- Simply return if the error has already been handled 96 if string.find(err, "Error propagation. Do not display") ~= nil then 97 return err 98 end 99 -- Display the error message 100 logMessage(1, "Lua runtime error: "..err) 101 end 102 103 -- Start debugger if possible 104 if _LOADED and _LOADED["Debugger"] ~= nil then 105 pause() 106 else 107 -- Fallback: print stack trace 108 logMessage(3, debug.traceback("")) 109 end 110 return err -- Hello Lua debugger user! Please type 'set 2' to get to the 111 -- actual position in the stack where the error occurred 112 end 113 55 114 56 115 -- Convenience function for console commands
Note: See TracChangeset
for help on using the changeset viewer.