mirror of
https://github.com/joe7575/techpack.git
synced 2025-01-11 23:37:28 +01:00
Lua error messages improved
This commit is contained in:
parent
f1fc163628
commit
df9d252f61
@ -60,6 +60,13 @@ function safer_lua.config(max_code_size, max_table_size)
|
|||||||
safer_lua.MaxTableSize = max_table_size
|
safer_lua.MaxTableSize = max_table_size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function format_error(err, tab)
|
||||||
|
err = err:gsub('%[string "%-%-.-"%]:', "in "..tab.." line ")
|
||||||
|
err = err:gsub('in main chunk.+', "")
|
||||||
|
err = err:gsub('%.%.%..-:%d+:', "Error")
|
||||||
|
return err
|
||||||
|
end
|
||||||
|
|
||||||
local function compile(pos, text, label, err_clbk)
|
local function compile(pos, text, label, err_clbk)
|
||||||
if safer_lua:check(pos, text, label, err_clbk) == 0 then
|
if safer_lua:check(pos, text, label, err_clbk) == 0 then
|
||||||
text = text:gsub("%$", "S:")
|
text = text:gsub("%$", "S:")
|
||||||
@ -91,10 +98,9 @@ function safer_lua.init(pos, init, loop, environ, err_clbk)
|
|||||||
env.S = {}
|
env.S = {}
|
||||||
env.S = map(env.S, environ)
|
env.S = map(env.S, environ)
|
||||||
setfenv(code, env)
|
setfenv(code, env)
|
||||||
local res, err = pcall(code)
|
local res, err = xpcall(code, debug.traceback)
|
||||||
if not res then
|
if not res then
|
||||||
err = err:gsub("%[string .+%]:", "init() ")
|
err_clbk(pos, format_error(err, "init()"))
|
||||||
err_clbk(pos, err)
|
|
||||||
else
|
else
|
||||||
env = getfenv(code)
|
env = getfenv(code)
|
||||||
code = compile(pos, loop, "loop() ", err_clbk)
|
code = compile(pos, loop, "loop() ", err_clbk)
|
||||||
@ -115,14 +121,13 @@ function safer_lua.run_loop(pos, elapsed, code, err_clbk)
|
|||||||
env.event = false
|
env.event = false
|
||||||
env.ticks = env.ticks + 1
|
env.ticks = env.ticks + 1
|
||||||
end
|
end
|
||||||
local res, err = pcall(code)
|
local res, err = xpcall(code, debug.traceback)
|
||||||
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
|
if calc_used_mem_size(env) > safer_lua.MaxTableSize then
|
||||||
err_clbk(pos, "Memory limit exceeded")
|
err_clbk(pos, "Memory limit exceeded")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
if not res then
|
if not res then
|
||||||
err = err:gsub("%[string .+%]:", "loop() ")
|
err_clbk(pos, format_error(err, "loop()"))
|
||||||
err_clbk(pos, err)
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
|
Loading…
Reference in New Issue
Block a user