Implement support for Lua 5.1

This commit is contained in:
Robert Paprocki
2018-06-21 11:15:55 -07:00
committed by Victor Hora
parent eed6b5f86d
commit dee9898449
3 changed files with 40 additions and 15 deletions

View File

@@ -82,7 +82,7 @@ bool Lua::load(std::string script, std::string *err) {
return false;
}
#ifdef WITH_LUA_5_2
#if defined (WITH_LUA_5_2) || defined (WITH_LUA_5_1)
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob))) {
#else
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob), 0)) {
@@ -138,8 +138,12 @@ int Lua::run(Transaction *t) {
luaL_setfuncs(L, mscLuaLib, 0);
lua_setglobal(L, "m");
#ifdef WITH_LUA_5_1
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str());
#else
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str(),
NULL);
#endif
if (rc != LUA_OK) {
std::string e;
e.assign("Failed to execute lua script: " + m_scriptName + ". ");
@@ -150,9 +154,11 @@ int Lua::run(Transaction *t) {
case LUA_ERRMEM:
e.assign("Memory error. ");
break;
#ifndef WITH_LUA_5_1
case LUA_ERRGCMM:
e.assign("Garbage Collector error. ");
break;
#endif
}
e.append(lua_tostring(L, -1));
#ifndef NO_LOGS
@@ -402,7 +408,11 @@ std::string Lua::applyTransformations(lua_State *L, Transaction *t,
if (lua_istable(L, idx)) {
const char *name = NULL;
#ifdef WITH_LUA_5_1
int i, n = lua_objlen(L, idx);
#else
int i, n = lua_rawlen(L, idx);
#endif
for (i = 1; i <= n; i++) {
lua_rawgeti(L, idx, i);

View File

@@ -101,4 +101,29 @@ static const struct luaL_Reg mscLuaLib[] = {
} // namespace engine
} // namespace modsecurity
#ifdef WITH_LUA
#if defined LUA_VERSION_NUM && LUA_VERSION_NUM < 502
/*
** Adapted from Lua 5.2.0
*/
#define LUA_OK 0
static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup + 1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup + 1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif
#endif
#endif // SRC_ENGINE_LUA_H_