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:
parent
e3b3261a62
commit
9e4f0cc543
@ -73,6 +73,7 @@ that of Atlas depending on commandline parameters.
|
|||||||
#include "graphics/TextureManager.h"
|
#include "graphics/TextureManager.h"
|
||||||
#include "gui/GUIManager.h"
|
#include "gui/GUIManager.h"
|
||||||
#include "renderer/Renderer.h"
|
#include "renderer/Renderer.h"
|
||||||
|
#include "scriptinterface/ScriptEngine.h"
|
||||||
#include "simulation2/Simulation2.h"
|
#include "simulation2/Simulation2.h"
|
||||||
|
|
||||||
#if OS_UNIX
|
#if OS_UNIX
|
||||||
@ -422,10 +423,10 @@ static void RunGameOrAtlas(int argc, const char* argv[])
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to initialise libxml2 in the main thread before
|
// We need to initialize libxml2 and SpiderMonkey in the main thread before
|
||||||
// any thread uses it. So initialise it here before we
|
// any thread uses them. So initialize them here before we might run Atlas.
|
||||||
// might run Atlas.
|
|
||||||
CXeromyces::Startup();
|
CXeromyces::Startup();
|
||||||
|
ScriptEngine scriptEngine;
|
||||||
|
|
||||||
// Atlas handles the whole init/shutdown/etc sequence by itself;
|
// Atlas handles the whole init/shutdown/etc sequence by itself;
|
||||||
if (ATLAS_RunIfOnCmdLine(args, false))
|
if (ATLAS_RunIfOnCmdLine(args, false))
|
||||||
|
55
source/scriptinterface/ScriptEngine.h
Normal file
55
source/scriptinterface/ScriptEngine.h
Normal 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
|
@ -433,11 +433,6 @@ ScriptInterface::~ScriptInterface()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptInterface::ShutDown()
|
|
||||||
{
|
|
||||||
JS_ShutDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScriptInterface::SetCallbackData(void* pCBData)
|
void ScriptInterface::SetCallbackData(void* pCBData)
|
||||||
{
|
{
|
||||||
m_CxPrivate.pCBData = pCBData;
|
m_CxPrivate.pCBData = pCBData;
|
||||||
|
@ -97,12 +97,6 @@ public:
|
|||||||
|
|
||||||
~ScriptInterface();
|
~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
|
struct CxPrivate
|
||||||
{
|
{
|
||||||
ScriptInterface* pScriptInterface; // the ScriptInterface object the current context belongs to
|
ScriptInterface* pScriptInterface; // the ScriptInterface object the current context belongs to
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include "ps/GameSetup/Config.h"
|
#include "ps/GameSetup/Config.h"
|
||||||
#include "ps/Profile.h"
|
#include "ps/Profile.h"
|
||||||
|
#include "scriptinterface/ScriptEngine.h"
|
||||||
|
|
||||||
|
|
||||||
void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc))
|
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);
|
m_FinalizationListObjectIdCache.push_back(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptRuntime::m_Initialized = false;
|
|
||||||
|
|
||||||
ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger):
|
ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger):
|
||||||
m_LastGCBytes(0),
|
m_LastGCBytes(0),
|
||||||
m_LastGCCheck(0.0f),
|
m_LastGCCheck(0.0f),
|
||||||
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger),
|
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger),
|
||||||
m_RuntimeSize(runtimeSize)
|
m_RuntimeSize(runtimeSize)
|
||||||
{
|
{
|
||||||
if (!m_Initialized)
|
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptRuntimes!");
|
||||||
{
|
|
||||||
ENSURE(JS_Init());
|
|
||||||
m_Initialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSRuntime* parentJSRuntime = parentRuntime ? parentRuntime->m_rt : nullptr;
|
JSRuntime* parentJSRuntime = parentRuntime ? parentRuntime->m_rt : nullptr;
|
||||||
m_rt = JS_NewRuntime(runtimeSize, JS_USE_HELPER_THREADS, parentJSRuntime);
|
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);
|
m_dummyContext = JS_NewContext(m_rt, STACK_CHUNK_SIZE);
|
||||||
ENSURE(m_dummyContext);
|
ENSURE(m_dummyContext);
|
||||||
|
|
||||||
|
ScriptEngine::GetSingleton().RegisterRuntime(m_rt);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptRuntime::~ScriptRuntime()
|
ScriptRuntime::~ScriptRuntime()
|
||||||
@ -143,6 +140,9 @@ ScriptRuntime::~ScriptRuntime()
|
|||||||
JS_SetGCCallback(m_rt, nullptr, nullptr);
|
JS_SetGCCallback(m_rt, nullptr, nullptr);
|
||||||
JS_DestroyRuntime(m_rt);
|
JS_DestroyRuntime(m_rt);
|
||||||
ENSURE(m_FinalizationListObjectIdCache.empty() && "Leak: Removing callback while some objects still aren't finalized!");
|
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)
|
void ScriptRuntime::RegisterContext(JSContext* cx)
|
||||||
|
@ -79,7 +79,6 @@ private:
|
|||||||
|
|
||||||
std::list<JSContext*> m_Contexts;
|
std::list<JSContext*> m_Contexts;
|
||||||
std::vector<std::shared_ptr<void> > m_FinalizationListObjectIdCache;
|
std::vector<std::shared_ptr<void> > m_FinalizationListObjectIdCache;
|
||||||
static bool m_Initialized;
|
|
||||||
|
|
||||||
int m_RuntimeSize;
|
int m_RuntimeSize;
|
||||||
int m_HeapGrowthBytesGCTrigger;
|
int m_HeapGrowthBytesGCTrigger;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2014 Wildfire Games.
|
/* Copyright (C) 2016 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||||
@ -36,15 +36,13 @@
|
|||||||
#include "lib/timer.h"
|
#include "lib/timer.h"
|
||||||
#include "lib/sysdep/sysdep.h"
|
#include "lib/sysdep/sysdep.h"
|
||||||
#include "ps/Profiler2.h"
|
#include "ps/Profiler2.h"
|
||||||
|
#include "scriptinterface/ScriptEngine.h"
|
||||||
#include "scriptinterface/ScriptInterface.h"
|
#include "scriptinterface/ScriptInterface.h"
|
||||||
|
|
||||||
class LeakReporter : public CxxTest::GlobalFixture
|
class LeakReporter : public CxxTest::GlobalFixture
|
||||||
{
|
{
|
||||||
virtual bool tearDownWorld()
|
virtual bool tearDownWorld()
|
||||||
{
|
{
|
||||||
// Shut down JS to prevent leak reports from it
|
|
||||||
ScriptInterface::ShutDown();
|
|
||||||
|
|
||||||
// Enable leak reporting on exit.
|
// Enable leak reporting on exit.
|
||||||
// (This is done in tearDownWorld so that it doesn't report 'leaks'
|
// (This is done in tearDownWorld so that it doesn't report 'leaks'
|
||||||
// if the program is aborted before finishing cleanly.)
|
// if the program is aborted before finishing cleanly.)
|
||||||
@ -81,6 +79,7 @@ class MiscSetup : public CxxTest::GlobalFixture
|
|||||||
ThreadUtil::SetMainThread();
|
ThreadUtil::SetMainThread();
|
||||||
|
|
||||||
g_Profiler2.Initialise();
|
g_Profiler2.Initialise();
|
||||||
|
m_ScriptEngine = new ScriptEngine;
|
||||||
g_ScriptRuntime = ScriptInterface::CreateRuntime();
|
g_ScriptRuntime = ScriptInterface::CreateRuntime();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -89,10 +88,17 @@ class MiscSetup : public CxxTest::GlobalFixture
|
|||||||
virtual bool tearDownWorld()
|
virtual bool tearDownWorld()
|
||||||
{
|
{
|
||||||
g_ScriptRuntime.reset();
|
g_ScriptRuntime.reset();
|
||||||
|
SAFE_DELETE(m_ScriptEngine);
|
||||||
g_Profiler2.Shutdown();
|
g_Profiler2.Shutdown();
|
||||||
|
|
||||||
return true;
|
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;
|
static LeakReporter leakReporter;
|
||||||
|
Loading…
Reference in New Issue
Block a user