Skip to content

Instantly share code, notes, and snippets.

@GoodClover
Created September 23, 2023 15:47
Show Gist options
  • Save GoodClover/fdfe38e20d8f79570f5710b8f40c2b4b to your computer and use it in GitHub Desktop.
Save GoodClover/fdfe38e20d8f79570f5710b8f40c2b4b to your computer and use it in GitHub Desktop.
Change to a modifiable _ENV, whilst keeping access to existing environments.
local mkenvlist_mt = {
__index = function(self, index)
for _, env in ipairs(self._ENV_READ_LIST) do
local value = env[index]
if value != nil then
return value
end
end
return nil
end,
__newindex = function(self, index, value)
self._ENV_WRITE[index] = value
end,
}
local function mkenvlist(write, reads)
return setmetatable({ _ENV_WRITE=write, _ENV_READ_LIST=reads }, mkenvlist_mt)
end
@GoodClover
Copy link
Author

e.g.

local foolib = {}
local _ENV = mkenvlist(foolib, { foolib, _ENV })

function bar(baz, boink)
	return baz .. boink .. baz
end

return foolib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment