Skip to content

Commit

Permalink
iolib: fix different behavior in read function
Browse files Browse the repository at this point in the history
$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> file = "/tmp"
> fd, _, code = io.open(file, "r")
> _, _, ecode = fd:read(1)
> print(ecode)
21
>

gopher-lua throws an exception:
        read /tmp: is a directory
stack traceback:
        [G]: in function 'read'
        extra/wrapper.lua:17: in function 'exec'
        <string>:1: in main chunk
        [G]: ?

This patch results in behavior similar to the vanilla lua
implementation.

Closes #455
  • Loading branch information
0x501D committed Dec 11, 2023
1 parent da9f439 commit 5b5b27f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ function test()
end
test()

-- issue #455
function test()
local path = "."
local fd, _, code = io.open(path, "r")
assert(fd ~= nil)
local _, _, ecode = fd:read(1)
assert(ecode == 1)
end
test()

-- issue #459
function test()
local a, b = io.popen("ls", nil)
Expand Down
8 changes: 4 additions & 4 deletions iolib.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ normalreturn:
return L.GetTop() - top

errreturn:
L.RaiseError(err.Error())
//L.Push(LNil)
//L.Push(LString(err.Error()))
return 2
L.Push(LNil)
L.Push(LString(err.Error()))
L.Push(LNumber(1)) // C-Lua compatibility: Original Lua pushes errno to the stack
return 3
}

var fileSeekOptions = []string{"set", "cur", "end"}
Expand Down

0 comments on commit 5b5b27f

Please sign in to comment.