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