Module:utilities/require when needed: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Created page with "local loaded = package.loaded local require = require local setmetatable = setmetatable return function(text, key) local module = loaded[text] if module then return key and module[key] or module end local mt = {} function mt:__index(k) module = module or key and require(text)[key] or require(text) return module[k] end function mt:__call(...) module = module or key and require(text)[key] or require(text) return module(...) end return setmetat..."
 
No edit summary
 
Line 1: Line 1:
local getmetatable = getmetatable
local ipairs = ipairs
local loaded = package.loaded
local loaded = package.loaded
local pairs = pairs
local require = require
local require = require
local select = select
local setmetatable = setmetatable
local setmetatable = setmetatable
local tostring = tostring
local unpack = unpack or table.unpack -- Lua 5.2 compatibility


return function(text, key)
local function get_nested(obj, ...)
local module = loaded[text]
local n = select("#", ...)
if n == 0 then
if module then
return obj
return key and module[key] or module
end
end
obj = obj[...]
local mt = {}
for i = 2, n do
obj = obj[select(i, ...)]
function mt:__index(k)
module = module or key and require(text)[key] or require(text)
return module[k]
end
end
return obj
function mt:__call(...)
end
module = module or key and require(text)[key] or require(text)
 
return module(...)
local function get_obj(mt)
local obj = require(mt[1])
if #mt > 1 then
obj = get_nested(obj, unpack(mt, 2))
end
mt[0] = obj
return obj
end
 
local function __call(self, ...)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
return obj(...)
end
 
local function __index(self, k)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
return obj[k]
end
 
local function __ipairs(self)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
return ipairs(obj)
end
 
local function __newindex(self, k, v)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
obj[k] = v
end
 
local function __pairs(self)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
return pairs(obj)
end
 
local function __tostring(self)
local mt = getmetatable(self)
local obj = mt[0]
if obj == nil then
obj = get_obj(mt)
end
return tostring(obj)
end
 
return function(modname, ...)
local mod = loaded[modname]
if mod ~= nil then
return get_nested(mod, ...)
end
end
return setmetatable({}, {
return setmetatable({}, mt)
modname,
__call = __call,
__index = __index,
__ipairs = __ipairs,
__newindex = __newindex,
__pairs = __pairs,
__tostring = __tostring,
-- TODO: other metamethods, if needed.
...
})
end
end

Latest revision as of 12:27, 21 June 2026



local getmetatable = getmetatable
local ipairs = ipairs
local loaded = package.loaded
local pairs = pairs
local require = require
local select = select
local setmetatable = setmetatable
local tostring = tostring
local unpack = unpack or table.unpack -- Lua 5.2 compatibility

local function get_nested(obj, ...)
	local n = select("#", ...)
	if n == 0 then
		return obj
	end
	obj = obj[...]
	for i = 2, n do
		obj = obj[select(i, ...)]
	end
	return obj
end

local function get_obj(mt)
	local obj = require(mt[1])
	if #mt > 1 then
		obj = get_nested(obj, unpack(mt, 2))
	end
	mt[0] = obj
	return obj
end

local function __call(self, ...)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	return obj(...)
end

local function __index(self, k)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	return obj[k]
end

local function __ipairs(self)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	return ipairs(obj)
end

local function __newindex(self, k, v)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	obj[k] = v
end

local function __pairs(self)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	return pairs(obj)
end

local function __tostring(self)
	local mt = getmetatable(self)
	local obj = mt[0]
	if obj == nil then
		obj = get_obj(mt)
	end
	return tostring(obj)
end

return function(modname, ...)
	local mod = loaded[modname]
	if mod ~= nil then
		return get_nested(mod, ...)
	end
	return setmetatable({}, {
		modname,
		__call = __call,
		__index = __index,
		__ipairs = __ipairs,
		__newindex = __newindex,
		__pairs = __pairs,
		__tostring = __tostring,
		-- TODO: other metamethods, if needed.
		...
	})
end