Add modlib.G & modlib.set_environment

This commit is contained in:
Lars Mueller 2022-10-03 18:22:59 +02:00
parent cc0c11edb4
commit b19d4febb2

@ -77,11 +77,13 @@ modules.number = "math"
local parent_dir local parent_dir
if not minetest then if not minetest then
-- TOFIX
local init_path = arg and arg[0] local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or "" parent_dir = init_path and init_path:match"^.[/\\]" or ""
end end
local dir_delim = rawget(_G, "DIR_DELIM") or (rawget(_G, "package") and package.config and assert(package.config:match("^(.-)[\r\n]"))) or "/" local dir_delim = rawget(_G, "DIR_DELIM") -- Minetest
or (rawget(_G, "package") and package.config and assert(package.config:match("^(.-)[\r\n]"))) or "/"
local function concat_path(path) local function concat_path(path)
return table.concat(path, dir_delim) return table.concat(path, dir_delim)
@ -116,19 +118,43 @@ local function load_module(self, module_name_or_alias)
end end
local rawget, rawset = rawget, rawset local rawget, rawset = rawget, rawset
modlib = setmetatable({ modlib = setmetatable({}, { __index = load_module })
-- TODO bump on release
version = 98, -- TODO bump on release
modname = minetest and minetest.get_current_modname(), modlib.version = 98
_RG = setmetatable({}, {
__index = function(_, index) if minetest then
return rawget(_G, index) modlib.modname = minetest.get_current_modname()
end, end
__newindex = function(_, index, value)
return rawset(_G, index, value) -- Raw globals
end modlib._RG = setmetatable({}, {
}), __index = function(_, index)
}, { __index = load_module }) return rawget(_G, index)
end,
__newindex = function(_, index, value)
return rawset(_G, index, value)
end
})
-- Globals merged with modlib
modlib.G = setmetatable({}, {__index = function(self, module_name)
local module = load_module(self, module_name)
if module == nil then
return _G[module_name]
end
if _G[module_name] then
setmetatable(module, {__index = _G[module_name]})
end
return module
end})
-- "Imports" modlib by changing the environment of the calling function
--! This alters environments at the expense of performance. Use with caution.
--! Prefer localizing modlib library functions or API tables if possible.
function modlib.set_environment()
setfenv(2, setmetatable({}, {__index = modlib.G}))
end
-- Force load file module to pass dir_delim & to set concat_path -- Force load file module to pass dir_delim & to set concat_path
modlib.file = assert(loadfile(get_resource"file.lua"))(dir_delim) modlib.file = assert(loadfile(get_resource"file.lua"))(dir_delim)