14,061
edits
m (1 revision) |
(fmt) |
||
Line 46: | Line 46: | ||
local function tidyValNoChange(key, val) | local function tidyValNoChange(key, val) | ||
return val | return val | ||
end | end | ||
Line 60: | Line 55: | ||
--[[ | --[[ | ||
-- Get the | -- Get the arguments from the frame object if available. If the frame object | ||
-- is not available, we are being called from another Lua module or from the | |||
-- debug console, so assign the args to a new variable so we can | |||
-- | -- differentiate them. | ||
-- | |||
-- | |||
--]] | --]] | ||
local fargs, pargs, luaArgs | local fargs, pargs, luaArgs | ||
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then | if type(frame.args) == 'table' and type(frame.getParent) == 'function' then | ||
if | if not options.parentOnly then | ||
fargs = frame.args | |||
end | |||
if not options.frameOnly then | |||
pargs = frame:getParent().args | |||
end | end | ||
if options.parentFirst then | if options.parentFirst then | ||
Line 170: | Line 122: | ||
setmetatable(args, metatable) | setmetatable(args, metatable) | ||
local function mergeArgs(tables) | local function mergeArgs(iterator, tables) | ||
--[[ | --[[ | ||
-- Accepts multiple tables as input and merges their keys and values | -- Accepts multiple tables as input and merges their keys and values | ||
-- into one table. If a value is already present it is not overwritten; | -- into one table using the specified iterator. If a value is already | ||
-- present it is not overwritten; tables listed earlier have precedence. | |||
-- | -- We are also memoizing nil values, but those values can be | ||
-- overwritten. | |||
--]] | --]] | ||
for _, t in ipairs(tables) do | for _, t in ipairs(tables) do | ||
for key, val in | for key, val in iterator(t) do | ||
if metaArgs[key] == nil | if metaArgs[key] == nil then | ||
local tidiedVal = tidyVal(key, val) | local tidiedVal = tidyVal(key, val) | ||
if tidiedVal == nil then | if tidiedVal == nil then | ||
nilArgs[key] = | nilArgs[key] = true | ||
else | else | ||
metaArgs[key] = tidiedVal | metaArgs[key] = tidiedVal | ||
Line 223: | Line 176: | ||
for _, argTable in ipairs(argTables) do | for _, argTable in ipairs(argTables) do | ||
local argTableVal = tidyVal(key, argTable[key]) | local argTableVal = tidyVal(key, argTable[key]) | ||
if argTableVal | if argTableVal == nil then | ||
nilArgs[key] = true | |||
else | |||
metaArgs[key] = argTableVal | metaArgs[key] = argTableVal | ||
return argTableVal | return argTableVal | ||
end | end | ||
end | end | ||
return nil | return nil | ||
end | end | ||
Line 258: | Line 212: | ||
--]] | --]] | ||
metaArgs[key] = nil | metaArgs[key] = nil | ||
nilArgs[key] = | nilArgs[key] = true -- Memoize nils. | ||
else | else | ||
metaArgs[key] = val | metaArgs[key] = val | ||
Line 267: | Line 221: | ||
-- Called when pairs is run on the args table. | -- Called when pairs is run on the args table. | ||
if not metatable.donePairs then | if not metatable.donePairs then | ||
mergeArgs(argTables) | mergeArgs(pairs, argTables) | ||
metatable.donePairs = true | metatable.donePairs = true | ||
metatable.doneIpairs = true | |||
end | end | ||
return pairs(metaArgs) | return pairs(metaArgs) | ||
end | end | ||
metatable.__ipairs = function ( | metatable.__ipairs = function () | ||
-- Called when ipairs is run on the args table. | -- Called when ipairs is run on the args table. | ||
return | if not metatable.doneIpairs then | ||
mergeArgs(ipairs, argTables) | |||
metatable.doneIpairs = true | |||
end | |||
return ipairs(metaArgs) | |||
end | end | ||