Module:Sandbox/Artoria2e5/Fallback: Difference between revisions

Content deleted Content added
A mostly irrelevant util function...
 
Implement loop protection
Line 6:
local oldArgsMeta = getmetatable(args) or {}
local newArgsMeta = {}
-- Forget about thread-safety.
local referencedKeys = {} -- dataType:Set/hashtable-impl
-- Start the new metatable as a copy of the old metatable.
for k, v in ipairs(oldArgsMeta) do
newArgsMeta[k] = v
end
 
-- Change the __index metamethod to our implementation.
-- See https://www.lua.org/pil/13.4.1.html.
newArgsMeta.__index = function (t, k)
-- My friend, why are you here again?
-- Try the old metamethod first.
if referencedKeys[k] then
-- Thanks to closures, this whole oldArgsMeta object will stay.
-- You have to be drunk. Go home.
if oldArgsMeta.__index ~= nil then
return valnil
local val = oldArgsMeta.__index(t, k)
end
if val ~= nil then
referencedKeys[k] = true
return val
end
-- Try the old metamethod first.
end
-- Thanks to closures, this whole oldArgsMeta object will stay.
if oldArgsMeta.__index ~= nil then
local val = oldArgsMeta.__index(t, k)
if val ~= nil then
referencedKeys = {}
return val
end
end
-- Now try use the aliases given.
for _, v in ipairs(arg_aliases[k] or {}) do
-- If a working value is found, use it.
-- Note: mw-argument-specific empty str chk.
if t[v] ~= nil and t[v] ~= '' then
if t[v] ~= nil returnand t[v] ~= '' then
referencedKeys = {}
end
return t[v]
end
end
return nil