From 8e02cded171d8de766810d297ea2c87a2f694da5 Mon Sep 17 00:00:00 2001 From: yabu76 Date: Sat, 15 Nov 2025 22:46:54 +0900 Subject: [PATCH] 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. --- src/script.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/script.c b/src/script.c index 13d20da..e5b2f4f 100644 --- a/src/script.c +++ b/src/script.c @@ -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)