LCOV - code coverage report
Current view: top level - source/scriptinterface - Engine.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 11 11 100.0 %
Date: 2026-12-03 01:19:39 Functions: 2 2 100.0 %

          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_ENGINE
      19             : #define INCLUDED_SCRIPT_ENGINE
      20             : 
      21             : #include "lib/debug.h"
      22             : #include "ps/Singleton.h"
      23             : 
      24             : #include <js/Initialization.h>
      25             : #include <list>
      26             : 
      27             : #if MOZJS_MAJOR_VERSION != 140
      28             : #error Your compiler is trying to use an incorrect major version of the \
      29             : SpiderMonkey library. The SpiderMonkey API is subject to changes, and the \
      30             : game will not build with the selected version of the library. Make sure \
      31             : you have got all the right files and include paths.
      32             : #endif
      33             : 
      34             : struct JSContext;
      35             : 
      36             : /**
      37             :  * A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization
      38             :  * and shutdown of the SpiderMonkey script engine. It also keeps a count of active script contexts
      39             :  * in order to validate the following constraints:
      40             :  *  1. JS_Init must be called before any ScriptContexts are initialized
      41             :  *  2. JS_Shutdown must be called after all ScriptContexts have been destroyed
      42             :  */
      43             : 
      44             : namespace Script
      45             : {
      46             : 
      47             : class Engine : public Singleton<Engine>
      48             : {
      49             : public:
      50           1 :     Engine()
      51           1 :     {
      52           1 :         ENSURE(m_Contexts.empty() && "JS_Init must be called before any contexts are created!");
      53           1 :         JS_Init();
      54           1 :     }
      55             : 
      56           1 :     ~Engine()
      57             :     {
      58           1 :         ENSURE(m_Contexts.empty() && "All contexts must be destroyed before calling JS_ShutDown!");
      59           1 :         JS_ShutDown();
      60           2 :     }
      61             : 
      62           1 :     void RegisterContext(const JSContext* cx) { m_Contexts.push_back(cx); }
      63           1 :     void UnRegisterContext(const JSContext* cx) { m_Contexts.remove(cx); }
      64             : 
      65             : private:
      66             :     std::list<const JSContext*> m_Contexts;
      67             : };
      68             : 
      69             : }
      70             : 
      71             : #endif // INCLUDED_SCRIPT_ENGINE

Generated by: LCOV version 1.16