Index: source/lobby/IXmppClient.h =================================================================== --- source/lobby/IXmppClient.h +++ source/lobby/IXmppClient.h @@ -50,11 +50,11 @@ virtual void GetPresence(const std::string& nickname, std::string& presence) = 0; virtual void GetRole(const std::string& nickname, std::string& role) = 0; virtual void GetSubject(std::string& subject) = 0; - virtual void GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; + virtual JS::Value GUIGetPlayerList(const ScriptInterface& scriptInterface) = 0; virtual void ClearPresenceUpdates() = 0; - virtual void GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; - virtual void GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; - virtual void GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; + virtual JS::Value GUIGetGameList(const ScriptInterface& scriptInterface) = 0; + virtual JS::Value GUIGetBoardList(const ScriptInterface& scriptInterface) = 0; + virtual JS::Value GUIGetProfile(const ScriptInterface& scriptInterface) = 0; virtual JS::Value GuiPollNewMessage(const ScriptInterface& scriptInterface) = 0; virtual JS::Value GuiPollHistoricMessages(const ScriptInterface& scriptInterface) = 0; virtual void SendMUCMessage(const std::string& message) = 0; Index: source/lobby/XmppClient.h =================================================================== --- source/lobby/XmppClient.h +++ source/lobby/XmppClient.h @@ -27,6 +27,8 @@ class ScriptInterface; +typedef std::vector GlooxTagList; + namespace glooxwrapper { class Client; @@ -84,10 +86,10 @@ void GetRole(const std::string& nickname, std::string& role); void GetSubject(std::string& subject); - void GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret); - void GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret); - void GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret); - void GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret); + JS::Value GUIGetPlayerList(const ScriptInterface& scriptInterface); + JS::Value GUIGetGameList(const ScriptInterface& scriptInterface); + JS::Value GUIGetBoardList(const ScriptInterface& scriptInterface); + JS::Value GUIGetProfile(const ScriptInterface& scriptInterface); void SendStunEndpointToHost(StunClient::StunEndpoint* stunEndpoint, const std::string& hostJID); @@ -170,11 +172,11 @@ /// Map of players std::map > m_PlayerMap; /// List of games - std::vector m_GameList; + GlooxTagList m_GameList; /// List of rankings - std::vector m_BoardList; + GlooxTagList m_BoardList; /// Profile data - std::vector m_Profile; + GlooxTagList m_Profile; /// Queue of messages for the GUI std::deque m_GuiMessageQueue; /// Cache of all GUI messages received since the login Index: source/lobby/XmppClient.cpp =================================================================== --- source/lobby/XmppClient.cpp +++ source/lobby/XmppClient.cpp @@ -27,6 +27,7 @@ #include "i18n/L10n.h" #include "lib/external_libraries/enet.h" #include "lib/utf8.h" +#include "lobby/LobbyScriptConversions.cpp" #include "network/NetServer.h" #include "network/StunClient.h" #include "ps/CLogger.h" @@ -497,98 +498,52 @@ * * @return A JS array containing all known players and their presences */ -void XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) +JS::Value XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.Eval("([])", ret); - // Convert the internal data structure to a Javascript object. + JS::RootedObject playerList(cx, JS_NewArrayObject(cx, 0)); + int i = 0; for (const std::pair >& p : m_PlayerMap) { - JS::RootedValue player(cx); - scriptInterface.Eval("({})", &player); + JS::RootedValue player(cx, JS::ObjectValue(*JS_NewObject(cx, nullptr))); scriptInterface.SetProperty(player, "name", wstring_from_utf8(p.first)); scriptInterface.SetProperty(player, "presence", wstring_from_utf8(p.second[0])); scriptInterface.SetProperty(player, "rating", wstring_from_utf8(p.second[1])); scriptInterface.SetProperty(player, "role", wstring_from_utf8(p.second[2])); - scriptInterface.CallFunctionVoid(ret, "push", player); + JS_SetElement(cx, playerList, i++, player); } + + return JS::ObjectValue(*playerList); } -/** - * Handle requests from the GUI for the list of all active games. - * - * @return A JS array containing all known games - */ -void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) +JS::Value XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - - scriptInterface.Eval("([])", ret); - const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", - "nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", - "victoryCondition", "startTime", "mods" }; - for(const glooxwrapper::Tag* const& t : m_GameList) - { - JS::RootedValue game(cx); - scriptInterface.Eval("({})", &game); - - for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) - scriptInterface.SetProperty(game, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); - - scriptInterface.CallFunctionVoid(ret, "push", game); - } + JS::RootedValue value(cx); + ScriptInterface::ToJSVal(cx, &value, m_GameList); + return value; } -/** - * Handle requests from the GUI for leaderboard data. - * - * @return A JS array containing all known leaderboard data - */ -void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) +JS::Value XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - - scriptInterface.Eval("([])", ret); - const char* attributes[] = { "name", "rank", "rating" }; - for(const glooxwrapper::Tag* const& t : m_BoardList) - { - JS::RootedValue board(cx); - scriptInterface.Eval("({})", &board); - - for (size_t i = 0; i < ARRAY_SIZE(attributes); ++i) - scriptInterface.SetProperty(board, attributes[i], wstring_from_utf8(t->findAttribute(attributes[i]).to_string())); - - scriptInterface.CallFunctionVoid(ret, "push", board); - } + JS::RootedValue value(cx); + ScriptInterface::ToJSVal(cx, &value, m_BoardList); + return value; } -/** - * Handle requests from the GUI for profile data. - * - * @return A JS array containing the specific user's profile data - */ -void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) +JS::Value XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - - scriptInterface.Eval("([])", ret); - const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; - for (const glooxwrapper::Tag* const& t : m_Profile) - { - JS::RootedValue profile(cx); - scriptInterface.Eval("({})", &profile); - - for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) - scriptInterface.SetProperty(profile, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); - - scriptInterface.CallFunctionVoid(ret, "push", profile); - } + JS::RootedValue value(cx); + ScriptInterface::ToJSVal(cx, &value, m_Profile); + return value; } /***************************************************** Index: source/lobby/glooxwrapper/glooxwrapper.h =================================================================== --- source/lobby/glooxwrapper/glooxwrapper.h +++ source/lobby/glooxwrapper/glooxwrapper.h @@ -574,6 +574,7 @@ bool addAttribute(const string& name, const string& value); string findAttribute(const string& name) const; + const gloox::Tag::AttributeList& attributes() const; Tag* clone() const; string xmlns() const; bool setXmlns(const string& xmlns); Index: source/lobby/glooxwrapper/glooxwrapper.cpp =================================================================== --- source/lobby/glooxwrapper/glooxwrapper.cpp +++ source/lobby/glooxwrapper/glooxwrapper.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #if OS_WIN @@ -725,6 +726,11 @@ return m_Wrapped->findAttribute(name.to_string()); } +const gloox::Tag::AttributeList& glooxwrapper::Tag::attributes() const +{ + return m_Wrapped->attributes(); +} + glooxwrapper::Tag* glooxwrapper::Tag::clone() const { return new glooxwrapper::Tag(m_Wrapped->clone(), true); Index: source/lobby/scripting/JSInterface_Lobby.cpp =================================================================== --- source/lobby/scripting/JSInterface_Lobby.cpp +++ source/lobby/scripting/JSInterface_Lobby.cpp @@ -187,13 +187,7 @@ if (!g_XmppClient) return JS::UndefinedValue(); - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - - JS::RootedValue playerList(cx); - g_XmppClient->GUIGetPlayerList(*(pCxPrivate->pScriptInterface), &playerList); - - return playerList; + return g_XmppClient->GUIGetPlayerList(*(pCxPrivate->pScriptInterface)); } void JSI_Lobby::LobbyClearPresenceUpdates(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) @@ -209,13 +203,7 @@ if (!g_XmppClient) return JS::UndefinedValue(); - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - - JS::RootedValue gameList(cx); - g_XmppClient->GUIGetGameList(*(pCxPrivate->pScriptInterface), &gameList); - - return gameList; + return g_XmppClient->GUIGetGameList(*(pCxPrivate->pScriptInterface)); } JS::Value JSI_Lobby::GetBoardList(ScriptInterface::CxPrivate* pCxPrivate) @@ -223,13 +211,7 @@ if (!g_XmppClient) return JS::UndefinedValue(); - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - - JS::RootedValue boardList(cx); - g_XmppClient->GUIGetBoardList(*(pCxPrivate->pScriptInterface), &boardList); - - return boardList; + return g_XmppClient->GUIGetBoardList(*(pCxPrivate->pScriptInterface)); } JS::Value JSI_Lobby::GetProfile(ScriptInterface::CxPrivate* pCxPrivate) @@ -237,13 +219,7 @@ if (!g_XmppClient) return JS::UndefinedValue(); - JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); - JSAutoRequest rq(cx); - - JS::RootedValue profileFetch(cx); - g_XmppClient->GUIGetProfile(*(pCxPrivate->pScriptInterface), &profileFetch); - - return profileFetch; + return g_XmppClient->GUIGetProfile(*(pCxPrivate->pScriptInterface)); } JS::Value JSI_Lobby::LobbyGuiPollNewMessage(ScriptInterface::CxPrivate* pCxPrivate)