Skip to content

Commit

Permalink
better handling of loaded chunks in inline and also file names
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianGray committed Oct 19, 2015
1 parent 8ed7aeb commit f6a1d10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lusty-0.8-0.rockspec → lusty-0.8-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package = "lusty"
version = "0.8-0"
version = "0.8-1"
source = {
url = "https://github.com/Olivine-Labs/lusty/archive/v0.8.tar.gz",
dir = "lusty-0.8"
Expand Down
52 changes: 31 additions & 21 deletions lusty/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local loaded = {}
local chunks = {}
local fileNames = {}

local function loadModule(name)
Expand All @@ -17,21 +18,16 @@ local function loadModule(name)
return nil, table.concat(errors)
end

local function rewriteError(message, fileName)
local ok, err = pcall(function()
if type(message) == 'string' then
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
local function rewriteLineNumber(message, lineMod)
if type(message) == 'string' then
if lineMod ~= 0 then
local _, _, lineNumber = message:find(':(%d):')
if tonumber(lineNumber) and tonumber(lineNumber) > 0 then
message = message:gsub(':'..lineNumber..':', ':'..(lineNumber+lineMod)..':')
end
end
return message
end)
return err
end
return message
end

--load file, memoize, execute loaded function inside environment
Expand All @@ -48,29 +44,43 @@ local function inline(name, env)
lineMod = -1
end

local file = loaded[name..'/'..table.concat(keys, '/')]
local namespace = loaded[name]
local file, signature = nil, table.concat(keys, '/')
if namespace then
file = namespace[signature]
end
if not file then
local fileName = nil
local code, err = loadModule(name)
local code = chunks[name]
local err
if not code then
error(err)
code, err = loadModule(name)
if not code then
error(err)
end
chunks[name] = code
end
if #keys > 0 then
file, err = loadstring(
'local '..table.concat(keys, ',')..'=select(2, ...)\n'..code
'local '..table.concat(keys, ',')..'=select(2, ...)\n'..code,
fileNames[name]
)
else
file, err = loadstring(code)
file, err = loadstring(code, fileNames[name])
end
if not file then error(rewriteLineNumber(err, lineMod)) end
if not namespace then
namespace = {}
loaded[name] = namespace
end
if not file then error(rewriteError(err, fileNames[name], lineMod)) end
loaded[name..'/'..table.concat(keys, '/')] = file
loaded[name][signature] = file
end

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

Expand Down
1 change: 0 additions & 1 deletion spec/dummy/inlineFunction.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

return foo, bat
1 change: 1 addition & 0 deletions spec/inline_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package.path = './?.lua;'..package.path
describe('verify that inline handles environments properly', function()
local util = require 'lusty.util'

Expand Down

0 comments on commit f6a1d10

Please sign in to comment.