LCOV - code coverage report
Current view: top level - source/scriptinterface - ScriptContext.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 1 100.0 %
Date: 2023-01-19 00:18:29 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2023 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_SCRIPTCONTEXT
      19             : #define INCLUDED_SCRIPTCONTEXT
      20             : 
      21             : #include "ScriptTypes.h"
      22             : #include "ScriptExtraHeaders.h"
      23             : 
      24             : #include <list>
      25             : 
      26             : // Those are minimal defaults. The runtime for the main game is larger and GCs upon a larger growth.
      27             : constexpr int DEFAULT_CONTEXT_SIZE = 16 * 1024 * 1024;
      28             : constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER = 2 * 1024 * 1024;
      29             : 
      30             : /**
      31             :  * Abstraction around a SpiderMonkey JSContext.
      32             :  *
      33             :  * A single ScriptContext, with the associated context,
      34             :  * should only be used on a single thread.
      35             :  *
      36             :  * (One means to share data between threads and contexts is to create
      37             :  * a Script::StructuredClone.)
      38             :  */
      39             : 
      40             : class ScriptContext
      41             : {
      42             : public:
      43             :     ScriptContext(int contextSize, int heapGrowthBytesGCTrigger);
      44             :     ~ScriptContext();
      45             : 
      46             :     /**
      47             :      * Returns a context, in which any number of ScriptInterfaces compartments can live.
      48             :      * Each context should only ever be used on a single thread.
      49             :      * @param parentContext Parent context from the parent thread, with which we share some thread-safe data
      50             :      * @param contextSize Maximum size in bytes of the new context
      51             :      * @param heapGrowthBytesGCTrigger Size in bytes of cumulated allocations after which a GC will be triggered
      52             :      */
      53             :     static std::shared_ptr<ScriptContext> CreateContext(
      54             :         int contextSize = DEFAULT_CONTEXT_SIZE,
      55             :         int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
      56             : 
      57             : 
      58             :     /**
      59             :      * MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough memory to
      60             :      * be worth the amount of time it would take. It does this with our own logic and NOT some predefined JSAPI logic because
      61             :      * such functionality currently isn't available out of the box.
      62             :      * It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done.
      63             :      * This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC
      64             :      * in seconds (the delay should be a fraction of a second in most cases though).
      65             :      * It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is
      66             :      * responsible for ensuring that GC can run with a small enough delay to get done with the work.
      67             :      */
      68             :     void MaybeIncrementalGC(double delay);
      69             :     void ShrinkingGC();
      70             : 
      71             :     /**
      72             :      * This is used to keep track of realms which should be prepared for a GC.
      73             :      */
      74             :     void RegisterRealm(JS::Realm* realm);
      75             :     void UnRegisterRealm(JS::Realm* realm);
      76             : 
      77             :     /**
      78             :      * GetGeneralJSContext returns the context without starting a GC request and without
      79             :      * entering any compartment. It should only be used in specific situations, such as
      80             :      * creating a new compartment, or when initializing a persistent rooted.
      81             :      * If you need the compartmented context of a ScriptInterface, you should create a
      82             :      * ScriptRequest and use the context from that.
      83             :      */
      84         855 :     JSContext* GetGeneralJSContext() const { return m_cx; }
      85             : 
      86             : private:
      87             : 
      88             :     JSContext* m_cx;
      89             : 
      90             :     void PrepareZonesForIncrementalGC() const;
      91             :     std::list<JS::Realm*> m_Realms;
      92             : 
      93             :     int m_ContextSize;
      94             :     int m_HeapGrowthBytesGCTrigger;
      95             :     int m_LastGCBytes;
      96             :     double m_LastGCCheck;
      97             : };
      98             : 
      99             : // Using a global object for the context is a workaround until Simulation, AI, etc,
     100             : // use their own threads and also their own contexts.
     101             : extern thread_local std::shared_ptr<ScriptContext> g_ScriptContext;
     102             : 
     103             : #endif // INCLUDED_SCRIPTCONTEXT

Generated by: LCOV version 1.13