From d0d9254dcac947f92525d66078c64bee18814144 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 14 Mar 2014 12:44:55 +0100
Subject: Swiftob: Fix compilation against Lua 5.2

Change-Id: I0cbee4085d87cf39b55d6d429e3e45389469885f

diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp
index c2478cd..0358f98 100644
--- a/Swiftob/LuaCommands.cpp
+++ b/Swiftob/LuaCommands.cpp
@@ -24,6 +24,33 @@
 #define LUA_COMMANDS "__Lua_Commands"
 #define STORAGE "__Storage"
 
+static const luaL_Reg defaultLibraries[] = {
+	{"", luaopen_base},
+	{LUA_LOADLIBNAME, luaopen_package},
+	{LUA_TABLIBNAME, luaopen_table},
+	{LUA_IOLIBNAME, luaopen_io},
+	{LUA_OSLIBNAME, luaopen_os},
+	{LUA_STRLIBNAME, luaopen_string},
+	{LUA_MATHLIBNAME, luaopen_math},
+	{LUA_DBLIBNAME, luaopen_debug},
+	{NULL, NULL}
+};
+
+static void initialize(lua_State* L) {
+	lua_gc(L, LUA_GCSTOP, 0);
+	for (const luaL_Reg* lib = defaultLibraries; lib->func; lib++) {
+#if LUA_VERSION_NUM >= 502
+		luaL_requiref(L, lib->name, lib->func, 1);
+		lua_pop(L, 1);
+#else
+		lua_pushcfunction(L, lib->func);
+		lua_pushstring(L, lib->name);
+		lua_call(L, 1, 0);
+#endif
+	}
+	lua_gc(L, LUA_GCRESTART, 0);
+}
+
 LuaCommands::LuaCommands(Commands* commands, const std::string& path, Client* client, TimerFactory* timerFactory, MUCs* mucs) : path_(path), scriptsPath_(boost::filesystem::path(path_) / "scripts") {
 	commands_ = commands;
 	client_ = client;
@@ -405,8 +432,8 @@ void LuaCommands::messageOntoStack(Swift::Message::ref message, lua_State* L) {
 
 void LuaCommands::loadScript(boost::filesystem::path filePath) {
 	std::cout << "Trying to load file from " << filePath << std::endl;
-	lua_State* lua = lua_open();
-	luaL_openlibs(lua);
+	lua_State* lua = luaL_newstate();
+	initialize(lua);
 	lua_pushlightuserdata(lua, this);
 	lua_setfield(lua, LUA_REGISTRYINDEX, LUA_COMMANDS);
 #if BOOST_FILESYSTEM_VERSION == 2 // TODO: Delete this when boost 1.44 becomes a minimum requirement, and we no longer need v2
diff --git a/Swiftob/SConscript b/Swiftob/SConscript
index a830b84..b78ade9 100644
--- a/Swiftob/SConscript
+++ b/Swiftob/SConscript
@@ -13,7 +13,6 @@ elif env["SCONS_STAGE"] == "build":
 	myenv.UseFlags(myenv["SWIFTEN_FLAGS"])
 	myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"])
 	sources = [
-		"linit.c",
 		"Swiftob.cpp",
 		"Users.cpp",
 		"Commands.cpp",
diff --git a/Swiftob/linit.c b/Swiftob/linit.c
deleted file mode 100644
index 13c5b09..0000000
--- a/Swiftob/linit.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "../3rdParty/Lua/src/linit.c"
-- 
cgit v0.10.2-6-g49f6