diff options
-rw-r--r-- | BuildTools/SCons/SConstruct | 2 | ||||
-rw-r--r-- | Sluift/.gitignore | 2 | ||||
-rw-r--r-- | Sluift/Lua/LuaUtils.cpp | 4 | ||||
-rw-r--r-- | Sluift/SConscript | 8 | ||||
-rw-r--r-- | Sluift/SluiftGlobals.h | 2 | ||||
-rw-r--r-- | Sluift/client.cpp | 2 | ||||
-rw-r--r-- | Sluift/core.lua (renamed from Sluift/boot.lua) | 35 | ||||
-rw-r--r-- | Sluift/linit.c | 5 | ||||
-rw-r--r-- | Sluift/sluift.cpp | 29 | ||||
-rw-r--r-- | Sluift/sluift.h | 4 |
10 files changed, 59 insertions, 34 deletions
diff --git a/BuildTools/SCons/SConstruct b/BuildTools/SCons/SConstruct index 9eeaabe..8c5258e 100644 --- a/BuildTools/SCons/SConstruct +++ b/BuildTools/SCons/SConstruct @@ -423,5 +423,5 @@ if env.get("lua_includedir", None) : lua_conf_env.MergeFlags(lua_flags) conf = Configure(lua_conf_env) -if not env.get("lua_force_bundled", False) and conf.CheckCXXHeader("lua.hpp") and conf.CheckLib(env["lua_libname"]) : +if not env.get("lua_force_bundled", False) and conf.CheckLibWithHeader(env["lua_libname"], "lua.hpp", "cxx", autoadd = 0) : env["HAVE_LUA"] = 1 env["LUA_FLAGS"] = { "LIBS": [env["lua_libname"]] } diff --git a/Sluift/.gitignore b/Sluift/.gitignore index e5fd1e5..940fc86 100644 --- a/Sluift/.gitignore +++ b/Sluift/.gitignore @@ -3,5 +3,5 @@ sluift_dll.cpp sluift dll.c -boot.c +core.c dll/ exe/ diff --git a/Sluift/Lua/LuaUtils.cpp b/Sluift/Lua/LuaUtils.cpp index 7052abe..b00ab56 100644 --- a/Sluift/Lua/LuaUtils.cpp +++ b/Sluift/Lua/LuaUtils.cpp @@ -23,5 +23,5 @@ static const std::string INDENT = " "; void Swift::Lua::registerTableToString(lua_State* L, int index) { index = Lua::absoluteOffset(L, index); - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, "register_table_tostring"); lua_pushvalue(L, index); @@ -34,5 +34,5 @@ void Swift::Lua::registerTableToString(lua_State* L, int index) { void Swift::Lua::registerGetByTypeIndex(lua_State* L, int index) { index = Lua::absoluteOffset(L, index); - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, "register_get_by_type_index"); lua_pushvalue(L, index); diff --git a/Sluift/SConscript b/Sluift/SConscript index 1a29e43..c8f1108 100644 --- a/Sluift/SConscript +++ b/Sluift/SConscript @@ -33,5 +33,5 @@ elif env["SCONS_STAGE"] == "build" : "ClientHelpers.cpp", "SluiftClient.cpp", - "boot.c", + "core.c", "client.cpp", "sluift.cpp" @@ -43,4 +43,6 @@ elif env["SCONS_STAGE"] == "build" : sluift_env.UseFlags(env["SWIFTEN_FLAGS"]) sluift_env.UseFlags(env["SWIFTEN_DEP_FLAGS"]) + # Support compilation on both Lua 5.1 and Lua 5.2 + sluift_env.Append(CPPDEFINES = ["LUA_COMPAT_ALL"]) if sluift_env["PLATFORM"] == "win32" : sluift_env.Append(CPPDEFINES = ["SLUIFT_BUILD_DLL"]) @@ -63,5 +65,5 @@ elif env["SCONS_STAGE"] == "build" : sluift_env.Command("lua.c", ["#/3rdParty/Lua/src/lua.c", sluift_env.Value(sluift_env["SLUIFT_VERSION"])], env.Action(patchLua, cmdstr = "$GENCOMSTR")) - # Generate boot.cpp + # Generate core.cpp def generate_embedded_lua(env, target, source) : f = open(source[0].abspath, "r") @@ -71,5 +73,5 @@ elif env["SCONS_STAGE"] == "build" : f.write('const char ' + source[0].name.replace(".", "_") + "[] = \"" + data.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"') + "\";") f.close() - sluift_env.Command("boot.c", ["boot.lua"], env.Action(generate_embedded_lua, cmdstr="$GENCOMSTR")) + sluift_env.Command("core.c", ["core.lua"], env.Action(generate_embedded_lua, cmdstr="$GENCOMSTR")) if sluift_env.get("HAVE_READLINE", False) : diff --git a/Sluift/SluiftGlobals.h b/Sluift/SluiftGlobals.h index 03c1c1a..18a90c2 100644 --- a/Sluift/SluiftGlobals.h +++ b/Sluift/SluiftGlobals.h @@ -20,5 +20,5 @@ namespace Swift { SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories; - int bootIndex; + int coreLibIndex; }; } diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 97f9106..9eac84b 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -420,5 +420,5 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& event) { lua_setfield(L, -2, "from"); - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, "process_pubsub_event"); lua_pushvalue(L, -3); diff --git a/Sluift/boot.lua b/Sluift/core.lua index a28a189..e2d4f8e 100644 --- a/Sluift/boot.lua +++ b/Sluift/core.lua @@ -5,4 +5,10 @@ --]] +local _G = _G +local pairs, ipairs, print, tostring, type, error = pairs, ipairs, print, tostring, type, error +local setmetatable, getmetatable = setmetatable, getmetatable +local string = string +_ENV = nil + local Client = {} local PubSub = {} @@ -29,4 +35,5 @@ end local function parse_options(unnamed_parameters, arg1, arg2) local options = {} + local f if type(arg1) == 'table' then options = arg1 @@ -119,4 +126,18 @@ local function register_get_by_type_index(table) end +local function call(options) + local f = options[1] + local result = { xpcall(f, debug.traceback) } + if options.finally then + options.finally() + end + if result[1] then + table.remove(result, 1) + return unpack(result) + else + error(result[2]) + end +end + -------------------------------------------------------------------------------- -- Client @@ -129,12 +150,5 @@ function Client:connect (...) self:wait_connected() if f then - local result = { xpcall(function() return f(self) end, debug.traceback) } - self:disconnect() - if result[1] then - table.remove(result, 1) - return unpack(result) - else - error(result[2]) - end + return call {function() return f(self) end, finally = function() self:disconnect() end} end return true @@ -181,4 +195,9 @@ for method, event_type in pairs({messages = 'message', pubsub_events = 'pubsub'} end +-- Process all pending events +function Client:process_events () + for event in self:events{timeout=0} do end +end + -- -- Register get_* and set_* convenience methods for some type of queries diff --git a/Sluift/linit.c b/Sluift/linit.c index 8f0614b..ad27b37 100644 --- a/Sluift/linit.c +++ b/Sluift/linit.c @@ -27,9 +27,10 @@ LUALIB_API void luaL_openlibs (lua_State *L) { /* Import sluift into global namespace */ - lua_getfield(L, LUA_GLOBALSINDEX, "sluift"); + lua_pushglobaltable(L); + lua_getfield(L, -1, "sluift"); for (lua_pushnil(L); lua_next(L, -2); ) { lua_pushvalue(L, -2); lua_pushvalue(L, -2); - lua_settable(L, LUA_GLOBALSINDEX); + lua_settable(L, -6); lua_pop(L, 1); } diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 95e7101..e6b2bb6 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -41,5 +41,5 @@ namespace Swift { } -extern "C" const char boot_lua[]; +extern "C" const char core_lua[]; /******************************************************************************* @@ -264,5 +264,5 @@ SLUIFT_LUA_FUNCTION(IDN, stringprep) { ******************************************************************************/ -static const luaL_reg sluift_functions[] = { {NULL, NULL} }; +static const luaL_Reg sluift_functions[] = { {NULL, NULL} }; SLUIFT_API int luaopen_sluift(lua_State* L) { @@ -271,12 +271,12 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { Sluift::globals.timeout = -1; - luaL_register(L, "sluift", sluift_functions); + luaL_register(L, lua_tostring(L, 1), sluift_functions); - // Load bootstrap code - if (luaL_loadbuffer(L, boot_lua, strlen(boot_lua), "boot.lua") != 0) { + // Load core lib code + if (luaL_loadbuffer(L, core_lua, strlen(core_lua), "core.lua") != 0) { lua_error(L); } lua_call(L, 0, 1); - Sluift::globals.bootIndex = luaL_ref(L, LUA_REGISTRYINDEX); + Sluift::globals.coreLibIndex = luaL_ref(L, LUA_REGISTRYINDEX); // Register functions @@ -290,12 +290,11 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Register convenience functions - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); - lua_getfield(L, -1, "tprint"); - lua_setfield(L, -3, "tprint"); - lua_pop(L, 1); - - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); - lua_getfield(L, -1, "disco"); - lua_setfield(L, -3, "disco"); + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); + std::vector<std::string> coreLibExports = boost::assign::list_of + ("tprint")("disco"); + foreach (const std::string& coreLibExport, coreLibExports) { + lua_getfield(L, -1, coreLibExport.c_str()); + lua_setfield(L, -3, coreLibExport.c_str()); + } lua_pop(L, 1); @@ -313,5 +312,5 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { Lua::FunctionRegistry::getInstance().registerTypeMetaTable(L, table); luaL_getmetatable(L, Lua::FunctionRegistry::getMetaTableNameForType(table).c_str()); - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, table.c_str()); if (!lua_isnil(L, -1)) { diff --git a/Sluift/sluift.h b/Sluift/sluift.h index b82e1c4..2613370 100644 --- a/Sluift/sluift.h +++ b/Sluift/sluift.h @@ -21,4 +21,8 @@ #endif +#if LUA_VERSION_NUM < 502 +#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +#endif + #if defined(__cplusplus) extern "C" |