Skip to content

Instantly share code, notes, and snippets.

@sanny-io
Created August 15, 2018 06:33
Show Gist options
  • Select an option

  • Save sanny-io/0f491f08578a063f619e0f09295f9839 to your computer and use it in GitHub Desktop.

Select an option

Save sanny-io/0f491f08578a063f619e0f09295f9839 to your computer and use it in GitHub Desktop.
local base = _G
require("string")
require("table")
module("config")
settings = {}
function loadFunction(fct)
local old_mt = base.getmetatable(settings)
local loader = { active = true }
base.setmetatable(settings,
{
__index = function(table, key)
local t = {}
base.setmetatable(t, base.getmetatable(table))
if loader.active and base.rawget(table, key) == nil then base.rawset(table, key, t) end
return base.rawget(table, key)
end,
})
base.setfenv(fct, settings)
local ret, err = base.pcall(fct)
loader.active = false
if ret then return true
else return false, err end
end
function loadString(str)
local fct = base.loadstring(str)
if fct then
return loadFunction(fct)
end
return nil, "Could not load string"
end
function loadFile(file)
local fct = base.loadfile(file)
if fct then
return loadFunction(fct)
end
return nil, "Could not load file"
end
load = loadFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment