1
0
forked from 0ad/0ad

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:
Yves 2016-08-06 15:41:59 +00:00
parent e3b3261a62
commit 9e4f0cc543
7 changed files with 76 additions and 26 deletions

View File

@ -73,6 +73,7 @@ that of Atlas depending on commandline parameters.
#include "graphics/TextureManager.h"
#include "gui/GUIManager.h"
#include "renderer/Renderer.h"
#include "scriptinterface/ScriptEngine.h"
#include "simulation2/Simulation2.h"
#if OS_UNIX
@ -422,10 +423,10 @@ static void RunGameOrAtlas(int argc, const char* argv[])
return;
}
// We need to initialise libxml2 in the main thread before
// any thread uses it. So initialise it here before we
// might run Atlas.
// We need to initialize libxml2 and SpiderMonkey in the main thread before
// any thread uses them. So initialize them here before we might run Atlas.
CXeromyces::Startup();
ScriptEngine scriptEngine;
// Atlas handles the whole init/shutdown/etc sequence by itself;
if (ATLAS_RunIfOnCmdLine(args, false))

View File

@ -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

View File

@ -433,11 +433,6 @@ ScriptInterface::~ScriptInterface()
}
}
void ScriptInterface::ShutDown()
{
JS_ShutDown();
}
void ScriptInterface::SetCallbackData(void* pCBData)
{
m_CxPrivate.pCBData = pCBData;

View File

@ -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

View File

@ -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)

View File

@ -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;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* 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
@ -36,15 +36,13 @@
#include "lib/timer.h"
#include "lib/sysdep/sysdep.h"
#include "ps/Profiler2.h"
#include "scriptinterface/ScriptEngine.h"
#include "scriptinterface/ScriptInterface.h"
class LeakReporter : public CxxTest::GlobalFixture
{
virtual bool tearDownWorld()
{
// Shut down JS to prevent leak reports from it
ScriptInterface::ShutDown();
// Enable leak reporting on exit.
// (This is done in tearDownWorld so that it doesn't report 'leaks'
// if the program is aborted before finishing cleanly.)
@ -81,6 +79,7 @@ class MiscSetup : public CxxTest::GlobalFixture
ThreadUtil::SetMainThread();
g_Profiler2.Initialise();
m_ScriptEngine = new ScriptEngine;
g_ScriptRuntime = ScriptInterface::CreateRuntime();
return true;
@ -89,10 +88,17 @@ class MiscSetup : public CxxTest::GlobalFixture
virtual bool tearDownWorld()
{
g_ScriptRuntime.reset();
SAFE_DELETE(m_ScriptEngine);
g_Profiler2.Shutdown();
return true;
}
private:
// We're doing the initialization and shutdown of the ScriptEngine explicitly here
// to make sure it's only initialized when setUpWorld is called.
ScriptEngine* m_ScriptEngine;
};
static LeakReporter leakReporter;