From: Marek Vavruša Date: Tue, 31 Mar 2015 20:47:52 +0000 (+0200) Subject: daemon/lua: made ‘modules’ append-only X-Git-Tag: v1.0.0-beta1~274^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62ceadb012c789d8dfa3164879b4992ecbec83f4;p=thirdparty%2Fknot-resolver.git daemon/lua: made ‘modules’ append-only this makes some services append-only, so the configuration can look like: modules = { cache = ‘’, hints = true, } without wiping previously-set modules --- diff --git a/daemon/lua/init.lua b/daemon/lua/init.lua index 6c7a170d7..e71b08720 100644 --- a/daemon/lua/init.lua +++ b/daemon/lua/init.lua @@ -1,9 +1,32 @@ -- Syntactic sugar for module loading -- `modules. = ` -local modules_mt = { +setmetatable(modules, { __newindex = function (t,k,v) modules.load(k) - _G[k]['config'](v) + if _G[k] then + local config_call = _G[k]['config'] + if config_call and config_call[''] then + config_call(v) + end + end end -} -setmetatable(modules, modules_mt) \ No newline at end of file +}) + +-- Some services are append-only +function protect(defined) + local __protected = { ['modules'] = true } + return setmetatable({}, { + __index = defined, + __newindex = function (t, k, v) + if __protected[k] then + for k2,v2 in pairs(v) do + defined[k][k2] = v2 + end + else + defined[k] = v + end + end + }) +end +_G = protect(getfenv(0)) +setfenv(0, _G)