1
0
mirror of https://github.com/fluencelabs/redis synced 2025-03-31 14:51:04 +00:00

Fixed arity detection of Redis command executed from Lua script. Error reporting from Lua fixed. More work on the Redis reply to lua conversion code.

This commit is contained in:
antirez 2011-05-01 15:26:47 +02:00
parent 532e0f5ded
commit 3791000f15
2 changed files with 54 additions and 3 deletions

4
src/SCRIPTING.txt Normal file

@ -0,0 +1,4 @@
TODO:
redis('get',1) => crash
Fix multi bulk replies, redis('lrange', ...)

@ -8,6 +8,8 @@
char *redisProtocolToLuaType_Int(lua_State *lua, char *reply); char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply); char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
char *redisProtocolToLuaType_Status(lua_State *lua, char *reply); char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
/* Take a Redis reply in the Redis protocol format and convert it into a /* Take a Redis reply in the Redis protocol format and convert it into a
* Lua type. Thanks to this function, and the introduction of not connected * Lua type. Thanks to this function, and the introduction of not connected
@ -41,6 +43,12 @@ char *redisProtocolToLuaType(lua_State *lua, char* reply) {
case '+': case '+':
p = redisProtocolToLuaType_Status(lua,reply); p = redisProtocolToLuaType_Status(lua,reply);
break; break;
case '-':
p = redisProtocolToLuaType_Error(lua,reply);
break;
case '*':
p = redisProtocolToLuaType_MultiBulk(lua,reply);
break;
} }
return p; return p;
} }
@ -75,6 +83,38 @@ char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
return p+2; return p+2;
} }
char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
char *p = strchr(reply+1,'\r');
lua_newtable(lua);
lua_pushstring(lua,"err");
lua_pushlstring(lua,reply+1,p-reply-1);
lua_settable(lua,-3);
return p+2;
}
char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
char *p = strchr(reply+1,'\r');
long long mbulklen;
int j = 0;
printf("--%s-- (%d)\n", reply,(int)(p-reply-1));
string2ll(reply+1,p-reply-1,&mbulklen);
p += 2;
if (mbulklen == -1) {
lua_pushnil(lua);
return p;
}
printf("BL: %lld\n", mbulklen);
lua_newtable(lua);
for (j = 0; j < mbulklen; j++) {
lua_pushnumber(lua,j);
p = redisProtocolToLuaType(lua,p);
lua_settable(lua,-3);
}
return p;
}
int luaRedisCommand(lua_State *lua) { int luaRedisCommand(lua_State *lua) {
int j, argc = lua_gettop(lua); int j, argc = lua_gettop(lua);
struct redisCommand *cmd; struct redisCommand *cmd;
@ -90,12 +130,18 @@ int luaRedisCommand(lua_State *lua) {
/* Command lookup */ /* Command lookup */
cmd = lookupCommand(argv[0]->ptr); cmd = lookupCommand(argv[0]->ptr);
if (!cmd) { if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
(argc < -cmd->arity)))
{
for (j = 0; j < argc; j++) decrRefCount(argv[j]); for (j = 0; j < argc; j++) decrRefCount(argv[j]);
zfree(argv); zfree(argv);
lua_newtable(lua); lua_newtable(lua);
lua_pushstring(lua,"err"); lua_pushstring(lua,"err");
lua_pushstring(lua,"Unknown Redis command called from Lua script"); if (cmd)
lua_pushstring(lua,
"Wrong number of args calling Redis command From Lua script");
else
lua_pushstring(lua,"Unknown Redis command called from Lua script");
lua_settable(lua,-3); lua_settable(lua,-3);
return 1; return 1;
} }
@ -187,7 +233,8 @@ void luaReplyToRedisReply(redisClient *c, lua_State *lua) {
lua_gettable(lua,-2); lua_gettable(lua,-2);
t = lua_type(lua,-1); t = lua_type(lua,-1);
if (t == LUA_TSTRING) { if (t == LUA_TSTRING) {
addReplyError(c,(char*)lua_tostring(lua,-1)); addReplySds(c,sdscatprintf(sdsempty(),
"-%s\r\n",(char*)lua_tostring(lua,-1)));
lua_pop(lua,1); lua_pop(lua,1);
} else { } else {
void *replylen = addDeferredMultiBulkLength(c); void *replylen = addDeferredMultiBulkLength(c);