summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-08 20:10:43 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-08 20:10:43 +0100
commitb0adbda186961b6da0a0cd2736f75dd4402f508b (patch)
treee1f348790623eb1e3e2321c03df1177278248b1c
parent831eaab3dbeeccd9daf1b8c873776e62270783a4 (diff)
downloadsupple-b0adbda186961b6da0a0cd2736f75dd4402f508b.tar.gz
HOST: Allow set_globals to define a set of globals to pass to all supple sandboxes
-rw-r--r--lib/supple/host.lua26
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,
+}