diff options
-rw-r--r-- | indra/newview/scripts/lua/require/UI.lua | 9 | ||||
-rw-r--r-- | indra/newview/scripts/lua/require/util.lua | 19 |
2 files changed, 20 insertions, 8 deletions
diff --git a/indra/newview/scripts/lua/require/UI.lua b/indra/newview/scripts/lua/require/UI.lua index 2df70fd453..bbcae3514a 100644 --- a/indra/newview/scripts/lua/require/UI.lua +++ b/indra/newview/scripts/lua/require/UI.lua @@ -7,14 +7,7 @@ local util = require 'util' -- Allow lazily accessing UI submodules on demand, e.g. a reference to -- UI.Floater lazily loads the UI/Floater module. -local UI = util.setmetamethods{ - __index=function(t, key) - local mod = require('UI/' .. key) - -- cache the submodule - t[key] = mod - return mod - end -} +local UI = util.submoduledir({}, 'UI') -- *************************************************************************** -- registered menu actions diff --git a/indra/newview/scripts/lua/require/util.lua b/indra/newview/scripts/lua/require/util.lua index a000359226..40737a159a 100644 --- a/indra/newview/scripts/lua/require/util.lua +++ b/indra/newview/scripts/lua/require/util.lua @@ -92,4 +92,23 @@ function util.setmetamethods(specs) return t end +-- On the passed module (i.e. table), set an __index metamethod such that +-- referencing module.submodule lazily requires(path/submodule). +-- The loaded submodule is cached in the module table so it need not be passed +-- to require() again. +-- 'path', like any require() string, can be relative to LuaRequirePath. +-- Returns the enriched module, permitting e.g. +-- mymod = util.submoduledir({}, 'mymod') +function util.submoduledir(module, path) + return util.setmetamethods{ + module, + __index=function(t, key) + local mod = require(`{path}/{key}`) + -- cache the submodule + t[key] = mod + return mod + end + } +end + return util |