Skip to content

Instantly share code, notes, and snippets.

@sharow
Last active July 1, 2018 18:23
Show Gist options
  • Save sharow/058f175a14a29265ec3b to your computer and use it in GitHub Desktop.
Save sharow/058f175a14a29265ec3b to your computer and use it in GitHub Desktop.
lua: module reloader
-- -*- 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