This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gamax92's profile picture

LuaJ Bugs

Started by gamax92, 11 July 2014 - 02:59 PM
gamax92 #1
Posted 11 July 2014 - 04:59 PM
 * The string.sub+find combo
Nutshell: Strings from string.sub won t work properly in string.find, giving
    wrong offsets.
Detail: the indexOf in LuaString does not give numbers minus the string offset
    (which is usually zero). string.find uses said function, where strings with
    a different offset can be generated by string.sub (it reuses the string
    bytes)
By putting any special pattern character (and parameter 4 is not true), bug
    goes away.

 * string.format
Takes "%" as a valid format (no format letter passed afterwards)
Does not do a separate check to see if a value was specified for current code
    (shown by above bug).
Codes e,E,f,g,G are not implemented and do not handle flags
Codes s,c do nothing involving flags like precision and width
For o,u,x,X: the flags (space) and + are not ignored. the flag # is not
    implemented
You can never specify all flags at once, the internal counter is always one off

 * string.rep
Breaks if the amount to duplicate is a negative number, should instead return
    an empty string

 * tostring
doubles are checked as long and if its the same, Long.toString is used (result
    is still correct but just formatted differently)
doing tostring on a double will return a rather low precision string version.
LuaDouble is converting the double to a string, by converting it to Float first.

 * tonumber
Numbers are scanned as longs instead of doubles.
The simple character subtraction done in scanlong makes it simple, but also
    results in letters below lowercase "a" being accepted: [\]^_`
`
 * (internal compiler)
Code that relied on lexerror checking if the token was non zero (and adding the
    "near <something>") message will lack this message. lexerror does this
    token check, but then throws an error without the additional info.

I've noticed that the string.rep and string.format bugs are missing from the String (API) page.
Seeing as I cannot edit the wiki, it would be nice if someone adds up this info.

EDIT: Added bugs regarding o,u,x,X on string.format
Added bug about max flags
EDIT: Added %c to the no flags list.
EDIT: Mentioned no flags on e,E,f,g,G</something>
Edited on 11 July 2014 - 06:12 PM
LBPHacker #2
Posted 11 July 2014 - 05:19 PM
I'm about the edit that wiki page. But first
* string.format

Does not do a seperate check to see if a value was specified for current code
(shown by above bug).
What do you exactly mean by that?
gamax92 #3
Posted 11 July 2014 - 06:36 PM
so in the lua source code theres a check to see if a value is there for the current flag.
string.format("%s%d","hello")
Should give a "no value" error

Oh and I found more formatting bugs, will put an "EDIT" on the first post once they've been added.

EDIT: Also %c does nothing regarding flags
Edited on 11 July 2014 - 04:48 PM
Engineer #4
Posted 15 June 2015 - 10:22 PM
I really hate myself for bumping this thread, but this thread is really appropriate since it is a LuaJ bug.

I am currently creating a program which has the following snippet:

local sNumber = ""
local sChar = sSequence:sub (nIndex, nIndex)
while tonumber ( sChar ) do
   sNumber = sNumber .. sChar
   nIndex = nIndex + 1
   sChar = sSequence:sub (nIndex, nIndex)
   if nIndex == sSequence:len() then
     break
   end
end
local number = tonumber( sNumber )
As you might have noticed, I am parsing a big number until a non-digit has been hit and continue with that code. The problem what I was hitting is that sSequence contained dashes, which at first didn't should not be a problem in this code. However, "-" gets recognized as a number, more precisely as 0.

After getting odd errors in my code after this, I wanted to investigate and ran the following script on pure lua (v 5.1.4) and on CC Lua (currently written in CC-Lua to get to the point):

local file = fs.open ("log.log", "w")
for i = 1, 255 do
	local sChar = string.char ( i )
	file.writeLine( string.format( "%s -> %s", sChar, tostring(tonumber(sChar)) ) )
end
file.close()
The important output of pure Lua
Spoiler

+ -> nil
, -> nil
- -> nil
. -> nil
/ -> nil
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
6 -> 6
7 -> 7
8 -> 8
9 -> 9
: -> nil
; -> nil
The important output of CC Lua
Spoiler

+ -> nil
, -> nil
- -> 0
. -> nil
/ -> nil
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
6 -> 6
7 -> 7
8 -> 8
9 -> 9
: -> nil
; -> nil
As you can see it differs, which can be fatal in a case like where I use tonumber. I hope to see that this is fixed in a next version to prevent such weird errors. For the record, this has been done with CC 1.73 which is the latest version as of writing this post to my knowledge.

Also, this error only happens when the dash is the only character in the string, when it is combined with other digits tonumber will return nil!

Here is a temporary tonumber fix:

local backup_tonumber = tonumber
function tonumber ( s )
   local result = backup_tonumber ( s )
   if result == 0 and s == "-" then
     return
   end
   return result
end
Edited on 15 June 2015 - 08:23 PM