Module:LuaCall: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
force debug option
Implementing suggested fix (no, it doesn't savvy dots!)
Line 5: Line 5:
local debuglog="Parent: </nowiki><br /><nowiki>"
local debuglog="Parent: </nowiki><br /><nowiki>"
local reserved_value={}
local reserved_value={}
local reserved_counter=0
local reserved_function,reserved_contents
local reserved_function,reserved_contents
for k,v in pairs(parent.args or {}) do
for k,v in pairs(parent.args or {}) do
Line 20: Line 19:
--- this will not stop us from trying to implement one single lousy function call
--- this will not stop us from trying to implement one single lousy function call
if _G[1] then
if _G[1] then
reserved_function,reserved_contents=mw.ustring.match(_G[1],"^(%s*%a[^%s%(]*)%(([^%)]*)%)%s*$")
reserved_function,reserved_contents=mw.ustring.match(_G[1],"^%s*(%a[^%s%(]*)%(([^%)]*)%)%s*$")
debuglog=debuglog.."Function is:" .. reserved_function .. "</nowiki><br /><nowiki>Contents are:"..reserved_contents.."</nowiki><br /><nowiki>"
debuglog=debuglog.."Function is:" .. reserved_function .. "</nowiki><br /><nowiki>Contents are:"..reserved_contents.."</nowiki><br /><nowiki>"
if reserved_contents then
repeat
reserved_counter=reserved_counter+1
reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "</nowiki><br /><nowiki>"
reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
until not reserved_contents
end
end
end
if reserved_contents then
debuglog=debuglog.."Lookup _G value of "..tostring(reserved_function).." is "..tostring(reserved_call)
local reserved_counter=0
repeat
reserved_counter=reserved_counter+1
reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "</nowiki><br /><nowiki>"
reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
until not reserved_contents
end
local reserved_arraypart=_G
while reserved_function do
reserved_call=reserved_call[mw.ustring.match(reserved_function,"^(%a[^%.]*)%.") or reserved_arraypart]
local reserved_function=mw.ustring.match(reserved_function,"^%a[^%.]*%.(.*)$")
debuglog=debuglog.."->"..reserved_function.."</nowiki><br /><nowiki>"
end
debuglog=debuglog.."Lookup _G value is "..tostring(reserved_call)
if reserved_debug or not reserved_function then return frame.preprocess(frame,"<nowiki>"..debuglog.."</nowiki>") end
if reserved_debug or not reserved_function then return frame.preprocess(frame,"<nowiki>"..debuglog.."</nowiki>") end
local reserved_call=_G[reserved_function]
return _G[reserved_function](unpack(reserved_contents or {}))
return _G[reserved_function](unpack(reserved_contents or {}))
end
end

Revision as of 18:50, 17 April 2013

Documentation for this module may be created at Module:LuaCall/doc

local p={}

function p.main(frame)
    local parent=frame.getParent(frame) or {}
    local debuglog="Parent: </nowiki><br /><nowiki>"
    local reserved_value={}
    local reserved_function,reserved_contents
    for k,v in pairs(parent.args or {}) do
        _G[k]=v -- transfer every parameter directly to the global variable table
        debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"
    end
    debuglog=debuglog.."Frame: </nowiki><br /><nowiki>"
    for k,v in pairs(frame.args or {}) do
        debuglog=debuglog..k.."="..v.."</nowiki><br /><nowiki>"        
        _G[k]=v -- transfer every parameter directly to the global variable table
    end
     --- Alas Scribunto does NOT implement coroutines, according to
     --- http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#string.format
     --- this will not stop us from trying to implement one single lousy function call
    if _G[1] then
        reserved_function,reserved_contents=mw.ustring.match(_G[1],"^%s*(%a[^%s%(]*)%(([^%)]*)%)%s*$")
        debuglog=debuglog.."Function is:" .. reserved_function .. "</nowiki><br /><nowiki>Contents are:"..reserved_contents.."</nowiki><br /><nowiki>"
    end
    if reserved_contents then
        local reserved_counter=0
        repeat
            reserved_counter=reserved_counter+1
            reserved_value[reserved_counter]=mw.ustring.match(reserved_contents,"([^%,]+)")
            debuglog=debuglog.."["..reserved_counter.."] = "..tostring(reserved_value[reserved_counter]) .. "</nowiki><br /><nowiki>"
            reserved_contents=mw.ustring.match(reserved_contents,"[^%,]+,(.*)$")
        until not reserved_contents
    end
    local reserved_arraypart=_G
    while reserved_function do
        reserved_call=reserved_call[mw.ustring.match(reserved_function,"^(%a[^%.]*)%.") or reserved_arraypart]
        local reserved_function=mw.ustring.match(reserved_function,"^%a[^%.]*%.(.*)$")
        debuglog=debuglog.."->"..reserved_function.."</nowiki><br /><nowiki>"
    end
    debuglog=debuglog.."Lookup _G value is "..tostring(reserved_call)
    if reserved_debug or not reserved_function then return frame.preprocess(frame,"<nowiki>"..debuglog.."</nowiki>") end
    return _G[reserved_function](unpack(reserved_contents or {}))
end
return p