mirror of
https://github.com/appgurueu/modlib.git
synced 2024-08-19 21:04:13 +02:00
Patch globals leaking into modlib modules
This commit is contained in:
parent
b433b30dbe
commit
8dc3054dd0
15
init.lua
15
init.lua
@ -101,14 +101,21 @@ modlib = setmetatable({
|
|||||||
}, {
|
}, {
|
||||||
__index = function(self, module_name)
|
__index = function(self, module_name)
|
||||||
local files = modules[module_name]
|
local files = modules[module_name]
|
||||||
local module
|
local environment
|
||||||
if type(files) == "string" then
|
if type(files) == "string" then
|
||||||
module = load_module(files)
|
environment = load_module(files)
|
||||||
elseif files then
|
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
|
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
|
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
|
end
|
||||||
self[module_name] = module
|
self[module_name] = module
|
||||||
return module
|
return module
|
||||||
|
3
test.lua
3
test.lua
@ -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 random, huge = math.random, math.huge
|
||||||
local parent_env = getfenv(1)
|
local parent_env = getfenv(1)
|
||||||
setfenv(1, setmetatable({}, {
|
setfenv(1, setmetatable({}, {
|
||||||
|
Loading…
Reference in New Issue
Block a user