Skip to content

Commit

Permalink
Remove manual code concat in scripting (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Mar 31, 2023
1 parent 62d04c7 commit 54daf8d
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 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 @@ -416,7 +417,7 @@ void enableGlobalsProtection(lua_State *lua) {
"mt.__newindex = function (t, n, v)\n"
" if dbg.getinfo(2) then\n"
" local w = dbg.getinfo(2, \"S\").what\n"
" if w ~= \"main\" and w ~= \"C\" then\n"
" if w ~= \"user_script\" and w ~= \"C\" then\n"
" error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n"
" end\n"
" end\n"
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 54daf8d

Please sign in to comment.