Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Engine.h
Go to the documentation of this file.
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 != 128
28#error Your compiler is trying to use an incorrect major version of the \
29SpiderMonkey library. The SpiderMonkey API is subject to changes, and the \
30game will not build with the selected version of the library. Make sure \
31you have got all the right files and include paths.
32#endif
33
34struct 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
44namespace Script
45{
46
47class Engine : public Singleton<Engine>
48{
49public:
51 {
52 ENSURE(m_Contexts.empty() && "JS_Init must be called before any contexts are created!");
53 JS_Init();
54 }
55
57 {
58 ENSURE(m_Contexts.empty() && "All contexts must be destroyed before calling JS_ShutDown!");
59 JS_ShutDown();
60 }
61
62 void RegisterContext(const JSContext* cx) { m_Contexts.push_back(cx); }
63 void UnRegisterContext(const JSContext* cx) { m_Contexts.remove(cx); }
64
65private:
66 std::list<const JSContext*> m_Contexts;
67};
68
69}
70
71#endif // INCLUDED_SCRIPT_ENGINE
Definition: Engine.h:48
std::list< const JSContext * > m_Contexts
Definition: Engine.h:66
void RegisterContext(const JSContext *cx)
Definition: Engine.h:62
Engine()
Definition: Engine.h:50
~Engine()
Definition: Engine.h:56
void UnRegisterContext(const JSContext *cx)
Definition: Engine.h:63
Template base class for singletons.
Definition: Singleton.h:35
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32