Skip to content

Commit

Permalink
more error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianGray committed Oct 19, 2015
1 parent ee4e834 commit b44ccac
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lusty-0.7-7.rockspec → lusty-0.7-8.rockspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package = "lusty"
version = "0.7-7"
version = "0.7-8"
source = {
url = "https://github.com/Olivine-Labs/lusty/archive/v0.7.tar.gz",
dir = "lusty-0.7"
Expand Down
34 changes: 23 additions & 11 deletions lusty/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ end
local function rewriteError(message, fileName)
local ok, err = pcall(function()
if type(message) == 'string' then
local _, _, lineNumber = message:find(':(%d):')
if tonumber(lineNumber) and tonumber(lineNumber) > 0 then
lineNumber = lineNumber - 1
if message:find('%[string .*%]') then
return message:gsub('%[.*%]', fileName):gsub(':%d:', ':'..lineNumber..':')
if message:find('%[string .*%]') then
message = message:gsub('%[string .*%]', fileName)
local _, _, lineNumber = message:find(':(%d):')
if tonumber(lineNumber) and tonumber(lineNumber) > 0 then
lineNumber = lineNumber - 1
message = message:gsub(':%d:', ':'..lineNumber..':')
end
end
end
Expand All @@ -35,14 +36,18 @@ end

--load file, memoize, execute loaded function inside environment
local function inline(name, env)
local keys ={}
local values = {}
local keys, values = {}, {}
local n = 1
for k, v in pairs(env) do
keys[n] = k
values[n] = v
n = n + 1
end
local lineMod = 0
if n > 1 then
lineMod = -1
end

local file = loaded[name]
if not file then
local fileName = nil
Expand All @@ -57,13 +62,20 @@ local function inline(name, env)
else
file, err = loadstring(code)
end
if not file then error(rewriteError(err, fileNames[name])) end
if not file then error(rewriteError(err, fileNames[name], lineMod)) end
loaded[name] = file
end
local res = {xpcall(function() return file(name, unpack(values, 1, n)) end, function(m) return rewriteError(m, fileNames[name]) end)}

local res = {
xpcall(function()
return file(name, unpack(values, 1, n))
end, function(m)
return rewriteError(m, fileNames[name], lineMod)
end)
}

if res[1] then
local maxn = table.maxn(res)
return unpack(res, 2, maxn)
return unpack(res, 2, table.maxn(res))
else
error(res[2])
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/error.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error(test)
7 changes: 7 additions & 0 deletions spec/dummy/multilevel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local util = require 'lusty.util'
count = count + 1
if count == 5 then
util.inline('spec.dummy.error', {test=test, foo=foo, count=count})
else
util.inline('spec.dummy.multilevel', {test=test, foo=foo, count=count})
end
7 changes: 3 additions & 4 deletions spec/inline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ describe('verify that inline handles environments properly', function()
util.clearCache()
end)

pending('can set an environment', function()
it('can set an environment', function()
local env = {foo='bar'}
assert('bar' == util.inline('spec.dummy.inlineFunction', env))
end)

pending('can set an environment with multiple variables', function()
it('can set an environment with multiple variables', function()
local env = {foo='bar', bat='baz'}
assert.same({'bar','baz'}, {util.inline('spec.dummy.inlineFunction', env)})
end)

pending('can set an environment with no variables', function()
it('can set an environment with no variables', function()
local env = {}
assert.same({}, {util.inline('spec.dummy.inlineFunction', env)})
end)


it('can set an environment with nil variables', function()
local env = {
foo = nil,
Expand Down

0 comments on commit b44ccac

Please sign in to comment.