2023-06-21 09:50:00 +02:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-07-27 22:54:46 +02:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 20:20:14 +01:00
|
|
|
*
|
2023-07-27 22:54:46 +02:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 20:20:14 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2023-07-27 22:54:46 +02:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 20:20:14 +01:00
|
|
|
* 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
|
2023-07-27 22:54:46 +02:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "ScriptComponent.h"
|
|
|
|
|
2021-05-01 16:04:53 +02:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2021-06-07 20:48:16 +02:00
|
|
|
#include "scriptinterface/JSON.h"
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2021-05-13 19:23:52 +02:00
|
|
|
#include "scriptinterface/Object.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "simulation2/serialization/ISerializer.h"
|
|
|
|
#include "simulation2/serialization/IDeserializer.h"
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) :
|
2021-05-16 17:12:39 +02:00
|
|
|
m_ScriptInterface(scriptInterface)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2021-05-16 17:12:39 +02:00
|
|
|
m_Instance.init(ScriptRequest(m_ScriptInterface).cx, instance);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2021-05-01 16:04:53 +02:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
|
|
|
|
Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
|
2021-05-01 16:04:53 +02:00
|
|
|
ScriptFunction::CallVoid(rq, m_Instance, "Init");
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
void CComponentTypeScript::Deinit()
|
2015-01-24 15:46:52 +01:00
|
|
|
{
|
2021-05-01 16:04:53 +02:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
ScriptFunction::CallVoid(rq, m_Instance, "Deinit");
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
|
2010-01-09 20:20:14 +01: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2023-06-21 09:50:00 +02:00
|
|
|
JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(rq));
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2021-05-01 16:04:53 +02:00
|
|
|
if (!ScriptFunction::CallVoid(rq, m_Instance, name, msgVal))
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Script message handler %s failed", name);
|
2010-01-09 20:20:14 +01: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2011-03-05 23:30:32 +01:00
|
|
|
|
2021-06-07 20:48:16 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
serialize.ScriptVal("comp", &m_Instance);
|
|
|
|
}
|
|
|
|
catch(PSERROR_Serialize& err)
|
|
|
|
{
|
|
|
|
int ent = INVALID_ENTITY;
|
|
|
|
Script::GetProperty(rq, m_Instance, "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));
|
|
|
|
// Rethrow now that we added more details
|
|
|
|
throw;
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
|
2016-11-23 15:09:58 +01: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 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
|
|
|
|
Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
|
2015-11-14 20:03:20 +01:00
|
|
|
|
2021-01-12 17:15:40 +01:00
|
|
|
deserialize.ScriptObjectAssign("comp", m_Instance);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|