MCPcopy Create free account
hub / github.com/F-Stack/f-stack / luaReplyToRedisReply

Function luaReplyToRedisReply

app/redis-6.2.6/src/scripting.c:357–495  ·  view source on GitHub ↗

Reply to client 'c' converting the top element in the Lua stack to a * Redis reply. As a side effect the element is consumed from the stack. */

Source from the content-addressed store, hash-verified

355/* Reply to client 'c' converting the top element in the Lua stack to a
356 * Redis reply. As a side effect the element is consumed from the stack. */
357void luaReplyToRedisReply(client *c, lua_State *lua) {
358
359 if (!lua_checkstack(lua, 4)) {
360 /* Increase the Lua stack if needed to make sure there is enough room
361 * to push 4 elements to the stack. On failure, return error.
362         * Notice that we need, in the worst case, 4 elements because returning a map might
363 * require push 4 elements to the Lua stack.*/
364 addReplyErrorFormat(c, "reached lua stack limit");
365 lua_pop(lua,1); /* pop the element from the stack */
366 return;
367 }
368
369 int t = lua_type(lua,-1);
370
371 switch(t) {
372 case LUA_TSTRING:
373 addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));
374 break;
375 case LUA_TBOOLEAN:
376 if (server.lua_client->resp == 2)
377 addReply(c,lua_toboolean(lua,-1) ? shared.cone :
378 shared.null[c->resp]);
379 else
380 addReplyBool(c,lua_toboolean(lua,-1));
381 break;
382 case LUA_TNUMBER:
383 addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
384 break;
385 case LUA_TTABLE:
386 /* We need to check if it is an array, an error, or a status reply.
387 * Error are returned as a single element table with 'err' field.
388 * Status replies are returned as single element table with 'ok'
389 * field. */
390
391 /* Handle error reply. */
392 /* we took care of the stack size on function start */
393 lua_pushstring(lua,"err");
394 lua_gettable(lua,-2);
395 t = lua_type(lua,-1);
396 if (t == LUA_TSTRING) {
397 addReplyErrorFormat(c,"-%s",lua_tostring(lua,-1));
398 lua_pop(lua,2);
399 return;
400 }
401 lua_pop(lua,1); /* Discard field name pushed before. */
402
403 /* Handle status reply. */
404 lua_pushstring(lua,"ok");
405 lua_gettable(lua,-2);
406 t = lua_type(lua,-1);
407 if (t == LUA_TSTRING) {
408 sds ok = sdsnew(lua_tostring(lua,-1));
409 sdsmapchars(ok,"\r\n"," ",2);
410 addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok));
411 sdsfree(ok);
412 lua_pop(lua,2);
413 return;
414 }

Callers 1

evalGenericCommandFunction · 0.85

Calls 15

addReplyErrorFormatFunction · 0.85
addReplyBulkCBufferFunction · 0.85
addReplyFunction · 0.85
addReplyBoolFunction · 0.85
addReplyLongLongFunction · 0.85
lua_tonumberFunction · 0.85
sdsnewFunction · 0.85
sdsmapcharsFunction · 0.85
addReplySdsFunction · 0.85
sdscatprintfFunction · 0.85
sdsemptyFunction · 0.85
sdsfreeFunction · 0.85

Tested by

no test coverage detected