Repository: yuin/gopher-lua (go)

Issue:
fread/fclose different behavior
* GopherLua version : 2b3f02d9173010b12bb3ff2c0e758921accb4096
* go version go1.21.1 linux/amd64
* Linux 6.5.4-gentoo-x86_64

code:
```lua
file = "/tmp"                                                                            
fd, _, code = io.open(file, "r")                                                          
_, _, ecode = fd:read(1)                                                                  
print(ecode) 
```

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio:
```sh
$ lua ~/tmp/f.lua
21
```
Gopher-lua:
   ⨯ extra/wrapper.lua:17: read /tmp: is a directory
stack traceback:
	[G]: in function 'read'
	extra/wrapper.lua:17: in function 'exec'
	<string>:1: in main chunk
	[G]: ?

Lua works fine, gopher lua throws an exception.

Pull request description:
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


Patch:
diff --git a/iolib.go b/iolib.go
index 3f5f295..781b18f 100644
--- a/iolib.go
+++ b/iolib.go
@@ -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"}


Test patch:
diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua
index 65a2c7f..6d6343a 100644
--- a/_glua-tests/issues.lua
+++ b/_glua-tests/issues.lua
@@ -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)