Extension:Scribunto/Debug console: Difference between revisions
mNo edit summary Tags: Mobile edit Mobile web edit |
mNo edit summary Tags: Mobile edit Mobile web edit |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 3: | Line 3: | ||
[[File:Scribunto Debug console 2021-03-19.png|thumb|Debug console on [https://commons.wikimedia.org/w/index.php?title=Module:Color/sandbox&action=edit#mw-scribunto-console Module:Color/sandbox]]] |
[[File:Scribunto Debug console 2021-03-19.png|thumb|Debug console on [https://commons.wikimedia.org/w/index.php?title=Module:Color/sandbox&action=edit#mw-scribunto-console 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. |
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 <code>=</code>, for example <code>=a</code> will return value of the variable "a" if it was previously defined. |
This includes the ability to return value of a variable using <code>=</code>, for example <code>=a</code> 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 <code>=p</code>. (Note that the actual name of the variable doesn't matter, as it is always only accessible under that name.) |
|||
<br>{{anchor|export_table}}If that variable is a table, it is known as the "export table" and all functions belonging to it can be accessed with the <code>#invoke</code> [[Parser function]]. |
|||
⚫ | |||
To simulate calling a function inside the module, you need to enter a line in the following format: |
To simulate calling a function inside the module, you need to enter a line in the following format: |
||
<code>= |
<code>=p.function(frame)</code> |
||
<br>Where " |
<br>Where "function" is the function in the Lua module [[#export_table|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== |
==Examples== |
||
===Invoking module directly=== |
===Invoking module directly=== |
||
Given [[Module:Test]] with the following contents: |
Given [[Module:Test]] with the following contents: |
||
Line 19: | Line 21: | ||
local args = frame.args |
local args = frame.args |
||
mw.log("log test") |
mw.log("log test") |
||
return args[1]..args[" |
return args[1]..args["sep"]..args[2] |
||
end |
end |
||
return p |
return p |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
The following invoke call: |
The following invoke call: |
||
<code><nowiki>{{#invoke:Test|a| |
<code><nowiki>{{#invoke:Test|a|sep=;|b}}</nowiki></code> |
||
Can be simulated accurately with the following |
Can be simulated accurately with the following code in the debug console: |
||
<syntaxhighlight lang="lua"> |
|||
=p.main( |
|||
mw.getCurrentFrame():newChild{ |
|||
title="Module:Test", |
|||
args={"a",["sep"]=";","b"} |
|||
} |
|||
)</syntaxhighlight> |
|||
Note that in this example the element in the export table which is being evaluated is called "main". |
|||
====Simplified version==== |
====Simplified version==== |
||
⚫ | If the module you are debugging does not use any [[Extension:Scribunto/Lua_reference_manual#Frame_object|frame object methods]] (such as <code>frame:preprocess()</code>), like the example [[#Invoking module directly|above]], then the frame object analog(?) does not have to be complete, and the debugging code can be simplified into the following: |
||
<syntaxhighlight lang="lua"> |
|||
⚫ | If the module you are debugging does not use any [[Extension:Scribunto/Lua_reference_manual#Frame_object|frame object methods]] (such as <code>frame:preprocess()</code>), like the example [[#Invoking module directly|above]], then the frame object analog(?) does not have to be complete, and the debugging |
||
=p.main{ |
|||
args={"a",["sep"]=";","b"} |
|||
}</syntaxhighlight> |
|||
===Invoking through a template=== |
===Invoking through a template=== |
||
Line 39: | Line 51: | ||
local args = frame:getParent().args |
local args = frame:getParent().args |
||
mw.log("log test") |
mw.log("log test") |
||
return args[1]..args[" |
return args[1]..args["sep"]..args[2] |
||
end |
end |
||
return p |
return p |
||
Line 46: | Line 58: | ||
<br><code><nowiki>{{#invoke|Test|main}}</nowiki></code> |
<br><code><nowiki>{{#invoke|Test|main}}</nowiki></code> |
||
<br>The following template call: |
<br>The following template call: |
||
<code><nowiki>{{Test|a| |
<code><nowiki>{{Test|a|sep=;|b}}</nowiki></code> |
||
<br>Can be simulated accurately with the following |
<br>Can be simulated accurately with the following code in the debug console: |
||
<syntaxhighlight lang="lua"> |
|||
<pre>=p.main(mw.getCurrentFrame():newChild{title="Template:Test",args={"a",["delimiter"]=";","b"}}:newChild{title="Module:Test",args={}})</pre> |
|||
=p.main( |
|||
⚫ | |||
mw.getCurrentFrame():newChild{ |
|||
⚫ | |||
title="Template:Test", |
|||
args={"a",["sep"]=";","b"} |
|||
}:newChild{ |
|||
title="Module:Test", |
|||
args={} |
|||
} |
|||
) |
|||
</syntaxhighlight> |
|||
⚫ | |||
⚫ | |||
<syntaxhighlight lang="lua"> |
|||
=p.main( |
|||
mw.getCurrentFrame():newChild{ |
|||
args={"a",["sep"]=";","b"} |
|||
}:newChild{} |
|||
) |
|||
</syntaxhighlight> |
Latest revision as of 23:32, 12 June 2024
Usage
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{}
)