summaryrefslogtreecommitdiff
path: root/lib/supple/sandbox.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/supple/sandbox.lua')
-rw-r--r--lib/supple/sandbox.lua27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/supple/sandbox.lua b/lib/supple/sandbox.lua
index 3c807cb..57e5cd3 100644
--- a/lib/supple/sandbox.lua
+++ b/lib/supple/sandbox.lua
@@ -28,17 +28,23 @@ local sio = require 'luxio.simple'
local loadstring = loadstring
local load = load
local setfenv = setfenv
+local gc = collectgarbage
+
+local function set_limits(ltab)
+ local count = ltab.count
+ local memory = ltab.memory
+ local limits = { count = count, memory = memory}
+ if limits.memory then
+ -- Bump memory limit by current usage to be kinder
+ limits.memory = limits.memory + (gc "count")
+ end
+ if not limits.count and not limits.memory then
+ return false, "Expected an opcode count or total memory limit"
+ end
+ comms.set_limits(limits)
+ return true
+end
--- Run fn with globs as its globals. Returns a function to run which
--- returns the return values of fn, and also wrap returns the table
--- which will be filled with any new globals fn creates.
---
--- If fn is a string then it is used as the source of the function, if it
--- is a function then it is expected to be a closure which when called
--- repeatedly returns more data loaded (perhaps from a file?). src is
--- expected to be the name associated with this source code.
---
--- In case of error, returns nil, errmsg
local function _wrap(fn, src, globs)
globs = globs or {}
local fn_glob = setmetatable({}, { __index = globs, __metatable=true })
@@ -127,6 +133,7 @@ local function run()
-- Pretend we've "given" the host an object called 'supple:loadstring'
-- which is the loadstring/load function
+ objects.give(set_limits, "supple:set_limits")
objects.give(wrappered_load, "supple:loadstring")
objects.give(objects.clean_down, "supple:clean_down")
comms._set_fd(0)