Commit e50b9a07 authored by antirez's avatar antirez
Browse files

Scripting: handle trailing comments.

This fix, provided by Paul Kulchenko (@pkulchenko), allows the Lua
scripting engine to evaluate statements with a trailing comment like the
following one:

    EVAL "print() --comment" 0

Lua can't parse the above if the string does not end with a newline, so
now a final newline is always added automatically. This does not change
the SHA1 of scripts since the SHA1 is computed on the body we pass to
EVAL, without the other code we add to register the function.

Close #2951.
parent e9abc944
...@@ -895,7 +895,7 @@ int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body ...@@ -895,7 +895,7 @@ int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body
funcdef = sdscatlen(funcdef,funcname,42); funcdef = sdscatlen(funcdef,funcname,42);
funcdef = sdscatlen(funcdef,"() ",3); funcdef = sdscatlen(funcdef,"() ",3);
funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr)); funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr));
funcdef = sdscatlen(funcdef," end",4); funcdef = sdscatlen(funcdef,"\nend",4);
if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) { if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) {
addReplyErrorFormat(c,"Error compiling script (new function): %s\n", addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
......
...@@ -272,6 +272,10 @@ start_server {tags {"scripting"}} { ...@@ -272,6 +272,10 @@ start_server {tags {"scripting"}} {
} 0 } 0
} {} } {}
test {EVAL - Able to parse trailing comments} {
r eval {return 'hello' --trailing comment} 0
} {hello}
test {SCRIPTING FLUSH - is able to clear the scripts cache?} { test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
r set mykey myval r set mykey myval
set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey] set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment