]>
git.saurik.com Git - redis.git/blob - src/scripting.c
8 void scriptingInit(void) {
9 lua_State
*lua
= lua_open();
14 /* Hash the scripit into a SHA1 digest. We use this as Lua function name.
15 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
16 * hexadecimal number, plus 1 byte for null term. */
17 void hashScript(char *digest
, char *script
, size_t len
) {
19 unsigned char hash
[20];
20 char *cset
= "0123456789abcdef";
24 SHA1Update(&ctx
,(unsigned char*)script
,len
);
27 for (j
= 0; j
< 20; j
++) {
28 digest
[j
*2] = cset
[((hash
[j
]&0xF0)>>4)];
29 digest
[j
*2+1] = cset
[(hash
[j
]&0xF)];
34 void luaReplyToRedisReply(redisClient
*c
, lua_State
*lua
) {
35 int t
= lua_type(lua
,1);
39 addReplyBulkCBuffer(c
,(char*)lua_tostring(lua
,1),lua_strlen(lua
,1));
42 addReply(c
,lua_toboolean(lua
,1) ? shared
.cone
: shared
.czero
);
45 addReplyLongLong(c
,(long long)lua_tonumber(lua
,1));
48 addReply(c
,shared
.nullbulk
);
53 void evalCommand(redisClient
*c
) {
54 lua_State
*lua
= server
.lua
;
57 /* We obtain the script SHA1, then check if this function is already
58 * defined into the Lua state */
61 hashScript(funcname
+2,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
62 lua_getglobal(lua
, funcname
);
63 if (lua_isnil(lua
,1)) {
64 /* Function not defined... let's define it. */
65 sds funcdef
= sdsempty();
67 lua_pop(lua
,1); /* remove the nil from the stack */
68 funcdef
= sdscat(funcdef
,"function ");
69 funcdef
= sdscatlen(funcdef
,funcname
,42);
70 funcdef
= sdscatlen(funcdef
," ()\n",4);
71 funcdef
= sdscatlen(funcdef
,c
->argv
[1]->ptr
,sdslen(c
->argv
[1]->ptr
));
72 funcdef
= sdscatlen(funcdef
,"\nend\n",5);
73 printf("Defining:\n%s\n",funcdef
);
75 if (luaL_loadbuffer(lua
,funcdef
,sdslen(funcdef
),"func definition")) {
76 addReplyErrorFormat(c
,"Error compiling script (new function): %s\n",
77 lua_tostring(lua
,-1));
81 if (lua_pcall(lua
,0,0,0)) {
82 addReplyErrorFormat(c
,"Error running script (new function): %s\n",
83 lua_tostring(lua
,-1));
87 lua_getglobal(lua
, funcname
);
90 /* At this point whatever this script was never seen before or if it was
91 * already defined, we can call it. We have zero arguments and expect
92 * a single return value. */
93 if (lua_pcall(lua
,0,1,0)) {
94 addReplyErrorFormat(c
,"Error running script (call to %s): %s\n",
95 funcname
, lua_tostring(lua
,-1));
99 luaReplyToRedisReply(c
,lua
);