Авторизация
plurk
Задай мне вопрос
Голосования
| lua eval |
|
| Written by xternalx | |||
| Sunday, 17 January 2010 02:41 | |||
|
Кто знает для чего нужен eval и когда он может пригодиться - тот знает. Кто не знает - тому оно не надо. Для нуждающихся выкладываю копипаст откуда то:
sandbox = sandbox or { ai = { patterns = {} }}; function eval(e, ex) if not e:match("^[%a%d%p%s]+$") then print(e.." does not match\n\n"); return false, "Send me some real Lua, eh?"; end local chunk, errmsg = loadstring("return "..e); array = table; if not chunk then chunk, errmsg = loadstring(e); if chunk and ex and (not ex.allow_functions) then return false, "That's something I could not allow to happen."; end if not chunk then return false, "Syntax error: "..errmsg; end end for k, v in pairs(math) do sandbox[k] = v end for k, v in pairs(string) do sandbox[k] = v end if ex and ex.data then for k, v in pairs(ex.data) do sandbox[k] = v end end sandbox.dump = nil; sandbox.array = array; sandbox.table = table; sandbox.time = os.time; sandbox.difftime = os.difftime; sandbox.clock = os.clock; sandbox.date = os.date; sandbox.RAM = MemUsage; sandbox.tostring = tostring; sandbox.tonumber = tonumber; sandbox.select = select; sandbox.type = type; sandbox.unpack = unpack; sandbox.print = function (a) return a; end sandbox.pairs = pairs; sandbox.ipairs = ipairs; sandbox.assert = assert; sandbox.count = table.fieldcount; if ex and ex.allow_mtaccess then sandbox.setmetatable = setmetatable; sandbox.getmetatable = getmetatable; end setfenv(chunk, sandbox) sandbox["sandbox"] = sandbox; StartTime = os.time(); debug.sethook(NewWatchdog(),"", 10); results = { pcall(chunk) }; debug.sethook(); -- Remove hook StartTime = nil; if not results[1] then return false, "Error: "..results[2], "error"; end table.remove(results, 1); -- Remove status code if #results == 0 then return nil, ChooseResponse("{Done :)}{Done}{Ok}{That's done}{As you wish}"); end for k, v in ipairs(results) do results[k] = tostring(v); end return true, table.concat(results, ", "); end function NewWatchdog(tim) local StartTime = os.time(); local MaxRunTime = 2; return function () -- This is a debug hook, to ensure functions run in the sandbox end within n seconds -- if StartTime == nil then print("StartTime is nil"); StartTime = os.time(); end -- print("Hook time: "..os.difftime(StartTime, os.time())) if os.difftime(os.time(),StartTime) > MaxRunTime then debug.sethook(); error("Sorry, the function ran too long, unable to complete", 2); end if MemUsage() > (1024 * 2) then -- 2MB debug.sethook(); error("Sorry, I am not allowed to use that much memory. Please try more efficient code ;)", 2); end end end sandbox = sandbox or { ai = { patterns = {} }}; function eval(e, ex) if not e:match("^[%a%d%p%s]+$") then print(e.." does not match\n\n"); return false, "Send me some real Lua, eh?"; end local chunk, errmsg = loadstring("return "..e); array = table; if not chunk then chunk, errmsg = loadstring(e); if chunk and ex and (not ex.allow_functions) then return false, "That's something I could not allow to happen."; end if not chunk then return false, "Syntax error: "..errmsg; end end for k, v in pairs(math) do sandbox[k] = v end for k, v in pairs(string) do sandbox[k] = v end if ex and ex.data then for k, v in pairs(ex.data) do sandbox[k] = v end end sandbox.dump = nil; sandbox.array = array; sandbox.table = table; sandbox.time = os.time; sandbox.difftime = os.difftime; sandbox.clock = os.clock; sandbox.date = os.date; sandbox.RAM = MemUsage; sandbox.tostring = tostring; sandbox.tonumber = tonumber; sandbox.select = select; sandbox.type = type; sandbox.unpack = unpack; sandbox.print = function (a) return a; end sandbox.pairs = pairs; sandbox.ipairs = ipairs; sandbox.assert = assert; sandbox.count = table.fieldcount; if ex and ex.allow_mtaccess then sandbox.setmetatable = setmetatable; sandbox.getmetatable = getmetatable; end setfenv(chunk, sandbox) sandbox["sandbox"] = sandbox; StartTime = os.time(); debug.sethook(NewWatchdog(),"", 10); results = { pcall(chunk) }; debug.sethook(); -- Remove hook StartTime = nil; if not results[1] then return false, "Error: "..results[2], "error"; end table.remove(results, 1); -- Remove status code if #results == 0 then return nil, ChooseResponse("{Done :)}{Done}{Ok}{That's done}{As you wish}"); end for k, v in ipairs(results) do results[k] = tostring(v); end return true, table.concat(results, ", "); end function NewWatchdog(tim) local StartTime = os.time(); local MaxRunTime = 2; return function () -- This is a debug hook, to ensure functions run in the sandbox end within n seconds -- if StartTime == nil then print("StartTime is nil"); StartTime = os.time(); end -- print("Hook time: "..os.difftime(StartTime, os.time())) if os.difftime(os.time(),StartTime) > MaxRunTime then debug.sethook(); error("Sorry, the function ran too long, unable to complete", 2); end if MemUsage() > (1024 * 2) then -- 2MB debug.sethook(); error("Sorry, I am not allowed to use that much memory. Please try more efficient code ;)", 2); end end end
|
|||
| Last Updated on Wednesday, 03 February 2010 23:37 |

