diff options
Diffstat (limited to 'Sluift/sluift.cpp')
-rw-r--r-- | Sluift/sluift.cpp | 94 |
1 files changed, 73 insertions, 21 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index e6b2bb6..b2bdc29 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -19,4 +19,5 @@ #include <Sluift/globals.h> #include <Sluift/Lua/Exception.h> +#include <Sluift/Lua/LuaUtils.h> #include <Sluift/Lua/FunctionRegistration.h> #include <Swiften/Base/sleep.h> @@ -26,4 +27,5 @@ #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/Serializer/PayloadSerializer.h> +#include <Sluift/LuaElementConvertor.h> #include <Sluift/Lua/Debug.h> #include <Swiften/StringCodecs/Base64.h> @@ -42,4 +44,5 @@ namespace Swift { extern "C" const char core_lua[]; +extern "C" size_t core_lua_size; /******************************************************************************* @@ -47,5 +50,14 @@ extern "C" const char core_lua[]; ******************************************************************************/ -SLUIFT_LUA_FUNCTION(Sluift, new_client) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, new_client, + + "Creates a new client.\n\nReturns a @{Client} object.\n", + + "jid The JID to connect as\n" + "passphrase The passphrase to use\n", + + "" +) { Lua::checkString(L, 1); JID jid(std::string(Lua::checkString(L, 1))); @@ -53,6 +65,9 @@ SLUIFT_LUA_FUNCTION(Sluift, new_client) { SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*))); - luaL_getmetatable(L, Lua::FunctionRegistry::getMetaTableNameForType("Client").c_str()); - lua_setmetatable(L, -2); + + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); + lua_getfield(L, -1, "Client"); + lua_setmetatable(L, -3); + lua_pop(L, 1); *client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop, &Sluift::globals); @@ -60,5 +75,10 @@ SLUIFT_LUA_FUNCTION(Sluift, new_client) { } -SLUIFT_LUA_FUNCTION(Sluift, sha1) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, sha1, + "Compute the SHA-1 hash of given data", + "data the data to hash", + "" +) { static boost::shared_ptr<CryptoProvider> crypto(PlatformCryptoProvider::create()); if (!lua_isstring(L, 1)) { @@ -72,5 +92,10 @@ SLUIFT_LUA_FUNCTION(Sluift, sha1) { } -SLUIFT_LUA_FUNCTION(Sluift, sleep) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, sleep, + "Sleeps for the given time.", + "milliseconds the amount of milliseconds to sleep", + "" +) { Sluift::globals.eventLoop.runOnce(); int timeout = Lua::checkIntNumber(L, 1); @@ -83,5 +108,8 @@ SLUIFT_LUA_FUNCTION(Sluift, sleep) { } -SLUIFT_LUA_FUNCTION(Sluift, new_uuid) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, new_uuid, + "Generates a new UUID", "", "" +) { lua_pushstring(L, IDGenerator().generateID().c_str()); return 1; @@ -123,5 +151,10 @@ static int sluift_newindex(lua_State* L) { } -SLUIFT_LUA_FUNCTION(Sluift, from_xml) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, from_xml, + "Convert a raw XML string into a structured representation.", + "string the string to convert", + "" +) { PayloadsParserTester parser; if (!parser.parse(Lua::checkString(L, 1))) { @@ -131,5 +164,10 @@ SLUIFT_LUA_FUNCTION(Sluift, from_xml) { } -SLUIFT_LUA_FUNCTION(Sluift, to_xml) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, to_xml, + "Convert a structured element into XML.", + "element the element to convert", + "" +) { static FullPayloadSerializerCollection serializers; boost::shared_ptr<Payload> payload = Sluift::globals.elementConvertor.convertFromLua(L, 1); @@ -145,5 +183,10 @@ SLUIFT_LUA_FUNCTION(Sluift, to_xml) { } -SLUIFT_LUA_FUNCTION(Sluift, hexify) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, hexify, + "Convert binary data into hexadecimal format.", + "data the data to convert", + "" +) { if (!lua_isstring(L, 1)) { throw Lua::Exception("Expected string"); @@ -155,5 +198,10 @@ SLUIFT_LUA_FUNCTION(Sluift, hexify) { } -SLUIFT_LUA_FUNCTION(Sluift, unhexify) { +SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, unhexify, + "Convert hexadecimal data into binary data.", + "data the data in hexadecimal format", + "" +) { if (!lua_isstring(L, 1)) { throw Lua::Exception("Expected string"); @@ -274,5 +322,5 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Load core lib code - if (luaL_loadbuffer(L, core_lua, strlen(core_lua), "core.lua") != 0) { + if (luaL_loadbuffer(L, core_lua, core_lua_size, "core.lua") != 0) { lua_error(L); } @@ -292,5 +340,5 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> coreLibExports = boost::assign::list_of - ("tprint")("disco"); + ("tprint")("disco")("help")("get_help")("copy"); foreach (const std::string& coreLibExport, coreLibExports) { lua_getfield(L, -1, coreLibExport.c_str()); @@ -308,20 +356,24 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Load client metatable + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> tables = boost::assign::list_of("Client"); foreach(const std::string& table, tables) { - Lua::FunctionRegistry::getInstance().registerTypeMetaTable(L, table); - luaL_getmetatable(L, Lua::FunctionRegistry::getMetaTableNameForType(table).c_str()); - lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, table.c_str()); - if (!lua_isnil(L, -1)) { - for (lua_pushnil(L); lua_next(L, -2); ) { - lua_pushvalue(L, -2); - lua_pushvalue(L, -2); - lua_settable(L, -7); + Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, table); lua_pop(L, 1); } + lua_pop(L, 1); + + // Register documentation for all elements + foreach (boost::shared_ptr<LuaElementConvertor> convertor, Sluift::globals.elementConvertor.getConvertors()) { + boost::optional<LuaElementConvertor::Documentation> documentation = convertor->getDocumentation(); + if (documentation) { + Lua::registerClassHelp(L, documentation->className, documentation->description); } - lua_pop(L, 2); } + + // Register global documentation + Lua::registerExtraHelp(L, -1, "sluift"); + return 1; } |