feat(sandbox) return multiple values

This commit is contained in:
eskerda 2020-12-13 18:54:40 +01:00
parent 11ee23ae30
commit 67728e9ea4
2 changed files with 9 additions and 3 deletions

View File

@ -166,12 +166,13 @@ function sandbox.protect(f, options)
string.rep = nil string.rep = nil
local ok, result = pcall(f, ...) local t = table.pack(pcall(f, ...))
cleanup() cleanup()
if not ok then error(result) end if not t[1] then error(t[2]) end
return result
return table.unpack(t, 2, t.n)
end end
end end

View File

@ -34,6 +34,11 @@ describe('sandbox.run', function()
assert.error(function() sandbox.run('return setmetatable({}, {})') end) assert.error(function() sandbox.run('return setmetatable({}, {})') end)
assert.error(function() sandbox.run('return string.rep("hello", 5)') end) assert.error(function() sandbox.run('return string.rep("hello", 5)') end)
end) end)
it('does return multiple values', function()
local result = { sandbox.run("return 'hello', 'world'") }
assert.same({ 'hello', 'world' }, result)
end)
end) end)
describe('when handling string.rep', function() describe('when handling string.rep', function()