Module:Math: Difference between revisions

    m>Mr. Stradivarius
    (replace frame:preprocess with frame:callParserFunction per protected edit request by User:Jackmcbarn)
    m>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: Line 5:
    ]]
    ]]


    local yesno = require('Module:Yesno')
    local yesno, getArgs -- 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.
    local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.
    Line 135: Line 134:
    local input_number;
    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]].
    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);
    local pos = string.find(input_string, '/', 1, true);
    Line 491: Line 493:
    -- If failed, attempt to evaluate input as an expression
    -- If failed, attempt to evaluate input as an expression
    if number == nil then
    if number == nil then
    local frame = mw.getCurrentFrame()
    local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
    local attempt = frame:callParserFunction('#expr', number_string)
    if success then
    attempt = tonumber(attempt)
    number = tonumber(result)
    if attempt ~= nil then
    number = attempt
    number_string = tostring(number)
    number_string = tostring(number)
    else
    else
    Line 518: Line 518:
    ]]
    ]]


    local function makeWrapper(funcName)
    local mt = { __index = function(t, k)
    return function (frame)
    return function(frame)
    local args = getArgs(frame) -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
    if not getArgs then
    return wrap[funcName](args)
    getArgs = require('Module:Arguments').getArgs
    end
    return wrap[k](getArgs(frame)-- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed.
    end
    end
    end
    end }
     
    for funcName in pairs(wrap) do
    p[funcName] = makeWrapper(funcName)
    end


    return p
    return setmetatable(p, mt)