]>
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(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
->bufpos
,c
->buf
);
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_pushnumber(lua
,1);
48 /* Clean up. Command code may have changed argv/argc so we use the
49 * argv/argc of the client instead of the local variables. */
50 for (j
= 0; j
< c
->argc
; j
++)
51 decrRefCount(c
->argv
[j
]);
57 void scriptingInit(void) {
58 lua_State
*lua
= lua_open();
61 /* Register the 'r' command */
62 lua_pushcfunction(lua
,luaRedisCommand
);
63 lua_setglobal(lua
,"r");
65 /* Create the (non connected) client that we use to execute Redis commands
66 * inside the Lua interpreter */
67 server
.lua_client
= createClient(-1);
72 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
73 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
74 * hexadecimal number, plus 1 byte for null term. */
75 void hashScript(char *digest
, char *script
, size_t len
) {
77 unsigned char hash
[20];
78 char *cset
= "0123456789abcdef";
82 SHA1Update(&ctx
,(unsigned char*)script
,len
);
85 for (j
= 0; j
< 20; j
++) {
86 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
87 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
92 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
93 int t
= lua_type(lua
,1);
97 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
100 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
103 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
106 addReply(c
,shared
.nullbulk
);
111 void evalCommand(redisClient
*c
) {
112 lua_State
*lua
= server
.lua
;
115 /* We obtain the script SHA1, then check if this function is already
116 * defined into the Lua state */
119 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
120 lua_getglobal(lua
, funcname
);
121 if (lua_isnil(lua
,1)) {
122 /* Function not defined... let's define it. */
123 sds funcdef
= sdsempty();
125 lua_pop(lua
,1); /* remove the nil from the stack */
126 funcdef
= sdscat(funcdef
,"function ");
127 funcdef
= sdscatlen(funcdef
,funcname
,42);
128 funcdef
= sdscatlen(funcdef
," ()\n",4);
129 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
130 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
131 printf("Defining:\n%s\n",funcdef
);
133 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
134 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
135 lua_tostring(lua
,-1));
139 if (lua_pcall(lua
,0,0,0)) {
140 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
141 lua_tostring(lua
,-1));
145 lua_getglobal(lua
, funcname
);
148 /* At this point whatever this script was never seen before or if it was
149 * already defined, we can call it. We have zero arguments and expect
150 * a single return value. */
151 if (lua_pcall(lua
,0,1,0)) {
152 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
153 funcname
, lua_tostring(lua
,-1));
157 luaReplyToRedisReply(c
,lua
);