@@ -112,7 +112,7 @@ char *lua_compile(msc_script **script, const char *filename, apr_pool_t *pool) {
112112 dump .parts = apr_array_make (pool , 128 , sizeof (msc_script_part * ));
113113
114114#if LUA_VERSION_NUM >= 503
115- lua_dump (L , dump_writer , & dump , 1 );
115+ lua_dump (L , dump_writer , & dump , 0 );
116116#else
117117 lua_dump (L , dump_writer , & dump );
118118#endif
@@ -420,23 +420,32 @@ int lua_execute(msc_script *script, char *param, modsec_rec *msr, msre_rule *rul
420420 time_before = apr_time_now ();
421421
422422#ifdef CACHE_LUA
423+
423424 L = msr -> L ;
424425 rc = lua_gettop (L );
425426 if (rc )
426427 lua_pop (L , rc );
428+
427429#else
430+
428431 /* Create new state. */
429- #if LUA_VERSION_NUM > 501
432+ #if LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503 || LUA_VERSION_NUM == 501
430433 L = luaL_newstate ();
431- #else
434+ #elif LUA_VERSION_NUM == 500
432435 L = lua_open ();
436+ #else
437+ #error We are only tested under Lua 5.0, 5.1, 5.2, or 5.3.
433438#endif
434439 luaL_openlibs (L );
440+
435441#endif
436442
437443 if (L == NULL )
438444 return -1 ;
439445
446+ luaL_newmetatable (L , "luaL_msc" );
447+ lua_newtable (L );
448+
440449 /* Associate msr with the state. */
441450 lua_pushlightuserdata (L , (void * )msr );
442451 lua_setglobal (L , "__msr" );
@@ -448,13 +457,16 @@ int lua_execute(msc_script *script, char *param, modsec_rec *msr, msre_rule *rul
448457 }
449458
450459 /* Register functions. */
451- #if LUA_VERSION_NUM > 501
452- luaL_setfuncs (L ,mylib ,0 );
453- lua_setglobal (L ,"m" );
454- #else
460+ #if LUA_VERSION_NUM == 500 || LUA_VERSION_NUM == 501
455461 luaL_register (L , "m" , mylib );
462+ #elif LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503
463+ luaL_setfuncs (L , mylib , 0 );
464+ #else
465+ #error We are only tested under Lua 5.0, 5.1, 5.2, or 5.3.
456466#endif
457467
468+ lua_setglobal (L , "m" );
469+
458470 rc = lua_restore (L , script );
459471 if (rc ) {
460472 * error_msg = apr_psprintf (msr -> mp , "Lua: Failed to restore script with %i." , rc );
0 commit comments