Content deleted Content added
tidy style for better HTML5 compatibility |
new key for Category:Module help: "Debugging" using HotCat |
||
(30 intermediate revisions by 15 users not shown) | |||
Line 1:
{{WikiProject Lua
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 "
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 8:
== Causes of Script error ==
There can be many reasons why a Lua module triggers the message "
However, some errors are more common than others:
Line 16:
:* '''Invalid function name:''' <code>len = string.long(my_str)</code> - The string-length operator is hash mark '#' (as in: <code>#my_str</code>), or use function '<code>string.len(my_str)</code>' but trying to use unknown function '<code>string.long(my_str)</code>' will successfully pass during the pre-compile of edit-save, yet it will hit "Script error" when that portion of the Lua script is executed.
There are many other causes of "
== Saving fall-back stable versions ==
To avoid facing too many errors at once, the general strategy is to make "one small change" at a time, do a run show-preview, and then save that tested version after a few good changes to have a stable version to restore, in case terrible errors occur after numerous later changes. Note: the saving of each working copy can be done to a set of offline text files, perhaps each with a specific version name (and internal comments), rather than saving each version into the Wikipedia module revisions.
== How to debug==
===Simple review===
To see the value of a variable in a single point of the module:
====To see the value of a variable by modifying the code====
The <code>error()</code> function can be used to show the value of a variable at any point in the module. So to know the value of a variable <code>var</code> at a certain point in the code, add the line <code>error(var)</code> at that point.
In the case that the variable is a table (let's call the variable <code>tab</code>), <code>mw.dumpObject(tab)</code> can be used to show the table. If the variable does not have nested tables, <code>table.concat(tab, ',')</code> can also be used as parameter in the <code>error()</code> function, i.e. <code>error(table.concat(tab, ','))</code>.
==== To see the value of a variable without changing the code ====
To obtain variables and values returned from functions (not local in both cases) the "Debug console" can be used. The "Debug console" appears below in the module page (when it is in edit mode). Then <code>mw.log</code>, <code>mw.logObject</code>, and <code>=</code> can be used. Let's see its usefulness in the next example:
{{#tag:syntaxhighlight|
{{Module:Example of lua debugging}}
|lang=lua}}
Requests to "Debug Console":
{| class="wikitable"
|-
! Request !! Returned value
|-
| {{code|2=lua|mw.log(p.Hello)}}|| {{samp|"Hello"}}
|-
| {{code|2=lua|mw.log(p.calc(10/2))}} || {{samp|5}}
|-
| {{code|2=lua|mw.log(p.sum_mult(3))}} || {{samp|6 9}}
|-
| {{code|2=lua|mw.log(p.mtable(4))}} || {{samp|table}}
|-
| {{code|2=lua|mw.logObject(p.mtable(4))}} || {{sxhl|2=lua|
table#1 {
:4,
:5,
}
}}
|-
| {{code|2=lua|{{=}}p.sum_mult(10)}} || {{samp|20 100}}
|-
| {{code|2=lua|{{=}}p.Hello .. ', World!'}} || {{samp|Hello, World!}}
|}
{{edit|Module:Example of lua debugging|Try it out in Module:Example of lua debugging}}. The Debug console does not store the requests, then they will have to be copied or rewritten again in each module modification.
===Review of the flow and the variable values in several points===
The functions of the [[Module:SimpleDebug]] can be used for cases such as those mentioned above or for more complex cases:
* When variables or the returned value (or values) by a function are local.
* To see if the flow of the program goes through a point (to which you will label).
* To limit the number of values returned from a loop or set conditions to enable the registration of values.
== Debugging older modules ==
In some situations, a Lua module might be an older effort, perhaps modified by various other editors along the way, with multiple unknown problems. A Lua module can contain numerous logic errors, and even misspelled variables, if the module was not properly tested, for all features, when initially developed. The message "
A common, and complex, problem is to try expanding an older module, for newer features, but unaware how some prior, unsolved bugs will only be triggered when the expanded functionality is added. Note how the errors are, often, not in the newly added Lua script, but rather waiting silently in older sections to be triggered (surprise) when the new code activates other parts of the Lua script. A hidden bug can be as simple as an undefined (or misspelled) variable name, which had been processed with a default value, but when used with newly added features, generates completely mysterious results, as though the new code had errors, when actually, the older misspelled variables, in other areas, completely trashed the operation of the new features.
Line 38 ⟶ 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
{|class="wikitable"
|-
! wikitext !! result
|-
|
|-
|
|}
Line 61 ⟶ 108:
The overall effort appears unstable, or unreliable, as if Lua suddenly "gets too tired" and starts complaining by storing "Script error" into the resultant page, where formatted text would have been expected instead. The timeout problem is somewhat rare, and when a formatted article contains "Script error" text inside the page, then perhaps edit the page slightly, to make a small change, to force the replacement of the page with a clean Lua run.
== Exasperating bugs ==
{{Main|Help:Lua for beginners#Exasperating bugs}}
== Off-wiki tools ==
=== 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 various debugger plugins ([https://plugins.jetbrains.com/plugin/9768-emmylua Emmy Lua] or [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 is [https://gitspartv.github.io/lua-patterns/ GitSparTV's Lua Patterns].
== Support ==
Line 66 ⟶ 128:
== See also ==
* [[Wikipedia:Lua]]
* [[Template:
{{Wikipedia technical help|collapsed}}
[[Category:Wikipedia how-to]]
[[Category:Wikipedia text help]]
[[Category:Module help|Debugging]]
|