Module:LuaCall: Difference between revisions

From Test Wiki
Content deleted Content added
This entire function is out of scope: the purpose of this module is to provide an interpreter for Lua callable via #invoke, not to provide functions to other modules
toNumberOrString() is also used in this module's p.call()/p.get(), they share the same logic of converting template parameters to number/string as in Module:Ustring
Line 40: Line 40:
return reserved_output[reserved_return or 1]
return reserved_output[reserved_return or 1]
end
end
end

local function tonumberOrString(v)
return tonumber(v) or v:gsub("^\\", "", 1)
end

local function tonumberOrStringOnPairs(f, s, i)
local args = {}
for _, v in f, s, i do
--table.insert(args, tonumber(v) or v:gsub("^\\", "", 1))
table.insert(args, tonumberOrString(v))
end
return args
end

-- This function is modified from https://en.wikipedia.org/wiki/Module:Ustring?oldid=885619921
-- All frame's arguments are coerced as number type if possible. If you wish for something to remain a string, you can simply escape it by insert \ at the beginning of the string.
function p.import(o)
local p = {}

for k, v in pairs(o) do if type(v)~='function' then p[k]=v else
p[k] = function(frame)
--local args = {}
--for _, v in ipairs(frame.args) do
-- --table.insert(args, tonumber(v) or v:gsub("^\\", "", 1))
-- table.insert(args, tonumberOrString(v))
--end
--return (v(unpack(args)))
return (v(unpack(
tonumberOrStringOnPairs(ipairs(frame.args))
)))
end
end
end

return p
end
end