From a84e2e57dfc443598efc20184c3169aea136f26d Mon Sep 17 00:00:00 2001 From: elexis Date: Fri, 13 Sep 2019 00:56:51 +0000 Subject: [PATCH] Use ScriptInterface::CreateObject for ToJSVal, and for ToJSVal >, ToJSVal > used by the AIManager obtaining the pathfinder grid. Make that function static, so that it can be used for these functions without slowly having to obtain the ScriptInterface instance using GetScriptInterfaceAndCBData just to get the JSContext again. Remove few redundant conversions for CreateObject arguments. Differential Revision: https://code.wildfiregames.com/D2128 Tested on: gcc 9.1.0, clang 8.0.1, Jenkins Tedious performance testing in: D2128, D2127 This was SVN commit r22894. --- source/graphics/MapGenerator.cpp | 3 +- source/graphics/MapReader.cpp | 2 +- source/gui/IGUIObject.cpp | 3 +- source/gui/MiniMap.cpp | 2 +- source/gui/scripting/GuiScriptConversions.cpp | 7 +- .../gui/scripting/JSInterface_IGUIObject.cpp | 2 +- source/lobby/XmppClient.cpp | 22 ++++--- source/network/NetClient.cpp | 7 +- source/network/NetClient.h | 2 +- source/network/StunClient.cpp | 2 +- source/network/tests/test_Net.h | 24 ++++--- source/network/tests/test_NetMessage.h | 4 +- source/ps/GameSetup/GameSetup.cpp | 39 ++++++----- source/ps/GameSetup/HWDetect.cpp | 12 ++-- source/ps/Mod.cpp | 5 +- source/ps/ProfileViewer.cpp | 7 +- source/ps/Pyrogenesis.cpp | 4 +- source/ps/Pyrogenesis.h | 4 +- source/ps/Replay.cpp | 2 +- source/ps/SavedGame.cpp | 13 ++-- source/ps/VisualReplay.cpp | 12 ++-- source/ps/scripting/JSInterface_ModIo.cpp | 6 +- source/ps/scripting/JSInterface_VFS.cpp | 2 +- source/scriptinterface/ScriptConversions.cpp | 2 + source/scriptinterface/ScriptInterface.cpp | 7 +- source/scriptinterface/ScriptInterface.h | 15 ++--- source/simulation2/Simulation2.cpp | 2 +- .../simulation2/components/CCmpAIManager.cpp | 12 ++-- .../simulation2/components/ICmpAIManager.cpp | 2 +- .../scripting/EngineScriptConversions.cpp | 64 ++++++------------- .../GameInterface/Handlers/MapHandlers.cpp | 32 ++++++---- 31 files changed, 161 insertions(+), 161 deletions(-) diff --git a/source/graphics/MapGenerator.cpp b/source/graphics/MapGenerator.cpp index 80914e7c29..d539f90e3e 100644 --- a/source/graphics/MapGenerator.cpp +++ b/source/graphics/MapGenerator.cpp @@ -397,7 +397,8 @@ JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPri JS::RootedValue returnValue(cx); - self->m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &returnValue, "height", heightmap, "textureNames", textureNames, diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index a60fbafde8..98bd89178b 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -372,7 +372,7 @@ void CMapSummaryReader::GetMapSettings(const ScriptInterface& scriptInterface, J JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateObject(ret); + ScriptInterface::CreateObject(cx, ret); if (m_ScriptSettings.empty()) return; diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp index 316db69aea..c0ef85c024 100644 --- a/source/gui/IGUIObject.cpp +++ b/source/gui/IGUIObject.cpp @@ -407,7 +407,8 @@ void IGUIObject::ScriptEvent(const CStr& Action) const CPos& mousePos = m_pGUI.GetMousePos(); - m_pGUI.GetScriptInterface()->CreateObject( + ScriptInterface::CreateObject( + cx, &mouse, "x", mousePos.x, "y", mousePos.y, diff --git a/source/gui/MiniMap.cpp b/source/gui/MiniMap.cpp index 28b833b09a..8e487cf6ba 100644 --- a/source/gui/MiniMap.cpp +++ b/source/gui/MiniMap.cpp @@ -245,7 +245,7 @@ void CMiniMap::FireWorldClickEvent(int UNUSED(button), int UNUSED(clicks)) GetMouseWorldCoordinates(x, z); JS::RootedValue coords(cx); - g_GUI->GetActiveGUI()->GetScriptInterface()->CreateObject(&coords, "x", x, "z", z); + ScriptInterface::CreateObject(cx, &coords, "x", x, "z", z); JS::AutoValueVector paramData(cx); paramData.append(coords); diff --git a/source/gui/scripting/GuiScriptConversions.cpp b/source/gui/scripting/GuiScriptConversions.cpp index 24314fa8a1..068780280c 100644 --- a/source/gui/scripting/GuiScriptConversions.cpp +++ b/source/gui/scripting/GuiScriptConversions.cpp @@ -157,7 +157,7 @@ template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleV template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CSize& val) { - ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject(ret, "width", val.cx, "height", val.cy); + CreateObject(cx, ret, "width", val.cx, "height", val.cy); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CSize& out) @@ -185,7 +185,7 @@ template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CPos& val) { - ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject(ret, "x", val.x, "y", val.y); + CreateObject(cx, ret, "x", val.x, "y", val.y); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CPos& out) @@ -213,7 +213,8 @@ template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CRect& val) { - ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->CreateObject( + CreateObject( + cx, ret, "left", val.left, "right", val.right, diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index c5a1967aaa..1162da1022 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -102,7 +102,7 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle } else if (propName == "children") { - pScriptInterface->CreateArray(vp); + ScriptInterface::CreateArray(cx, vp); for (size_t i = 0; i < e->m_Children.size(); ++i) pScriptInterface->SetPropertyInt(vp, i, e->m_Children[i]); diff --git a/source/lobby/XmppClient.cpp b/source/lobby/XmppClient.cpp index 097ab219e4..d48c6f986a 100644 --- a/source/lobby/XmppClient.cpp +++ b/source/lobby/XmppClient.cpp @@ -539,14 +539,15 @@ void XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::Mu JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); int j = 0; for (const std::pair& p : m_PlayerMap) { JS::RootedValue player(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &player, "name", p.first, "presence", p.second.m_Presence, @@ -567,7 +568,7 @@ void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::Muta JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); int j = 0; const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", @@ -577,7 +578,7 @@ void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::Muta for(const glooxwrapper::Tag* const& t : m_GameList) { JS::RootedValue game(cx); - scriptInterface.CreateObject(&game); + ScriptInterface::CreateObject(cx, &game); for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) scriptInterface.SetProperty(game, stats[i], t->findAttribute(stats[i])); @@ -596,7 +597,7 @@ void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::Mut JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); int j = 0; const char* attributes[] = { "name", "rank", "rating" }; @@ -604,7 +605,7 @@ void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::Mut for(const glooxwrapper::Tag* const& t : m_BoardList) { JS::RootedValue board(cx); - scriptInterface.CreateObject(&board); + ScriptInterface::CreateObject(cx, &board); for (size_t i = 0; i < ARRAY_SIZE(attributes); ++i) scriptInterface.SetProperty(board, attributes[i], t->findAttribute(attributes[i])); @@ -623,7 +624,7 @@ void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::Mutab JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); int j = 0; const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; @@ -631,7 +632,7 @@ void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::Mutab for (const glooxwrapper::Tag* const& t : m_Profile) { JS::RootedValue profile(cx); - scriptInterface.CreateObject(&profile); + ScriptInterface::CreateObject(cx, &profile); for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) scriptInterface.SetProperty(profile, stats[i], t->findAttribute(stats[i])); @@ -670,7 +671,8 @@ void XmppClient::CreateGUIMessage( JSContext* cx = m_ScriptInterface->GetContext(); JSAutoRequest rq(cx); JS::RootedValue message(cx); - m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &message, "type", type, "level", level, @@ -720,7 +722,7 @@ JS::Value XmppClient::GuiPollHistoricMessages(const ScriptInterface& scriptInter JSAutoRequest rq(cx); JS::RootedValue ret(cx); - scriptInterface.CreateArray(&ret); + ScriptInterface::CreateArray(cx, &ret); int j = 0; for (const JS::Heap& message : m_HistoricGuiMessages) diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index cef551a65a..118c356984 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -277,15 +277,16 @@ void CNetClient::PostPlayerAssignmentsToScript() JSAutoRequest rq(cx); JS::RootedValue newAssignments(cx); - GetScriptInterface().CreateObject(&newAssignments); + ScriptInterface::CreateObject(cx, &newAssignments); for (const std::pair& p : m_PlayerAssignments) { JS::RootedValue assignment(cx); - GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &assignment, - "name", CStrW(p.second.m_Name), + "name", p.second.m_Name, "player", p.second.m_PlayerID, "status", p.second.m_Status); diff --git a/source/network/NetClient.h b/source/network/NetClient.h index c56be2a739..b33e436dfd 100644 --- a/source/network/NetClient.h +++ b/source/network/NetClient.h @@ -161,7 +161,7 @@ public: JSAutoRequest rq(cx); JS::RootedValue message(cx); - GetScriptInterface().CreateObject(&message, args...); + ScriptInterface::CreateObject(cx, &message, args...); m_GuiMessageQueue.push_back(JS::Heap(message)); } diff --git a/source/network/StunClient.cpp b/source/network/StunClient.cpp index 6e8140727f..efd17cba6b 100644 --- a/source/network/StunClient.cpp +++ b/source/network/StunClient.cpp @@ -394,7 +394,7 @@ JS::Value StunClient::FindStunEndpointHost(const ScriptInterface& scriptInterfac JSAutoRequest rq(cx); JS::RootedValue stunEndpoint(cx); - scriptInterface.CreateObject(&stunEndpoint, "ip", CStr(ipStr), "port", m_Port); + ScriptInterface::CreateObject(cx, &stunEndpoint, "ip", ipStr, "port", m_Port); return stunEndpoint; } diff --git a/source/network/tests/test_Net.h b/source/network/tests/test_Net.h index 8593217472..05fd1a32dc 100644 --- a/source/network/tests/test_Net.h +++ b/source/network/tests/test_Net.h @@ -151,7 +151,8 @@ public: CNetServer server; JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, "mapType", "scenario", "map", "maps/scenarios/Saharan Oases", @@ -184,7 +185,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command]\\n"); @@ -193,7 +195,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client2 test sim command]\\n"); @@ -228,7 +231,8 @@ public: CNetServer server; JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, "mapType", "scenario", "map", "maps/scenarios/Saharan Oases", @@ -265,7 +269,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command 1]\\n"); @@ -281,7 +286,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command 2]\\n"); @@ -339,7 +345,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command 3]\\n"); @@ -355,7 +362,8 @@ public: { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command 4]\\n"); diff --git a/source/network/tests/test_NetMessage.h b/source/network/tests/test_NetMessage.h index 8e90411a75..3ea3887822 100644 --- a/source/network/tests/test_NetMessage.h +++ b/source/network/tests/test_NetMessage.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -31,7 +31,7 @@ public: JSAutoRequest rq(cx); JS::RootedValue val(cx); - script.CreateArray(&val); + ScriptInterface::CreateArray(cx, &val); script.SetPropertyInt(val, 0, 4); CSimulationMessage msg(script, 1, 2, 3, val); diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 85cac57c7e..7de7880ed7 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -520,18 +520,19 @@ void InitPsAutostart(bool networked, JS::HandleValue attrs) JSAutoRequest rq(cx); JS::RootedValue playerAssignments(cx); - scriptInterface.CreateObject(&playerAssignments); + ScriptInterface::CreateObject(cx, &playerAssignments); if (!networked) { JS::RootedValue localPlayer(cx); - scriptInterface.CreateObject(&localPlayer, "player", g_Game->GetPlayerID()); + ScriptInterface::CreateObject(cx, &localPlayer, "player", g_Game->GetPlayerID()); scriptInterface.SetProperty(playerAssignments, "local", localPlayer); } JS::RootedValue sessionInitData(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &sessionInitData, "attribs", attrs, "playerAssignments", playerAssignments); @@ -1103,8 +1104,7 @@ void InitGraphics(const CmdLineArgs& args, int flags, const std::vector& i JS::RootedValue data(cx); if (g_GUI) { - scriptInterface->CreateObject(&data); - scriptInterface->SetProperty(data, "isStartup", true); + ScriptInterface::CreateObject(cx, &data, "isStartup", true); if (!installedMods.empty()) scriptInterface->SetProperty(data, "installedMods", installedMods); } @@ -1266,9 +1266,9 @@ bool Autostart(const CmdLineArgs& args) JS::RootedValue settings(cx); JS::RootedValue playerData(cx); - scriptInterface.CreateObject(&attrs); - scriptInterface.CreateObject(&settings); - scriptInterface.CreateArray(&playerData); + ScriptInterface::CreateObject(cx, &attrs); + ScriptInterface::CreateObject(cx, &settings); + ScriptInterface::CreateArray(cx, &playerData); // The directory in front of the actual map name indicates which type // of map is being loaded. Drawback of this approach is the association @@ -1324,7 +1324,7 @@ bool Autostart(const CmdLineArgs& args) // We could load player_defaults.json here, but that would complicate the logic // even more and autostart is only intended for developers anyway - scriptInterface.CreateObject(&player, "Civ", std::string("athen")); + ScriptInterface::CreateObject(cx, &player, "Civ", "athen"); scriptInterface.SetPropertyInt(playerData, i, player); } @@ -1354,7 +1354,7 @@ bool Autostart(const CmdLineArgs& args) } scriptInterface.SetProperty(attrs, "mapType", mapType); - scriptInterface.SetProperty(attrs, "map", std::string("maps/" + autoStartName)); + scriptInterface.SetProperty(attrs, "map", "maps/" + autoStartName); scriptInterface.SetProperty(settings, "mapType", mapType); scriptInterface.SetProperty(settings, "CheatsEnabled", true); @@ -1408,7 +1408,7 @@ bool Autostart(const CmdLineArgs& args) LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); continue; } - scriptInterface.CreateObject(&player); + ScriptInterface::CreateObject(cx, &player); } int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; @@ -1439,13 +1439,12 @@ bool Autostart(const CmdLineArgs& args) LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); continue; } - scriptInterface.CreateObject(&player); + ScriptInterface::CreateObject(cx, &player); } - CStr name = aiArgs[i].AfterFirst(":"); - scriptInterface.SetProperty(player, "AI", std::string(name)); + scriptInterface.SetProperty(player, "AI", aiArgs[i].AfterFirst(":")); scriptInterface.SetProperty(player, "AIDiff", 3); - scriptInterface.SetProperty(player, "AIBehavior", std::string("balanced")); + scriptInterface.SetProperty(player, "AIBehavior", "balanced"); scriptInterface.SetPropertyInt(playerData, playerID-offset, player); } } @@ -1467,11 +1466,10 @@ bool Autostart(const CmdLineArgs& args) LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); continue; } - scriptInterface.CreateObject(&player); + ScriptInterface::CreateObject(cx, &player); } - int difficulty = civArgs[i].AfterFirst(":").ToInt(); - scriptInterface.SetProperty(player, "AIDiff", difficulty); + scriptInterface.SetProperty(player, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); scriptInterface.SetPropertyInt(playerData, playerID-offset, player); } } @@ -1495,11 +1493,10 @@ bool Autostart(const CmdLineArgs& args) LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); continue; } - scriptInterface.CreateObject(&player); + ScriptInterface::CreateObject(cx, &player); } - CStr name = civArgs[i].AfterFirst(":"); - scriptInterface.SetProperty(player, "Civ", std::string(name)); + scriptInterface.SetProperty(player, "Civ", civArgs[i].AfterFirst(":")); scriptInterface.SetPropertyInt(playerData, playerID-offset, player); } } diff --git a/source/ps/GameSetup/HWDetect.cpp b/source/ps/GameSetup/HWDetect.cpp index 36b34c069d..cca604b32a 100644 --- a/source/ps/GameSetup/HWDetect.cpp +++ b/source/ps/GameSetup/HWDetect.cpp @@ -76,7 +76,7 @@ void ConvertCaches(const ScriptInterface& scriptInterface, x86_x64::IdxCache idx JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); for (size_t idxLevel = 0; idxLevel < x86_x64::Cache::maxLevels; ++idxLevel) { @@ -86,7 +86,8 @@ void ConvertCaches(const ScriptInterface& scriptInterface, x86_x64::IdxCache idx JS::RootedValue cache(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &cache, "type", static_cast(pcache->m_Type), "level", static_cast(pcache->m_Level), @@ -104,7 +105,7 @@ void ConvertTLBs(const ScriptInterface& scriptInterface, JS::MutableHandleValue JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); for(size_t i = 0; ; i++) { @@ -114,7 +115,8 @@ void ConvertTLBs(const ScriptInterface& scriptInterface, JS::MutableHandleValue JS::RootedValue tlb(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &tlb, "type", static_cast(ptlb->m_Type), "level", static_cast(ptlb->m_Level), @@ -247,7 +249,7 @@ void RunHardwareDetection() // includes some fields that aren't directly useful for the hwdetect script) JS::RootedValue settings(cx); - scriptInterface.CreateObject(&settings); + ScriptInterface::CreateObject(cx, &settings); scriptInterface.SetProperty(settings, "os_unix", OS_UNIX); scriptInterface.SetProperty(settings, "os_bsd", OS_BSD); diff --git a/source/ps/Mod.cpp b/source/ps/Mod.cpp index f348f1fead..0bf73fabbd 100644 --- a/source/ps/Mod.cpp +++ b/source/ps/Mod.cpp @@ -146,9 +146,10 @@ JS::Value Mod::GetEngineInfo(const ScriptInterface& scriptInterface) JS::RootedValue mods(cx, Mod::GetLoadedModsWithVersions(scriptInterface)); JS::RootedValue metainfo(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &metainfo, - "engine_version", std::string(engine_version), + "engine_version", engine_version, "mods", mods); scriptInterface.FreezeObject(metainfo, true); diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index 31af14b585..c80dda3c6c 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -505,7 +505,8 @@ namespace JSAutoRequest rq(cx); JS::RootedValue t(cx); - m_ScriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &t, "cols", DumpCols(table), "data", DumpRows(table)); @@ -531,14 +532,14 @@ namespace JSAutoRequest rq(cx); JS::RootedValue data(cx); - m_ScriptInterface.CreateObject(&data); + ScriptInterface::CreateObject(cx, &data); const std::vector& columns = table->GetColumns(); for (size_t r = 0; r < table->GetNumberRows(); ++r) { JS::RootedValue row(cx); - m_ScriptInterface.CreateArray(&row); + ScriptInterface::CreateArray(cx, &row); m_ScriptInterface.SetProperty(data, table->GetCellText(r, 0).c_str(), row); diff --git a/source/ps/Pyrogenesis.cpp b/source/ps/Pyrogenesis.cpp index ec1e1ea295..274eb21fb2 100644 --- a/source/ps/Pyrogenesis.cpp +++ b/source/ps/Pyrogenesis.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -24,7 +24,7 @@ #include "lib/sysdep/sysdep.h" #include "lib/svn_revision.h" -const char engine_version[] = "0.0.24"; +const char* engine_version = "0.0.24"; // convert contents of file from char to wchar_t and // append to file. diff --git a/source/ps/Pyrogenesis.h b/source/ps/Pyrogenesis.h index 01aa5e4cd7..4fa01634e5 100644 --- a/source/ps/Pyrogenesis.h +++ b/source/ps/Pyrogenesis.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -26,7 +26,7 @@ Standard declarations which are included in all projects. #include "lib/os_path.h" -extern const char engine_version[]; +extern const char* engine_version; extern void psBundleLogs(FILE* f); // set during InitVfs extern void psSetLogDir(const OsPath& logDir); // set during InitVfs diff --git a/source/ps/Replay.cpp b/source/ps/Replay.cpp index ac3b1ded19..51b5cc7cea 100644 --- a/source/ps/Replay.cpp +++ b/source/ps/Replay.cpp @@ -68,7 +68,7 @@ void CReplayLogger::StartGame(JS::MutableHandleValue attribs) m_ScriptInterface.SetProperty(attribs, "timestamp", (double)std::time(nullptr)); // Add engine version and currently loaded mods for sanity checks when replaying - m_ScriptInterface.SetProperty(attribs, "engine_version", CStr(engine_version)); + m_ScriptInterface.SetProperty(attribs, "engine_version", engine_version); JS::RootedValue mods(cx, Mod::GetLoadedModsWithVersions(m_ScriptInterface)); m_ScriptInterface.SetProperty(attribs, "mods", mods); diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index f8524df20d..b1b429c0ba 100644 --- a/source/ps/SavedGame.cpp +++ b/source/ps/SavedGame.cpp @@ -83,9 +83,10 @@ Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation JS::RootedValue metadata(cx); - simulation.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &metadata, - "engine_version", std::string(engine_version), + "engine_version", engine_version, "time", static_cast(now), "playerID", g_Game->GetPlayerID(), "mods", mods, @@ -100,7 +101,8 @@ Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation JS::RootedValue cameraMetadata(cx); - simulation.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cameraMetadata, "PosX", cameraPosition.X, "PosY", cameraPosition.Y, @@ -232,7 +234,7 @@ JS::Value SavedGames::GetSavedGames(const ScriptInterface& scriptInterface) JSAutoRequest rq(cx); JS::RootedValue games(cx); - scriptInterface.CreateArray(&games); + ScriptInterface::CreateArray(cx, &games); Status err; @@ -268,7 +270,8 @@ JS::Value SavedGames::GetSavedGames(const ScriptInterface& scriptInterface) JS::RootedValue metadata(cx, loader.GetMetadata()); JS::RootedValue game(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &game, "id", pathnames[i].Basename(), "metadata", metadata); diff --git a/source/ps/VisualReplay.cpp b/source/ps/VisualReplay.cpp index fcdb4e00aa..2b58b46443 100644 --- a/source/ps/VisualReplay.cpp +++ b/source/ps/VisualReplay.cpp @@ -190,7 +190,8 @@ JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptIn CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &replayData, "directory", directory.string(), "fileSize", static_cast(fileInfo.Size())); @@ -235,7 +236,7 @@ JS::Value VisualReplay::GetReplays(const ScriptInterface& scriptInterface, bool JS::RootedObject replays(cx, ReloadReplayCache(scriptInterface, compareFiles)); // Only take entries with data JS::RootedValue replaysWithoutNullEntries(cx); - scriptInterface.CreateArray(&replaysWithoutNullEntries); + ScriptInterface::CreateArray(cx, &replaysWithoutNullEntries); u32 replaysLength = 0; JS_GetArrayLength(cx, replays, &replaysLength); @@ -405,11 +406,12 @@ JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, c // Return the actual data JS::RootedValue replayData(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &replayData, "directory", directory.string(), "fileSize", static_cast(fileSize), - "duration", static_cast(duration)); + "duration", duration); scriptInterface.SetProperty(replayData, "attribs", attribs); @@ -431,7 +433,7 @@ JS::Value VisualReplay::GetReplayAttributes(ScriptInterface::CxPrivate* pCxPriva JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); JSAutoRequest rq(cx); JS::RootedValue attribs(cx); - pCxPrivate->pScriptInterface->CreateObject(&attribs); + ScriptInterface::CreateObject(cx, &attribs); // Return empty object if file doesn't exist const OsPath replayFile = GetDirectoryPath() / directoryName / L"commands.txt"; diff --git a/source/ps/scripting/JSInterface_ModIo.cpp b/source/ps/scripting/JSInterface_ModIo.cpp index de1899d3ab..52f150875a 100644 --- a/source/ps/scripting/JSInterface_ModIo.cpp +++ b/source/ps/scripting/JSInterface_ModIo.cpp @@ -92,13 +92,13 @@ JS::Value JSI_ModIo::GetMods(ScriptInterface::CxPrivate* pCxPrivate) const std::vector& availableMods = g_ModIo->GetMods(); JS::RootedValue mods(cx); - scriptInterface->CreateArray(&mods, availableMods.size()); + ScriptInterface::CreateArray(cx, &mods, availableMods.size()); u32 i = 0; for (const ModIoModData& mod : availableMods) { JS::RootedValue m(cx); - scriptInterface->CreateObject(&m); + ScriptInterface::CreateObject(cx, &m); for (const std::pair& prop : mod.properties) scriptInterface->SetProperty(m, prop.first.c_str(), prop.second, true); @@ -139,7 +139,7 @@ JS::Value JSI_ModIo::GetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate) const DownloadProgressData& progress = g_ModIo->GetDownloadProgress(); JS::RootedValue progressData(cx); - scriptInterface->CreateObject(&progressData); + ScriptInterface::CreateObject(cx, &progressData); scriptInterface->SetProperty(progressData, "status", statusStrings.at(progress.status), true); scriptInterface->SetProperty(progressData, "progress", progress.progress, true); scriptInterface->SetProperty(progressData, "error", progress.error, true); diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index b56626b651..71dd1c777b 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -169,7 +169,7 @@ JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const s std::stringstream ss(contents); JS::RootedValue line_array(cx); - scriptInterface.CreateArray(&line_array); + ScriptInterface::CreateArray(cx, &line_array); std::string line; int cur_line = 0; diff --git a/source/scriptinterface/ScriptConversions.cpp b/source/scriptinterface/ScriptConversions.cpp index 5876d169e3..67cf8ea314 100644 --- a/source/scriptinterface/ScriptConversions.cpp +++ b/source/scriptinterface/ScriptConversions.cpp @@ -314,9 +314,11 @@ TOJSVAL_CHAR(17) TOJSVAL_CHAR(18) TOJSVAL_CHAR(19) TOJSVAL_CHAR(20) +TOJSVAL_CHAR(24) TOJSVAL_CHAR(29) TOJSVAL_CHAR(33) TOJSVAL_CHAR(35) +TOJSVAL_CHAR(256) #undef TOJSVAL_CHAR template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CStrW& val) diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index d494a0d4e9..d7ac77666c 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -567,11 +567,11 @@ bool ScriptInterface::CallFunction_(JS::HandleValue val, const char* name, JS::H return ok; } -bool ScriptInterface::CreateObject_(JS::MutableHandleObject object) const +bool ScriptInterface::CreateObject_(JSContext* cx, JS::MutableHandleObject object) { // JSAutoRequest is the responsibility of the caller - object.set(JS_NewPlainObject(GetContext())); + object.set(JS_NewPlainObject(cx)); if (!object) throw PSERROR_Scripting_CreateObjectFailed(); @@ -579,9 +579,8 @@ bool ScriptInterface::CreateObject_(JS::MutableHandleObject object) const return true; } -void ScriptInterface::CreateArray(JS::MutableHandleValue objectValue, size_t length) const +void ScriptInterface::CreateArray(JSContext* cx, JS::MutableHandleValue objectValue, size_t length) { - JSContext* cx = GetContext(); JSAutoRequest rq(cx); objectValue.setObjectOrNull(JS_NewArrayObject(cx, length)); diff --git a/source/scriptinterface/ScriptInterface.h b/source/scriptinterface/ScriptInterface.h index 3924d10a76..afb91f2b93 100644 --- a/source/scriptinterface/ScriptInterface.h +++ b/source/scriptinterface/ScriptInterface.h @@ -132,16 +132,16 @@ public: /** * Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties. + * This is static so that callers like ToJSVal can use it with the JSContext directly instead of having to obtain the instance using GetScriptInterfaceAndCBData. * Can throw an exception. */ template - bool CreateObject(JS::MutableHandleValue objectValue, Args const&... args) const + static bool CreateObject(JSContext* cx, JS::MutableHandleValue objectValue, Args const&... args) { - JSContext* cx = GetContext(); JSAutoRequest rq(cx); JS::RootedObject obj(cx); - if (!CreateObject_(&obj, args...)) + if (!CreateObject_(cx, &obj, args...)) return false; objectValue.setObject(*obj); @@ -151,7 +151,7 @@ public: /** * Sets the given value to a new JS object or Null Value in case of out-of-memory. */ - void CreateArray(JS::MutableHandleValue objectValue, size_t length = 0) const; + static void CreateArray(JSContext* cx, JS::MutableHandleValue objectValue, size_t length = 0); JS::Value GetGlobalObject() const; @@ -402,17 +402,16 @@ private: /** * Careful, the CreateObject_ helpers avoid creation of the JSAutoRequest! */ - bool CreateObject_(JS::MutableHandleObject obj) const; + static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj); template - bool CreateObject_(JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args) const + static bool CreateObject_(JSContext* cx, JS::MutableHandleObject obj, const char* propertyName, const T& propertyValue, Args const&... args) { // JSAutoRequest is the responsibility of the caller - JSContext* cx = GetContext(); JS::RootedValue val(cx); AssignOrToJSVal(cx, &val, propertyValue); - return CreateObject_(obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE); + return CreateObject_(cx, obj, args...) && JS_DefineProperty(cx, obj, propertyName, val, JSPROP_ENUMERATE); } bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const; diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 174cfdbb62..14ea413749 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -983,7 +983,7 @@ std::string CSimulation2::GetAIData() // Build single JSON string with array of AI data JS::RootedValue ais(cx); - if (!scriptInterface.CreateObject(&ais, "AIData", aiData)) + if (!ScriptInterface::CreateObject(cx, &ais, "AIData", aiData)) return std::string(); return scriptInterface.StringifyJSON(&ais); diff --git a/source/simulation2/components/CCmpAIManager.cpp b/source/simulation2/components/CCmpAIManager.cpp index 549d143060..996c5c5eb1 100644 --- a/source/simulation2/components/CCmpAIManager.cpp +++ b/source/simulation2/components/CCmpAIManager.cpp @@ -145,7 +145,8 @@ private: // Set up the data to pass as the constructor argument JS::RootedValue settings(cx); - m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &settings, "player", m_Player, "difficulty", m_Difficulty, @@ -442,7 +443,7 @@ public: // Set up the data to pass as the constructor argument JS::RootedValue playersID(cx); - m_ScriptInterface->CreateObject(&playersID); + ScriptInterface::CreateObject(cx, &playersID); for (size_t i = 0; i < m_Players.size(); ++i) { @@ -454,7 +455,8 @@ public: ENSURE(m_HasLoadedEntityTemplates); JS::RootedValue settings(cx); - m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &settings, "players", playersID, "templates", m_EntityTemplates); @@ -635,7 +637,7 @@ public: m_HasLoadedEntityTemplates = true; - m_ScriptInterface->CreateObject(&m_EntityTemplates); + ScriptInterface::CreateObject(cx, &m_EntityTemplates); JS::RootedValue val(cx); for (size_t i = 0; i < templates.size(); ++i) @@ -1188,7 +1190,7 @@ private: JSAutoRequest rq(cx); JS::RootedValue classesVal(cx); - scriptInterface.CreateObject(&classesVal); + ScriptInterface::CreateObject(cx, &classesVal); std::map classes; cmpPathfinder->GetPassabilityClasses(classes); diff --git a/source/simulation2/components/ICmpAIManager.cpp b/source/simulation2/components/ICmpAIManager.cpp index dd03e4ed1b..521451b334 100644 --- a/source/simulation2/components/ICmpAIManager.cpp +++ b/source/simulation2/components/ICmpAIManager.cpp @@ -64,7 +64,7 @@ public: std::wstring dirname = GetWstringFromWpath(*it); JS::RootedValue ai(cx); - self->m_ScriptInterface.CreateObject(&ai); + ScriptInterface::CreateObject(cx, &ai); JS::RootedValue data(cx); self->m_ScriptInterface.ReadJSONFile(pathname, &data); diff --git a/source/simulation2/scripting/EngineScriptConversions.cpp b/source/simulation2/scripting/EngineScriptConversions.cpp index 89ae84eaf9..352e9ff6e8 100644 --- a/source/simulation2/scripting/EngineScriptConversions.cpp +++ b/source/simulation2/scripting/EngineScriptConversions.cpp @@ -114,29 +114,13 @@ template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValu template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, CColor const& val) { - JSAutoRequest rq(cx); - JS::RootedObject obj(cx, JS_NewPlainObject(cx)); - if (!obj) - { - ret.setUndefined(); - return; - } - - JS::RootedValue r(cx); - JS::RootedValue g(cx); - JS::RootedValue b(cx); - JS::RootedValue a(cx); - ToJSVal(cx, &r, val.r); - ToJSVal(cx, &g, val.g); - ToJSVal(cx, &b, val.b); - ToJSVal(cx, &a, val.a); - - JS_SetProperty(cx, obj, "r", r); - JS_SetProperty(cx, obj, "g", g); - JS_SetProperty(cx, obj, "b", b); - JS_SetProperty(cx, obj, "a", a); - - ret.setObject(*obj); + CreateObject( + cx, + ret, + "r", val.r, + "g", val.g, + "b", val.b, + "a", val.a); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, fixed& out) @@ -246,17 +230,12 @@ template<> void ScriptInterface::ToJSVal >(JSContext* cx, JS::MutableHa } JS::RootedValue data(cx, JS::ObjectValue(*objArr)); - JS::RootedValue w(cx); - JS::RootedValue h(cx); - ScriptInterface::ToJSVal(cx, &w, val.m_W); - ScriptInterface::ToJSVal(cx, &h, val.m_H); - - JS::RootedObject obj(cx, JS_NewPlainObject(cx)); - JS_SetProperty(cx, obj, "width", w); - JS_SetProperty(cx, obj, "height", h); - JS_SetProperty(cx, obj, "data", data); - - ret.setObject(*obj); + CreateObject( + cx, + ret, + "width", val.m_W, + "height", val.m_H, + "data", data); } template<> void ScriptInterface::ToJSVal >(JSContext* cx, JS::MutableHandleValue ret, const Grid& val) @@ -273,17 +252,12 @@ template<> void ScriptInterface::ToJSVal >(JSContext* cx, JS::MutableH } JS::RootedValue data(cx, JS::ObjectValue(*objArr)); - JS::RootedValue w(cx); - JS::RootedValue h(cx); - ScriptInterface::ToJSVal(cx, &w, val.m_W); - ScriptInterface::ToJSVal(cx, &h, val.m_H); - - JS::RootedObject obj(cx, JS_NewPlainObject(cx)); - JS_SetProperty(cx, obj, "width", w); - JS_SetProperty(cx, obj, "height", h); - JS_SetProperty(cx, obj, "data", data); - - ret.setObject(*obj); + CreateObject( + cx, + ret, + "width", val.m_W, + "height", val.m_H, + "data", data); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, TNSpline& out) diff --git a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp index 21393d2edd..668059a7ae 100644 --- a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -96,13 +96,14 @@ QUERYHANDLER(GenerateMap) JS::RootedValue settings(cx); scriptInterface.ParseJSON(*msg->settings, &settings); - scriptInterface.SetProperty(settings, "mapType", std::string("random")); + scriptInterface.SetProperty(settings, "mapType", "random"); JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, - "mapType", std::string("random"), - "script", std::wstring(*msg->filename), + "mapType", "random", + "script", *msg->filename, "settings", settings); StartGame(&attrs); @@ -124,26 +125,28 @@ QUERYHANDLER(GenerateMap) // Set up 8-element array of empty objects to satisfy init JS::RootedValue playerData(cx); - scriptInterface.CreateArray(&playerData); + ScriptInterface::CreateArray(cx, &playerData); for (int i = 0; i < 8; ++i) { JS::RootedValue player(cx); - scriptInterface.CreateObject(&player); + ScriptInterface::CreateObject(cx, &player); scriptInterface.SetPropertyInt(playerData, i, player); } JS::RootedValue settings(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &settings, - "mapType", std::string("scenario"), + "mapType", "scenario", "PlayerData", playerData); JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, - "mapType", std::string("scenario"), - "map", std::wstring(L"maps/scenarios/_default"), + "mapType", "scenario", + "map", "maps/scenarios/_default", "settings", settings); StartGame(&attrs); @@ -166,10 +169,11 @@ MESSAGEHANDLER(LoadMap) JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, - "mapType", std::string("scenario"), - "map", std::wstring(mapBase)); + "mapType", "scenario", + "map", mapBase); StartGame(&attrs); }