Change the Lua API timeout specification to allow for non-blocking reads.

Change timeout argument of tio.read / readline / expect[s] to be similar
to the timeout specification for poll(2) and LuaSocket.

If timeout is negative value or not provided or nil, it will wait
indefinitely until data is ready to read.
If timeout is 0, it will read data that has arrived with non-blocking.

Prior to this fix, non-blocking reads could not be specified.
This commit is contained in:
yabu76 2025-11-15 22:46:54 +09:00
parent 56c378f5f9
commit 8e02cded17

View file

@ -325,12 +325,7 @@ static int api_twrite(lua_State *L)
static int api_read(lua_State *L)
{
int size = luaL_checkinteger(L, 1);
int timeout = lua_tointeger(L, 2);
if (timeout == 0)
{
timeout = -1; // Wait forever
}
int timeout = luaL_optinteger(L, 2, -1); // ms, negative value means forever.
luaL_Buffer buffer;
luaL_buffinit(L, &buffer);
@ -367,15 +362,10 @@ static int api_read(lua_State *L)
// lua: string = tio.readline(timeout)
static int api_readline(lua_State *L)
{
int timeout = lua_tointeger(L, 1); //ms
int timeout = luaL_optinteger(L, 1, -1); // ms, negative value means forever.
luaL_Buffer b;
char ch;
if (timeout == 0)
{
timeout = -1; // Wait forever
}
luaL_buffinit(L, &b);
luaL_prepbuffer(&b);
while (true)