Jump to content

Extension:Scribunto/Debug console

From mediawiki.org

Quick Guide

Presented below the Debug console are the following quick tips:

  • The module exports are available as the variable p, including unsaved modifications.
  • Precede a line with "=" to evaluate it as an expression or use print(). Use mw.logObject() for tables.
  • Use mw.log() and mw.logObject() in module code to send messages to this console.

Usage

Debug console on Module:Color/sandbox

Under the module editing window there is a "Debug console" window, where you can enter any line of text for Lua to evaluate. This includes the ability to return value of a variable using =, for example =a will return value of the variable "a" if it was previously defined in the console.

More importantly, it is also possible for the console to return value of the first variable returned by the module, using =p. (Note that the actual name of the variable doesn't matter, as it is always only accessible under that name.)
If that variable is a table, it is known as the "export table" and all functions belonging to it can be accessed with the #invoke Parser function.
Additionally, the console will also show output from print(), mw.log() and mw.logObject() functions.

To simulate calling a function inside the module, you need to enter a line in the following format: =p.function(frame)
Where "function" is the function in the Lua module export table that you want to call (for example "main"), and "frame" is a frame object analog(?) of the call that invokes the module.

Examples

Invoking module directly

Given Module:Test with the following contents:

local p = {}
function p.main(frame)
   local args = frame.args
   mw.log("log test")
   return args[1]..args["sep"]..args[2]
end
return p

The following invoke call: {{#invoke:Test|a|sep=;|b}} Can be simulated accurately with the following code in the debug console:

=p.main(
   mw.getCurrentFrame():newChild{
      title="Module:Test",
      args={"a",["sep"]=";","b"}
   }
)

Note that in this example the element in the export table which is being evaluated is called "main".

Simplified version

If the module you are debugging does not use any frame object methods (such as frame:preprocess()), like the example above, then the frame object analog(?) does not have to be complete, and the debugging code can be simplified into the following:

=p.main{
   args={"a",["sep"]=";","b"}
}

Invoking through a template

Given Module:Test with the following contents:

local p = {}
function p.main(frame)
   local args = frame:getParent().args
   mw.log("log test")
   return args[1]..args["sep"]..args[2]
end
return p

And the following "Template:Test":
{{#invoke|Test|main}}
The following template call: {{Test|a|sep=;|b}}
Can be simulated accurately with the following code in the debug console:

=p.main(
   mw.getCurrentFrame():newChild{
      title="Template:Test",
      args={"a",["sep"]=";","b"}
   }:newChild{
      title="Module:Test",
      args={}
   }
)

This example does not declare any extra arguments directly in the #invoke call, if that's required then appropiate elements should be added to the empty args table near the end of the debug code.

Note that this module uses frame:getParent() method, which means the debug code cannot be simplified as much, although it is still possible:

=p.main(
   mw.getCurrentFrame():newChild{
      args={"a",["sep"]=";","b"}
   }:newChild{}
)

Invoking module exports

Given Module:Test with the following contents:

local p = {}
function p.log(str)
   mw.log(str)
end
return p

Allows you to enter, p.log("hello") to call the modules exported function.

Workflow

As Scribunto, will reload the module live without saving changes, it can lead to a better workflow.

To use this workflow:

  1. Edit your Module
  2. Do not save changes
  3. Try calling your Modules function in the debug console

This also prevents, a lot of edits and frustration when editing.