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

[1.48] os.queueEvent/os.pullEvent fail for 8-bit strings

Started by Eric, 25 February 2013 - 01:52 AM
Eric #1
Posted 25 February 2013 - 02:52 AM
This is the root cause of this "suggestion": http://www.computercraft.info/forums2/index.php?/topic/10695-stop-rednet-being-crap/

Test #1: check that :byte and .char work as expected:

for i = 0, 255 do
    local c = string.char(i)
    local j = c:byte()
    if j ~= i then
        print(("FAIL: Converting %d from int->char->int gave %d"):format(i, j))
    end
end
Passes

Test #2: Check that strings are preserved through events:

for i = 0, 255 do
    local c = string.char(i)
    os.queueEvent('test', c)
    local _, c2 = os.pullEvent('test')
    assert(#c == 1 and #c2 == 1) -- to prove these are single-byte strings

    local j = c2:byte()
    if c ~= c2 then
         print(("FAIL: Passing %d through an event gives %d"):format(i, j))
    end
end
Fails:

FAIL: Passing 128 through an event gives 239
FAIL: Passing 129 through an event gives 239
FAIL: Passing 130 through an event gives 239
...
FAIL: Passing 255 through an event gives 239

To summarize, either queueEvent or pullEvent fail to pass strings containing characters above character 127.

This bug is almost certainly present in 1.5 as well, although I haven't tested it.
Pinkishu #2
Posted 25 February 2013 - 03:17 AM
So thats what caused rednet to screw up, as messages go over events ^^
Eric #3
Posted 25 February 2013 - 03:31 AM
Found another related bug in the file system:

fs.makeDir('d')  -- empty directory
local h = fs.open('d/'..string.char(128), 'w')  -- odd filename
assert(h == nil) -- which doesn't get created - fair enough

-- So there should be no files in the directory, right?
local files = fs.list('d')
assert(#files == 1)  -- except there are

-- Can you guess what the name of the file is?
local file = files[1]
print(files[1]:byte(1, math.huge))
That outputs:

239
190
128
Cloudy #4
Posted 25 February 2013 - 04:47 AM
Way to add two and two together and get five. Again, this is the same as the last thread. All we do is pass the string to and from Java/Lua. NOTHING MORE. Unless you want to delve through LuaJ to find a fix, I consider this unimportant.