Skip to content

Instantly share code, notes, and snippets.

@octacian
Created January 29, 2017 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octacian/dc93abbda602351dd5c00658538cfe00 to your computer and use it in GitHub Desktop.
Save octacian/dc93abbda602351dd5c00658538cfe00 to your computer and use it in GitHub Desktop.
digicompute main env functions
-- [function] run code
function digicompute.run_code(code, env)
if code:byte(1) == 27 then
return nil, "Binary code prohibited."
end
local f, msg = loadstring(code)
digicompute.log(dump(f)..", "..dump(msg))
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
end
-- [function] run file
function digicompute.run_file(path, env)
local code = digicompute.builtin.read(path)
local ok, res = digicompute.run_code(code, env)
digicompute.log("OK: "..dump(ok)..", "..dump(res))
return ok, res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment