mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-21 01:29:50 +00:00
Wrap JSAutoRequest and replace usage of JSContext* with the wrapper.
JSAutoRequest is required before calling into most JSAPI methods, for GC reasons. Calling it is required and fragile as one must not forget. Further, SM52 and later make manipulating JSContext* dangerous as that can cross Compartment(Realm in SM68) barriers (and ScriptInterface now matches a Compartment). The solution to both problems is to avoid using JSContext* in 0 A.D. itself. To achieve this, a Request class is introduced, and must be used to access a JSContext* from a scriptInterface. Further, Request is passed to other ScriptInterface functions isntead of JSContext*, making it obvious that the caller has already called it, reducing errors and redundant JSAutoRequest calls. Only JSNative functions now get a naked JSContext* without protection, but the likelihood of forgetting a request is lower since many ScriptInterface functions now expect it. JSContext* is directly passed to JSAPI functions only. Part of the SM52 migration, stage: SM45 compatible Based on a patch by: Itms Tested By: Freagarach Refs #4893 Differential Revision: https://code.wildfiregames.com/D3088 This was SVN commit r24176.
This commit is contained in:
+8
-10
@@ -191,10 +191,9 @@ bool CGame::StartVisualReplay(const OsPath& replayPath)
|
||||
std::getline(*m_ReplayStream, line);
|
||||
|
||||
const ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface();
|
||||
JSContext* cx = scriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
ScriptInterface::Request rq(scriptInterface);
|
||||
|
||||
JS::RootedValue attribs(cx);
|
||||
JS::RootedValue attribs(rq.cx);
|
||||
scriptInterface.ParseJSON(line, &attribs);
|
||||
StartGame(&attribs, "");
|
||||
|
||||
@@ -209,8 +208,7 @@ bool CGame::StartVisualReplay(const OsPath& replayPath)
|
||||
void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& savedState)
|
||||
{
|
||||
const ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface();
|
||||
JSContext* cx = scriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
ScriptInterface::Request rq(scriptInterface);
|
||||
|
||||
m_InitialSavedState = savedState;
|
||||
m_IsSavedGame = !savedState.empty();
|
||||
@@ -240,7 +238,7 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
|
||||
{
|
||||
// Load random map attributes
|
||||
std::wstring scriptFile;
|
||||
JS::RootedValue settings(cx);
|
||||
JS::RootedValue settings(rq.cx);
|
||||
|
||||
scriptInterface.GetProperty(attribs, "script", scriptFile);
|
||||
scriptInterface.GetProperty(attribs, "settings", &settings);
|
||||
@@ -250,7 +248,7 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
|
||||
else
|
||||
{
|
||||
std::wstring mapFile;
|
||||
JS::RootedValue settings(cx);
|
||||
JS::RootedValue settings(rq.cx);
|
||||
scriptInterface.GetProperty(attribs, "map", mapFile);
|
||||
scriptInterface.GetProperty(attribs, "settings", &settings);
|
||||
|
||||
@@ -325,9 +323,9 @@ PSRETURN CGame::ReallyStartGame()
|
||||
if (g_GUI && g_GUI->GetPageCount())
|
||||
{
|
||||
shared_ptr<ScriptInterface> scriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface();
|
||||
JSContext* cx = scriptInterface->GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
JS::RootedValue global(cx, scriptInterface->GetGlobalObject());
|
||||
ScriptInterface::Request rq(scriptInterface);
|
||||
|
||||
JS::RootedValue global(rq.cx, scriptInterface->GetGlobalObject());
|
||||
if (scriptInterface->HasProperty(global, "reallyStartGame"))
|
||||
scriptInterface->CallFunctionVoid(global, "reallyStartGame");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user