mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-28 08:47:14 +00:00
Quite a lot of stack rooting related changes.
Changes GetProperty, SetProperty and HasProperty and a few other functions to take handles. The conversions to CScriptVal or CScriptValRooted at some places should be removed in the future. I've done that to avoid an even larger patch. Refs #2415 Refs #2462 This was SVN commit r15568.
This commit is contained in:
@@ -26,28 +26,40 @@ CComponentTypeScript::CComponentTypeScript(ScriptInterface& scriptInterface, jsv
|
||||
m_ScriptInterface(scriptInterface), m_Instance(CScriptValRooted(scriptInterface.GetContext(), instance))
|
||||
{
|
||||
// Cache the property detection for efficiency
|
||||
m_HasCustomSerialize = m_ScriptInterface.HasProperty(m_Instance.get(), "Serialize");
|
||||
m_HasCustomDeserialize = m_ScriptInterface.HasProperty(m_Instance.get(), "Deserialize");
|
||||
JSContext* cx = m_ScriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
JS::RootedValue tmpInstance(cx, m_Instance.get()); // TODO: Check if this temporary root can be removed after SpiderMonkey 31 upgrade
|
||||
m_HasCustomSerialize = m_ScriptInterface.HasProperty(tmpInstance, "Serialize");
|
||||
m_HasCustomDeserialize = m_ScriptInterface.HasProperty(tmpInstance, "Deserialize");
|
||||
|
||||
m_HasNullSerialize = false;
|
||||
if (m_HasCustomSerialize)
|
||||
{
|
||||
CScriptVal val;
|
||||
if (m_ScriptInterface.GetProperty(m_Instance.get(), "Serialize", val) && JSVAL_IS_NULL(val.get()))
|
||||
JS::RootedValue val(cx);
|
||||
if (m_ScriptInterface.GetProperty(tmpInstance, "Serialize", &val) && val.isNull())
|
||||
m_HasNullSerialize = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
|
||||
{
|
||||
m_ScriptInterface.SetProperty(m_Instance.get(), "entity", (int)ent, true, false);
|
||||
m_ScriptInterface.SetProperty(m_Instance.get(), "template", paramNode, true, false);
|
||||
m_ScriptInterface.CallFunctionVoid(m_Instance.get(), "Init");
|
||||
JSContext* cx = m_ScriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
JS::RootedValue tmpInstance(cx, m_Instance.get()); // TODO: Check if this temporary root can be removed after SpiderMonkey 31 upgrade
|
||||
m_ScriptInterface.SetProperty(tmpInstance, "entity", (int)ent, true, false);
|
||||
m_ScriptInterface.SetProperty(tmpInstance, "template", paramNode, true, false);
|
||||
m_ScriptInterface.CallFunctionVoid(tmpInstance, "Init");
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Deinit()
|
||||
{
|
||||
m_ScriptInterface.CallFunctionVoid(m_Instance.get(), "Deinit");
|
||||
{
|
||||
JSContext* cx = m_ScriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
JS::RootedValue tmpInstance(cx, m_Instance.get()); // TODO: Check if this temporary root can be removed after SpiderMonkey 31 upgrade
|
||||
m_ScriptInterface.CallFunctionVoid(tmpInstance, "Deinit");
|
||||
}
|
||||
|
||||
void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
|
||||
@@ -82,18 +94,22 @@ void CComponentTypeScript::Serialize(ISerializer& serialize)
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
||||
{
|
||||
{
|
||||
JSContext* cx = m_ScriptInterface.GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
JS::RootedValue tmpInstance(cx, m_Instance.get()); // TODO: Check if this temporary root can be removed after SpiderMonkey 31 upgrade
|
||||
// Support a custom "Deserialize" function, to which we pass the deserialized data
|
||||
// instead of automatically adding the deserialized properties onto the object
|
||||
if (m_HasCustomDeserialize)
|
||||
{
|
||||
CScriptVal val;
|
||||
JS::RootedValue val(cx);
|
||||
|
||||
// If Serialize = null, we'll still call Deserialize but with undefined argument
|
||||
if (!m_HasNullSerialize)
|
||||
deserialize.ScriptVal("object", val);
|
||||
deserialize.ScriptVal("object", val.get());
|
||||
|
||||
if (!m_ScriptInterface.CallFunctionVoid(m_Instance.get(), "Deserialize", val))
|
||||
if (!m_ScriptInterface.CallFunctionVoid(tmpInstance, "Deserialize", val))
|
||||
LOGERROR(L"Script Deserialize call failed");
|
||||
}
|
||||
else
|
||||
@@ -106,6 +122,6 @@ void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserialize
|
||||
}
|
||||
}
|
||||
|
||||
m_ScriptInterface.SetProperty(m_Instance.get(), "entity", (int)ent, true, false);
|
||||
m_ScriptInterface.SetProperty(m_Instance.get(), "template", paramNode, true, false);
|
||||
m_ScriptInterface.SetProperty(tmpInstance, "entity", (int)ent, true, false);
|
||||
m_ScriptInterface.SetProperty(tmpInstance, "template", paramNode, true, false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user