Hi all. I came across a strange issue with regards to Lua 5.1 and Lua 5.2 this weekend. The defect was caused by a change in behavior for "tonumber" between 5.1 and 5.2. I detail more below for anyone interested.
With that in mind, I wanted to ask a few questions to the group:
* Will Scribunto support Lua 5.2 in the future? According to this comment, it may: https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/Scri...
* If so, what is the best approach to ensure forward compatibility of Module code for 5.2? Note that code that is valid for 5.1 may "break" in 5.2. (Again, see below for details) ** Should there be a reference page on MediaWiki or in the enwiki Wikipedia Namespace that details these breaking issues for other Module writers? ** Or should 5.1 vs 5.2 issues be corrected on a case-by-case basis for each Module? ** Or should they be left as is, as any future-proofing may be unnecessary and / or premature?
* Lastly, is there a general purpose mailing list for Scribunto issues? I've had a few technical questions in the past that have been painful to sort out on my own. I'd like to think that there might be other Module writers who would also be interested in a mailing list as well.
Detail on the "tonumber" issue follows below.
----
== In brief == * For a call of "tonumber(213.5616, 10)" ** 5.1 will return back 213.5616 ** 5.2 will return back NIL
== In detail == * Template:Weather_box uses Module:WeatherBox which eventually calls Module:BaseConvert * Module:WeatherBox often calls Module:BaseConvert with decimal values. For example, here is a sample call: {{#invoke:BaseConvert|convert|n=213.5615|base = 16|width = 2|precision = 0}} * Module:BaseConvert has the following lines from = from or 10 local num = tonumber(n, from) * Note that if no {{{from}}} is passed, the default is 10. In general, most callers do not specify a {{{from}}} (as seen above) * Lua 5.1 has code that specifically says if tonumber is called with no base or a base of 10, convert it as a number // http://www.lua.org/source/5.1/lbaselib.c.html static int luaB_tonumber (lua_State *L) { int base = luaL_optint(L, 2, 10); if (base == 10) { /* standard conversion */ * In contrast, Lua 5.2 has code that only cares if a base is not specified. // http://www.lua.org/source/5.2/lbaselib.c.html static int luaB_tonumber (lua_State *L) { if (lua_isnoneornil(L, 2)) { /* standard conversion */ ** If a base is specified, Lua 5.2 will not do the standard conversion, and instead try to parse the number ** This parse code only accepts alpha-numeric characters. The dot is considered invalid, and any decimal number is converted to NIL * As a result, for a call of "tonumber(213.5616, 10)" ** 5.1 will return back 213.5616 ** 5.2 will return back NIL * The NIL removes the background (and sometimes text colors) in weather boxes.
For those curious, I discovered this in XOWA which uses Luaj -- a Java port of Lua 5.2. I had to backport the 5.1 behavior to get the Weather box to show the right colors again.
A proposed fix that would be forward compatible with 5.2 would be as follows:
local num = 0;
-- {{{from}}} is specified, and {{{from}}} is not base 10 if (from && from <> 10) then -- call overload that will always do parsing num = tonumber(n, from ) -- {{{from}}} is not specified or {{{from}}} is base 10 else -- call overload that will always convert to number num = tonumber(n)
from = from or 10
Finally, I've come across a related issue with the varargs operator ("..."). This operator is valid in 5.1, but not in 5.2. I've seen some Modules use this varargs operator, that will presumably just break in 5.2. See https://en.wikipedia.org/wiki/Module:Horizontal_timeline and "function getNotNilValue(...)"
On Mon, Jul 14, 2014 at 12:22 AM, gnosygnu gnosygnu@gmail.com wrote:
- Will Scribunto support Lua 5.2 in the future? According to this comment,
it may:
https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/Scri...
It may someday, but there's no hurry to change over. And changing over will involve some rewriting of the sandboxing which will need security auditing.
See https://gerrit.wikimedia.org/r/#/c/139479/ for example.
- If so, what is the best approach to ensure forward compatibility of
Module code for 5.2? Note that code that is valid for 5.1 may "break" in 5.2. (Again, see below for details) ** Should there be a reference page on MediaWiki or in the enwiki Wikipedia Namespace that details these breaking issues for other Module writers?
Feel free to create one.
** Or should 5.1 vs 5.2 issues be corrected on a case-by-case basis for each Module?
They'd have to be anyway.
** Or should they be left as is, as any future-proofing may be unnecessary and / or premature?
Possibly.
- Lastly, is there a general purpose mailing list for Scribunto issues?
I've had a few technical questions in the past that have been painful to sort out on my own. I'd like to think that there might be other Module writers who would also be interested in a mailing list as well.
No. There are various talk pages on enwiki and other wikis that are used for this purpose, although I don't remember the specifics offhand and am too lazy to search right now. ;)
- In contrast, Lua 5.2 has code that only cares if a base is not specified. // http://www.lua.org/source/5.2/lbaselib.c.html static int luaB_tonumber (lua_State *L) { if (lua_isnoneornil(L, 2)) { /* standard conversion */
** If a base is specified, Lua 5.2 will not do the standard conversion, and instead try to parse the number ** This parse code only accepts alpha-numeric characters. The dot is considered invalid, and any decimal number is converted to NIL
Possibly sensible since it removes a weird special case of base-10 being different from all other bases.
Finally, I've come across a related issue with the varargs operator ("..."). This operator is valid in 5.1, but not in 5.2. I've seen some Modules use this varargs operator, that will presumably just break in 5.2. See https://en.wikipedia.org/wiki/Module:Horizontal_timeline and "function getNotNilValue(...)"
Note the 'arg' parameter is already deprecated in 5.1, so people should already be avoiding it. See http://www.lua.org/manual/5.1/manual.html#7.1
I reply inline below, but in brief, my general questions have been answered. Thanks for the comprehensive reply.
On Mon, Jul 14, 2014 at 10:57 AM, Brad Jorsch (Anomie) < bjorsch@wikimedia.org> wrote:
On Mon, Jul 14, 2014 at 12:22 AM, gnosygnu gnosygnu@gmail.com wrote:
- Will Scribunto support Lua 5.2 in the future? According to this
comment,
it may:
https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/Scri...
It may someday, but there's no hurry to change over. And changing over will involve some rewriting of the sandboxing which will need security auditing.
See https://gerrit.wikimedia.org/r/#/c/139479/ for example.
Thanks for the link. I didn't know about the abandoned patch for 5.2, and Tim's replies and links to the lua mailing thread made for interesting reading.
Whenever Wikimedia decides to go to 5.2, the "intermediate runtime between 5.1 and 5.2" may be the best way. For what it's worth, I did the same in XOWA for Scribunto, and it wasn't that difficult. I used the Luaj project ( https://sourceforge.net/projects/luaj/files/luaj-3.0/3.0-beta2/), which is a pretty faithful port of Lua 5.2. I then added a handful of patches to be backward compatible with 5.1. The resulting version handles the Module code for all Mainspace/Wikipedia articles in 20+ wikis (enwiki; ruwiki; dewiki; enwiktionary; ruwikisource; etc). However, it didn't pick up more subtle issues like the "tonumber" one (which returns a different value and doesn't throw an error).
- If so, what is the best approach to ensure forward compatibility of
Module code for 5.2? Note that code that is valid for 5.1 may "break" in 5.2. (Again, see below for details) ** Should there be a reference page on MediaWiki or in the enwiki
Wikipedia
Namespace that details these breaking issues for other Module writers?
Feel free to create one.
Okay. I created https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_5.2_changes and linked to it from https://www.mediawiki.org/wiki/Extension:Scribunto#Other_pages. Feel free to (re)move it as necessary.
** Or should 5.1 vs 5.2 issues be corrected on a case-by-case basis for each Module?
They'd have to be anyway.
Okay. I'll comment later on each of the cited Module's talk pages, and recommend changes as per this thread.
** Or should they be left as is, as any future-proofing may be
unnecessary
and / or premature?
Possibly.
Okay.
- Lastly, is there a general purpose mailing list for Scribunto issues?
I've had a few technical questions in the past that have been painful to sort out on my own. I'd like to think that there might be other Module writers who would also be interested in a mailing list as well.
No. There are various talk pages on enwiki and other wikis that are used for this purpose, although I don't remember the specifics offhand and am too lazy to search right now. ;)
Understood. For the record, I wasn't able to find a central hub / discussion page for Scribunto issues, though I'm probably looking in the wrong places.
- In contrast, Lua 5.2 has code that only cares if a base is not
specified.
// http://www.lua.org/source/5.2/lbaselib.c.html static int luaB_tonumber (lua_State *L) { if (lua_isnoneornil(L, 2)) { /* standard conversion */
** If a base is specified, Lua 5.2 will not do the standard conversion,
and
instead try to parse the number ** This parse code only accepts alpha-numeric characters. The dot is considered invalid, and any decimal number is converted to NIL
Possibly sensible since it removes a weird special case of base-10 being different from all other bases.
Agreed. The earlier version induced a few head-scratches, but I kind of wish the later version didn't just drop it altogether.
Finally, I've come across a related issue with the varargs operator ("..."). This operator is valid in 5.1, but not in 5.2. I've seen some Modules use this varargs operator, that will presumably just break in
5.2.
See https://en.wikipedia.org/wiki/Module:Horizontal_timeline and
"function
getNotNilValue(...)"
Note the 'arg' parameter is already deprecated in 5.1, so people should already be avoiding it. See http://www.lua.org/manual/5.1/manual.html#7.1
Agreed that it shouldn't be used. Unfortunately, there's at least one other user who didn't realize it was deprecated. That's why I recommended publicizing known 5.2 migration issues amongst Module editors. :)
-- Brad Jorsch (Anomie) Software Engineer Wikimedia Foundation _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
wikitech-l@lists.wikimedia.org