From 7156f43c040414494829a8dcd4a7be4c26d241d8 Mon Sep 17 00:00:00 2001 From: antirez Date: Sun, 1 May 2011 03:12:53 +0200 Subject: [PATCH] Correctly glue the reply buffer. For now returned as it is to Lua, but will be converted into Lua native type later. --- src/networking.c | 1 + src/redis.h | 1 + src/scripting.c | 10 +++++++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/networking.c b/src/networking.c index b26c2059..5f0d1b25 100644 --- a/src/networking.c +++ b/src/networking.c @@ -65,6 +65,7 @@ redisClient *createClient(int fd) { /* Set the event loop to listen for write events on the client's socket. * Typically gets called every time a reply is built. */ int _installWriteEvent(redisClient *c) { + if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK; if (c->fd <= 0) return REDIS_ERR; if (c->bufpos == 0 && listLength(c->reply) == 0 && (c->replstate == REDIS_REPL_NONE || diff --git a/src/redis.h b/src/redis.h index 0071c0aa..d9609991 100644 --- a/src/redis.h +++ b/src/redis.h @@ -148,6 +148,7 @@ #define REDIS_CLOSE_AFTER_REPLY 128 /* Close after writing entire reply. */ #define REDIS_UNBLOCKED 256 /* This client was unblocked and is stored in server.unblocked_clients */ +#define REDIS_LUA_CLIENT 512 /* This is a non connected client used by Lua */ /* Client request types */ #define REDIS_REQ_INLINE 1 diff --git a/src/scripting.c b/src/scripting.c index 9b957c92..bde532ca 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -14,7 +14,7 @@ int luaRedisCommand(lua_State *lua) { argv = zmalloc(sizeof(robj*)*argc); for (j = 0; j < argc; j++) - argv[j] = createStringObject(lua_tostring(lua,j+1),lua_strlen(lua,j+1)); + argv[j] = createStringObject((char*)lua_tostring(lua,j+1),lua_strlen(lua,j+1)); /* Command lookup */ cmd = lookupCommand(argv[0]->ptr); @@ -34,7 +34,7 @@ int luaRedisCommand(lua_State *lua) { * output buffers. */ reply = sdsempty(); if (c->bufpos) { - reply = sdscatlen(reply,c->bufpos,c->buf); + reply = sdscatlen(reply,c->buf,c->bufpos); c->bufpos = 0; } while(listLength(c->reply)) { @@ -43,7 +43,8 @@ int luaRedisCommand(lua_State *lua) { sdscatlen(reply,o->ptr,sdslen(o->ptr)); listDelNode(c->reply,listFirst(c->reply)); } - lua_pushnumber(lua,1); + lua_pushlstring(lua,reply,sdslen(reply)); + sdsfree(reply); /* Clean up. Command code may have changed argv/argc so we use the * argv/argc of the client instead of the local variables. */ @@ -65,6 +66,7 @@ void scriptingInit(void) { /* Create the (non connected) client that we use to execute Redis commands * inside the Lua interpreter */ server.lua_client = createClient(-1); + server.lua_client->flags |= REDIS_LUA_CLIENT; server.lua = lua; } @@ -134,8 +136,10 @@ void evalCommand(redisClient *c) { addReplyErrorFormat(c,"Error compiling script (new function): %s\n", lua_tostring(lua,-1)); lua_pop(lua,1); + sdsfree(funcdef); return; } + sdsfree(funcdef); if (lua_pcall(lua,0,0,0)) { addReplyErrorFormat(c,"Error running script (new function): %s\n", lua_tostring(lua,-1)); -- 2.45.2