Module:Math: Difference between revisions

get #expr without a frame, don't make unnecessary wrappers, and lazily initialise dependent modules, per protected edit request by User:Jackmcbarn
en>Mr. Stradivarius
(replace frame:preprocess with frame:callParserFunction per protected edit request by User:Jackmcbarn)
en>Mr. Stradivarius
(get #expr without a frame, don't make unnecessary wrappers, and lazily initialise dependent modules, per protected edit request by User:Jackmcbarn)
Line 5:
]]
 
local yesno, =getArgs require('Module:Yesno')-- lazily initialized
local getArgs = require('Module:Arguments').getArgs
 
local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.
Line 135 ⟶ 134:
local input_number;
 
if not yesno then
yesno = require('Module:Yesno')
end
if yesno(trap_fraction, true) then -- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]].
local pos = string.find(input_string, '/', 1, true);
Line 491 ⟶ 493:
-- If failed, attempt to evaluate input as an expression
if number == nil then
local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
local frame = mw.getCurrentFrame()
if attempt ~= nilsuccess then
local attempt = frame:callParserFunction('#expr', number_string)
attempt number = tonumber(attemptresult)
if attempt ~= nil then
number = attempt
number_string = tostring(number)
else
Line 518:
]]
 
local mt = { __index = function(t, k)
local function makeWrapper(funcName)
return function (frame)
if not getArgs then
local args = getArgs(frame) -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
local getArgs = require('Module:Arguments').getArgs
return wrap[funcName](args)
end
local args =return wrap[k](getArgs(frame)) -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
end
end }
 
for funcName in pairs(wrap) do
p[funcName] = makeWrapper(funcName)
end
 
return setmetatable(p, mt)
Anonymous user