1
0
forked from 0ad/0ad

Don't use std::shared_ptr in ScriptInterface

Differential Revision: https://code.wildfiregames.com/D5159
This was SVN commit r27945.
This commit is contained in:
phosit 2023-11-19 20:13:19 +00:00
parent e33aafc4e2
commit 1bccfef6fb
20 changed files with 96 additions and 86 deletions

View File

@ -380,7 +380,7 @@ void CGUIManager::TickObjects()
// We share the script context with everything else that runs in the same thread. // We share the script context with everything else that runs in the same thread.
// This call makes sure we trigger GC regularly even if the simulation is not running. // This call makes sure we trigger GC regularly even if the simulation is not running.
m_ScriptInterface->GetContext()->MaybeIncrementalGC(1.0f); m_ScriptInterface->GetContext().MaybeIncrementalGC(1.0f);
// Save an immutable copy so iterators aren't invalidated by tick handlers // Save an immutable copy so iterators aren't invalidated by tick handlers
PageStackType pageStack = m_PageStack; PageStackType pageStack = m_PageStack;

View File

@ -445,7 +445,7 @@ bool CNetServerWorker::RunStep()
// (Do as little work as possible while the mutex is held open, // (Do as little work as possible while the mutex is held open,
// to avoid performance problems and deadlocks.) // to avoid performance problems and deadlocks.)
m_ScriptInterface->GetContext()->MaybeIncrementalGC(0.5f); m_ScriptInterface->GetContext().MaybeIncrementalGC(0.5f);
ScriptRequest rq(m_ScriptInterface); ScriptRequest rq(m_ScriptInterface);

View File

