mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
Properly shut down SpiderMonkey using JS_ShutDown.
This also adds some validation to ensure the correct order of JS_Init, JS_NewRuntime, JS_DestroyRuntime and JS_ShutDown calls. Refs #3708 This was SVN commit r18584.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/* Copyright (C) 2016 Wildfire Games.
|
||||
* 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"
|
||||
|
||||
/**
|
||||
* A class using the RAII (Resource Acquisition Is Initialization) idiom to manage initialization
|
||||
* and shutdown of the SpiderMonkey script engine. It also keeps a count of active script runtimes
|
||||
* in order to validate the following constraints:
|
||||
* 1. JS_Init must be called before any ScriptRuntimes are initialized
|
||||
* 2. JS_Shutdown must be called after all ScriptRuntimes have been destroyed
|
||||
*/
|
||||
|
||||
class ScriptEngine : public Singleton<ScriptEngine>
|
||||
{
|
||||
public:
|
||||
ScriptEngine()
|
||||
{
|
||||
ENSURE(m_Runtimes.size() == 0 && "JS_Init must be called before any runtimes are created!");
|
||||
JS_Init();
|
||||
}
|
||||
|
||||
~ScriptEngine()
|
||||
{
|
||||
ENSURE(m_Runtimes.size() == 0 && "All runtimes must be destroyed before calling JS_ShutDown!");
|
||||
JS_ShutDown();
|
||||
}
|
||||
|
||||
void RegisterRuntime(const JSRuntime* rt) { m_Runtimes.push_back(rt); }
|
||||
void UnRegisterRuntime(const JSRuntime* rt) { m_Runtimes.remove(rt); }
|
||||
|
||||
private:
|
||||
|
||||
std::list<const JSRuntime*> m_Runtimes;
|
||||
};
|
||||
|
||||
#endif // INCLUDED_SCRIPTENGINE
|
||||
@@ -433,11 +433,6 @@ ScriptInterface::~ScriptInterface()
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptInterface::ShutDown()
|
||||
{
|
||||
JS_ShutDown();
|
||||
}
|
||||
|
||||
void ScriptInterface::SetCallbackData(void* pCBData)
|
||||
{
|
||||
m_CxPrivate.pCBData = pCBData;
|
||||
|
||||
@@ -97,12 +97,6 @@ public:
|
||||
|
||||
~ScriptInterface();
|
||||
|
||||
/**
|
||||
* Shut down the JS system to clean up memory. Must only be called when there
|
||||
* are no ScriptInterfaces alive.
|
||||
*/
|
||||
static void ShutDown();
|
||||
|
||||
struct CxPrivate
|
||||
{
|
||||
ScriptInterface* pScriptInterface; // the ScriptInterface object the current context belongs to
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "ps/GameSetup/Config.h"
|
||||
#include "ps/Profile.h"
|
||||
#include "scriptinterface/ScriptEngine.h"
|
||||
|
||||
|
||||
void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc))
|
||||
@@ -104,19 +105,13 @@ void ScriptRuntime::AddDeferredFinalizationObject(const std::shared_ptr<void>& o
|
||||
m_FinalizationListObjectIdCache.push_back(obj);
|
||||
}
|
||||
|
||||
bool ScriptRuntime::m_Initialized = false;
|
||||
|
||||
ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger):
|
||||
m_LastGCBytes(0),
|
||||
m_LastGCCheck(0.0f),
|
||||
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger),
|
||||
m_RuntimeSize(runtimeSize)
|
||||
{
|
||||
if (!m_Initialized)
|
||||
{
|
||||
ENSURE(JS_Init());
|
||||
m_Initialized = true;
|
||||
}
|
||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptRuntimes!");
|
||||
|
||||
JSRuntime* parentJSRuntime = parentRuntime ? parentRuntime->m_rt : nullptr;
|
||||
m_rt = JS_NewRuntime(runtimeSize, JS_USE_HELPER_THREADS, parentJSRuntime);
|
||||
@@ -135,6 +130,8 @@ ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtim
|
||||
|
||||
m_dummyContext = JS_NewContext(m_rt, STACK_CHUNK_SIZE);
|
||||
ENSURE(m_dummyContext);
|
||||
|
||||
ScriptEngine::GetSingleton().RegisterRuntime(m_rt);
|
||||
}
|
||||
|
||||
ScriptRuntime::~ScriptRuntime()
|
||||
@@ -143,6 +140,9 @@ ScriptRuntime::~ScriptRuntime()
|
||||
JS_SetGCCallback(m_rt, nullptr, nullptr);
|
||||
JS_DestroyRuntime(m_rt);
|
||||
ENSURE(m_FinalizationListObjectIdCache.empty() && "Leak: Removing callback while some objects still aren't finalized!");
|
||||
|
||||
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptRuntime!");
|
||||
ScriptEngine::GetSingleton().UnRegisterRuntime(m_rt);
|
||||
}
|
||||
|
||||
void ScriptRuntime::RegisterContext(JSContext* cx)
|
||||
|
||||
@@ -79,7 +79,6 @@ private:
|
||||
|
||||
std::list<JSContext*> m_Contexts;
|
||||
std::vector<std::shared_ptr<void> > m_FinalizationListObjectIdCache;
|
||||
static bool m_Initialized;
|
||||
|
||||
int m_RuntimeSize;
|
||||
int m_HeapGrowthBytesGCTrigger;
|
||||
|
||||
Reference in New Issue
Block a user