Patch globals leaking into modlib modules

This commit is contained in:
Lars Mueller 2021-05-12 18:30:16 +02:00
parent b433b30dbe
commit 8dc3054dd0
2 changed files with 14 additions and 4 deletions

@ -101,14 +101,21 @@ modlib = setmetatable({
}, {
__index = function(self, module_name)
local files = modules[module_name]
local module
local environment
if type(files) == "string" then
module = load_module(files)
environment = load_module(files)
elseif files then
module = loadfile_exports(get_resource(self.modname, module_name, files[1] .. ".lua"))
environment = loadfile_exports(get_resource(self.modname, module_name, files[1] .. ".lua"))
for index = 2, #files do
self.mod.include_env(get_resource(self.modname, module_name, files[index] .. ".lua"), module)
self.mod.include_env(get_resource(self.modname, module_name, files[index] .. ".lua"), environment)
end
else
return
end
local module = {}
for key, value in pairs(environment) do
-- Shallow copy. Doesn't use `modlib.table.shallowcopy` as that is part of a module, too.
module[key] = value
end
self[module_name] = module
return module

@ -1,3 +1,6 @@
-- ensure modlib API isn't leaking into global environment
assert(modlib.bluon.assert ~= assert)
local random, huge = math.random, math.huge
local parent_env = getfenv(1)
setfenv(1, setmetatable({}, {