Remove ScriptContext::CreateContext

This function nudges one into using it instead of the constructor. Even
though a `std::shared_ptr` isn't required.
This commit is contained in:
phosit 2025-06-04 08:02:00 +02:00
parent 5daae13525
commit 13453a3c7b
No known key found for this signature in database
GPG key ID: C9430B600671C268
7 changed files with 13 additions and 24 deletions

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -1320,9 +1320,7 @@ PS::Loader::Task CMapReader::RunMapGeneration(const CStrW& scriptFile)
const VfsPath scriptPath{scriptFile.empty() ? L"" :
static_cast<std::wstring>(RANDOM_MAP_PREFIX) + scriptFile};
const std::shared_ptr<ScriptContext> mapgenContext{ScriptContext::CreateContext(
MAP_GENERATION_CONTEXT_SIZE)};
ScriptContext mapgenContext{MAP_GENERATION_CONTEXT_SIZE};
ScriptInterface mapgenInterface{"Engine", "MapGenerator", mapgenContext,
[](const VfsPath& path){
// Only allow to load modules inside the maps folder.

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -415,7 +415,7 @@ void CNetServerWorker::Run()
g_Profiler2.RegisterCurrentThread("Net server");
// We create a new ScriptContext for this network thread, with a single ScriptInterface.
std::shared_ptr<ScriptContext> netServerContext = ScriptContext::CreateContext();
ScriptContext netServerContext;
m_ScriptInterface = new ScriptInterface("Engine", "Net server", netServerContext);
m_InitAttributes.init(m_ScriptInterface->GetGeneralJSContext(), JS::UndefinedValue());

View file

@ -555,7 +555,7 @@ bool Init(const CmdLineArgs& args, int flags)
// their own threads and also their own contexts.
const int contextSize = 384 * 1024 * 1024;
const int heapGrowthBytesGCTrigger = 12 * 1024 * 1024;
g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger);
g_ScriptContext = std::make_shared<ScriptContext>(contextSize, heapGrowthBytesGCTrigger);
// On the first Init (INIT_MODS), check for command-line arguments
// or use the default mods from the config and enable those.

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -202,7 +202,7 @@ void CReplayPlayer::Replay(const bool serializationtest, const int rejointesttur
const int contextSize = 384 * 1024 * 1024;
const int heapGrowthBytesGCTrigger = 12 * 1024 * 1024;
g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger);
g_ScriptContext = std::make_shared<ScriptContext>(contextSize, heapGrowthBytesGCTrigger);
std::vector<SimulationCommand> commands;
u32 turn = 0;

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -102,11 +102,6 @@ void GCSliceCallbackHook(JSContext*, JS::GCProgress progress, const JS::GCDescri
#endif
}
std::shared_ptr<ScriptContext> ScriptContext::CreateContext(int contextSize, uint32_t heapGrowthBytesGCTrigger)
{
return std::make_shared<ScriptContext>(contextSize, heapGrowthBytesGCTrigger);
}
ScriptContext::ScriptContext(int contextSize, uint32_t heapGrowthBytesGCTrigger):
m_JobQueue{std::make_unique<Script::JobQueue>()},
m_ContextSize{contextSize},

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -47,9 +47,6 @@ class JobQueue;
class ScriptContext
{
public:
ScriptContext(int contextSize, uint32_t heapGrowthBytesGCTrigger);
~ScriptContext();
/**
* Returns a context, in which any number of ScriptInterfaces compartments can live.
* Each context should only ever be used on a single thread.
@ -57,10 +54,9 @@ public:
* @param contextSize Maximum size in bytes of the new context
* @param heapGrowthBytesGCTrigger Size in bytes of cumulated allocations after which a GC will be triggered
*/
static std::shared_ptr<ScriptContext> CreateContext(
int contextSize = DEFAULT_CONTEXT_SIZE,
ScriptContext(int contextSize = DEFAULT_CONTEXT_SIZE,
uint32_t heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
~ScriptContext();
/**
* MaybeIncrementalGC checks if running a GC is worth the time that will take.

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -85,7 +85,7 @@ class MiscSetup : public CxxTest::GlobalFixture
g_Profiler2.Initialise();
m_ScriptEngine = new ScriptEngine;
g_ScriptContext = ScriptContext::CreateContext();
g_ScriptContext = std::make_shared<ScriptContext>();
taskManager.emplace();