Skip to content

Commit

Permalink
Remove manual code concat in scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice committed Mar 31, 2023
1 parent 62d04c7 commit 385cae8
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/storage/scripting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "commands/commander.h"
#include "fmt/format.h"
#include "lua.h"
#include "parse_util.h"
#include "rand.h"
#include "server/redis_connection.h"
Expand Down Expand Up @@ -878,18 +879,13 @@ Status createFunction(Server *srv, const std::string &body, std::string *sha, lu
std::copy(sha->begin(), sha->end(), funcname + 2);
}

auto funcdef = fmt::format("function {}() {}\nend", funcname, body);

if (luaL_loadbuffer(lua, funcdef.c_str(), funcdef.size(), "@user_script")) {
if (luaL_loadbuffer(lua, body.c_str(), body.size(), "@user_script")) {
std::string errMsg = lua_tostring(lua, -1);
lua_pop(lua, 1);
return {Status::NotOK, "Error compiling script (new function): " + errMsg};
}
if (lua_pcall(lua, 0, 0, 0)) {
std::string errMsg = lua_tostring(lua, -1);
lua_pop(lua, 1);
return {Status::NotOK, "Error running script (new function): " + errMsg};
return {Status::NotOK, "Error while compiling new script: " + errMsg};
}
lua_setglobal(lua, funcname);

// would store lua function into propagate column family and propagate those scripts to slaves
return need_to_store ? srv->ScriptSet(*sha, body) : Status::OK();
}
Expand Down

0 comments on commit 385cae8

Please sign in to comment.