Help:Lua debugging: Difference between revisions

Content deleted Content added
Off-wiki tools: add section "Exasperating bugs"
new key for Category:Module help: "Debugging" using HotCat
 
(10 intermediate revisions by 4 users not shown)
Line 1:
{{WikiProject Lua header}}
This help-page, '''Help:Lua debugging''', explains issues of writing [[Lua (programming language)|Lua script]] and [[debugging]] the [[source code]], to remove errors or improve performance. Because Lua is a "semi-compiled" interpreted language, it does not prescreen for all common syntax errors, nor detect misspelled variables, which are only found at runtime when seeing the "{{Script error}}" message. Also, unlike [[wp:template]]s which can be run interactively by edit-preview, the Lua script must be tested by show-preview (or run preview) of another page which uses a template which <code>#invoke</code>'s the Lua module being edited. In general, make one small change at a time, do a run show-preview, and save after a few good changes to have a version to restore, in case terrible errors occur after numerous later changes. Note: the saving of a working copy can be done to an offline text file, rather than saving each version into the module revisions.
 
Other pre-compiled, or semi-compiled, languages can pinpoint the line number, or code phrase, where a syntax error, or undefined variable name, has been detected; however, that processing can require extra time to perform. Instead, it might be possible to use a [[lexical analysis]] tool, specifically for Lua script, to better detect and pinpoint misspelled variables or logic errors in Lua source code.
Line 40:
! Request !! Returned value
|-
| {{colorcode|blue2=lua|mw.log(p.Hello)}}|| {{samp|"Hello"}}
|-
| {{colorcode|blue2=lua|mw.log(p.calc(10/2))}} || {{samp|5}}
|-
| {{colorcode|blue2=lua|mw.log(p.sum_mult(3))}} || {{samp|6&nbsp;&nbsp;&nbsp;9}}
|-
| {{colorcode|blue2=lua|mw.log(p.mtable(4))}} || {{samp|table}}
|-
| {{colorcode|blue2=lua|mw.logObject(p.mtable(4))}} || {{sxhl|2=lua|
table#1 {
:4,
:5,
}
}}
|-
| {{bluecode|2=lua|{{=}}p.sum_mult(10)}} || {{samp|20 100}}
|-
| {{bluecode|2=lua|{{=}}p.Hello .. ', World!'}} || {{samp|Hello, World!}}
|}
 
Line 84 ⟶ 85:
 
=== Small caveat: The case of the wrong case ===
Wikipedia has a small perversion regarding page name casing: it will convert the first character of a page name to uppercase, regardless of the way it was entered. To compensate, it also converts the first letter of a template (when transcluded) and module (when invoked) to uppercase. however, this does not extend to the modules stored in User: namespace viewed through the special [[Template sandbox]], because in the sandbox "the first letter" is not the first letter any more. Here is an actual example: for real modules, let's say [[Module:String]], mediawiki software will allow you to use <nowiki>{{#invoke:mlx|String|replace|Hara I am|a|e}}</nowiki> to return "Here I em" (try it). It will also allow you to use <nowiki>{{#invoke:mlx|string|replace|Hara I am|a|e}}</nowiki> to get the same result:
{|class="wikitable"
|-
! wikitext !! result
|-
|<nowiki>{{#invoke:mlx|String | replace | Hara I am |a|e}}</nowiki>||{{#invoke:String | replace | Hara I am |a|e}}
|-
|<nowiki>{{#invoke:mlx|string | replace | Hara I am |a|e}}</nowiki>||{{#invoke:string | replace | Hara I am |a|e}}
|}
 
Line 114 ⟶ 115:
 
=== IDE ===
Running your code through an IDE is helpful for its text highlighting features, which can help you quickly spot syntax errors. Any IDE with the ability to install a Lua text highlighting plugin should work. This could be as simple as [[Notepad++]] (with Lua selected from the language menu) or as full-featured as [[Visual Studio Code]].
 
=== Step debugger ===
Step debugging is a powerful tool that allows you to step through code one line at a time, and hover over variables to see their contents at the time of execution. IDEs that easily support Lua step debugging include [[ZeroBrane Studio]], and [[IntelliJ IDEA]] with itsvarious debugger plugins ([https://plugins.jetbrains.com/plugin/9768-emmylua Emmy Lua] pluginor [https://github.com/Benjamin-Dobell/IntelliJ-Luanalysis Lunalysis]).
 
=== RegEx ===
{{See also|mw:Extension:Scribunto/Lua reference manual#Patterns}}
Lua uses a unique flavor of [[Regular expression|regular expressions]] that is not supported by top RegEx tools. One tool that can be used for checking Lua RegEx includesis [https://gitspartv.github.io/lua-patterns/ GitSparTV's Lua patternsPatterns].
 
== Support ==
Line 126 ⟶ 128:
 
== See also ==
* [[Wikipedia:Lua]]
* [[wp:Lua in Wikipedia]] - essay about using [[Lua script]] in Wikipedia
* [[Template:Wikipedia help pages]] - large navbox linking to other topics
 
Line 132 ⟶ 134:
[[Category:Wikipedia how-to]]
[[Category:Wikipedia text help]]
[[Category:WikipediaModule moduleshelp|Debugging]]