From 67728e9ea4f31eda7e79f19028bca9bd9731b9e5 Mon Sep 17 00:00:00 2001 From: eskerda Date: Sun, 13 Dec 2020 18:54:40 +0100 Subject: [PATCH] feat(sandbox) return multiple values --- sandbox.lua | 7 ++++--- spec/sandbox_spec.lua | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sandbox.lua b/sandbox.lua index 58143f2..c17604f 100644 --- a/sandbox.lua +++ b/sandbox.lua @@ -166,12 +166,13 @@ function sandbox.protect(f, options) string.rep = nil - local ok, result = pcall(f, ...) + local t = table.pack(pcall(f, ...)) cleanup() - if not ok then error(result) end - return result + if not t[1] then error(t[2]) end + + return table.unpack(t, 2, t.n) end end diff --git a/spec/sandbox_spec.lua b/spec/sandbox_spec.lua index 2bb04df..9f33388 100644 --- a/spec/sandbox_spec.lua +++ b/spec/sandbox_spec.lua @@ -34,6 +34,11 @@ describe('sandbox.run', function() assert.error(function() sandbox.run('return setmetatable({}, {})') end) assert.error(function() sandbox.run('return string.rep("hello", 5)') end) end) + + it('does return multiple values', function() + local result = { sandbox.run("return 'hello', 'world'") } + assert.same({ 'hello', 'world' }, result) + end) end) describe('when handling string.rep', function()