Created
January 29, 2017 03:12
-
-
Save octacian/dc93abbda602351dd5c00658538cfe00 to your computer and use it in GitHub Desktop.
digicompute main env functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- [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