From 82c6b8257a5f465aa257160930572c0ef257e5a8 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 13 May 2011 16:42:43 +0200 Subject: [PATCH] Now it is possible to return multi bulks of multi bulks from Lua, just returning tables of tables. The new conversion of the Null bulk and multi bulk type is the Lua false type. --- src/scripting.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/scripting.c b/src/scripting.c index 4b7a89c4..21b3214d 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -68,7 +68,7 @@ char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) { string2ll(reply+1,p-reply-1,&bulklen); if (bulklen == -1) { - lua_pushnil(lua); + lua_pushboolean(lua,0); return p+2; } else { lua_pushlstring(lua,p+2,bulklen); @@ -104,7 +104,7 @@ char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) { string2ll(reply+1,p-reply-1,&mbulklen); p += 2; if (mbulklen == -1) { - lua_pushnil(lua); + lua_pushboolean(lua,0); return p; } lua_newtable(lua); @@ -248,17 +248,17 @@ void hashScript(char *digest, char *script, size_t len) { } void luaReplyToRedisReply(redisClient *c, lua_State *lua) { - int t = lua_type(lua,1); + int t = lua_type(lua,-1); switch(t) { case LUA_TSTRING: - addReplyBulkCBuffer(c,(char*)lua_tostring(lua,1),lua_strlen(lua,1)); + addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1)); break; case LUA_TBOOLEAN: - addReply(c,lua_toboolean(lua,1) ? shared.cone : shared.czero); + addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk); break; case LUA_TNUMBER: - addReplyLongLong(c,(long long)lua_tonumber(lua,1)); + addReplyLongLong(c,(long long)lua_tonumber(lua,-1)); break; case LUA_TTABLE: /* We need to check if it is an array, an error, or a status reply. @@ -294,17 +294,9 @@ void luaReplyToRedisReply(redisClient *c, lua_State *lua) { if (t == LUA_TNIL) { lua_pop(lua,1); break; - } else if (t == LUA_TSTRING) { - size_t len; - char *s = (char*) lua_tolstring(lua,-1,&len); - - addReplyBulkCBuffer(c,s,len); - mbulklen++; - } else if (t == LUA_TNUMBER) { - addReplyLongLong(c,(long long)lua_tonumber(lua,-1)); - mbulklen++; } - lua_pop(lua,1); + luaReplyToRedisReply(c, lua); + mbulklen++; } setDeferredMultiBulkLength(c,replylen,mbulklen); } -- 2.45.2