2025-08-04 10:04:30 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2016-08-06 08:41:59 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2016-08-06 08:41:59 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2016-08-06 08:41:59 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-06 08:41:59 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SCRIPTENGINE
|
|
|
|
|
#define INCLUDED_SCRIPTENGINE
|
|
|
|
|
|
2025-08-04 10:04:30 -07:00
|
|
|
#include "lib/debug.h"
|
2016-08-06 08:41:59 -07:00
|
|
|
#include "ps/Singleton.h"
|
|
|
|
|
|
2025-08-04 10:04:30 -07:00
|
|
|
#include <js/Initialization.h>
|
2021-01-15 08:30:05 -08:00
|
|
|
#include <list>
|
|
|
|
|
|
2025-09-10 03:18:16 -07:00
|
|
|
#if MOZJS_MAJOR_VERSION != 128
|
|
|
|
|
#error Your compiler is trying to use an incorrect major version of the \
|
|
|
|
|
SpiderMonkey library. The SpiderMonkey API is subject to changes, and the \
|
|
|
|
|
game will not build with the selected version of the library. Make sure \
|
|
|
|
|
you have got all the right files and include paths.
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-08-04 10:04:30 -07:00
|
|
|
struct JSContext;
|
|
|
|
|
|
2016-08-06 08:41:59 -07:00
|
|
|
/**
|
|
|
|
|
* A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization
|
2020-11-14 02:57:50 -08:00
|
|
|
* and shutdown of the SpiderMonkey script engine. It also keeps a count of active script contexts
|
2016-08-06 08:41:59 -07:00
|
|
|
* in order to validate the following constraints:
|
2020-11-14 02:57:50 -08:00
|
|
|
* 1. JS_Init must be called before any ScriptContexts are initialized
|
|
|
|
|
* 2. JS_Shutdown must be called after all ScriptContexts have been destroyed
|
2016-08-06 08:41:59 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class ScriptEngine : public Singleton<ScriptEngine>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ScriptEngine()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ENSURE(m_Contexts.empty() && "JS_Init must be called before any contexts are created!");
|
2016-08-06 08:41:59 -07:00
|
|
|
JS_Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScriptEngine()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ENSURE(m_Contexts.empty() && "All contexts must be destroyed before calling JS_ShutDown!");
|
2016-08-06 08:41:59 -07:00
|
|
|
JS_ShutDown();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 02:57:50 -08:00
|
|
|
void RegisterContext(const JSContext* cx) { m_Contexts.push_back(cx); }
|
|
|
|
|
void UnRegisterContext(const JSContext* cx) { m_Contexts.remove(cx); }
|
2016-08-06 08:41:59 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-11-14 02:57:50 -08:00
|
|
|
std::list<const JSContext*> m_Contexts;
|
2016-08-06 08:41:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // INCLUDED_SCRIPTENGINE
|