2020-11-13 05:18:22 -08:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2010-01-09 11:20:14 -08:00
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ScriptComponent.h"
|
|
|
|
|
|
|
|
|
|
#include "simulation2/serialization/ISerializer.h"
|
|
|
|
|
#include "simulation2/serialization/IDeserializer.h"
|
|
|
|
|
|
2017-08-23 17:32:42 -07:00
|
|
|
CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) :
|
2020-11-18 06:39:04 -08:00
|
|
|
m_ScriptInterface(scriptInterface), m_Instance(scriptInterface.GetGeneralJSContext(), instance)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 11:18:32 -07:00
|
|
|
// Cache the property detection for efficiency
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
m_HasCustomSerialize = m_ScriptInterface.HasProperty(m_Instance, "Serialize");
|
|
|
|
|
m_HasCustomDeserialize = m_ScriptInterface.HasProperty(m_Instance, "Deserialize");
|
2011-03-05 14:30:32 -08:00
|
|
|
|
|
|
|
|
m_HasNullSerialize = false;
|
|
|
|
|
if (m_HasCustomSerialize)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2015-01-24 06:46:52 -08:00
|
|
|
if (m_ScriptInterface.GetProperty(m_Instance, "Serialize", &val) && val.isNull())
|
2011-03-05 14:30:32 -08:00
|
|
|
m_HasNullSerialize = true;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-16 06:08:38 -08:00
|
|
|
void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
m_ScriptInterface.SetProperty(m_Instance, "entity", (int)ent, true, false);
|
|
|
|
|
m_ScriptInterface.SetProperty(m_Instance, "template", paramNode, true, false);
|
|
|
|
|
m_ScriptInterface.CallFunctionVoid(m_Instance, "Init");
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-16 06:08:38 -08:00
|
|
|
void CComponentTypeScript::Deinit()
|
2015-01-24 06:46:52 -08:00
|
|
|
{
|
|
|
|
|
m_ScriptInterface.CallFunctionVoid(m_Instance, "Deinit");
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-16 06:08:38 -08:00
|
|
|
void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2010-01-22 12:03:14 -08:00
|
|
|
const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName();
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(m_ScriptInterface));
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
if (!m_ScriptInterface.CallFunctionVoid(m_Instance, name, msgVal))
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGERROR("Script message handler %s failed", name);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CComponentTypeScript::Serialize(ISerializer& serialize)
|
|
|
|
|
{
|
2011-03-05 14:30:32 -08:00
|
|
|
// If the component set Serialize = null, then do no work here
|
|
|
|
|
if (m_HasNullSerialize)
|
|
|
|
|
return;
|
2016-11-23 06:09:58 -08:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2011-03-05 14:30:32 -08:00
|
|
|
|
2010-05-22 16:02:07 -07:00
|
|
|
// Support a custom "Serialize" function, which returns a new object that will be
|
|
|
|
|
// serialized instead of the component itself
|
2010-05-25 11:18:32 -07:00
|
|
|
if (m_HasCustomSerialize)
|
2010-05-22 16:02:07 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2015-01-24 06:46:52 -08:00
|
|
|
if (!m_ScriptInterface.CallFunction(m_Instance, "Serialize", &val))
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Script Serialize call failed");
|
2014-08-03 10:29:49 -07:00
|
|
|
serialize.ScriptVal("object", &val);
|
2010-05-22 16:02:07 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
serialize.ScriptVal("object", &m_Instance);
|
2010-05-22 16:02:07 -07:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2011-01-16 06:08:38 -08:00
|
|
|
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
2016-11-23 06:09:58 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-11-14 11:03:20 -08:00
|
|
|
m_ScriptInterface.SetProperty(m_Instance, "entity", (int)ent, true, false);
|
|
|
|
|
m_ScriptInterface.SetProperty(m_Instance, "template", paramNode, true, false);
|
|
|
|
|
|
2011-01-12 04:29:00 -08:00
|
|
|
// 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)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue val(rq.cx);
|
2011-03-05 14:30:32 -08:00
|
|
|
|
|
|
|
|
// If Serialize = null, we'll still call Deserialize but with undefined argument
|
|
|
|
|
if (!m_HasNullSerialize)
|
2014-07-31 12:18:40 -07:00
|
|
|
deserialize.ScriptVal("object", &val);
|
2011-03-05 14:30:32 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
if (!m_ScriptInterface.CallFunctionVoid(m_Instance, "Deserialize", val))
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Script Deserialize call failed");
|
2011-01-12 04:29:00 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-03-05 14:30:32 -08:00
|
|
|
if (!m_HasNullSerialize)
|
|
|
|
|
{
|
|
|
|
|
// Use ScriptObjectAppend so we don't lose the carefully-constructed
|
|
|
|
|
// prototype/parent of this object
|
2015-01-24 06:46:52 -08:00
|
|
|
deserialize.ScriptObjectAppend("object", m_Instance);
|
2011-03-05 14:30:32 -08:00
|
|
|
}
|
2011-01-12 04:29:00 -08:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|