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 Sep 28, 2023
1 parent 2b3f02d commit 8deb886
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,12 @@ function test()
assert(c == 1)
assert(type(c) == "number")
end

-- issue #455
function test()
local path = "/tmp/"
local fd, _, code = io.open(path, "r")
assert(fd ~= nil)
local _, _, ecode = fd:read(1)
assert(ecode == 1)
end
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 8deb886

Please sign in to comment.