From f7206dc77df23e115fd980f8a51eb8fea155caef Mon Sep 17 00:00:00 2001 From: leper Date: Sat, 25 Nov 2017 06:49:58 +0000 Subject: [PATCH] More ScriptInterface const. Reviewed By: elexis; some comments by bb Differential Revision: https://code.wildfiregames.com/D863 This was SVN commit r20519. --- source/network/NetClient.cpp | 4 ++-- source/network/NetClient.h | 4 ++-- source/network/NetClientTurnManager.cpp | 2 +- source/network/NetServer.cpp | 6 +++--- source/network/NetServer.h | 4 ++-- source/ps/Game.cpp | 4 ++-- source/ps/TemplateLoader.h | 1 - source/scriptinterface/ScriptInterface.cpp | 7 +------ source/scriptinterface/ScriptInterface.h | 4 +--- source/simulation2/Simulation2.cpp | 4 ++-- source/simulation2/components/CCmpAIManager.cpp | 10 +++++----- source/simulation2/components/CCmpCommandQueue.cpp | 2 +- source/simulation2/system/ComponentTest.h | 2 +- .../tools/atlas/GameInterface/Handlers/MapHandlers.cpp | 8 ++++---- 14 files changed, 27 insertions(+), 35 deletions(-) diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index c547164f03..6f2572ddf6 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -269,7 +269,7 @@ std::string CNetClient::TestReadGuiMessages() return r; } -ScriptInterface& CNetClient::GetScriptInterface() +const ScriptInterface& CNetClient::GetScriptInterface() { return m_Game->GetSimulation2()->GetScriptInterface(); } @@ -328,7 +328,7 @@ void CNetClient::HandleDisconnect(u32 reason) SetCurrState(NCS_UNCONNECTED); } -void CNetClient::SendGameSetupMessage(JS::MutableHandleValue attrs, ScriptInterface& scriptInterface) +void CNetClient::SendGameSetupMessage(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface) { CGameSetupMessage gameSetup(scriptInterface); gameSetup.m_Data = attrs; diff --git a/source/network/NetClient.h b/source/network/NetClient.h index 1184434f0b..8180506c8b 100644 --- a/source/network/NetClient.h +++ b/source/network/NetClient.h @@ -159,7 +159,7 @@ public: * Get the script interface associated with this network client, * which is equivalent to the one used by the CGame in the constructor. */ - ScriptInterface& GetScriptInterface(); + const ScriptInterface& GetScriptInterface(); /** * Send a message to the server. @@ -189,7 +189,7 @@ public: */ void LoadFinished(); - void SendGameSetupMessage(JS::MutableHandleValue attrs, ScriptInterface& scriptInterface); + void SendGameSetupMessage(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface); void SendAssignPlayerMessage(const int playerID, const CStr& guid); diff --git a/source/network/NetClientTurnManager.cpp b/source/network/NetClientTurnManager.cpp index abf785b431..f43ae541fb 100644 --- a/source/network/NetClientTurnManager.cpp +++ b/source/network/NetClientTurnManager.cpp @@ -135,7 +135,7 @@ void CNetClientTurnManager::OnSyncError(u32 turn, const CStr& expectedHash, cons LOGERROR("Out-Of-Sync on turn %d\nPlayers: %s\nDumping state to %s", turn, playerNamesString.str().c_str(), oosdumpPath.string8()); - ScriptInterface& scriptInterface = m_NetClient.GetScriptInterface(); + const ScriptInterface& scriptInterface = m_NetClient.GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index c97613fab1..e7b3f343b8 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -845,7 +845,7 @@ void CNetServerWorker::SendPlayerAssignments() Broadcast(&message, { NSS_PREGAME, NSS_JOIN_SYNCING, NSS_INGAME }); } -ScriptInterface& CNetServerWorker::GetScriptInterface() +const ScriptInterface& CNetServerWorker::GetScriptInterface() { return *m_ScriptInterface; } @@ -1068,7 +1068,7 @@ bool CNetServerWorker::OnInGame(void* context, CFsmEvent* event) // Ignore messages sent by one player on behalf of another player // unless cheating is enabled bool cheatsEnabled = false; - ScriptInterface& scriptInterface = server.GetScriptInterface(); + const ScriptInterface& scriptInterface = server.GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); JS::RootedValue settings(cx); @@ -1502,7 +1502,7 @@ void CNetServer::StartGame() m_Worker->m_StartGameQueue.push_back(true); } -void CNetServer::UpdateGameAttributes(JS::MutableHandleValue attrs, ScriptInterface& scriptInterface) +void CNetServer::UpdateGameAttributes(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface) { // Pass the attributes as JSON, since that's the easiest safe // cross-thread way of passing script data diff --git a/source/network/NetServer.h b/source/network/NetServer.h index f4bdcfdca1..12bf94b8e2 100644 --- a/source/network/NetServer.h +++ b/source/network/NetServer.h @@ -126,7 +126,7 @@ public: * The changes will be asynchronously propagated to all clients. * @param attrs game attributes, in the script context of scriptInterface */ - void UpdateGameAttributes(JS::MutableHandleValue attrs, ScriptInterface& scriptInterface); + void UpdateGameAttributes(JS::MutableHandleValue attrs, const ScriptInterface& scriptInterface); /** * Set the turn length to a fixed value. @@ -221,7 +221,7 @@ private: /** * Get the script context used for game attributes. */ - ScriptInterface& GetScriptInterface(); + const ScriptInterface& GetScriptInterface(); /** * Set the turn length to a fixed value. diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index cd90a18f71..0502de3b84 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -173,7 +173,6 @@ bool CGame::StartVisualReplay(const OsPath& replayPath) debug_printf("Starting to replay %s\n", replayPath.string8().c_str()); m_IsVisualReplay = true; - ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); SetTurnManager(new CReplayTurnManager(*m_Simulation2, GetReplayLogger())); @@ -186,6 +185,7 @@ bool CGame::StartVisualReplay(const OsPath& replayPath) std::string line; std::getline(*m_ReplayStream, line); + const ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); @@ -203,7 +203,7 @@ bool CGame::StartVisualReplay(const OsPath& replayPath) **/ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& savedState) { - ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); + const ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); diff --git a/source/ps/TemplateLoader.h b/source/ps/TemplateLoader.h index 918351785c..f5419368ac 100644 --- a/source/ps/TemplateLoader.h +++ b/source/ps/TemplateLoader.h @@ -19,7 +19,6 @@ #define INCLUDED_TEMPLATELOADER #include "simulation2/system/ParamNode.h" -#include "scriptinterface/ScriptInterface.h" enum ETemplatesType { diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index 65c04bb514..3447cecc1a 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -596,17 +596,12 @@ bool ScriptInterface::CallFunction_(JS::HandleValue val, const char* name, JS::H return ok; } -JS::Value ScriptInterface::GetGlobalObject() +JS::Value ScriptInterface::GetGlobalObject() const { JSAutoRequest rq(m->m_cx); return JS::ObjectValue(*JS::CurrentGlobalOrNull(m->m_cx)); } -JSClass* ScriptInterface::GetGlobalClass() -{ - return &global_class; -} - bool ScriptInterface::SetGlobal_(const char* name, JS::HandleValue value, bool replace) { JSAutoRequest rq(m->m_cx); diff --git a/source/scriptinterface/ScriptInterface.h b/source/scriptinterface/ScriptInterface.h index 323a1531c7..b47f8fe3b0 100644 --- a/source/scriptinterface/ScriptInterface.h +++ b/source/scriptinterface/ScriptInterface.h @@ -134,9 +134,7 @@ public: JSObject* CreateCustomObject(const std::string & typeName) const; void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs); - JS::Value GetGlobalObject(); - - JSClass* GetGlobalClass(); + JS::Value GetGlobalObject() const; /** * Set the named property on the global object. diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 3a72a72eea..5f531c8221 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -359,7 +359,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vector& commands) { - ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); + const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); diff --git a/source/simulation2/system/ComponentTest.h b/source/simulation2/system/ComponentTest.h index b6e399648e..d05fbb02cf 100644 --- a/source/simulation2/system/ComponentTest.h +++ b/source/simulation2/system/ComponentTest.h @@ -59,7 +59,7 @@ public: m_ComponentManager.LoadComponentTypes(); } - ScriptInterface& GetScriptInterface() + const ScriptInterface& GetScriptInterface() { return m_ComponentManager.GetScriptInterface(); } diff --git a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp index b35181264a..645aa359c6 100644 --- a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2016 Wildfire Games. +/* Copyright (C) 2017 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -89,7 +89,7 @@ QUERYHANDLER(GenerateMap) InitGame(); // Random map - ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); + const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); @@ -116,7 +116,7 @@ QUERYHANDLER(GenerateMap) InitGame(); - ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); + const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); @@ -149,7 +149,7 @@ MESSAGEHANDLER(LoadMap) { InitGame(); - ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); + const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx);