My code for actually executing code under the environment:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
if code:byte(1) == 27 then
return nil, "Binary code prohibited."
end
local f, msg = loadstring(code)
if not f then return false, msg end
setfenv(f, env)
-- Turn off JIT optimization for user code so that count
-- events are generated when adding debug hooks
if rawget(_G, "jit") then
jit.off(f, true)
end
-- Use instruction counter to stop execution
-- after 10000 events
debug.sethook(function()
return false, "Code timed out!"
end, "", 10000)
local ok, ret = pcall(f)
debug.sethook() -- Clear hook
if not ok then return false, ret end
return true, ret
As you might guess, that's based off the Mesecons LuaController code.
I've tried using error() as do LCs, but it messes a ton of other stuff up. The computer itself is expecting this function to return two variables, 1: successful (boolean), and 2: result message.
Anyways, if someone could give me a hand, it'd be greatly appreciated. If you'd like to test yourself, see the digicompute thread linked at the top of this post.