summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-11-11 22:29:56 +0100
committerantirez <antirez@gmail.com>2015-11-17 15:43:21 +0100
commite6eb6eadec7e34df82e2a5bd502fe9c3487d7e61 (patch)
tree413e645f6da55e6c57f992dcf70011f7d641f8d0
parent1f8fdafe65c8c19a94dc886b2cfc3f5de806c728 (diff)
downloadredis-e6eb6eadec7e34df82e2a5bd502fe9c3487d7e61.tar.gz
Lua debugger: try to eval as expression first.
It's handly to just eval "5+5" without the return and see it printed on the screen as result. However prepending "return" does not always result into valid Lua code. So what we do is to exploit a common Lua community trick of trying to compile with return prepended, and if compilation fails then it's not an expression that can be returned, so we try again without prepending "return". Works great apparently.
-rw-r--r--src/scripting.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/scripting.c b/src/scripting.c
index f64bc4e65..b98f94a10 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -1992,14 +1992,23 @@ void ldbBreak(sds *argv, int argc) {
void ldbEval(lua_State *lua, sds *argv, int argc) {
/* Glue the script together if it is composed of multiple arguments. */
sds code = sdsjoinsds(argv+1,argc-1," ",1);
+ sds expr = sdscatsds(sdsnew("return "),code);
- if (luaL_loadbuffer(lua,code,sdslen(code),"@ldb_eval")) {
- ldbLog(sdscatfmt(sdsempty(),"<error> %s",lua_tostring(lua,-1)));
+ /* Try to compile it as an expression, prepending "return ". */
+ if (luaL_loadbuffer(lua,expr,sdslen(expr),"@ldb_eval")) {
lua_pop(lua,1);
- sdsfree(code);
- return;
+ /* Failed? Try as a statement. */
+ if (luaL_loadbuffer(lua,code,sdslen(code),"@ldb_eval")) {
+ ldbLog(sdscatfmt(sdsempty(),"<error> %s",lua_tostring(lua,-1)));
+ lua_pop(lua,1);
+ sdsfree(code);
+ return;
+ }
}
+
+ /* Call it. */
sdsfree(code);
+ sdsfree(expr);
if (lua_pcall(lua,0,1,0)) {
ldbLog(sdscatfmt(sdsempty(),"<error> %s",lua_tostring(lua,-1)));
lua_pop(lua,1);