diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 059abccdb8..b458cb8da6 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -83,7 +83,7 @@ void CMapReader::LoadMap(const VfsPath& pathname, JSRuntime* rt, JS::HandleValu m_PlayerID = playerID_; m_SkipEntities = skipEntities; m_StartingCameraTarget = INVALID_ENTITY; - m_ScriptSettings.set(rt, settings); + m_ScriptSettings.init(rt, settings); filename_xml = pathname.ChangeExtension(L".xml"); @@ -158,7 +158,7 @@ void CMapReader::LoadRandomMap(const CStrW& scriptFile, JSRuntime* rt, JS::Handl m_ScriptFile = scriptFile; pSimulation2 = pSimulation2_; pSimContext = pSimulation2 ? &pSimulation2->GetSimContext() : NULL; - m_ScriptSettings.set(rt, settings); + m_ScriptSettings.init(rt, settings); pTerrain = pTerrain_; pLightEnv = pLightEnv_; pGameView = pGameView_; @@ -1244,7 +1244,7 @@ int CMapReader::LoadRMSettings() { // copy random map settings over to sim ENSURE(pSimulation2); - pSimulation2->SetMapSettings(m_ScriptSettings.get()); + pSimulation2->SetMapSettings(m_ScriptSettings); return 0; } @@ -1265,7 +1265,7 @@ int CMapReader::GenerateMap() scriptPath = L"maps/random/"+m_ScriptFile; // Stringify settings to pass across threads - std::string scriptSettings = pSimulation2->GetScriptInterface().StringifyJSON(&m_ScriptSettings.get()); + std::string scriptSettings = pSimulation2->GetScriptInterface().StringifyJSON(&m_ScriptSettings); // Try to generate map m_MapGen->GenerateMap(scriptPath, scriptSettings); @@ -1294,7 +1294,7 @@ int CMapReader::GenerateMap() } else { - m_MapData.set(cx, data); + m_MapData.init(cx, data); } } else @@ -1325,16 +1325,16 @@ int CMapReader::ParseTerrain() throw PSERROR_Game_World_MapLoadFailed("Error parsing terrain data.\nCheck application log for details"); } u32 size; - GET_TERRAIN_PROPERTY(m_MapData.get(), size, size) + GET_TERRAIN_PROPERTY(m_MapData, size, size) m_PatchesPerSide = size / PATCH_SIZE; // flat heightmap of u16 data - GET_TERRAIN_PROPERTY(m_MapData.get(), height, m_Heightmap) + GET_TERRAIN_PROPERTY(m_MapData, height, m_Heightmap) // load textures std::vector textureNames; - GET_TERRAIN_PROPERTY(m_MapData.get(), textureNames, textureNames) + GET_TERRAIN_PROPERTY(m_MapData, textureNames, textureNames) num_terrain_tex = textureNames.size(); while (cur_terrain_tex < num_terrain_tex) @@ -1350,7 +1350,7 @@ int CMapReader::ParseTerrain() m_Tiles.resize(SQR(size)); JS::RootedValue tileData(cx); - GET_TERRAIN_PROPERTY(m_MapData.get(), tileData, &tileData) + GET_TERRAIN_PROPERTY(m_MapData, tileData, &tileData) // parse tile data object into flat arrays std::vector tileIndex; @@ -1396,7 +1396,7 @@ int CMapReader::ParseEntities() // parse entities from map data std::vector entities; - if (!pSimulation2->GetScriptInterface().GetProperty(m_MapData.get(), "entities", entities)) + if (!pSimulation2->GetScriptInterface().GetProperty(m_MapData, "entities", entities)) LOGWARNING("CMapReader::ParseEntities() failed to get 'entities' property"); CSimulation2& sim = *pSimulation2; @@ -1463,7 +1463,7 @@ int CMapReader::ParseEnvironment() LOGWARNING("CMapReader::ParseEnvironment() failed to get '%s' property", #prop); JS::RootedValue envObj(cx); - GET_ENVIRONMENT_PROPERTY(m_MapData.get(), Environment, &envObj) + GET_ENVIRONMENT_PROPERTY(m_MapData, Environment, &envObj) if (envObj.isUndefined()) { @@ -1568,7 +1568,7 @@ int CMapReader::ParseCamera() LOGWARNING("CMapReader::ParseCamera() failed to get '%s' property", #prop); JS::RootedValue cameraObj(cx); - GET_CAMERA_PROPERTY(m_MapData.get(), Camera, &cameraObj) + GET_CAMERA_PROPERTY(m_MapData, Camera, &cameraObj) if (!cameraObj.isUndefined()) { // If camera property exists, read values diff --git a/source/graphics/MapReader.h b/source/graphics/MapReader.h index 9b3bf5f5b6..d1d9b36ef2 100644 --- a/source/graphics/MapReader.h +++ b/source/graphics/MapReader.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -123,8 +123,8 @@ private: // random map data CStrW m_ScriptFile; - DefPersistentRooted m_ScriptSettings; - DefPersistentRooted m_MapData; + JS::PersistentRootedValue m_ScriptSettings; + JS::PersistentRootedValue m_MapData; CMapGenerator* m_MapGen; diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp index b94c564b5d..e0efbf8701 100644 --- a/source/gui/IGUIObject.cpp +++ b/source/gui/IGUIObject.cpp @@ -504,9 +504,9 @@ JSObject* IGUIObject::GetJSObject() // Cache the object when somebody first asks for it, because otherwise // we end up doing far too much object allocation. TODO: Would be nice to // not have these objects hang around forever using up memory, though. - if (m_JSObject.uninitialized()) + if (!m_JSObject.initialized()) { - m_JSObject.set(cx, m_pGUI->GetScriptInterface()->CreateCustomObject("GUIObject")); + m_JSObject.init(cx, m_pGUI->GetScriptInterface()->CreateCustomObject("GUIObject")); JS_SetPrivate(m_JSObject.get(), this); } return m_JSObject.get(); diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index 2b4dd727c9..a3f7520d74 100644 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2015 Wildfire Games. +/* Copyright (C) 2016 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -542,10 +542,10 @@ private: CGUI *m_pGUI; // Internal storage for registered script handlers. - std::map > m_ScriptHandlers; + std::map > m_ScriptHandlers; // Cached JSObject representing this GUI object - DefPersistentRooted m_JSObject; + JS::PersistentRootedObject m_JSObject; }; diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index c7ac20ef70..50e8d19798 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -372,7 +372,7 @@ void CNetServerWorker::Run() // To avoid the need for JS_SetContextThread, we create and use and destroy // the script interface entirely within this network thread m_ScriptInterface = new ScriptInterface("Engine", "Net server", ScriptInterface::CreateRuntime(g_ScriptRuntime)); - m_GameAttributes.set(m_ScriptInterface->GetJSRuntime(), JS::UndefinedValue()); + m_GameAttributes.init(m_ScriptInterface->GetJSRuntime(), JS::UndefinedValue()); while (true) { @@ -388,7 +388,6 @@ void CNetServerWorker::Run() } // Clear roots before deleting their context - m_GameAttributes.clear(); m_SavedCommands.clear(); SAFE_DELETE(m_ScriptInterface); @@ -682,7 +681,7 @@ void CNetServerWorker::OnUserJoin(CNetServerSession* session) m_HostGUID = session->GetGUID(); CGameSetupMessage gameSetupMessage(GetScriptInterface()); - gameSetupMessage.m_Data = m_GameAttributes.get(); + gameSetupMessage.m_Data = m_GameAttributes; session->SendMessage(&gameSetupMessage); CPlayerAssignmentMessage assignMessage; @@ -1036,7 +1035,7 @@ bool CNetServerWorker::OnInGame(void* context, CFsmEvent* event) JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); JS::RootedValue settings(cx); - scriptInterface.GetProperty(server.m_GameAttributes.get(), "settings", &settings); + scriptInterface.GetProperty(server.m_GameAttributes, "settings", &settings); if (scriptInterface.HasProperty(settings, "CheatsEnabled")) scriptInterface.GetProperty(settings, "CheatsEnabled", cheatsEnabled); @@ -1341,7 +1340,7 @@ void CNetServerWorker::StartGame() m_State = SERVER_STATE_LOADING; // Send the final setup state to all clients - UpdateGameAttributes(&m_GameAttributes.get()); + UpdateGameAttributes(&m_GameAttributes); // Remove players and observers that are not present when the game starts for (PlayerAssignmentMap::iterator it = m_PlayerAssignments.begin(); it != m_PlayerAssignments.end();) @@ -1358,13 +1357,13 @@ void CNetServerWorker::StartGame() void CNetServerWorker::UpdateGameAttributes(JS::MutableHandleValue attrs) { - m_GameAttributes.set(m_ScriptInterface->GetJSRuntime(), attrs); + m_GameAttributes = attrs; if (!m_Host) return; CGameSetupMessage gameSetupMessage(GetScriptInterface()); - gameSetupMessage.m_Data = m_GameAttributes.get(); + gameSetupMessage.m_Data = m_GameAttributes; Broadcast(&gameSetupMessage); } diff --git a/source/network/NetServer.h b/source/network/NetServer.h index 3e6ec1896d..043b763bf8 100644 --- a/source/network/NetServer.h +++ b/source/network/NetServer.h @@ -23,7 +23,7 @@ #include "lib/config2.h" #include "ps/ThreadUtil.h" -#include "scriptinterface/ScriptVal.h" +#include "scriptinterface/ScriptTypes.h" #include @@ -280,7 +280,7 @@ private: /** * Stores the most current game attributes. */ - DefPersistentRooted m_GameAttributes; + JS::PersistentRootedValue m_GameAttributes; int m_AutostartPlayers; diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index bd51cee47b..c52ac4f317 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -68,11 +68,7 @@ struct ScriptInterface_impl boost::rand48* m_rng; JS::PersistentRootedObject m_nativeScope; // native function scope object - // TODO: we need DefPersistentRooted to work around a problem with JS::PersistentRooted - // that is already solved in newer versions of SpiderMonkey (related to std::pair and - // and the copy constructor of PersistentRooted taking a non-const reference). - // Switch this to PersistentRooted when upgrading to a newer SpiderMonkey version than v31. - typedef std::map > ScriptValCache; + typedef std::map ScriptValCache; ScriptValCache m_ScriptValCache; }; @@ -440,7 +436,7 @@ ScriptInterface::CxPrivate* ScriptInterface::GetScriptInterfaceAndCBData(JSConte JS::Value ScriptInterface::GetCachedValue(CACHED_VAL valueIdentifier) { - std::map>::iterator it = m->m_ScriptValCache.find(valueIdentifier); + std::map::iterator it = m->m_ScriptValCache.find(valueIdentifier); ENSURE(it != m->m_ScriptValCache.end()); return it->second.get(); } @@ -466,9 +462,9 @@ bool ScriptInterface::LoadGlobalScripts() JS::RootedValue proto(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); if (JS_GetProperty(m->m_cx, global, "Vector2Dprototype", &proto)) - m->m_ScriptValCache[CACHE_VECTOR2DPROTO] = DefPersistentRooted(GetJSRuntime(), proto); + m->m_ScriptValCache[CACHE_VECTOR2DPROTO].init(GetJSRuntime(), proto); if (JS_GetProperty(m->m_cx, global, "Vector3Dprototype", &proto)) - m->m_ScriptValCache[CACHE_VECTOR3DPROTO] = DefPersistentRooted(GetJSRuntime(), proto); + m->m_ScriptValCache[CACHE_VECTOR3DPROTO].init(GetJSRuntime(), proto); return true; } @@ -548,13 +544,11 @@ void ScriptInterface::DefineCustomObjectType(JSClass *clasp, JSNative constructo if (obj == NULL) throw PSERROR_Scripting_DefineType_CreationFailed(); - CustomType type; + CustomType& type = m_CustomObjectTypes[typeName]; - type.m_Prototype = DefPersistentRooted(m->m_cx, obj); + type.m_Prototype.init(m->m_cx, obj); type.m_Class = clasp; type.m_Constructor = constructor; - - m_CustomObjectTypes[typeName] = std::move(type); } JSObject* ScriptInterface::CreateCustomObject(const std::string& typeName) const diff --git a/source/scriptinterface/ScriptInterface.h b/source/scriptinterface/ScriptInterface.h index 3e843e7a70..3d5889c2e1 100644 --- a/source/scriptinterface/ScriptInterface.h +++ b/source/scriptinterface/ScriptInterface.h @@ -24,7 +24,6 @@ #include "maths/Fixed.h" #include "ScriptTypes.h" -#include "ScriptVal.h" #include "ps/Errors.h" ERROR_GROUP(Scripting); @@ -414,7 +413,7 @@ private: m_Constructor = std::move(other.m_Constructor); } - DefPersistentRooted m_Prototype; + JS::PersistentRootedObject m_Prototype; JSClass* m_Class; JSNative m_Constructor; }; diff --git a/source/simulation2/system/ComponentManager.h b/source/simulation2/system/ComponentManager.h index 0ab9beb56f..26a518c21b 100644 --- a/source/simulation2/system/ComponentManager.h +++ b/source/simulation2/system/ComponentManager.h @@ -21,6 +21,7 @@ #include "Entity.h" #include "Components.h" #include "scriptinterface/ScriptInterface.h" +#include "scriptinterface/ScriptVal.h" #include "simulation2/helpers/Player.h" #include "ps/Filesystem.h"