2020-11-14 11:57:50 +01:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2016-08-06 17:41:59 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SCRIPTENGINE
|
|
|
|
#define INCLUDED_SCRIPTENGINE
|
|
|
|
|
|
|
|
#include "ScriptTypes.h"
|
|
|
|
#include "ps/Singleton.h"
|
|
|
|
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-08 00:37:43 +02:00
|
|
|
#include "js/Initialization.h"
|
|
|
|
|
2016-08-06 17:41:59 +02:00
|
|
|
/**
|
|
|
|
* A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization
|
2020-11-14 11:57:50 +01:00
|
|
|
* and shutdown of the SpiderMonkey script engine. It also keeps a count of active script contexts
|
2016-08-06 17:41:59 +02:00
|
|
|
* in order to validate the following constraints:
|
2020-11-14 11:57:50 +01: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 17:41:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class ScriptEngine : public Singleton<ScriptEngine>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScriptEngine()
|
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
ENSURE(m_Contexts.empty() && "JS_Init must be called before any contexts are created!");
|
2016-08-06 17:41:59 +02:00
|
|
|
JS_Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScriptEngine()
|
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
ENSURE(m_Contexts.empty() && "All contexts must be destroyed before calling JS_ShutDown!");
|
2016-08-06 17:41:59 +02:00
|
|
|
JS_ShutDown();
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void RegisterContext(const JSContext* cx) { m_Contexts.push_back(cx); }
|
|
|
|
void UnRegisterContext(const JSContext* cx) { m_Contexts.remove(cx); }
|
2016-08-06 17:41:59 +02:00
|
|
|
|
|
|
|
private:
|
2020-11-14 11:57:50 +01:00
|
|
|
std::list<const JSContext*> m_Contexts;
|
2016-08-06 17:41:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDED_SCRIPTENGINE
|