Ticket #5636: TryPersistentRooted.patch
File TryPersistentRooted.patch, 7.5 KB (added by , 4 years ago) |
---|
-
source/lobby/IXmppClient.h
public: 53 53 virtual void GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; 54 54 virtual void GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; 55 55 virtual void GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; 56 56 virtual void GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0; 57 57 58 virtual JS::Value GuiPollNewMessage(const ScriptInterface& scriptInterface) = 0;58 virtual void GuiPollNewMessage(const ScriptInterface& scriptInterface, JS::MutableHandleValue value) = 0; 59 59 virtual JS::Value GuiPollHistoricMessages(const ScriptInterface& scriptInterface) = 0; 60 60 virtual bool GuiPollHasPlayerListUpdate() = 0; 61 61 62 62 virtual void SendMUCMessage(const std::string& message) = 0; 63 63 virtual void SendStunEndpointToHost(const StunClient::StunEndpoint& stunEndpoint, const std::string& hostJID) = 0; -
source/lobby/XmppClient.cpp
XmppClient::XmppClient(const ScriptInter 91 91 m_isConnected(false), 92 92 m_sessionManager(nullptr), 93 93 m_certStatus(gloox::CertStatus::CertOk), 94 94 m_PlayerMapUpdate(false) 95 95 { 96 if (m_ScriptInterface)97 JS_AddExtraGCRootsTracer(m_ScriptInterface->GetJSRuntime(), XmppClient::Trace, this);98 99 96 // Read lobby configuration from default.cfg 100 97 std::string sXpartamupp; 101 98 std::string sEchelon; 102 99 CFG_GET_VAL("lobby.server", m_server); 103 100 CFG_GET_VAL("lobby.xpartamupp", sXpartamupp); … … XmppClient::~XmppClient() 188 185 glooxwrapper::Tag::free(t); 189 186 for (const glooxwrapper::Tag* const& t : m_BoardList) 190 187 glooxwrapper::Tag::free(t); 191 188 for (const glooxwrapper::Tag* const& t : m_Profile) 192 189 glooxwrapper::Tag::free(t); 193 194 if (m_ScriptInterface)195 JS_RemoveExtraGCRootsTracer(m_ScriptInterface->GetJSRuntime(), XmppClient::Trace, this);196 }197 198 void XmppClient::TraceMember(JSTracer* trc)199 {200 for (JS::Heap<JS::Value>& guiMessage : m_GuiMessageQueue)201 JS_CallValueTracer(trc, &guiMessage, "m_GuiMessageQueue");202 203 for (JS::Heap<JS::Value>& guiMessage : m_HistoricGuiMessages)204 JS_CallValueTracer(trc, &guiMessage, "m_HistoricGuiMessages");205 190 } 206 191 207 192 /// Network 208 193 void XmppClient::connect() 209 194 { … … void XmppClient::CreateGUIMessage( 680 665 "time", static_cast<double>(time)); 681 666 682 667 JS::RootedObject messageObj(cx, message.toObjectOrNull()); 683 668 SetGUIMessageProperty(cx, messageObj, args...); 684 669 m_ScriptInterface->FreezeObject(message, true); 685 m_GuiMessageQueue.push_back(JS:: Heap<JS::Value>(message));670 m_GuiMessageQueue.push_back(JS::PersistentRootedValue(cx, message)); 686 671 } 687 672 688 673 bool XmppClient::GuiPollHasPlayerListUpdate() 689 674 { 690 675 bool hasUpdate = m_PlayerMapUpdate; 691 676 m_PlayerMapUpdate = false; 692 677 return hasUpdate; 693 678 } 694 679 695 JS::Value XmppClient::GuiPollNewMessage(const ScriptInterface& scriptInterface)680 void XmppClient::GuiPollNewMessage(const ScriptInterface& scriptInterface, JS::MutableHandleValue value) 696 681 { 697 682 if (m_GuiMessageQueue.empty()) 698 return JS::UndefinedValue(); 683 { 684 value.setUndefined(); 685 return; 686 } 699 687 700 688 JSContext* cx = scriptInterface.GetContext(); 701 689 JSAutoRequest rq(cx); 702 690 703 691 JS::RootedValue message(cx, m_GuiMessageQueue.front()); … … JS::Value XmppClient::GuiPollNewMessage( 706 694 JS::RootedValue messageCopy(cx); 707 695 if (JS_StructuredClone(cx, message, &messageCopy, nullptr, nullptr)) 708 696 { 709 697 scriptInterface.SetProperty(messageCopy, "historic", true); 710 698 scriptInterface.FreezeObject(messageCopy, true); 711 m_HistoricGuiMessages.push_back(JS:: Heap<JS::Value>(messageCopy));699 m_HistoricGuiMessages.push_back(JS::PersistentRootedValue(cx, messageCopy)); 712 700 } 713 701 else 714 702 LOGERROR("Could not clone historic lobby GUI message!"); 715 703 716 return message;704 value.set(message); 717 705 } 718 706 719 707 JS::Value XmppClient::GuiPollHistoricMessages(const ScriptInterface& scriptInterface) 720 708 { 721 709 JSContext* cx = scriptInterface.GetContext(); … … JS::Value XmppClient::GuiPollHistoricMes 723 711 724 712 JS::RootedValue ret(cx); 725 713 ScriptInterface::CreateArray(cx, &ret); 726 714 727 715 int j = 0; 728 for (const JS::Heap<JS::Value>& message : m_HistoricGuiMessages) 729 scriptInterface.SetPropertyInt(ret, j++, message); 716 for (const JS::PersistentRootedValue& message : m_HistoricGuiMessages) 717 { 718 JS::RootedValue messageVal(cx, message); 719 scriptInterface.SetPropertyInt(ret, j++, messageVal); 720 } 730 721 731 722 return ret; 732 723 } 733 724 734 725 /** -
source/lobby/XmppClient.h
private: 61 61 public: 62 62 // Basic 63 63 XmppClient(const ScriptInterface* scriptInterface, const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize = 0, const bool regOpt = false); 64 64 virtual ~XmppClient(); 65 65 66 // JS::Heap is better for GC performance than JS::PersistentRooted67 static void Trace(JSTracer *trc, void *data)68 {69 static_cast<XmppClient*>(data)->TraceMember(trc);70 }71 72 void TraceMember(JSTracer *trc);73 74 66 // Network 75 67 void connect(); 76 68 void disconnect(); 77 69 bool isConnected(); 78 70 void recv(); … … protected: 148 140 /* Session Handler */ 149 141 virtual void handleSessionAction(gloox::Jingle::Action action, glooxwrapper::Jingle::Session& session, const glooxwrapper::Jingle::Session::Jingle& jingle); 150 142 virtual void handleSessionInitiation(glooxwrapper::Jingle::Session& session, const glooxwrapper::Jingle::Session::Jingle& jingle); 151 143 152 144 public: 153 JS::Value GuiPollNewMessage(const ScriptInterface& scriptInterface);145 void GuiPollNewMessage(const ScriptInterface& scriptInterface, JS::MutableHandleValue value); 154 146 JS::Value GuiPollHistoricMessages(const ScriptInterface& scriptInterface); 155 147 bool GuiPollHasPlayerListUpdate(); 156 148 void SendMUCMessage(const std::string& message); 157 149 158 150 protected: … … private: 186 178 /// Profile data 187 179 std::vector<const glooxwrapper::Tag*> m_Profile; 188 180 /// ScriptInterface to root the values 189 181 const ScriptInterface* m_ScriptInterface; 190 182 /// Queue of messages for the GUI 191 std::deque<JS:: Heap<JS::Value>> m_GuiMessageQueue;183 std::deque<JS::PersistentRootedValue > m_GuiMessageQueue; 192 184 /// Cache of all GUI messages received since the login 193 std::vector<JS:: Heap<JS::Value>> m_HistoricGuiMessages;185 std::vector<JS::PersistentRootedValue > m_HistoricGuiMessages; 194 186 /// Current room subject/topic. 195 187 std::wstring m_Subject; 196 188 }; 197 189 198 190 #endif // XMPPCLIENT_H -
source/lobby/scripting/JSInterface_Lobby.cpp
bool JSI_Lobby::LobbyGuiPollHasPlayerLis 246 246 JS::Value JSI_Lobby::LobbyGuiPollNewMessage(ScriptInterface::CxPrivate* pCxPrivate) 247 247 { 248 248 if (!g_XmppClient) 249 249 return JS::UndefinedValue(); 250 250 251 return g_XmppClient->GuiPollNewMessage(*(pCxPrivate->pScriptInterface)); 251 JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); 252 JSAutoRequest rq(cx); 253 254 JS::RootedValue message(cx); 255 g_XmppClient->GuiPollNewMessage(*(pCxPrivate->pScriptInterface), &message); 256 257 return message; 252 258 } 253 259 254 260 JS::Value JSI_Lobby::LobbyGuiPollHistoricMessages(ScriptInterface::CxPrivate* pCxPrivate) 255 261 { 256 262 if (!g_XmppClient)