From c1e5b449389108382411206617d847d77a69617e Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 3 Sep 2013 13:20:38 +0200 Subject: [PATCH] naming & refactoring --- sandbox.lua | 10 +++++----- spec/sandbox_spec.lua | 10 ++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/sandbox.lua b/sandbox.lua index b663ca9..e8f2d78 100644 --- a/sandbox.lua +++ b/sandbox.lua @@ -65,14 +65,14 @@ local function run(f, options) -- I would love to be able to make step greater than 1 -- (say, 500000) but any value > 1 seems to choke with a simple while true do end -- After ~100 iterations, they stop calling timeout. So I need to use step = 1 and - -- count the steps separatedly + -- instructions_count the steps separatedly local step = 1 - local count = 0 + local instructions_count = 0 local timeout = function(str) - count = count + 1 - if count >= limit then + instructions_count = instructions_count + 1 + if instructions_count >= limit then cleanup() - error('Timeout') + error('Quota exceeded: ' .. tostring(instructions_count) .. '/' .. tostring(limit) .. ' instructions') end end debug.sethook(timeout, "", step) diff --git a/spec/sandbox_spec.lua b/spec/sandbox_spec.lua index 911246b..36b528d 100644 --- a/spec/sandbox_spec.lua +++ b/spec/sandbox_spec.lua @@ -40,13 +40,19 @@ describe('sandbox', function() describe('when handling infinite loops', function() + it('throws an error with infinite loops', function() assert.has_error(function() sandbox("while true do end") end) end) + it('restores string.rep even after a while true', function() + assert.has_error(function() sandbox("while true do end") end) + assert.equal('hellohello', string.rep('hello', 2)) + end) + it('#focus accepts a limit param', function() - --assert.no_has_error(function() sandbox("for i=1,10000 do end") end) - assert.has_error(function() sandbox("for i=1,10000 do end", {limit = 50}) end) + assert.no_has_error(function() sandbox("for i=1,100 do end") end) + assert.has_error(function() sandbox("for i=1,100 do end", {limit = 50}) end) end) end)