Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_SCRIPT_CONTEXT 19 : #define INCLUDED_SCRIPT_CONTEXT 20 : 21 : #include <cstdint> 22 : #include <list> 23 : #include <memory> 24 : 25 : namespace JS { class Realm; } 26 : struct JSContext; 27 : 28 : // Those are minimal defaults. The runtime for the main game is larger and GCs upon a larger growth. 29 : constexpr int DEFAULT_CONTEXT_SIZE = 16 * 1024 * 1024; 30 : constexpr uint32_t DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER = 2 * 1024 * 1024; 31 : 32 : namespace Script 33 : { 34 : class JobQueue; 35 : } 36 : 37 : /** 38 : * Abstraction around a SpiderMonkey JSContext. 39 : * 40 : * A single Script::Context, with the associated context, 41 : * should only be used on a single thread. 42 : * 43 : * (One means to share data between threads and contexts is to create 44 : * a Script::StructuredClone.) 45 : */ 46 : 47 : namespace Script 48 : { 49 : 50 : class Context 51 : { 52 : public: 53 : /** 54 : * Returns a context, in which any number of ScriptInterfaces compartments can live. 55 : * Each context should only ever be used on a single thread. 56 : * @param parentContext Parent context from the parent thread, with which we share some thread-safe data 57 : * @param contextSize Maximum size in bytes of the new context 58 : * @param heapGrowthBytesGCTrigger Size in bytes of cumulated allocations after which a GC will be triggered 59 : */ 60 : Context(int contextSize = DEFAULT_CONTEXT_SIZE, 61 : uint32_t heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER); 62 : ~Context(); 63 : 64 : /** 65 : * MaybeIncrementalGC checks if running a GC is worth the time that will take. 66 : * The logic is custom as Spidermonkey tends to assume 'idle time' will exist, 67 : * which is a thing in websites but not really in 0 A.D. 68 : * This can have a few behaviours: 69 : * - doing nothing 70 : * - starting a new incremental GC 71 : * - running a GC slice 72 : * - finishing the incremental GC 73 : * For details, check the SM doc in e.g. GC.cpp and GCapi.cpp 74 : */ 75 : void MaybeIncrementalGC(); 76 : 77 : /** 78 : * Does a non-incremental, shrinking GC. 79 : * A shrinking GC dumps JIT code and tries to defragment memory. 80 : */ 81 : void ShrinkingGC(); 82 : 83 : /** 84 : * This is used to keep track of realms which should be prepared for a GC. 85 : */ 86 : void RegisterRealm(JS::Realm* realm); 87 : void UnRegisterRealm(JS::Realm* realm); 88 : 89 : /** 90 : * Runs the promise continuation. 91 : * On contexts where promises can be used this function has to be 92 : * called. 93 : * This function has to be called frequently. 94 : */ 95 : void RunJobs(); 96 : 97 : /** 98 : * GetGeneralJSContext returns the context without starting a GC request and without 99 : * entering any compartment. It should only be used in specific situations, such as 100 : * creating a new compartment, or when initializing a persistent rooted. 101 : * If you need the compartmented context of a Interface, you should create a 102 : * Request and use the context from that. 103 : */ 104 395 : JSContext* GetGeneralJSContext() const { return m_cx; } 105 : 106 : private: 107 : 108 : JSContext* m_cx; 109 : const std::unique_ptr<JobQueue> m_JobQueue; 110 : 111 : void PrepareZonesForIncrementalGC() const; 112 : std::list<JS::Realm*> m_Realms; 113 : 114 : int m_ContextSize; 115 : uint32_t m_HeapGrowthBytesGCTrigger; 116 : uint32_t m_LastGCBytes{0}; 117 : }; 118 : 119 : } 120 : 121 : // Using a global object for the context is a workaround until Simulation, AI, etc, 122 : // use their own threads and also their own contexts. 123 713 : extern thread_local std::shared_ptr<Script::Context> g_ScriptContext; 124 : 125 : #endif // INCLUDED_SCRIPT_CONTEXT