Skip to content

Commit

Permalink
popen: support nil in second argument
Browse files Browse the repository at this point in the history
In the C implementation of the Lua interpreter,
in the io.popen function the second argument can be nil:

Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio:

> x,y = io.popen("ls", nil)
> assert(x)
> assert(y == nil)
>

Gopher lua throws an exception:
bad argument #2 to popen (string expected, got nil)

Closes #459
  • Loading branch information
0x501D committed Oct 10, 2023
1 parent 2b3f02d commit 1543e9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,14 @@ function test()
assert(c == 1)
assert(type(c) == "number")
end

-- issue #459
function test()
local a, b = io.popen("ls", nil)
assert(a)
assert(b == nil)
local a, b = io.popen("ls", nil, nil)
assert(a)
assert(b == nil)
end
test()
3 changes: 3 additions & 0 deletions iolib.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ func ioPopen(L *LState) int {
cmd := L.CheckString(1)
if L.GetTop() == 1 {
L.Push(LString("r"))
} else if L.GetTop() > 1 && (L.Get(2)).Type() == LTNil {
L.SetTop(1)
L.Push(LString("r"))
}
var file *LUserData
var err error
Expand Down

0 comments on commit 1543e9d

Please sign in to comment.