Skip to content

Commit

Permalink
let lua call redis commands (no replies yet though!)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebashi committed Jan 3, 2011
1 parent b3bccca commit 5e1a196
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lua.c
Expand Up @@ -6,6 +6,7 @@
#include <lauxlib.h>

lua_State *luaState;
redisClient *fakeClient;

static void *luaZalloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
Expand All @@ -26,12 +27,18 @@ static int luaPanic (lua_State *L) {
}

void initLua(void) {
fakeClient = zmalloc(sizeof(redisClient));
selectDb(fakeClient, 0);

luaState = lua_newstate(luaZalloc, NULL);
if (!luaState) {
redisPanic("Unable to initialize Lua!");
}
lua_atpanic(luaState, &luaPanic);
luaL_openlibs(luaState);

lua_pushcfunction(luaState, luaExecRedisCommand);
lua_setglobal(luaState, "redis");
}

void luaexecCommand(redisClient *c) {
Expand Down Expand Up @@ -72,3 +79,25 @@ void luaGenericExecCommand(redisClient *c, const char *code) {
return;
}
}

int luaExecRedisCommand(lua_State *L) {
int nargs = lua_gettop(L);
const char *cmdname = lua_tostring(L, 1);

struct redisCommand *cmd = lookupCommandByCString(cmdname);
if (nargs != cmd->arity) {
lua_pushstring(L, "incorrect number of arguments");
lua_error(L);
}
fakeClient->argc = nargs;
fakeClient->argv = zmalloc(sizeof(robj) * nargs);
for (int i = 0; i < nargs; i++) {
char *param = lua_tostring(L, i + 1);
fakeClient->argv[i] = createStringObject(param, strlen(param));
}

call(fakeClient, cmd);

return 0;
}

2 changes: 2 additions & 0 deletions src/lua.h
@@ -1,9 +1,11 @@
#ifndef __LUA_H
#define __LUA_H

#include <lua.h>
#include "redis.h"

void initLua(void);
void luaGenericExecCommand(redisClient *c, const char *);
int luaExecRedisCommand(lua_State *L);

#endif

0 comments on commit 5e1a196

Please sign in to comment.