Lazy alias handling, remove testing code

This commit is contained in:
Lars Mueller 2021-06-18 20:55:08 +02:00
parent cedfe1387b
commit 137e471262

@ -54,7 +54,7 @@ for _, file in pairs{
"persistence", "persistence",
"debug" "debug"
} do } do
modules[file] = true modules[file] = file
end end
if minetest then if minetest then
for _, file in pairs{ for _, file in pairs{
@ -64,9 +64,12 @@ if minetest then
-- deprecated -- deprecated
"conf" "conf"
} do } do
modules[file] = true modules[file] = file
end end
end end
-- aliases
modules.string = "text"
modules.number = "math"
local parent_dir local parent_dir
if not minetest then if not minetest then
@ -104,25 +107,28 @@ modlib = setmetatable({
end end
end end
}, { }, {
__index = function(self, module_name) __index = function(self, module_name_or_alias)
if not modules[module_name] then local module_name = modules[module_name_or_alias]
if not module_name then
-- module not available, return nil -- module not available, return nil
return return
end end
local environment = dofile(minetest local environment
if module_name ~= module_name_or_alias then
-- alias handling
environment = self[module_name]
else
environment = dofile(minetest
and get_resource(self.modname, module_name .. ".lua") and get_resource(self.modname, module_name .. ".lua")
or (parent_dir .. module_name .. ".lua")) or (parent_dir .. module_name .. ".lua"))
self[module_name] = environment end
self[module_name_or_alias] = environment
return environment return environment
end end
}) })
modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua")) modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua"))
-- Aliases
modlib.string = modlib.text
modlib.number = modlib.math
if minetest then if minetest then
modlib.conf.build_setting_tree() modlib.conf.build_setting_tree()
modlib.mod.get_resource = get_resource modlib.mod.get_resource = get_resource
@ -130,7 +136,6 @@ end
_ml = modlib _ml = modlib
modlib.mod.include"test.lua"
--[[ --[[
--modlib.mod.include"test.lua" --modlib.mod.include"test.lua"
]] ]]