Skip to content

Commit 5b5b27f

Browse files
committedDec 11, 2023
iolib: fix different behavior in read function
$ 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
1 parent da9f439 commit 5b5b27f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎_glua-tests/issues.lua

+10
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,16 @@ function test()
470470
end
471471
test()
472472

473+
-- issue #455
474+
function test()
475+
local path = "."
476+
local fd, _, code = io.open(path, "r")
477+
assert(fd ~= nil)
478+
local _, _, ecode = fd:read(1)
479+
assert(ecode == 1)
480+
end
481+
test()
482+
473483
-- issue #459
474484
function test()
475485
local a, b = io.popen("ls", nil)

‎iolib.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,10 @@ normalreturn:
404404
return L.GetTop() - top
405405

406406
errreturn:
407-
L.RaiseError(err.Error())
408-
//L.Push(LNil)
409-
//L.Push(LString(err.Error()))
410-
return 2
407+
L.Push(LNil)
408+
L.Push(LString(err.Error()))
409+
L.Push(LNumber(1)) // C-Lua compatibility: Original Lua pushes errno to the stack
410+
return 3
411411
}
412412

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

0 commit comments

Comments
 (0)