From 9a0781e52fd0f9eac8ac75c18d7f58936fb6c981 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 20 May 2024 15:49:28 +0200 Subject: linda:limit uses nil instead of -1 to unblock --- src/keeper.cpp | 10 +++++----- src/linda.cpp | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/keeper.cpp b/src/keeper.cpp index 2f8817c..9bde2d5 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -359,14 +359,14 @@ int keepercall_receive_batched(lua_State* L_) // ################################################################################################# -// in: linda_ud key n +// in: linda_ud key [n|nil] // out: true or nil int keepercall_limit(lua_State* L_) { - int const _limit{ static_cast(lua_tointeger(L_, 3)) }; - push_table(L_, 1); // L_: ud key n fifos - lua_replace(L_, 1); // L_: fifos key n - lua_pop(L_, 1); // L_: fifos key + int const _limit{ static_cast(luaL_optinteger(L_, 3, -1)) }; // -1 if we read nil because the argument is absent + push_table(L_, 1); // L_: ud key n? fifos + lua_replace(L_, 1); // L_: fifos key n? + lua_settop(L_, 2); // L_: fifos key lua_pushvalue(L_, -1); // L_: fifos key key lua_rawget(L_, -3); // L_: fifos key fifo|nil keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; diff --git a/src/linda.cpp b/src/linda.cpp index 76607a4..48da1cf 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -354,21 +354,22 @@ LUAG_FUNC(linda_get) // ################################################################################################# /* - * [true] = linda_limit( linda_ud, key_num|str|bool|lightuserdata, int) + * [true] = linda_limit( linda_ud, key_num|str|bool|lightuserdata, [int]) * * Set limit to 1 Linda keys. * Optionally wake threads waiting to write on the linda, in case the limit enables them to do so - * Limit can be 0 to completely block everything + * Limit can be 0 to completely block everything, nil to reset */ LUAG_FUNC(linda_limit) { auto _limit = [](lua_State* L_) { Linda* const _linda{ ToLinda(L_, 1) }; // make sure we got 3 arguments: the linda, a key and a limit - luaL_argcheck(L_, lua_gettop(L_) == 3, 2, "wrong number of arguments"); + int const _nargs{ lua_gettop(L_) }; + luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments"); // make sure we got a numeric limit - lua_Number const _limit{ luaL_checknumber(L_, 3) }; - if (_limit < 1) { + lua_Integer const _limit{ luaL_optinteger(L_, 3, 0) }; + if (_limit < 0) { raise_luaL_argerror(L_, 3, "limit must be >= 0"); } // make sure the key is of a valid type -- cgit v1.2.3-55-g6feb