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. */
| 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. */ |
| 357 | void 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 | } |
no test coverage detected