diff --git a/sandbox.lua b/sandbox.lua index 1025a35..aec70d2 100644 --- a/sandbox.lua +++ b/sandbox.lua @@ -54,10 +54,12 @@ local function run(f, options) string.rep = nil setfenv(f, BASE_ENV) - local result = f() + local ok, result = pcall(f) string.rep = string_rep + if not ok then error(result) end + return result end diff --git a/spec/sandbox_spec.lua b/spec/sandbox_spec.lua index 1461478..2e2dae8 100644 --- a/spec/sandbox_spec.lua +++ b/spec/sandbox_spec.lua @@ -33,6 +33,11 @@ describe('sandbox', function() assert.equal('hellohello', string.rep('hello', 2)) end) + it('restores string.rep even if there is an error', function() + assert.has_error(function() sandbox("error('foo')") end) + assert.equal('hellohello', string.rep('hello', 2)) + end) + end)