Index: ps/trunk/source/graphics/MapGenerator.cpp =================================================================== --- ps/trunk/source/graphics/MapGenerator.cpp +++ ps/trunk/source/graphics/MapGenerator.cpp @@ -397,7 +397,8 @@ JS::RootedValue returnValue(cx); - self->m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &returnValue, "height", heightmap, "textureNames", textureNames, Index: ps/trunk/source/graphics/MapReader.cpp =================================================================== --- ps/trunk/source/graphics/MapReader.cpp +++ ps/trunk/source/graphics/MapReader.cpp @@ -372,7 +372,7 @@ JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateObject(ret); + ScriptInterface::CreateObject(cx, ret); if (m_ScriptSettings.empty()) return; Index: ps/trunk/source/gui/IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/IGUIObject.cpp +++ ps/trunk/source/gui/IGUIObject.cpp @@ -407,7 +407,8 @@ const CPos& mousePos = m_pGUI.GetMousePos(); - m_pGUI.GetScriptInterface()->CreateObject( + ScriptInterface::CreateObject( + cx, &mouse, "x", mousePos.x, "y", mousePos.y, Index: ps/trunk/source/gui/MiniMap.cpp =================================================================== --- ps/trunk/source/gui/MiniMap.cpp +++ ps/trunk/source/gui/MiniMap.cpp @@ -245,7 +245,7 @@ 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); Index: ps/trunk/source/gui/scripting/GuiScriptConversions.cpp =================================================================== --- ps/trunk/source/gui/scripting/GuiScriptConversions.cpp +++ ps/trunk/source/gui/scripting/GuiScriptConversions.cpp @@ -157,7 +157,7 @@ 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<> 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<> 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, Index: ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp +++ ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -102,7 +102,7 @@ } 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]); Index: ps/trunk/source/lobby/XmppClient.cpp =================================================================== --- ps/trunk/source/lobby/XmppClient.cpp +++ ps/trunk/source/lobby/XmppClient.cpp @@ -539,14 +539,15 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ JSAutoRequest rq(cx); JS::RootedValue ret(cx); - scriptInterface.CreateArray(&ret); + ScriptInterface::CreateArray(cx, &ret); int j = 0; for (const JS::Heap& message : m_HistoricGuiMessages) Index: ps/trunk/source/network/NetClient.h =================================================================== --- ps/trunk/source/network/NetClient.h +++ ps/trunk/source/network/NetClient.h @@ -161,7 +161,7 @@ JSAutoRequest rq(cx); JS::RootedValue message(cx); - GetScriptInterface().CreateObject(&message, args...); + ScriptInterface::CreateObject(cx, &message, args...); m_GuiMessageQueue.push_back(JS::Heap(message)); } Index: ps/trunk/source/network/NetClient.cpp =================================================================== --- ps/trunk/source/network/NetClient.cpp +++ ps/trunk/source/network/NetClient.cpp @@ -277,15 +277,16 @@ 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); Index: ps/trunk/source/network/StunClient.cpp =================================================================== --- ps/trunk/source/network/StunClient.cpp +++ ps/trunk/source/network/StunClient.cpp @@ -394,7 +394,7 @@ 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; } Index: ps/trunk/source/network/tests/test_Net.h =================================================================== --- ps/trunk/source/network/tests/test_Net.h +++ ps/trunk/source/network/tests/test_Net.h @@ -151,7 +151,8 @@ CNetServer server; JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, "mapType", "scenario", "map", "maps/scenarios/Saharan Oases", @@ -184,7 +185,8 @@ { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command]\\n"); @@ -193,7 +195,8 @@ { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client2 test sim command]\\n"); @@ -228,7 +231,8 @@ CNetServer server; JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, "mapType", "scenario", "map", "maps/scenarios/Saharan Oases", @@ -265,7 +269,8 @@ { 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 @@ { 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 @@ { 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 @@ { JS::RootedValue cmd(cx); - client1.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cmd, "type", "debug-print", "message", "[>>> client1 test sim command 4]\\n"); Index: ps/trunk/source/network/tests/test_NetMessage.h =================================================================== --- ps/trunk/source/network/tests/test_NetMessage.h +++ ps/trunk/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 @@ 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); Index: ps/trunk/source/ps/GameSetup/GameSetup.cpp =================================================================== --- ps/trunk/source/ps/GameSetup/GameSetup.cpp +++ ps/trunk/source/ps/GameSetup/GameSetup.cpp @@ -520,18 +520,19 @@ 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 @@ 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 @@ 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 @@ // 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 @@ } 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 @@ 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 @@ 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 @@ 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 @@ 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); } } Index: ps/trunk/source/ps/GameSetup/HWDetect.cpp =================================================================== --- ps/trunk/source/ps/GameSetup/HWDetect.cpp +++ ps/trunk/source/ps/GameSetup/HWDetect.cpp @@ -76,7 +76,7 @@ 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 @@ 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 @@ JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - scriptInterface.CreateArray(ret); + ScriptInterface::CreateArray(cx, ret); for(size_t i = 0; ; i++) { @@ -114,7 +115,8 @@ 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 @@ // 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); Index: ps/trunk/source/ps/Mod.cpp =================================================================== --- ps/trunk/source/ps/Mod.cpp +++ ps/trunk/source/ps/Mod.cpp @@ -146,9 +146,10 @@ 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); Index: ps/trunk/source/ps/ProfileViewer.cpp =================================================================== --- ps/trunk/source/ps/ProfileViewer.cpp +++ ps/trunk/source/ps/ProfileViewer.cpp @@ -505,7 +505,8 @@ JSAutoRequest rq(cx); JS::RootedValue t(cx); - m_ScriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &t, "cols", DumpCols(table), "data", DumpRows(table)); @@ -531,14 +532,14 @@ 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); Index: ps/trunk/source/ps/Pyrogenesis.h =================================================================== --- ps/trunk/source/ps/Pyrogenesis.h +++ ps/trunk/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 @@ #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 Index: ps/trunk/source/ps/Pyrogenesis.cpp =================================================================== --- ps/trunk/source/ps/Pyrogenesis.cpp +++ ps/trunk/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. Index: ps/trunk/source/ps/Replay.cpp =================================================================== --- ps/trunk/source/ps/Replay.cpp +++ ps/trunk/source/ps/Replay.cpp @@ -68,7 +68,7 @@ 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); Index: ps/trunk/source/ps/SavedGame.cpp =================================================================== --- ps/trunk/source/ps/SavedGame.cpp +++ ps/trunk/source/ps/SavedGame.cpp @@ -83,9 +83,10 @@ 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 @@ JS::RootedValue cameraMetadata(cx); - simulation.GetScriptInterface().CreateObject( + ScriptInterface::CreateObject( + cx, &cameraMetadata, "PosX", cameraPosition.X, "PosY", cameraPosition.Y, @@ -232,7 +234,7 @@ JSAutoRequest rq(cx); JS::RootedValue games(cx); - scriptInterface.CreateArray(&games); + ScriptInterface::CreateArray(cx, &games); Status err; @@ -268,7 +270,8 @@ JS::RootedValue metadata(cx, loader.GetMetadata()); JS::RootedValue game(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &game, "id", pathnames[i].Basename(), "metadata", metadata); Index: ps/trunk/source/ps/VisualReplay.cpp =================================================================== --- ps/trunk/source/ps/VisualReplay.cpp +++ ps/trunk/source/ps/VisualReplay.cpp @@ -190,7 +190,8 @@ CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &replayData, "directory", directory.string(), "fileSize", static_cast(fileInfo.Size())); @@ -235,7 +236,7 @@ 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 @@ // 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 @@ 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"; Index: ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp +++ ps/trunk/source/ps/scripting/JSInterface_ModIo.cpp @@ -92,13 +92,13 @@ 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 @@ 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); Index: ps/trunk/source/ps/scripting/JSInterface_VFS.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_VFS.cpp +++ ps/trunk/source/ps/scripting/JSInterface_VFS.cpp @@ -169,7 +169,7 @@ 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; Index: ps/trunk/source/scriptinterface/ScriptConversions.cpp =================================================================== --- ps/trunk/source/scriptinterface/ScriptConversions.cpp +++ ps/trunk/source/scriptinterface/ScriptConversions.cpp @@ -314,9 +314,11 @@ 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) Index: ps/trunk/source/scriptinterface/ScriptInterface.h =================================================================== --- ps/trunk/source/scriptinterface/ScriptInterface.h +++ ps/trunk/source/scriptinterface/ScriptInterface.h @@ -132,16 +132,16 @@ /** * 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 @@ /** * 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 @@ /** * 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; Index: ps/trunk/source/scriptinterface/ScriptInterface.cpp =================================================================== --- ps/trunk/source/scriptinterface/ScriptInterface.cpp +++ ps/trunk/source/scriptinterface/ScriptInterface.cpp @@ -567,11 +567,11 @@ 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 @@ 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)); Index: ps/trunk/source/simulation2/Simulation2.cpp =================================================================== --- ps/trunk/source/simulation2/Simulation2.cpp +++ ps/trunk/source/simulation2/Simulation2.cpp @@ -983,7 +983,7 @@ // 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); Index: ps/trunk/source/simulation2/components/CCmpAIManager.cpp =================================================================== --- ps/trunk/source/simulation2/components/CCmpAIManager.cpp +++ ps/trunk/source/simulation2/components/CCmpAIManager.cpp @@ -145,7 +145,8 @@ // 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 @@ // 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 @@ ENSURE(m_HasLoadedEntityTemplates); JS::RootedValue settings(cx); - m_ScriptInterface->CreateObject( + ScriptInterface::CreateObject( + cx, &settings, "players", playersID, "templates", m_EntityTemplates); @@ -635,7 +637,7 @@ 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 @@ JSAutoRequest rq(cx); JS::RootedValue classesVal(cx); - scriptInterface.CreateObject(&classesVal); + ScriptInterface::CreateObject(cx, &classesVal); std::map classes; cmpPathfinder->GetPassabilityClasses(classes); Index: ps/trunk/source/simulation2/components/ICmpAIManager.cpp =================================================================== --- ps/trunk/source/simulation2/components/ICmpAIManager.cpp +++ ps/trunk/source/simulation2/components/ICmpAIManager.cpp @@ -64,7 +64,7 @@ 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); Index: ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp =================================================================== --- ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp +++ ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp @@ -114,29 +114,13 @@ 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 @@ } 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 @@ } 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) Index: ps/trunk/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp =================================================================== --- ps/trunk/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ ps/trunk/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -96,13 +96,14 @@ 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 @@ // 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 @@ JS::RootedValue attrs(cx); - scriptInterface.CreateObject( + ScriptInterface::CreateObject( + cx, &attrs, - "mapType", std::string("scenario"), - "map", std::wstring(mapBase)); + "mapType", "scenario", + "map", mapBase); StartGame(&attrs); }