2026-02-02 09:24:57 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-05-25 13:48:06 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-05-25 13:48:06 -07: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-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-05-25 13:48:06 -07: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-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-25 13:48:06 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "lib/self_test.h"
|
|
|
|
|
|
2025-07-06 11:15:27 -07:00
|
|
|
#include "lib/types.h"
|
2010-05-25 13:48:06 -07:00
|
|
|
#include "network/NetMessage.h"
|
2025-07-06 11:15:27 -07:00
|
|
|
#include "ps/CStr.h"
|
2021-05-13 10:23:52 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
2025-07-06 11:15:27 -07:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
2010-05-25 13:48:06 -07:00
|
|
|
|
|
|
|
|
class TestNetMessage : public CxxTest::TestSuite
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void test_sim()
|
|
|
|
|
{
|
2020-11-14 02:57:50 -08:00
|
|
|
ScriptInterface script("Test", "Test", g_ScriptContext);
|
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(script);
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2026-02-02 09:24:57 -08:00
|
|
|
JS::RootedValueArray<1> val{rq.cx, JS::ValueArray<1>{JS::NumberValue(4)}};
|
|
|
|
|
CSimulationMessage msg(script, 1, 2, 3,
|
|
|
|
|
JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, val))});
|
2010-05-25 13:48:06 -07:00
|
|
|
TS_ASSERT_STR_EQUALS(msg.ToString(), "CSimulationMessage { m_Client: 1, m_Player: 2, m_Turn: 3, m_Data: [4] }");
|
|
|
|
|
|
|
|
|
|
size_t len = msg.GetSerializedLength();
|
|
|
|
|
u8* buf = new u8[len+1];
|
|
|
|
|
buf[len] = '!';
|
|
|
|
|
TS_ASSERT_EQUALS(msg.Serialize(buf) - (buf+len), 0);
|
|
|
|
|
TS_ASSERT_EQUALS(buf[len], '!');
|
|
|
|
|
|
|
|
|
|
CNetMessage* msg2 = CNetMessageFactory::CreateMessage(buf, len, script);
|
|
|
|
|
TS_ASSERT_STR_EQUALS(((CSimulationMessage*)msg2)->ToString(), "CSimulationMessage { m_Client: 1, m_Player: 2, m_Turn: 3, m_Data: [4] }");
|
|
|
|
|
|
|
|
|
|
delete msg2;
|
|
|
|
|
delete[] buf;
|
|
|
|
|
}
|
|
|
|
|
};
|