diff options
Diffstat (limited to 'src/linda.cpp')
-rw-r--r-- | src/linda.cpp | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/src/linda.cpp b/src/linda.cpp index bbfbd69..40ef6c7 100644 --- a/src/linda.cpp +++ b/src/linda.cpp | |||
@@ -180,7 +180,7 @@ int Linda::ProtectedCall(lua_State* L_, lua_CFunction f_) | |||
180 | // ################################################################################################# | 180 | // ################################################################################################# |
181 | 181 | ||
182 | /* | 182 | /* |
183 | * bool= linda_send( linda_ud, [timeout_secs=-1,] [linda.null,] key_num|str|bool|lightuserdata, ... ) | 183 | * bool= linda:linda_send([timeout_secs=nil,] key_num|str|bool|lightuserdata, ...) |
184 | * | 184 | * |
185 | * Send one or more values to a Linda. If there is a limit, all values must fit. | 185 | * Send one or more values to a Linda. If there is a limit, all values must fit. |
186 | * | 186 | * |
@@ -192,25 +192,21 @@ LUAG_FUNC(linda_send) | |||
192 | { | 192 | { |
193 | auto send = [](lua_State* L_) { | 193 | auto send = [](lua_State* L_) { |
194 | Linda* const linda{ ToLinda<false>(L_, 1) }; | 194 | Linda* const linda{ ToLinda<false>(L_, 1) }; |
195 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | ||
196 | int key_i{ 2 }; // index of first key, if timeout not there | 195 | int key_i{ 2 }; // index of first key, if timeout not there |
197 | 196 | ||
197 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | ||
198 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion | 198 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion |
199 | lua_Duration const duration{ lua_tonumber(L_, 2) }; | 199 | lua_Duration const duration{ lua_tonumber(L_, 2) }; |
200 | if (duration.count() >= 0.0) { | 200 | if (duration.count() >= 0.0) { |
201 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); | 201 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); |
202 | } else { | ||
203 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); | ||
202 | } | 204 | } |
203 | ++key_i; | 205 | ++key_i; |
204 | } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key | 206 | } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key |
205 | ++key_i; | 207 | ++key_i; |
206 | } | 208 | } |
207 | 209 | ||
208 | bool const as_nil_sentinel{ kNilSentinel.equals(L_, key_i) }; // if not nullptr, send() will silently send a single nil if nothing is provided | ||
209 | if (as_nil_sentinel) { | ||
210 | // the real key to send data to is after the kNilSentinel marker | ||
211 | ++key_i; | ||
212 | } | ||
213 | |||
214 | // make sure the key is of a valid type | 210 | // make sure the key is of a valid type |
215 | check_key_types(L_, key_i, key_i); | 211 | check_key_types(L_, key_i, key_i); |
216 | 212 | ||
@@ -218,12 +214,7 @@ LUAG_FUNC(linda_send) | |||
218 | 214 | ||
219 | // make sure there is something to send | 215 | // make sure there is something to send |
220 | if (lua_gettop(L_) == key_i) { | 216 | if (lua_gettop(L_) == key_i) { |
221 | if (as_nil_sentinel) { | 217 | raise_luaL_error(L_, "no data to send"); |
222 | // send a single nil if nothing is provided | ||
223 | kNilSentinel.pushKey(L_); | ||
224 | } else { | ||
225 | raise_luaL_error(L_, "no data to send"); | ||
226 | } | ||
227 | } | 218 | } |
228 | 219 | ||
229 | // convert nils to some special non-nil sentinel in sent values | 220 | // convert nils to some special non-nil sentinel in sent values |
@@ -322,7 +313,7 @@ LUAG_FUNC(linda_send) | |||
322 | 313 | ||
323 | /* | 314 | /* |
324 | * 2 modes of operation | 315 | * 2 modes of operation |
325 | * [val, key]= linda_receive( linda_ud, [timeout_secs_num=-1], key_num|str|bool|lightuserdata [, ...] ) | 316 | * [val, key]= linda_receive( linda_ud, [timeout_secs_num=nil], key_num|str|bool|lightuserdata [, ...] ) |
326 | * Consumes a single value from the Linda, in any key. | 317 | * Consumes a single value from the Linda, in any key. |
327 | * Returns: received value (which is consumed from the slot), and the key which had it | 318 | * Returns: received value (which is consumed from the slot), and the key which had it |
328 | 319 | ||
@@ -335,13 +326,15 @@ LUAG_FUNC(linda_receive) | |||
335 | { | 326 | { |
336 | auto receive = [](lua_State* L_) { | 327 | auto receive = [](lua_State* L_) { |
337 | Linda* const linda{ ToLinda<false>(L_, 1) }; | 328 | Linda* const linda{ ToLinda<false>(L_, 1) }; |
338 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | ||
339 | int key_i{ 2 }; // index of first key, if timeout not there | 329 | int key_i{ 2 }; // index of first key, if timeout not there |
340 | 330 | ||
331 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | ||
341 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion | 332 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion |
342 | lua_Duration const duration{ lua_tonumber(L_, 2) }; | 333 | lua_Duration const duration{ lua_tonumber(L_, 2) }; |
343 | if (duration.count() >= 0.0) { | 334 | if (duration.count() >= 0.0) { |
344 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); | 335 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); |
336 | } else { | ||
337 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); | ||
345 | } | 338 | } |
346 | ++key_i; | 339 | ++key_i; |
347 | } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key | 340 | } else if (lua_isnil(L_, 2)) { // alternate explicit "infinite timeout" by passing nil before the key |