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:
wraitii
2020-11-13 13:18:22 +00:00
parent 6a029d2a84
commit ee0d204bf6
74 changed files with 1591 additions and 1812 deletions
+8 -9
View File
@@ -41,23 +41,22 @@ bool CGUISetting<T>::FromString(const CStrW& Value, const bool SendMessage)
};
template<>
bool CGUISetting<CGUIColor>::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage)
bool CGUISetting<CGUIColor>::FromJSVal(const ScriptInterface::Request& rq, JS::HandleValue Value, const bool SendMessage)
{
CGUIColor settingValue;
if (Value.isString())
{
CStr name;
if (!ScriptInterface::FromJSVal(cx, Value, name))
if (!ScriptInterface::FromJSVal(rq, Value, name))
return false;
if (!settingValue.ParseString(m_pObject.GetGUI(), name))
{
JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid color '%s'", name.c_str());
JS_ReportError(rq.cx, "Invalid color '%s'", name.c_str());
return false;
}
}
else if (!ScriptInterface::FromJSVal<CColor>(cx, Value, settingValue))
else if (!ScriptInterface::FromJSVal<CColor>(rq, Value, settingValue))
return false;
m_pObject.SetSetting<CGUIColor>(m_Name, settingValue, SendMessage);
@@ -65,10 +64,10 @@ bool CGUISetting<CGUIColor>::FromJSVal(JSContext* cx, JS::HandleValue Value, con
};
template<typename T>
bool CGUISetting<T>::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage)
bool CGUISetting<T>::FromJSVal(const ScriptInterface::Request& rq, JS::HandleValue Value, const bool SendMessage)
{
T settingValue;
if (!ScriptInterface::FromJSVal<T>(cx, Value, settingValue))
if (!ScriptInterface::FromJSVal<T>(rq, Value, settingValue))
return false;
m_pObject.SetSetting<T>(m_Name, settingValue, SendMessage);
@@ -76,9 +75,9 @@ bool CGUISetting<T>::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool
};
template<typename T>
void CGUISetting<T>::ToJSVal(JSContext* cx, JS::MutableHandleValue Value)
void CGUISetting<T>::ToJSVal(const ScriptInterface::Request& rq, JS::MutableHandleValue Value)
{
ScriptInterface::ToJSVal<T>(cx, Value, m_pSetting);
ScriptInterface::ToJSVal<T>(rq, Value, m_pSetting);
};
#define TYPE(T) \