Index: ps/trunk/source/simulation2/serialization/BinarySerializer.cpp
===================================================================
--- ps/trunk/source/simulation2/serialization/BinarySerializer.cpp (revision 25150)
+++ ps/trunk/source/simulation2/serialization/BinarySerializer.cpp (revision 25151)
@@ -1,474 +1,490 @@
-/* Copyright (C) 2020 Wildfire Games.
+/* Copyright (C) 2021 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "BinarySerializer.h"
#include "lib/alignment.h"
#include "ps/CLogger.h"
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/ScriptExtraHeaders.h"
#include "SerializedScriptTypes.h"
static u8 GetArrayType(js::Scalar::Type arrayType)
{
switch(arrayType)
{
case js::Scalar::Int8:
return SCRIPT_TYPED_ARRAY_INT8;
case js::Scalar::Uint8:
return SCRIPT_TYPED_ARRAY_UINT8;
case js::Scalar::Int16:
return SCRIPT_TYPED_ARRAY_INT16;
case js::Scalar::Uint16:
return SCRIPT_TYPED_ARRAY_UINT16;
case js::Scalar::Int32:
return SCRIPT_TYPED_ARRAY_INT32;
case js::Scalar::Uint32:
return SCRIPT_TYPED_ARRAY_UINT32;
case js::Scalar::Float32:
return SCRIPT_TYPED_ARRAY_FLOAT32;
case js::Scalar::Float64:
return SCRIPT_TYPED_ARRAY_FLOAT64;
case js::Scalar::Uint8Clamped:
return SCRIPT_TYPED_ARRAY_UINT8_CLAMPED;
default:
LOGERROR("Cannot serialize unrecognized typed array view: %d", arrayType);
throw PSERROR_Serialize_InvalidScriptValue();
}
}
CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(const ScriptInterface& scriptInterface, ISerializer& serializer) :
m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_ScriptBackrefsNext(0)
{
ScriptRequest rq(m_ScriptInterface);
JS_AddExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, this);
}
CBinarySerializerScriptImpl::~CBinarySerializerScriptImpl()
{
JS_RemoveExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, this);
}
void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val)
{
ScriptRequest rq(m_ScriptInterface);
switch (JS_TypeOfValue(rq.cx, val))
{
case JSTYPE_UNDEFINED:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID);
break;
}
case JSTYPE_NULL: // This type is never actually returned (it's a JS2 feature)
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
break;
}
case JSTYPE_OBJECT:
{
if (val.isNull())
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
break;
}
JS::RootedObject obj(rq.cx, &val.toObject());
// If we've already serialized this object, just output a reference to it
u32 tag = GetScriptBackrefTag(obj);
if (tag != 0)
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BACKREF);
m_Serializer.NumberU32("tag", tag, 0, JSVAL_INT_MAX);
break;
}
// Arrays, Maps and Sets are special cases of Objects
bool isArray;
bool isMap;
bool isSet;
if (JS::IsArrayObject(rq.cx, obj, &isArray) && isArray)
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY);
// TODO: probably should have a more efficient storage format
// Arrays like [1, 2, ] have an 'undefined' at the end which is part of the
// length but seemingly isn't enumerated, so store the length explicitly
uint length = 0;
if (!JS::GetArrayLength(rq.cx, obj, &length))
throw PSERROR_Serialize_ScriptError("JS::GetArrayLength failed");
m_Serializer.NumberU32_Unbounded("array length", length);
}
else if (JS_IsTypedArrayObject(obj))
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_TYPED_ARRAY);
m_Serializer.NumberU8_Unbounded("array type", GetArrayType(JS_GetArrayBufferViewType(obj)));
m_Serializer.NumberU32_Unbounded("byte offset", JS_GetTypedArrayByteOffset(obj));
m_Serializer.NumberU32_Unbounded("length", JS_GetTypedArrayLength(obj));
bool sharedMemory;
// Now handle its array buffer
// this may be a backref, since ArrayBuffers can be shared by multiple views
JS::RootedValue bufferVal(rq.cx, JS::ObjectValue(*JS_GetArrayBufferViewBuffer(rq.cx, obj, &sharedMemory)));
HandleScriptVal(bufferVal);
break;
}
else if (JS::IsArrayBufferObject(obj))
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY_BUFFER);
#if BYTE_ORDER != LITTLE_ENDIAN
#error TODO: need to convert JS ArrayBuffer data to little-endian
#endif
u32 length = JS::GetArrayBufferByteLength(obj);
m_Serializer.NumberU32_Unbounded("buffer length", length);
JS::AutoCheckCannotGC nogc;
bool sharedMemory;
m_Serializer.RawBytes("buffer data", (const u8*)JS::GetArrayBufferData(obj, &sharedMemory, nogc), length);
break;
}
else if (JS::IsMapObject(rq.cx, obj, &isMap) && isMap)
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP);
m_Serializer.NumberU32_Unbounded("map size", JS::MapSize(rq.cx, obj));
JS::RootedValue keyValueIterator(rq.cx);
if (!JS::MapEntries(rq.cx, obj, &keyValueIterator))
throw PSERROR_Serialize_ScriptError("JS::MapEntries failed");
JS::ForOfIterator it(rq.cx);
if (!it.init(keyValueIterator))
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed");
JS::RootedValue keyValuePair(rq.cx);
bool done;
while (true)
{
if (!it.next(&keyValuePair, &done))
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed");
if (done)
break;
JS::RootedObject keyValuePairObj(rq.cx, &keyValuePair.toObject());
JS::RootedValue key(rq.cx);
JS::RootedValue value(rq.cx);
ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 0, &key));
ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 1, &value));
HandleScriptVal(key);
HandleScriptVal(value);
}
break;
}
else if (JS::IsSetObject(rq.cx, obj, &isSet) && isSet)
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_SET);
m_Serializer.NumberU32_Unbounded("set size", JS::SetSize(rq.cx, obj));
JS::RootedValue valueIterator(rq.cx);
if (!JS::SetValues(rq.cx, obj, &valueIterator))
throw PSERROR_Serialize_ScriptError("JS::SetValues failed");
JS::ForOfIterator it(rq.cx);
if (!it.init(valueIterator))
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed");
JS::RootedValue value(rq.cx);
bool done;
while (true)
{
if (!it.next(&value, &done))
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed");
if (done)
break;
HandleScriptVal(value);
}
break;
}
else
{
// Find type of object
const JSClass* jsclass = JS_GetClass(obj);
if (!jsclass)
throw PSERROR_Serialize_ScriptError("JS_GetClass failed");
JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass);
if (protokey == JSProto_Object)
{
// Object class - check for user-defined prototype
JS::RootedObject proto(rq.cx);
if (!JS_GetPrototype(rq.cx, obj, &proto))
throw PSERROR_Serialize_ScriptError("JS_GetPrototype failed");
SPrototypeSerialization protoInfo = GetPrototypeInfo(rq, proto);
if (protoInfo.name == "Object")
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT);
else
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_PROTOTYPE);
m_Serializer.String("proto", wstring_from_utf8(protoInfo.name), 0, 256);
// Does it have custom Serialize function?
// if so, we serialize the data it returns, rather than the object's properties directly
if (protoInfo.hasCustomSerialize)
{
// If serialize is null, don't serialize anything more
if (!protoInfo.hasNullSerialize)
{
JS::RootedValue data(rq.cx);
if (!m_ScriptInterface.CallFunction(val, "Serialize", &data))
throw PSERROR_Serialize_ScriptError("Prototype Serialize function failed");
m_Serializer.ScriptVal("data", &data);
}
// Break here to skip the custom object property serialization logic below.
break;
}
}
}
else if (protokey == JSProto_Number)
{
- // Standard Number object
- m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_NUMBER);
// Get primitive value
double d;
if (!JS::ToNumber(rq.cx, val, &d))
throw PSERROR_Serialize_ScriptError("JS::ToNumber failed");
+
+ // Refuse to serialize NaN values: their representation can differ, leading to OOS
+ // and in general this is indicative of an underlying bug rather than desirable behaviour.
+ if (std::isnan(d))
+ {
+ LOGERROR("Cannot serialize NaN values.");
+ throw PSERROR_Serialize_InvalidScriptValue();
+ }
+
+ // Standard Number object
+ m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_NUMBER);
m_Serializer.NumberDouble_Unbounded("value", d);
break;
}
else if (protokey == JSProto_String)
{
// Standard String object
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_STRING);
// Get primitive value
JS::RootedString str(rq.cx, JS::ToString(rq.cx, val));
if (!str)
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
ScriptString("value", str);
break;
}
else if (protokey == JSProto_Boolean)
{
// Standard Boolean object
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_BOOLEAN);
// Get primitive value
bool b = JS::ToBoolean(val);
m_Serializer.Bool("value", b);
break;
}
else
{
// Unrecognized class
LOGERROR("Cannot serialise JS objects with unrecognized class '%s'", jsclass->name);
throw PSERROR_Serialize_InvalidScriptValue();
}
}
// Find all properties (ordered by insertion time)
JS::Rooted ida(rq.cx, JS::IdVector(rq.cx));
if (!JS_Enumerate(rq.cx, obj, &ida))
throw PSERROR_Serialize_ScriptError("JS_Enumerate failed");
m_Serializer.NumberU32_Unbounded("num props", (u32)ida.length());
for (size_t i = 0; i < ida.length(); ++i)
{
JS::RootedId id(rq.cx, ida[i]);
JS::RootedValue idval(rq.cx);
JS::RootedValue propval(rq.cx);
// Forbid getters, which might delete values and mess things up.
JS::Rooted desc(rq.cx);
if (!JS_GetPropertyDescriptorById(rq.cx, obj, id, &desc))
throw PSERROR_Serialize_ScriptError("JS_GetPropertyDescriptorById failed");
if (desc.hasGetterObject())
throw PSERROR_Serialize_ScriptError("Cannot serialize property getters");
// Get the property name as a string
if (!JS_IdToValue(rq.cx, id, &idval))
throw PSERROR_Serialize_ScriptError("JS_IdToValue failed");
JS::RootedString idstr(rq.cx, JS::ToString(rq.cx, idval));
if (!idstr)
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
ScriptString("prop name", idstr);
if (!JS_GetPropertyById(rq.cx, obj, id, &propval))
throw PSERROR_Serialize_ScriptError("JS_GetPropertyById failed");
HandleScriptVal(propval);
}
break;
}
case JSTYPE_FUNCTION:
{
// We can't serialise functions, but we can at least name the offender (hopefully)
std::wstring funcname(L"(unnamed)");
JS::RootedFunction func(rq.cx, JS_ValueToFunction(rq.cx, val));
if (func)
{
JS::RootedString string(rq.cx, JS_GetFunctionId(func));
if (string)
{
if (JS_StringHasLatin1Chars(string))
{
size_t length;
JS::AutoCheckCannotGC nogc;
const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length);
if (ch && length > 0)
funcname.assign(ch, ch + length);
}
else
{
size_t length;
JS::AutoCheckCannotGC nogc;
const char16_t* ch = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length);
if (ch && length > 0)
funcname.assign(ch, ch + length);
}
}
}
LOGERROR("Cannot serialise JS objects of type 'function': %s", utf8_from_wstring(funcname));
throw PSERROR_Serialize_InvalidScriptValue();
}
case JSTYPE_STRING:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING);
JS::RootedString stringVal(rq.cx, val.toString());
ScriptString("string", stringVal);
break;
}
case JSTYPE_NUMBER:
{
+ // Refuse to serialize NaN values: their representation can differ, leading to OOS
+ // and in general this is indicative of an underlying bug rather than desirable behaviour.
+ if (val == JS::NaNValue())
+ {
+ LOGERROR("Cannot serialize NaN values.");
+ throw PSERROR_Serialize_InvalidScriptValue();
+ }
+
// To reduce the size of the serialized data, we handle integers and doubles separately.
// We can't check for val.isInt32 and val.isDouble directly, because integer numbers are not guaranteed
// to be represented as integers. A number like 33 could be stored as integer on the computer of one player
// and as double on the other player's computer. That would cause out of sync errors in multiplayer games because
// their binary representation and thus the hash would be different.
-
double d;
d = val.toNumber();
i32 integer;
if (JS_DoubleIsInt32(d, &integer))
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT);
m_Serializer.NumberI32_Unbounded("value", integer);
}
else
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE);
m_Serializer.NumberDouble_Unbounded("value", d);
}
break;
}
case JSTYPE_BOOLEAN:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN);
bool b = val.toBoolean();
m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0);
break;
}
default:
{
debug_warn(L"Invalid TypeOfValue");
throw PSERROR_Serialize_InvalidScriptValue();
}
}
}
void CBinarySerializerScriptImpl::ScriptString(const char* name, JS::HandleString string)
{
ScriptRequest rq(m_ScriptInterface);
#if BYTE_ORDER != LITTLE_ENDIAN
#error TODO: probably need to convert JS strings to little-endian
#endif
size_t length;
JS::AutoCheckCannotGC nogc;
// Serialize strings directly as UTF-16 or Latin1, to avoid expensive encoding conversions
bool isLatin1 = JS_StringHasLatin1Chars(string);
m_Serializer.Bool("isLatin1", isLatin1);
if (isLatin1)
{
const JS::Latin1Char* chars = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length);
if (!chars)
throw PSERROR_Serialize_ScriptError("JS_GetLatin1StringCharsAndLength failed");
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
m_Serializer.RawBytes(name, (const u8*)chars, length);
}
else
{
const char16_t* chars = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length);
if (!chars)
throw PSERROR_Serialize_ScriptError("JS_GetTwoByteStringCharsAndLength failed");
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
m_Serializer.RawBytes(name, (const u8*)chars, length*2);
}
}
void CBinarySerializerScriptImpl::Trace(JSTracer *trc, void *data)
{
CBinarySerializerScriptImpl* serializer = static_cast(data);
serializer->m_ScriptBackrefTags.trace(trc);
}
u32 CBinarySerializerScriptImpl::GetScriptBackrefTag(JS::HandleObject obj)
{
// To support non-tree structures (e.g. "var x = []; var y = [x, x];"), we need a way
// to indicate multiple references to one object(/array). So every time we serialize a
// new object, we give it a new tag; when we serialize it a second time we just refer
// to that tag.
ScriptRequest rq(m_ScriptInterface);
ObjectTagMap::Ptr ptr = m_ScriptBackrefTags.lookup(JS::Heap(obj.get()));
if (!ptr.found())
{
if (!m_ScriptBackrefTags.put(JS::Heap(obj.get()), ++m_ScriptBackrefsNext))
{
JS::RootedValue objval(rq.cx, JS::ObjectValue(*obj.get()));
LOGERROR("BinarySerializer: error at insertion. Object was %s", m_ScriptInterface.ToString(&objval));
return 0;
}
// Return 0 to mean "you have to serialize this object";
return 0;
}
else
return ptr->value();
}
Index: ps/trunk/source/simulation2/tests/test_Serializer.h
===================================================================
--- ps/trunk/source/simulation2/tests/test_Serializer.h (revision 25150)
+++ ps/trunk/source/simulation2/tests/test_Serializer.h (revision 25151)
@@ -1,936 +1,939 @@
/* Copyright (C) 2021 Wildfire Games.
* 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 .
*/
#include "lib/self_test.h"
#include "simulation2/serialization/DebugSerializer.h"
#include "simulation2/serialization/HashSerializer.h"
#include "simulation2/serialization/StdSerializer.h"
#include "simulation2/serialization/StdDeserializer.h"
#include "scriptinterface/ScriptContext.h"
#include "scriptinterface/ScriptInterface.h"
#include "graphics/MapReader.h"
#include "graphics/Terrain.h"
#include "graphics/TerrainTextureManager.h"
#include "lib/timer.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/Loader.h"
#include "ps/XML/Xeromyces.h"
#include "simulation2/Simulation2.h"
#include "callgrind.h"
#include
#define TS_ASSERT_STREAM(stream, len, buffer) \
TS_ASSERT_EQUALS(stream.str().length(), (size_t)len); \
TS_ASSERT_SAME_DATA(stream.str().data(), buffer, len)
#define TSM_ASSERT_STREAM(m, stream, len, buffer) \
TSM_ASSERT_EQUALS(m, stream.str().length(), (size_t)len); \
TSM_ASSERT_SAME_DATA(m, stream.str().data(), buffer, len)
class TestSerializer : public CxxTest::TestSuite
{
public:
void serialize_types(ISerializer& serialize)
{
serialize.NumberI8_Unbounded("i8", (signed char)-123);
serialize.NumberU8_Unbounded("u8", (unsigned char)255);
serialize.NumberI16_Unbounded("i16", -12345);
serialize.NumberU16_Unbounded("u16", 56789);
serialize.NumberI32_Unbounded("i32", -123);
serialize.NumberU32_Unbounded("u32", (unsigned)-123);
serialize.NumberFloat_Unbounded("float", 1e+30f);
serialize.NumberDouble_Unbounded("double", 1e+300);
serialize.NumberFixed_Unbounded("fixed", fixed::FromFloat(1234.5f));
serialize.Bool("t", true);
serialize.Bool("f", false);
serialize.StringASCII("string", "example", 0, 255);
serialize.StringASCII("string 2", "example\"\\\"", 0, 255);
serialize.StringASCII("string 3", "example\n\ntest", 0, 255);
wchar_t testw[] = { 't', 0xEA, 's', 't', 0 };
serialize.String("string 4", testw, 0, 255);
serialize.RawBytes("raw bytes", (const u8*)"\0\1\2\3\x0f\x10", 6);
}
void test_Debug_basic()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CDebugSerializer serialize(script, stream);
serialize.NumberI32_Unbounded("x", -123);
serialize.NumberU32_Unbounded("y", 1234);
serialize.NumberI32("z", 12345, 0, 65535);
TS_ASSERT_STR_EQUALS(stream.str(), "x: -123\ny: 1234\nz: 12345\n");
}
void test_Debug_floats()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CDebugSerializer serialize(script, stream);
serialize.NumberFloat_Unbounded("x", 1e4f);
serialize.NumberFloat_Unbounded("x", 1e-4f);
serialize.NumberFloat_Unbounded("x", 1e5f);
serialize.NumberFloat_Unbounded("x", 1e-5f);
serialize.NumberFloat_Unbounded("x", 1e6f);
serialize.NumberFloat_Unbounded("x", 1e-6f);
serialize.NumberFloat_Unbounded("x", 1e10f);
serialize.NumberFloat_Unbounded("x", 1e-10f);
serialize.NumberDouble_Unbounded("x", 1e4);
serialize.NumberDouble_Unbounded("x", 1e-4);
serialize.NumberDouble_Unbounded("x", 1e5);
serialize.NumberDouble_Unbounded("x", 1e-5);
serialize.NumberDouble_Unbounded("x", 1e6);
serialize.NumberDouble_Unbounded("x", 1e-6);
serialize.NumberDouble_Unbounded("x", 1e10);
serialize.NumberDouble_Unbounded("x", 1e-10);
serialize.NumberDouble_Unbounded("x", 1e100);
serialize.NumberDouble_Unbounded("x", 1e-100);
serialize.NumberFixed_Unbounded("x", fixed::FromDouble(1e4));
TS_ASSERT_STR_EQUALS(stream.str(),
"x: 10000\nx: 9.9999997e-05\nx: 100000\nx: 9.9999997e-06\nx: 1000000\nx: 1e-06\nx: 1e+10\nx: 1e-10\n"
"x: 10000\nx: 0.0001\nx: 100000\nx: 1.0000000000000001e-05\nx: 1000000\nx: 9.9999999999999995e-07\nx: 10000000000\nx: 1e-10\nx: 1e+100\nx: 1e-100\n"
"x: 10000\n"
);
}
void test_Debug_types()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CDebugSerializer serialize(script, stream);
serialize.Comment("comment");
serialize_types(serialize);
TS_ASSERT_STR_EQUALS(stream.str(),
"# comment\n"
"i8: -123\n"
"u8: 255\n"
"i16: -12345\n"
"u16: 56789\n"
"i32: -123\n"
"u32: 4294967173\n"
"float: 1e+30\n"
"double: 1.0000000000000001e+300\n"
"fixed: 1234.5\n"
"t: true\n"
"f: false\n"
"string: \"example\"\n"
"string 2: \"example\\\"\\\\\\\"\"\n" // C-escaped form of: "example\"\\\""
"string 3: \"example\\n\\ntest\"\n"
"string 4: \"t\xC3\xAAst\"\n"
"raw bytes: (6 bytes) 00 01 02 03 0f 10\n"
);
}
void test_Std_basic()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CStdSerializer serialize(script, stream);
serialize.NumberI32_Unbounded("x", -123);
serialize.NumberU32_Unbounded("y", 1234);
serialize.NumberI32("z", 12345, 0, 65535);
TS_ASSERT_STREAM(stream, 12, "\x85\xff\xff\xff" "\xd2\x04\x00\x00" "\x39\x30\x00\x00");
CStdDeserializer deserialize(script, stream);
int32_t n;
deserialize.NumberI32_Unbounded("x", n);
TS_ASSERT_EQUALS(n, -123);
deserialize.NumberI32_Unbounded("y", n);
TS_ASSERT_EQUALS(n, 1234);
deserialize.NumberI32("z", n, 0, 65535);
TS_ASSERT_EQUALS(n, 12345);
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
TS_ASSERT(!stream.bad() && !stream.fail());
TS_ASSERT_EQUALS(stream.peek(), EOF);
}
void test_Std_types()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CStdSerializer serialize(script, stream);
serialize_types(serialize);
CStdDeserializer deserialize(script, stream);
int8_t i8v;
uint8_t u8v;
int16_t i16v;
uint16_t u16v;
int32_t i32v;
uint32_t u32v;
float flt;
double dbl;
fixed fxd;
bool bl;
std::string str;
std::wstring wstr;
u8 cbuf[256];
deserialize.NumberI8_Unbounded("i8", i8v);
TS_ASSERT_EQUALS(i8v, -123);
deserialize.NumberU8_Unbounded("u8", u8v);
TS_ASSERT_EQUALS(u8v, 255);
deserialize.NumberI16_Unbounded("i16", i16v);
TS_ASSERT_EQUALS(i16v, -12345);
deserialize.NumberU16_Unbounded("u16", u16v);
TS_ASSERT_EQUALS(u16v, 56789);
deserialize.NumberI32_Unbounded("i32", i32v);
TS_ASSERT_EQUALS(i32v, -123);
deserialize.NumberU32_Unbounded("u32", u32v);
TS_ASSERT_EQUALS(u32v, 4294967173u);
deserialize.NumberFloat_Unbounded("float", flt);
TS_ASSERT_EQUALS(flt, 1e+30f);
deserialize.NumberDouble_Unbounded("double", dbl);
TS_ASSERT_EQUALS(dbl, 1e+300);
deserialize.NumberFixed_Unbounded("fixed", fxd);
TS_ASSERT_EQUALS(fxd.ToDouble(), 1234.5);
deserialize.Bool("t", bl);
TS_ASSERT_EQUALS(bl, true);
deserialize.Bool("f", bl);
TS_ASSERT_EQUALS(bl, false);
deserialize.StringASCII("string", str, 0, 255);
TS_ASSERT_STR_EQUALS(str, "example");
deserialize.StringASCII("string 2", str, 0, 255);
TS_ASSERT_STR_EQUALS(str, "example\"\\\"");
deserialize.StringASCII("string 3", str, 0, 255);
TS_ASSERT_STR_EQUALS(str, "example\n\ntest");
wchar_t testw[] = { 't', 0xEA, 's', 't', 0 };
deserialize.String("string 4", wstr, 0, 255);
TS_ASSERT_WSTR_EQUALS(wstr, testw);
cbuf[6] = 0x42; // sentinel
deserialize.RawBytes("raw bytes", cbuf, 6);
TS_ASSERT_SAME_DATA(cbuf, (const u8*)"\0\1\2\3\x0f\x10\x42", 7);
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
TS_ASSERT(!stream.bad() && !stream.fail());
TS_ASSERT_EQUALS(stream.peek(), EOF);
}
void test_Hash_basic()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
CHashSerializer serialize(script);
serialize.NumberI32_Unbounded("x", -123);
serialize.NumberU32_Unbounded("y", 1234);
serialize.NumberI32("z", 12345, 0, 65535);
TS_ASSERT_EQUALS(serialize.GetHashLength(), (size_t)16);
TS_ASSERT_SAME_DATA(serialize.ComputeHash(), "\xa0\x3a\xe5\x3e\x9b\xd7\xfb\x11\x88\x35\xc6\xfb\xb9\x94\xa9\x72", 16);
// echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00" | openssl md5 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g'
}
void test_Hash_stream()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
CHashSerializer hashSerialize(script);
hashSerialize.NumberI32_Unbounded("x", -123);
hashSerialize.NumberU32_Unbounded("y", 1234);
hashSerialize.NumberI32("z", 12345, 0, 65535);
ISerializer& serialize = hashSerialize;
{
CStdSerializer streamSerialize(script, serialize.GetStream());
streamSerialize.NumberI32_Unbounded("x2", -456);
streamSerialize.NumberU32_Unbounded("y2", 5678);
streamSerialize.NumberI32("z2", 45678, 0, 65535);
}
TS_ASSERT_EQUALS(hashSerialize.GetHashLength(), (size_t)16);
TS_ASSERT_SAME_DATA(hashSerialize.ComputeHash(), "\x5c\xff\x33\xd1\x72\xdd\x6d\x77\xa8\xd4\xa1\xf6\x84\xcc\xaa\x10", 16);
// echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00\x38\xfe\xff\xff\x2e\x16\x00\x00\x6e\xb2\x00\x00" | openssl md5 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g'
}
void test_bounds()
{
ScriptInterface script("Test", "Test", g_ScriptContext);
std::stringstream stream;
CDebugSerializer serialize(script, stream);
serialize.NumberI32("x", 16, -16, 16);
serialize.NumberI32("x", -16, -16, 16);
TS_ASSERT_THROWS(serialize.NumberI32("x", 99, -16, 16), const PSERROR_Serialize_OutOfBounds&);
TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), const PSERROR_Serialize_OutOfBounds&);
}
// TODO: test exceptions more thoroughly
void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL)
{
ScriptInterface script("Test", "Test", g_ScriptContext);
ScriptRequest rq(script);
JS::RootedValue obj(rq.cx);
TSM_ASSERT(msg, script.Eval(input, &obj));
if (debug)
{
std::stringstream dbgstream;
CDebugSerializer serialize(script, dbgstream);
serialize.ScriptVal("script", &obj);
TS_ASSERT_STR_EQUALS(dbgstream.str(), debug);
}
std::stringstream stream;
CStdSerializer serialize(script, stream);
serialize.ScriptVal("script", &obj);
if (expstream)
{
TSM_ASSERT_STREAM(msg, stream, expstreamlen, expstream);
}
CStdDeserializer deserialize(script, stream);
JS::RootedValue newobj(rq.cx);
deserialize.ScriptVal("script", &newobj);
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions
TSM_ASSERT(msg, !stream.bad() && !stream.fail());
TSM_ASSERT_EQUALS(msg, stream.peek(), EOF);
std::stringstream stream2;
CStdSerializer serialize2(script, stream2);
CStdDeserializer deserialize2(script, stream2);
// Round-trip the deserialized value again. This helps ensure prototypes are correctly deserialized.
serialize2.ScriptVal("script2", &newobj);
deserialize2.ScriptVal("script2", &newobj);
std::string source;
TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source));
TS_ASSERT_STR_EQUALS(source, expected);
}
void test_script_basic()
{
helper_script_roundtrip("Object",
"({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})",
/* expected: */
"({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})",
/* expected stream: */
116,
"\x03" // SCRIPT_TYPE_OBJECT
"\x02\0\0\0" // num props
"\x01\x01\0\0\0" "x" // "x"
"\x05" // SCRIPT_TYPE_INT
"\x7b\0\0\0" // 123
"\x01\x01\0\0\0" "y" // "y"
"\x02" // SCRIPT_TYPE_ARRAY
"\x08\0\0\0" // array length
"\x08\0\0\0" // num props
"\x01\x01\0\0\0" "0" // "0"
"\x05" "\x01\0\0\0" // SCRIPT_TYPE_INT 1
"\x01\x01\0\0\0" "1" // "1"
"\x06" "\0\0\0\0\0\0\xf8\x3f" // SCRIPT_TYPE_DOUBLE 1.5
"\x01\x01\0\0\0" "2" // "2"
"\x04" "\x01\x01\0\0\0" "2" // SCRIPT_TYPE_STRING "2"
"\x01\x01\0\0\0" "3" // "3"
"\x04" "\x01\x04\0\0\0" "test" // SCRIPT_TYPE_STRING "test"
"\x01\x01\0\0\0" "4" // "4"
"\x00" // SCRIPT_TYPE_VOID
"\x01\x01\0\0\0" "5" // "5"
"\x01" // SCRIPT_TYPE_NULL
"\x01\x01\0\0\0" "6" // "6"
"\x07" "\x01" // SCRIPT_TYPE_BOOLEAN true
"\x01\x01\0\0\0" "7" // "7"
"\x07" "\x00", // SCRIPT_TYPE_BOOLEAN false
/* expected debug: */
"script: {\n"
" \"x\": 123,\n"
" \"y\": [\n"
" 1,\n"
" 1.5,\n"
" \"2\",\n"
" \"test\",\n"
" null,\n"
" null,\n"
" true,\n"
" false\n"
" ]\n"
"}\n"
);
}
void test_script_unicode()
{
helper_script_roundtrip("unicode", "({"
"'x': \"\\x01\\x80\\xff\\u0100\\ud7ff\", "
"'y': \"\\ue000\\ufffd\""
"})",
/* expected: */
"({"
"x:\"\\x01\\x80\\xFF\\u0100\\uD7FF\", "
"y:\"\\uE000\\uFFFD\""
"})");
// Disabled since we no longer do the UTF-8 conversion that rejects invalid characters
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 1", "(\"\\ud7ff\\ud800\")", "..."), PSERROR_Serialize_InvalidCharInString);
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 2", "(\"\\udfff\")", "..."), PSERROR_Serialize_InvalidCharInString);
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 3", "(\"\\uffff\")", "..."), PSERROR_Serialize_InvalidCharInString);
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 4", "(\"\\ud800\\udc00\")" /* U+10000 */, "..."), PSERROR_Serialize_InvalidCharInString);
helper_script_roundtrip("unicode", "\"\\ud800\\uffff\"", "(new String(\"\\uD800\\uFFFF\"))");
}
void test_script_objects()
{
helper_script_roundtrip("Number", "[1, new Number('2.0'), 3]", "[1, (new Number(2)), 3]");
helper_script_roundtrip("Number with props", "var n=new Number('2.0'); n.foo='bar'; n", "(new Number(2))");
helper_script_roundtrip("String", "['test1', new String('test2'), 'test3']", "[\"test1\", (new String(\"test2\")), \"test3\"]");
helper_script_roundtrip("String with props", "var s=new String('test'); s.foo='bar'; s", "(new String(\"test\"))");
helper_script_roundtrip("Boolean", "[new Boolean('true'), false]", "[(new Boolean(true)), false]");
helper_script_roundtrip("Boolean with props", "var b=new Boolean('true'); b.foo='bar'; b", "(new Boolean(true))");
}
void test_script_fancy_objects()
{
// This asserts that objects are deserialized with their correct prototypes.
helper_script_roundtrip("Custom Object", ""
"function customObj() { this.a = this.customFunc.name; };"
"customObj.prototype.customFunc = function customFunc(){};"
"new customObj();", "({a:\"customFunc\"})");
helper_script_roundtrip("Custom Class", ""
"class customObj {"
" constructor() { this.a = this.customFunc.name; }"
" customFunc(){};"
"}; new customObj();", "({a:\"customFunc\"})");
helper_script_roundtrip("Custom Class with Serialize/Deserialize()", ""
"class customObj {"
" constructor() { this.a = this.customFunc.name; }"
" Serialize() { return { 'foo': 'bar' }; }"
" Deserialize(data) { this.foo = data.foo; }"
" customFunc(){};"
"}; new customObj();", "({a:\"customFunc\", foo:\"bar\"})");
helper_script_roundtrip("Custom Class with null serialize & deserialize()", ""
"class customObj {"
" constructor() { this.a = this.customFunc.name; }"
" Deserialize(data) { this.test = 'test'; };"
" customFunc(){};"
"}; customObj.prototype.Serialize=null;"
"new customObj();", "({a:\"customFunc\", test:\"test\"})");
helper_script_roundtrip("Custom Class with arguments but still works", ""
"class customObj {"
" constructor(test) { this.a = test; }"
" Serialize() { return { 'data': this.a }; };"
" Deserialize(data) { this.a = data.data; };"
"}; new customObj(4);", "({a:4})");
}
void test_script_objects_properties()
{
helper_script_roundtrip("Object with null in prop name", "({\"foo\\0bar\":1})", "({\'foo\\x00bar\':1})");
}
void test_script_typed_arrays_simple()
{
helper_script_roundtrip("Int8Array",
"var arr=new Int8Array(8);"
"for(var i=0; iMount(L"", DataDir() / "mods" / "public" / "", VFS_MOUNT_MUST_EXIST));
TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir() / "_testcache" / "", 0, VFS_MAX_PRIORITY));
// Need some stuff for terrain movement costs:
// (TODO: this ought to be independent of any graphics code)
new CTerrainTextureManager;
g_TexMan.LoadTerrainTextures();
CTerrain terrain;
CSimulation2 sim2(NULL, g_ScriptContext, &terrain);
sim2.LoadDefaultScripts();
sim2.ResetState();
std::unique_ptr mapReader = std::make_unique();
LDR_BeginRegistering();
mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp",
*sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
sim2.Update(0);
{
std::stringstream str;
std::string hash;
sim2.SerializeState(str);
sim2.ComputeStateHash(hash, false);
debug_printf("\n");
debug_printf("# size = %d\n", (int)str.str().length());
debug_printf("# hash = ");
for (size_t i = 0; i < hash.size(); ++i)
debug_printf("%02x", (unsigned int)(u8)hash[i]);
debug_printf("\n");
}
double t = timer_Time();
CALLGRIND_START_INSTRUMENTATION;
size_t reps = 128;
for (size_t i = 0; i < reps; ++i)
{
std::string hash;
sim2.ComputeStateHash(hash, false);
}
CALLGRIND_STOP_INSTRUMENTATION;
t = timer_Time() - t;
debug_printf("# time = %f (%f/%d)\n", t/reps, t, (int)reps);
// Shut down the world
delete &g_TexMan;
g_VFS.reset();
DeleteDirectory(DataDir()/"_testcache");
CXeromyces::Terminate();
}
};