diff options
Diffstat (limited to 'lib/supple/host.lua')
-rw-r--r-- | lib/supple/host.lua | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/supple/host.lua b/lib/supple/host.lua index cfaf98b..6fbac05 100644 --- a/lib/supple/host.lua +++ b/lib/supple/host.lua @@ -18,7 +18,7 @@ local capi = require 'supple.capi' local hostname = "host" local counter = 0 -local limits +local limits, globals local function simplify(t, memo) if not memo then memo = {} end @@ -81,13 +81,10 @@ local function run_sandbox(codestr, codename, ...) comms._set_fd(commsfd) objects.set_proc_call(comms.call) - local func, err = comms.call("supple:loadstring", "__call", codestr, codename) + local func, env = comms.call("supple:loadstring", "__call", codestr, codename) if not func then - error(err) + error(env) end - -- Clear 'err' because it'll be the environment table for the function - -- which we do not need to fill out. - err = nil local limitsok, limitserr = true if limits then @@ -99,8 +96,15 @@ local function run_sandbox(codestr, codename, ...) local function capture() ret = {func(unpack(args))} end - local ok + + local ok, err if limitsok then + if globals then + -- Hand over the globals + for k, v in pairs(globals) do + env[k] = v + end + end ok, err = pcall(capture) else ok, err = limitsok, limitserr @@ -111,6 +115,7 @@ local function run_sandbox(codestr, codename, ...) end -- We need to clean up, so dump all the objects func = nil + env = nil -- And ask the supple API to clear down too objects.clean_down(true) @@ -133,8 +138,13 @@ local function set_limits(newlimits) limits = newlimits end +local function set_globals(newglobals) + globals = newglobals +end + return { run = run_sandbox, set_name = set_hostname, set_limits = set_limits, -}
\ No newline at end of file + set_globals = set_globals, +} |