2021-05-01 07:04:53 -07:00
|
|
|
/* Copyright (C) 2021 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"
|
|
|
|
|
|
2021-05-01 07:04:53 -07:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#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
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2021-05-01 07:04:53 -07:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
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);
|
2021-05-01 07:04:53 -07:00
|
|
|
ScriptFunction::CallVoid(rq, 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
|
|
|
{
|
2021-05-01 07:04:53 -07:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
|
ScriptFunction::CallVoid(rq, 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
|
|
|
|
2021-05-01 07:04:53 -07:00
|
|
|
if (!ScriptFunction::CallVoid(rq, 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)
|
|
|
|
|
{
|
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
|
|
|
|
2020-12-27 09:18:13 -08:00
|
|
|
serialize.ScriptVal("comp", &m_Instance);
|
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);
|
|
|
|
|
|
2021-01-12 08:15:40 -08:00
|
|
|
deserialize.ScriptObjectAssign("comp", m_Instance);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|