]>
git.saurik.com Git - redis.git/blob - src/scripting.c
8 int luaRedisCommand(lua_State
*lua
) {
9 int j
, argc
= lua_gettop(lua
);
10 struct redisCommand
*cmd
;
12 redisClient
*c
= server
.lua_client
;
15 argv
= zmalloc(sizeof(robj
*)*argc
);
16 for (j
= 0; j
< argc
; j
++)
17 argv
[j
] = createStringObject((char*)lua_tostring(lua
,j
+1),lua_strlen(lua
,j
+1));
20 cmd
= lookupCommand(argv
[0]->ptr
);
24 lua_pushstring(lua
,"Unknown Redis command called from Lua script");
27 /* Run the command in the context of a fake client */
32 /* Convert the result of the Redis command into a suitable Lua type.
33 * The first thing we need is to create a single string from the client
37 reply
= sdscatlen(reply
,c
->buf
,c
->bufpos
);
40 while(listLength(c
->reply
)) {
41 robj
*o
= listNodeValue(listFirst(c
->reply
));
43 sdscatlen(reply
,o
->ptr
,sdslen(o
->ptr
));
44 listDelNode(c
->reply
,listFirst(c
->reply
));
46 lua_pushlstring(lua
,reply
,sdslen(reply
));
49 /* Clean up. Command code may have changed argv/argc so we use the
50 * argv/argc of the client instead of the local variables. */
51 for (j
= 0; j
< c
->argc
; j
++)
52 decrRefCount(c
->argv
[j
]);
58 void scriptingInit(void) {
59 lua_State
*lua
= lua_open();
62 /* Register the 'r' command */
63 lua_pushcfunction(lua
,luaRedisCommand
);
64 lua_setglobal(lua
,"redis");
66 /* Create the (non connected) client that we use to execute Redis commands
67 * inside the Lua interpreter */
68 server
.lua_client
= createClient(-1);
69 server
.lua_client
->flags
|= REDIS_LUA_CLIENT
;
74 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
75 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
76 * hexadecimal number, plus 1 byte for null term. */
77 void hashScript(char *digest
, char *script
, size_t len
) {
79 unsigned char hash
[20];
80 char *cset
= "0123456789abcdef";
84 SHA1Update(&ctx
,(unsigned char*)script
,len
);
87 for (j
= 0; j
< 20; j
++) {
88 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
89 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
94 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
95 int t
= lua_type(lua
,1);
99 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
102 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
105 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
108 addReply(c
,shared
.nullbulk
);
113 void evalCommand(redisClient
*c
) {
114 lua_State
*lua
= server
.lua
;
117 /* We obtain the script SHA1, then check if this function is already
118 * defined into the Lua state */
121 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
122 lua_getglobal(lua
, funcname
);
123 if (lua_isnil(lua
,1)) {
124 /* Function not defined... let's define it. */
125 sds funcdef
= sdsempty();
127 lua_pop(lua
,1); /* remove the nil from the stack */
128 funcdef
= sdscat(funcdef
,"function ");
129 funcdef
= sdscatlen(funcdef
,funcname
,42);
130 funcdef
= sdscatlen(funcdef
," ()\n",4);
131 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
132 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
133 printf("Defining:\n%s\n",funcdef
);
135 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
136 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
137 lua_tostring(lua
,-1));
143 if (lua_pcall(lua
,0,0,0)) {
144 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
145 lua_tostring(lua
,-1));
149 lua_getglobal(lua
, funcname
);
152 /* At this point whatever this script was never seen before or if it was
153 * already defined, we can call it. We have zero arguments and expect
154 * a single return value. */
155 if (lua_pcall(lua
,0,1,0)) {
156 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
157 funcname
, lua_tostring(lua
,-1));
161 luaReplyToRedisReply(c
,lua
);