Last active
July 1, 2018 18:23
-
-
Save sharow/058f175a14a29265ec3b to your computer and use it in GitHub Desktop.
lua: module reloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- -*- Mode: lua; tab-width: 2; lua-indent-level: 2; indent-tabs-mode: nil; -*- | |
local loader = {} | |
local package = require "package" | |
loader.last_err = false | |
loader.last_err_msg = nil | |
function loader.load(module) | |
return require(module) | |
end | |
function loader.reload(module) | |
if package.loaded[module] == nil then | |
-- initial load | |
return require(module) | |
else | |
-- reload | |
local oldmod = package.loaded[module] | |
package.loaded[module] = nil | |
local ok, obj = pcall(require, module) | |
if ok then | |
loader.last_err = false | |
return obj | |
else | |
loader.last_err = true | |
loader.last_err_msg = obj | |
package.loaded[module] = oldmod | |
return oldmod | |
end | |
end | |
end | |
return loader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment