mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-27 16:29:00 +00:00
Cache JS component wrappers
This commit is contained in:
committed by
wraitii
parent
0a4bfefb1e
commit
ea5a350f83
@@ -25,31 +25,31 @@
|
||||
#include "scriptinterface/Object.h"
|
||||
#include "simulation2/serialization/ISerializer.h"
|
||||
#include "simulation2/serialization/IDeserializer.h"
|
||||
#include "simulation2/system/ComponentManager.h"
|
||||
|
||||
CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) :
|
||||
m_ScriptInterface(scriptInterface)
|
||||
{
|
||||
m_Instance.init(ScriptRequest(m_ScriptInterface).cx, instance);
|
||||
}
|
||||
m_ScriptInterface(scriptInterface), m_Instance(instance)
|
||||
{}
|
||||
|
||||
void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
|
||||
void CComponentTypeScript::Init(CComponentManager& cmpMgr, const CParamNode& paramNode, entity_id_t ent)
|
||||
{
|
||||
cmpMgr.RegisterTrace(ent, m_Instance);
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
|
||||
Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
|
||||
ScriptFunction::CallVoid(rq, m_Instance, "Init");
|
||||
Script::SetProperty(rq, GetInstance(), "entity", (int)ent, true, false);
|
||||
Script::SetProperty(rq, GetInstance(), "template", paramNode, true, false);
|
||||
ScriptFunction::CallVoid(rq, GetInstance(), "Init");
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Deinit()
|
||||
{
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
ScriptFunction::CallVoid(rq, m_Instance, "Deinit");
|
||||
ScriptFunction::CallVoid(rq, GetInstance(), "Deinit");
|
||||
}
|
||||
|
||||
bool CComponentTypeScript::HasMessageHandler(const CMessage& msg, const bool global)
|
||||
{
|
||||
const ScriptRequest rq(m_ScriptInterface);
|
||||
return Script::HasProperty(rq, m_Instance, global ? msg.GetScriptGlobalHandlerName() :
|
||||
return Script::HasProperty(rq, GetInstance(), global ? msg.GetScriptGlobalHandlerName() :
|
||||
msg.GetScriptHandlerName());
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
|
||||
|
||||
JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(rq));
|
||||
|
||||
if (!ScriptFunction::CallVoid(rq, m_Instance, name, msgVal))
|
||||
if (!ScriptFunction::CallVoid(rq, GetInstance(), name, msgVal))
|
||||
LOGERROR("Script message handler %s failed", name);
|
||||
}
|
||||
|
||||
@@ -71,26 +71,28 @@ void CComponentTypeScript::Serialize(ISerializer& serialize)
|
||||
|
||||
try
|
||||
{
|
||||
serialize.ScriptVal("comp", &m_Instance);
|
||||
serialize.ScriptVal("comp", GetMutInstance());
|
||||
}
|
||||
catch(PSERROR_Serialize& err)
|
||||
{
|
||||
int ent = INVALID_ENTITY;
|
||||
Script::GetProperty(rq, m_Instance, "entity", ent);
|
||||
Script::GetProperty(rq, GetInstance(), "entity", ent);
|
||||
std::string name = "(error)";
|
||||
Script::GetObjectClassName(rq, m_Instance, name);
|
||||
LOGERROR("Script component %s of entity %i failed to serialize: %s\nSerializing:\n%s", name, ent, err.what(), Script::ToString(rq, &m_Instance));
|
||||
Script::GetObjectClassName(rq, GetInstance(), name);
|
||||
LOGERROR("Script component %s of entity %i failed to serialize: %s\nSerializing:\n%s", name, ent, err.what(), Script::ToString(rq, GetMutInstance()));
|
||||
// Rethrow now that we added more details
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
||||
void CComponentTypeScript::Deserialize(CComponentManager& cmpMgr, const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
||||
{
|
||||
cmpMgr.RegisterTrace(ent, m_Instance);
|
||||
|
||||
ScriptRequest rq(m_ScriptInterface);
|
||||
|
||||
Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
|
||||
Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
|
||||
Script::SetProperty(rq, GetInstance(), "entity", (int)ent, true, false);
|
||||
Script::SetProperty(rq, GetInstance(), "template", paramNode, true, false);
|
||||
|
||||
deserialize.ScriptObjectAssign("comp", m_Instance);
|
||||
deserialize.ScriptObjectAssign("comp", GetInstance());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user