FANDOM


--[[ //Helps debug lua modules using console by creating a pseudo frame 
and other useful functions
Syntax:
require("Module:debug").frame(childtable,parenttable)
frame = require("Module:debug").frame({'aaa','sss'},{'zz'=8,'1'})
ex. frame.showargs() , p.main(frame)
Todo:
Add more frame methods
add debug traceback and debugging tools ]]--
 
local p = {}
local debugFrame = {}
local libraryUtil = require( 'libraryUtil' )
 
-- Creates a pseudo frame with some useful functions available in
-- [[mw:Extension:Scribunto]], e.g. `newChild`.
--------------------------------------------------------------------------------
function p.makePseudoFrame(frame, parentArgs, childArgs)
 
    local pseudoFrame = {parent = {}}
    local checkSelf = libraryUtil.makeCheckSelfFunction("pseudoFrame", "obj", pseudoFrame, "PseudoFrame object")
    local parent = pseudoFrame.parent
    local pArgs = {}
    local args = {}
 
    if frame then 
        if type(frame) ~= "table" then
            error("frame must be a table")
        end
        args = frame.args 
 
        for name in pairs(frame) do
            pseudoFrame[name] = function (pseudoFrame, ...)
                return frame[name](frame, unpack(arg))
            end
 
            parent[name] = function (parent, ...)
                return pseudoFrame[name](frame, unpack(arg))
            end
        end
 
        if frame:getParent() then
            pArgs = frame:getParent().args
        end
    end
 
    pseudoFrame.parent.args  = mw.clone(parentArgs or pArgs)
    pseudoFrame.args  = mw.clone(childArgs or args )
    pseudoFrame.title = tostring(mw.title.getCurrentTitle())
    pseudoFrame.parent.title = ""
 
    function pseudoFrame:getTitle()
        checkSelf(self, "getTitle")
 
        return self.title
    end
 
    function pseudoFrame.parent:getTitle()
        return self.parent.title
    end
 
    function pseudoFrame:getParent()
        -- Needs some testing to know why this causes bugs
        -- checkSelf(self, "getParent") 
 
        return self.parent
    end
 
    function pseudoFrame:newChild(...)
        checkSelf(self, "newChild")
 
        local params = unpack(arg)
        local tmpTable = p.makePseudoFrame(frame, self.args, params.args)
 
        tmpTable.parent.title = self.title
        tmpTable.title = params.title
 
        return tmpTable
    end
 
    function pseudoFrame:showargs()
        checkSelf(self,"showargs")
 
        local sChild = "Child parameters:\n"
        local sParent = "Parent parameters:\n"
 
        if (self.args) then
            for i,v in pairs(self.args) do
                sChild = sChild .."\n Key: "..i .. "- Value: " ..v
            end
        end
 
        if (self.parent.args) then
            for i,v in pairs(self.parent.args) do
                sParent = sParent .."\n Key: "..i .. " - Value: " ..v 
            end
        end
 
        return sChild .."\n\n"..sParent
    end
 
    function pseudoFrame:setArgs(childArgs,parentArgs)
        checkSelf(self,"setArgs")
 
        if childArgs then
            self.args =   childArgs or {}
        end
 
        if (parentArgs) then
           self.parent.args =  parentArgs
        end
    end
 
    if not pseudoFrame.preprocess then 
        function pseudoFrame:preprocess (sWikiText)
            checkSelf(self,"preprocess")
 
            return sWikiText
        end
    end
 
    return pseudoFrame
end
 
-- args,pArgs  - table, e.g. {"abc","zz"} parameters from a page calling this module directly
function p.frame(args,pArgs)
    return makePseudoFrame.new(args,pArgs)
end
 
-- Generates a tracebackfor a function
-- ex. function fFunction() x=1 +c end ; p.trace(fFunction)
function p.trace(errFunction,...)
    local function testFunc ()
         return errFunction(unpack(arg))
    end
 
    local success, result = xpcall(testFunc,
        function(err) 
            mw.log(debug.traceback(err) )
            return debug.traceback(err) 
        end)
    return success, result
end 
 
function p.test(frame)
    return frame.args[1]
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.