diff options
| author | Remko Tronçon <git@el-tramo.be> | 2014-03-14 11:44:55 (GMT) |
|---|---|---|
| committer | Swift Review <review@swift.im> | 2014-03-21 10:42:23 (GMT) |
| commit | d0d9254dcac947f92525d66078c64bee18814144 (patch) | |
| tree | 811cfecdeddb6e33f08561b8a7d235483c123ad7 | |
| parent | dfe21a4a486e234441ccbe49fbf1445c227647ff (diff) | |
| download | swift-contrib-d0d9254dcac947f92525d66078c64bee18814144.zip swift-contrib-d0d9254dcac947f92525d66078c64bee18814144.tar.bz2 | |
Swiftob: Fix compilation against Lua 5.2
Change-Id: I0cbee4085d87cf39b55d6d429e3e45389469885f
| -rw-r--r-- | Swiftob/LuaCommands.cpp | 31 | ||||
| -rw-r--r-- | Swiftob/SConscript | 1 | ||||
| -rw-r--r-- | Swiftob/linit.c | 1 |
3 files changed, 29 insertions, 4 deletions
diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp index c2478cd..0358f98 100644 --- a/Swiftob/LuaCommands.cpp +++ b/Swiftob/LuaCommands.cpp @@ -1,61 +1,88 @@ /* * Copyright (c) 2011 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <Swiftob/LuaCommands.h> #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> #include <Swiften/Base/foreach.h> #include <Swiften/Client/Client.h> #include <Swiften/Network/TimerFactory.h> #include <boost/filesystem/operations.hpp> #include <Swiften/Base/Path.h> #include <Swiftob/Commands.h> #include <Swiften/Base/BoostFilesystemVersion.h> #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; timerFactory_ = timerFactory; mucs_ = mucs; commands_->onReset.connect(boost::bind(&LuaCommands::registerCommands, this)); registerCommands(); } void LuaCommands::registerCommands() { std::cout << "Trying to load all scripts in " << scriptsPath_ << std::endl; if (boost::filesystem::exists(scriptsPath_) && boost::filesystem::is_directory(scriptsPath_)) { std::vector<boost::filesystem::path> files; copy(boost::filesystem::directory_iterator(scriptsPath_), boost::filesystem::directory_iterator(), std::back_inserter(files)); foreach (boost::filesystem::path file, files) { if (boost::filesystem::is_regular_file(file) && file.extension() == ".lua") { loadScript(file); } } } } static int l_register_listener(lua_State *L) { LuaCommands* commands = NULL; lua_getfield(L, LUA_REGISTRYINDEX, LUA_COMMANDS); commands = static_cast<LuaCommands*>(lua_touserdata(L, -1)); lua_pop(L, 1); if (!lua_isfunction(L, 1)) { return luaL_error(L, "register_listener parameter must be a callback function"); } lua_pushvalue(L, 1); int callbackIndex = luaL_ref(L, LUA_REGISTRYINDEX); lua_pop(L, 1); commands->getCommands()->registerListener(boost::bind(&LuaCommands::handleLuaListener, commands, callbackIndex, L, _1)); return 0; @@ -373,66 +400,66 @@ void LuaCommands::handleLuaCommand(int callbackIndex, lua_State* L, const std::s messageOntoStack(message, L); int result = lua_pcall(L, 3, 0, 0); if (result != 0) { std::string error(lua_tostring(L, -1)); lua_pop(L, 1); error = "Command '" + command + "' failed: " + error; std::cout << error << std::endl; commands_->replyTo(message, error, false); } } void LuaCommands::messageOntoStack(Swift::Message::ref message, lua_State* L) { lua_createtable(L, 0, 4); std::string typeString; switch (message->getType()) { case Message::Chat : typeString = "chat";break; case Message::Groupchat : typeString = "groupchat";break; case Message::Normal : typeString = "normal";break; case Message::Error : typeString = "error";break; case Message::Headline : typeString = "headline";break; } lua_pushstring(L, typeString.c_str()); lua_setfield(L, -2, "type"); lua_pushstring(L, message->getFrom().toString().c_str()); lua_setfield(L, -2, "from"); lua_pushstring(L, message->getFrom().toBare().toString().c_str()); lua_setfield(L, -2, "frombare"); lua_pushstring(L, message->getTo().toString().c_str()); lua_setfield(L, -2, "to"); lua_pushstring(L, message->getBody().c_str()); lua_setfield(L, -2, "body"); } 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 std::string filename = filePath.filename(); #else std::string filename = filePath.filename().string(); #endif filename += ".storage"; boost::filesystem::path storagePath(boost::filesystem::path(path_) / stringToPath(filename)); Storage* storage = new Storage(storagePath); lua_pushlightuserdata(lua, storage); lua_setfield(lua, LUA_REGISTRYINDEX, STORAGE); lua_register(lua, "swiftob_register_command", &l_register_command); lua_register(lua, "swiftob_register_listener", &l_register_listener); lua_register(lua, "swiftob_reply_to", &l_reply_to); lua_register(lua, "swiftob_get_software_version", &l_get_software_version); lua_register(lua, "swiftob_muc_input_to_jid", &l_muc_input_to_jid); lua_register(lua, "swiftob_store_setting", &l_store_setting); lua_register(lua, "swiftob_get_setting", &l_get_setting); lua_register(lua, "swiftob_muc_kick", &l_muc_kick); int fileLoaded = luaL_dofile(lua, filePath.string().c_str()); if (fileLoaded == 0 ) { std::cout << "Loaded" << std::endl; } else { const char* error = lua_tostring(lua, -1); std::cout << "Error: " << error << std::endl; lua_pop(lua, -1); } } diff --git a/Swiftob/SConscript b/Swiftob/SConscript index a830b84..b78ade9 100644 --- a/Swiftob/SConscript +++ b/Swiftob/SConscript @@ -1,25 +1,24 @@ Import("env") if env["SCONS_STAGE"] == "build" and not GetOption("help") and not env.get("HAVE_LUA", 0) : print "Warning: Lua was not found. Swiftob will not be built." if "Sluift" in env["PROJECTS"] : env["PROJECTS"].remove("Sluift") elif env["SCONS_STAGE"] == "build": myenv = env.Clone() # Too many compile warnings here at the moment myenv.Replace(CXXFLAGS = [flag for flag in env["CXXFLAGS"] if flag != "-Weverything"]) myenv.UseFlags(myenv.get("LUA_FLAGS", {})) myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) sources = [ - "linit.c", "Swiftob.cpp", "Users.cpp", "Commands.cpp", "MUCs.cpp", "Storage.cpp", "LuaCommands.cpp", "main.cpp" ] swiftob = myenv.Program("swiftob", sources) 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" |
Swift