@ -254,7 +254,7 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
Script::GetProperty(rq, attribs, "script", scriptFile); Script::GetProperty(rq, attribs, "script", scriptFile);
Script::GetProperty(rq, attribs, "settings", &settings); Script::GetProperty(rq, attribs, "settings", &settings);
m_World->RegisterInitRMS(scriptFile, *scriptInterface.GetContext(), settings, m_PlayerID); m_World->RegisterInitRMS(scriptFile, scriptInterface.GetContext(), settings, m_PlayerID);
} }
else else
{ {
@ -263,7 +263,7 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
Script::GetProperty(rq, attribs, "map", mapFile); Script::GetProperty(rq, attribs, "map", mapFile);
Script::GetProperty(rq, attribs, "settings", &settings); Script::GetProperty(rq, attribs, "settings", &settings);
m_World->RegisterInit(mapFile, *scriptInterface.GetContext(), settings, m_PlayerID); m_World->RegisterInit(mapFile, scriptInterface.GetContext(), settings, m_PlayerID);
} }
if (m_GameView) if (m_GameView)
LDR_Register([&waterManager = g_Renderer.GetSceneRenderer().GetWaterManager()](const double) LDR_Register([&waterManager = g_Renderer.GetSceneRenderer().GetWaterManager()](const double)

View File

@ -52,12 +52,12 @@
struct ScriptInterface_impl struct ScriptInterface_impl
{ {
ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context, JS::Compartment* compartment); ScriptInterface_impl(const char* nativeScopeName, ScriptContext& context, JS::Compartment* compartment);
~ScriptInterface_impl(); ~ScriptInterface_impl();
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted // Take care to keep this declaration before heap rooted members. Destructors of heap rooted
// members have to be called before the context destructor. // members have to be called before the context destructor.
std::shared_ptr<ScriptContext> m_context; ScriptContext& m_context;
friend ScriptRequest; friend ScriptRequest;
private: private:
@ -300,8 +300,10 @@ bool ScriptInterface::Math_random(JSContext* cx, uint argc, JS::Value* vp)
return true; return true;
} }
ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const std::shared_ptr<ScriptContext>& context, JS::Compartment* compartment) : ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, ScriptContext& context,
m_context(context), m_cx(context->GetGeneralJSContext()), m_glob(context->GetGeneralJSContext()), m_nativeScope(context->GetGeneralJSContext()) JS::Compartment* compartment) :
m_context(context), m_cx(context.GetGeneralJSContext()), m_glob(context.GetGeneralJSContext()),
m_nativeScope(context.GetGeneralJSContext())
{ {
JS::RealmCreationOptions creationOpt; JS::RealmCreationOptions creationOpt;
// Keep JIT code during non-shrinking GCs. This brings a quite big performance improvement. // Keep JIT code during non-shrinking GCs. This brings a quite big performance improvement.
@ -339,15 +341,15 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const st
ScriptFunction::Register<&ProfileStop>(m_cx, m_nativeScope, "ProfileStop"); ScriptFunction::Register<&ProfileStop>(m_cx, m_nativeScope, "ProfileStop");
ScriptFunction::Register<&ProfileAttribute>(m_cx, m_nativeScope, "ProfileAttribute"); ScriptFunction::Register<&ProfileAttribute>(m_cx, m_nativeScope, "ProfileAttribute");
m_context->RegisterRealm(JS::GetObjectRealmOrNull(m_glob)); m_context.RegisterRealm(JS::GetObjectRealmOrNull(m_glob));
} }
ScriptInterface_impl::~ScriptInterface_impl() ScriptInterface_impl::~ScriptInterface_impl()
{ {
m_context->UnRegisterRealm(JS::GetObjectRealmOrNull(m_glob)); m_context.UnRegisterRealm(JS::GetObjectRealmOrNull(m_glob));
} }
ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugName, const std::shared_ptr<ScriptContext>& context) : ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugName, ScriptContext& context) :
m(std::make_unique<ScriptInterface_impl>(nativeScopeName, context, nullptr)) m(std::make_unique<ScriptInterface_impl>(nativeScopeName, context, nullptr))
{ {
// Profiler stats table isn't thread-safe, so only enable this on the main thread // Profiler stats table isn't thread-safe, so only enable this on the main thread
@ -456,10 +458,10 @@ bool ScriptInterface::ReplaceNondeterministicRNG(boost::rand48& rng)
JSContext* ScriptInterface::GetGeneralJSContext() const JSContext* ScriptInterface::GetGeneralJSContext() const
{ {
return m->m_context->GetGeneralJSContext(); return m->m_context.GetGeneralJSContext();
} }
std::shared_ptr<ScriptContext> ScriptInterface::GetContext() const ScriptContext& ScriptInterface::GetContext() const
{ {
return m->m_context; return m->m_context;
} }

View File

@ -83,7 +83,15 @@ public:
* @param debugName Name of this interface for CScriptStats purposes. * @param debugName Name of this interface for CScriptStats purposes.
* @param context ScriptContext to use when initializing this interface. * @param context ScriptContext to use when initializing this interface.
*/ */
ScriptInterface(const char* nativeScopeName, const char* debugName, const std::shared_ptr<ScriptContext>& context); ScriptInterface(const char* nativeScopeName, const char* debugName, ScriptContext& context);
template<typename Context>
ScriptInterface(const char* nativeScopeName, const char* debugName, Context&& context) :
ScriptInterface(nativeScopeName, debugName, *context)
{
static_assert(std::is_lvalue_reference_v<Context>, "`ScriptInterface` doesn't take ownership "
"of the context.");
}
/** /**
* Alternate constructor. This creates the new Realm in the same Compartment as the neighbor scriptInterface. * Alternate constructor. This creates the new Realm in the same Compartment as the neighbor scriptInterface.
@ -137,7 +145,7 @@ public:
* ScriptInterface::Request and use the context from that. * ScriptInterface::Request and use the context from that.
*/ */
JSContext* GetGeneralJSContext() const; JSContext* GetGeneralJSContext() const;
std::shared_ptr<ScriptContext> GetContext() const; ScriptContext& GetContext() const;
/** /**
* Load global scripts that most script interfaces need, * Load global scripts that most script interfaces need,

View File

@ -54,7 +54,7 @@ class CSimulation2Impl
{ {
public: public:
CSimulation2Impl(CUnitManager* unitManager, std::shared_ptr<ScriptContext> cx, CTerrain* terrain) : CSimulation2Impl(CUnitManager* unitManager, std::shared_ptr<ScriptContext> cx, CTerrain* terrain) :
m_SimContext(), m_ComponentManager(m_SimContext, cx), m_SimContext(), m_ComponentManager(m_SimContext, *cx),
m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false),
m_MapSettings(cx->GetGeneralJSContext()), m_InitAttributes(cx->GetGeneralJSContext()) m_MapSettings(cx->GetGeneralJSContext()), m_InitAttributes(cx->GetGeneralJSContext())
{ {
@ -443,7 +443,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationComman
Script::GetProperty(rq, m_InitAttributes, "map", mapFile); Script::GetProperty(rq, m_InitAttributes, "map", mapFile);
VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp"); VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp");
mapReader->LoadMap(mapfilename, *scriptInterface.GetContext(), JS::UndefinedHandleValue, mapReader->LoadMap(mapfilename, scriptInterface.GetContext(), JS::UndefinedHandleValue,
m_SecondaryTerrain.get(), NULL, NULL, NULL, NULL, NULL, NULL, m_SecondaryTerrain.get(), NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, m_SecondaryContext.get(), INVALID_PLAYER, true); // throws exception on failure NULL, NULL, m_SecondaryContext.get(), INVALID_PLAYER, true); // throws exception on failure
} }
@ -502,9 +502,9 @@ void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationComman
// (TODO: we ought to schedule this for a frame where we're not // (TODO: we ought to schedule this for a frame where we're not
// running the sim update, to spread the load) // running the sim update, to spread the load)
if (m_TurnNumber % 500 == 0) if (m_TurnNumber % 500 == 0)
scriptInterface.GetContext()->ShrinkingGC(); scriptInterface.GetContext().ShrinkingGC();
else else
scriptInterface.GetContext()->MaybeIncrementalGC(0.0f); scriptInterface.GetContext().MaybeIncrementalGC(0.0f);
if (m_EnableOOSLog) if (m_EnableOOSLog)
DumpState(); DumpState();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -34,7 +34,7 @@ public:
void test_basic() void test_basic()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ICmpCinemaManager* cmp = test.Add<ICmpCinemaManager>(CID_CinemaManager, "", SYSTEM_ENTITY); ICmpCinemaManager* cmp = test.Add<ICmpCinemaManager>(CID_CinemaManager, "", SYSTEM_ENTITY);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -34,7 +34,7 @@ public:
void test_basic() void test_basic()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ScriptRequest rq(test.GetScriptInterface()); ScriptRequest rq(test.GetScriptInterface());
std::vector<SimulationCommand> empty; std::vector<SimulationCommand> empty;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -103,7 +103,7 @@ public:
ent3z = ent2z + ent2c + ent3c; // ensure it just touches the border of ent2 ent3z = ent2z + ent2c + ent3c; // ensure it just touches the border of ent2
ent3g = ent3; ent3g = ent3;
testHelper = new ComponentTestHelper(g_ScriptContext); testHelper = new ComponentTestHelper(*g_ScriptContext);
cmp = testHelper->Add<ICmpObstructionManager>(CID_ObstructionManager, "", SYSTEM_ENTITY); cmp = testHelper->Add<ICmpObstructionManager>(CID_ObstructionManager, "", SYSTEM_ENTITY);
cmp->SetBounds(fixed::FromInt(0), fixed::FromInt(0), fixed::FromInt(1000), fixed::FromInt(1000)); cmp->SetBounds(fixed::FromInt(0), fixed::FromInt(0), fixed::FromInt(1000), fixed::FromInt(1000));

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -145,7 +145,7 @@ public:
LDR_BeginRegistering(); LDR_BeginRegistering();
mapReader->LoadMap(L"maps/skirmishes/Median Oasis (2).pmp", mapReader->LoadMap(L"maps/skirmishes/Median Oasis (2).pmp",
*sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue, sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false); &sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering(); LDR_EndRegistering();
@ -259,7 +259,7 @@ public:
LDR_BeginRegistering(); LDR_BeginRegistering();
mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp", mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp",
*sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue, sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false); &sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering(); LDR_EndRegistering();
@ -316,7 +316,7 @@ public:
LDR_BeginRegistering(); LDR_BeginRegistering();
mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp", mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp",
*sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue, sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false); &sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering(); LDR_EndRegistering();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -65,7 +65,7 @@ public:
void test_basic() void test_basic()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
MockTerrain terrain; MockTerrain terrain;
test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain); test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain);
@ -137,7 +137,7 @@ public:
void test_water() void test_water()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
MockTerrain terrain; MockTerrain terrain;
test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain); test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain);
@ -210,7 +210,7 @@ public:
void test_serialize() void test_serialize()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
MockTerrain terrain; MockTerrain terrain;
test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain); test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -130,7 +130,7 @@ public:
void test_basic() void test_basic()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY); ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY);
@ -199,7 +199,7 @@ public:
void test_queries() void test_queries()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY); ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY);
@ -271,7 +271,7 @@ public:
void test_IsInTargetParabolicRange() void test_IsInTargetParabolicRange()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY); ICmpRangeManager* cmp = test.Add<ICmpRangeManager>(CID_RangeManager, "", SYSTEM_ENTITY);
const entity_id_t source = 200; const entity_id_t source = 200;
const entity_id_t target = 201; const entity_id_t target = 201;

View File

@ -160,7 +160,7 @@ public:
// Regression test for D5181 / fix for rP27673 issue // Regression test for D5181 / fix for rP27673 issue
void test_calculate_territories_uninitialised() void test_calculate_territories_uninitialised()
{ {
ComponentTestHelper test(g_ScriptContext); ComponentTestHelper test(*g_ScriptContext);
ICmpTerritoryManager* cmp = test.Add<ICmpTerritoryManager>(CID_TerritoryManager, "", SYSTEM_ENTITY); ICmpTerritoryManager* cmp = test.Add<ICmpTerritoryManager>(CID_TerritoryManager, "", SYSTEM_ENTITY);
MockPathfinderTerrMan pathfinder; MockPathfinderTerrMan pathfinder;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -90,7 +90,7 @@ public:
for (const VfsPath& path : paths) for (const VfsPath& path : paths)
{ {
CSimContext context; CSimContext context;
CComponentManager componentManager(context, g_ScriptContext, true); CComponentManager componentManager(context, *g_ScriptContext, true);
ScriptTestSetup(componentManager.GetScriptInterface()); ScriptTestSetup(componentManager.GetScriptInterface());
ScriptRequest rq(componentManager.GetScriptInterface()); ScriptRequest rq(componentManager.GetScriptInterface());
@ -117,7 +117,7 @@ public:
// Clean up previous scripts. // Clean up previous scripts.
g_ScriptContext->ShrinkingGC(); g_ScriptContext->ShrinkingGC();
CSimContext context; CSimContext context;
CComponentManager componentManager(context, g_ScriptContext, true); CComponentManager componentManager(context, *g_ScriptContext, true);
ScriptTestSetup(componentManager.GetScriptInterface()); ScriptTestSetup(componentManager.GetScriptInterface());

View File

@ -57,7 +57,7 @@ public:
JS::PersistentRootedValue msg; JS::PersistentRootedValue msg;
}; };
CComponentManager::CComponentManager(CSimContext& context, std::shared_ptr<ScriptContext> cx, bool skipScriptFunctions) : CComponentManager::CComponentManager(CSimContext& context, ScriptContext& cx, bool skipScriptFunctions) :
m_NextScriptComponentTypeId(CID__LastNative), m_NextScriptComponentTypeId(CID__LastNative),
m_ScriptInterface("Engine", "Simulation", cx), m_ScriptInterface("Engine", "Simulation", cx),
m_SimContext(context), m_CurrentlyHotloading(false) m_SimContext(context), m_CurrentlyHotloading(false)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -74,7 +74,7 @@ private:
}; };
public: public:
CComponentManager(CSimContext&, std::shared_ptr<ScriptContext> cx, bool skipScriptFunctions = false); CComponentManager(CSimContext&, ScriptContext& cx, bool skipScriptFunctions = false);
~CComponentManager(); ~CComponentManager();
void LoadComponentTypes(); void LoadComponentTypes();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -55,7 +55,7 @@ class ComponentTestHelper
bool m_isSystemEntityInit = false; bool m_isSystemEntityInit = false;
public: public:
ComponentTestHelper(std::shared_ptr<ScriptContext> scriptContext) : ComponentTestHelper(ScriptContext& scriptContext) :
m_Context(), m_ComponentManager(m_Context, scriptContext), m_Cmp(NULL) m_Context(), m_ComponentManager(m_Context, scriptContext), m_Cmp(NULL)
{ {
m_ComponentManager.LoadComponentTypes(); m_ComponentManager.LoadComponentTypes();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -54,7 +54,7 @@ public:
void test_LoadTemplate() void test_LoadTemplate()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;
@ -88,7 +88,7 @@ public:
void test_LoadTemplate_scriptcache() void test_LoadTemplate_scriptcache()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;
@ -148,7 +148,7 @@ public:
void test_LoadTemplate_errors() void test_LoadTemplate_errors()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;
@ -180,7 +180,7 @@ public:
void test_LoadTemplate_multiple() void test_LoadTemplate_multiple()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -59,14 +59,14 @@ public:
void test_Load() void test_Load()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
} }
void test_LookupCID() void test_LookupCID()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT_EQUALS(man.LookupCID("Test1A"), (int)CID_Test1A); TS_ASSERT_EQUALS(man.LookupCID("Test1A"), (int)CID_Test1A);
@ -76,7 +76,7 @@ public:
void test_AllocateNewEntity() void test_AllocateNewEntity()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)2); TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)2);
TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)3); TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)3);
@ -102,7 +102,7 @@ public:
double first; double first;
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.SetRNGSeed(123); man.SetRNGSeed(123);
if (!man.m_ScriptInterface.MathRandom(first)) if (!man.m_ScriptInterface.MathRandom(first))
@ -112,7 +112,7 @@ public:
double second; double second;
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.SetRNGSeed(123); man.SetRNGSeed(123);
if (!man.m_ScriptInterface.MathRandom(second)) if (!man.m_ScriptInterface.MathRandom(second))
@ -125,7 +125,7 @@ public:
void test_AddComponent_errors() void test_AddComponent_errors()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
CEntityHandle hnd1 = man.AllocateEntityHandle(1); CEntityHandle hnd1 = man.AllocateEntityHandle(1);
@ -148,7 +148,7 @@ public:
void test_QueryInterface() void test_QueryInterface()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;
@ -173,7 +173,7 @@ public:
void test_SendMessage() void test_SendMessage()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2, ent3 = 3, ent4 = 4; entity_id_t ent1 = 1, ent2 = 2, ent3 = 3, ent4 = 4;
@ -247,7 +247,7 @@ public:
void test_ParamNode() void test_ParamNode()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1, ent2 = 2; entity_id_t ent1 = 1, ent2 = 2;
@ -268,7 +268,7 @@ public:
void test_script_basic() void test_script_basic()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test.js"));
@ -312,7 +312,7 @@ public:
void test_script_helper_basic() void test_script_helper_basic()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-helper.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-helper.js"));
TS_ASSERT(man.LoadScript(L"simulation/helpers/test-helper.js")); TS_ASSERT(man.LoadScript(L"simulation/helpers/test-helper.js"));
@ -329,7 +329,7 @@ public:
void test_script_global_helper() void test_script_global_helper()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-global-helper.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-global-helper.js"));
@ -345,7 +345,7 @@ public:
void test_script_interface() void test_script_interface()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/interfaces/test-interface.js")); TS_ASSERT(man.LoadScript(L"simulation/components/interfaces/test-interface.js"));
TS_ASSERT(man.LoadScript(L"simulation/components/test-interface.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-interface.js"));
@ -363,7 +363,7 @@ public:
void test_script_errors() void test_script_errors()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
ScriptTestSetup(man.m_ScriptInterface); ScriptTestSetup(man.m_ScriptInterface);
man.LoadComponentTypes(); man.LoadComponentTypes();
@ -378,7 +378,7 @@ public:
void test_script_entityID() void test_script_entityID()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
ScriptTestSetup(man.m_ScriptInterface); ScriptTestSetup(man.m_ScriptInterface);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-entityid.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-entityid.js"));
@ -398,7 +398,7 @@ public:
void test_script_QueryInterface() void test_script_QueryInterface()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-query.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-query.js"));
@ -419,7 +419,7 @@ public:
void test_script_AddEntity() void test_script_AddEntity()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js"));
TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js"));
@ -452,7 +452,7 @@ public:
void test_script_AddLocalEntity() void test_script_AddLocalEntity()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js"));
TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js"));
@ -485,7 +485,7 @@ public:
void test_script_DestroyEntity() void test_script_DestroyEntity()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-destroyentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-destroyentity.js"));
@ -505,7 +505,7 @@ public:
void test_script_messages() void test_script_messages()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-msg.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-msg.js"));
@ -538,7 +538,7 @@ public:
void test_script_template() void test_script_template()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js"));
@ -560,7 +560,7 @@ public:
void test_script_template_readonly() void test_script_template_readonly()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js"));
@ -582,7 +582,7 @@ public:
void test_script_hotload() void test_script_hotload()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-hotload1.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-hotload1.js"));
@ -618,7 +618,7 @@ public:
void test_script_modding() void test_script_modding()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
CParamNode testParam; CParamNode testParam;
@ -643,7 +643,7 @@ public:
void test_serialization() void test_serialization()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 10, ent2 = 20, ent3 = FIRST_LOCAL_ENTITY; entity_id_t ent1 = 10, ent2 = 20, ent3 = FIRST_LOCAL_ENTITY;
@ -714,7 +714,7 @@ public:
); );
CSimContext context2; CSimContext context2;
CComponentManager man2(context2, g_ScriptContext); CComponentManager man2(context2, *g_ScriptContext);
man2.LoadComponentTypes(); man2.LoadComponentTypes();
TS_ASSERT(man2.QueryInterface(ent1, IID_Test1) == NULL); TS_ASSERT(man2.QueryInterface(ent1, IID_Test1) == NULL);
@ -734,7 +734,7 @@ public:
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
ScriptTestSetup(man.m_ScriptInterface); ScriptTestSetup(man.m_ScriptInterface);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js"));
@ -810,7 +810,7 @@ entities:\n\
TS_ASSERT(man.SerializeState(stateStream)); TS_ASSERT(man.SerializeState(stateStream));
CSimContext context2; CSimContext context2;
CComponentManager man2(context2, g_ScriptContext); CComponentManager man2(context2, *g_ScriptContext);
man2.LoadComponentTypes(); man2.LoadComponentTypes();
TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js")); TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js"));
@ -827,7 +827,7 @@ entities:\n\
void test_script_serialization_errors() void test_script_serialization_errors()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js"));
@ -847,7 +847,7 @@ entities:\n\
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js"));
man.InitSystemEntity(); man.InitSystemEntity();
@ -871,7 +871,7 @@ entities:\n\
TS_ASSERT(man.SerializeState(stateStream)); TS_ASSERT(man.SerializeState(stateStream));
CSimContext context2; CSimContext context2;
CComponentManager man2(context2, g_ScriptContext); CComponentManager man2(context2, *g_ScriptContext);
man2.LoadComponentTypes(); man2.LoadComponentTypes();
TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js")); TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js"));
@ -882,7 +882,7 @@ entities:\n\
void test_dynamic_subscription() void test_dynamic_subscription()
{ {
CSimContext context; CSimContext context;
CComponentManager man(context, g_ScriptContext); CComponentManager man(context, *g_ScriptContext);
man.LoadComponentTypes(); man.LoadComponentTypes();
entity_id_t ent1 = 1; entity_id_t ent1 = 1;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games. /* Copyright (C) 2023 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -895,7 +895,7 @@ public:
LDR_BeginRegistering(); LDR_BeginRegistering();
mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp", mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp",
*sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue, sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false); &sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering(); LDR_EndRegistering();