Skip to content

Commit

Permalink
Correctly wrap nested requires.
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-linard committed Jan 14, 2016
1 parent 917ad94 commit fdaab38
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/hotswap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function Hotswap:require (name, no_error)
if loaded then
return loaded
end
local back = {
require = (_G or _ENV).require,
package = (_G or _ENV).package,
}
local function exit (...)
(_G or _ENV).require = back.require;
(_G or _ENV).package = back.package;
return ...
end
(_G or _ENV).require = function (...)
return self.require (...)
end
(_G or _ENV).package = self;
local errors = {
"module '" .. tostring (name) .. "' not found:",
}
Expand All @@ -93,7 +106,7 @@ function Hotswap:require (name, no_error)
local ok
ok, result = pcall (factory, name)
if not ok then
return nil, result
return exit (nil, result)
end
else
result = factory (name)
Expand All @@ -108,16 +121,16 @@ function Hotswap:require (name, no_error)
for _, f in pairs (self.on_change) do
f (name, wrapper)
end
return wrapper
return exit (wrapper)
else
errors [#errors+1] = path
end
end
errors = table.concat (errors, "\n")
if no_error then
return nil, errors
return exit (nil, errors)
else
error (errors)
error (exit (errors))
end
end

Expand Down

0 comments on commit fdaab38

Please sign in to comment.