From 1543e9dd7f91a69a8aaa390a6e54172e73d472de Mon Sep 17 00:00:00 2001 From: Pavel Balaev Date: Mon, 9 Oct 2023 16:29:57 +0300 Subject: [PATCH] popen: support nil in second argument 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 --- _glua-tests/issues.lua | 11 +++++++++++ iolib.go | 3 +++ 2 files changed, 14 insertions(+) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index 514d0b7a..aafb5d7c 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -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() diff --git a/iolib.go b/iolib.go index b59e82b2..3f5f295c 100644 --- a/iolib.go +++ b/iolib.go @@ -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