Content deleted Content added
Artoria2e5 (talk | contribs) A mostly irrelevant util function... |
Plastikspork (talk | contribs) m Plastikspork moved page Module:Arguments/Fallback to Module:Sandbox/Artoria2e5/Fallback without leaving a redirect: Per user request |
||
(4 intermediate revisions by one other user not shown) | |||
Line 6:
local oldArgsMeta = getmetatable(args) or {}
local newArgsMeta = {}
-- Forget about thread-safety.
-- States kept to avoid strange loops.
local referencedKeys = {} -- dataType:Set/hashtable-impl
local attemptDepth = 0 -- useful for "last nil" case
-- Start the new metatable as a copy of the old metatable.
for k, v in ipairs(oldArgsMeta) do
-- 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▼
local val = oldArgsMeta.__index(t, k)▼
if val ~= nil then▼
referencedKeys[k] = true
▲ return val
end▼
end▼
referencedKeys = {}
return val
end
end
attemptDepth = attemptDepth + 1
-- 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
referencedKeys = {}
▲ end
return t[v]
end
end
attemptDepth = attemptDepth - 1
if attemptDepth == 0 then
referencedKeys = {}
end
return nil
Line 38 ⟶ 59:
return makeFallback
--[[
P.S. Don't blame me for writing these obj-states. Sure, we can use an
accumulator and do some "index with acc" ourselves, but that sounds like too
much work done just to avoid states.
Here is that custom index function in case anyone is curious about it:
function index_w_acc(t, k, acc)
local v = rawget(t, k)
if v ~= nil
else
return getmetatable(t).__index(k, acc)
end
My guess is that it will certainly be slower than the native t[k] one. Perhaps
not getting the metatable every time will help a bit.
]]--
|