Module:Math: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
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: | Line 5: | ||
]] |
]] |
||
local yesno |
local yesno, getArgs -- lazily initialized |
||
⚫ | |||
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') |
|||
⚫ | |||
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 success, result = pcall(mw.ext.ParserFunctions.expr, number_string) |
|||
local frame = mw.getCurrentFrame() |
|||
⚫ | |||
local attempt = frame:callParserFunction('#expr', number_string) |
|||
number = tonumber(result) |
|||
⚫ | |||
number = attempt |
|||
number_string = tostring(number) |
number_string = tostring(number) |
||
else |
else |
||
Line 518: | Line 518: | ||
]] |
]] |
||
local mt = { __index = function(t, k) |
|||
local function makeWrapper(funcName) |
|||
return function |
return function(frame) |
||
if not getArgs then |
|||
⚫ | |||
⚫ | |||
return wrap[funcName](args) |
|||
end |
|||
⚫ | |||
end |
end |
||
end |
end } |
||
for funcName in pairs(wrap) do |
|||
p[funcName] = makeWrapper(funcName) |
|||
⚫ | |||
return p |
return setmetatable(p, mt) |