2016-01-11 12:03:28 -08:00
|
|
|
/* Copyright (C) 2016 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 "BinarySerializer.h"
|
|
|
|
|
|
2011-04-30 06:22:46 -07:00
|
|
|
#include "lib/alignment.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2016-08-02 09:12:11 -07:00
|
|
|
#include "scriptinterface/ScriptExtraHeaders.h"
|
2014-03-28 13:26:32 -07:00
|
|
|
#include "SerializedScriptTypes.h"
|
2013-05-26 14:57:24 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
static u8 GetArrayType(JSArrayBufferViewType arrayType)
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
switch(arrayType)
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_INT8:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT8;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_UINT8:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_INT16:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT16;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_UINT16:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT16;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_INT32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT32;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_UINT32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT32;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_FLOAT32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT32;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_FLOAT64:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT64;
|
2014-03-28 13:26:32 -07:00
|
|
|
case js::ArrayBufferView::TYPE_UINT8_CLAMPED:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8_CLAMPED;
|
|
|
|
|
default:
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Cannot serialize unrecognized typed array view: %d", arrayType);
|
2013-05-26 14:57:24 -07:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-25 11:44:33 -07:00
|
|
|
|
2010-05-25 12:01:30 -07:00
|
|
|
CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(ScriptInterface& scriptInterface, ISerializer& serializer) :
|
2015-01-24 06:46:52 -08:00
|
|
|
m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_ScriptBackrefs(scriptInterface.GetRuntime()),
|
|
|
|
|
m_SerializablePrototypes(new ObjectIdCache<std::wstring>(scriptInterface.GetRuntime())), m_ScriptBackrefsNext(1)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
m_ScriptBackrefs.init();
|
|
|
|
|
m_SerializablePrototypes->init();
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-31 12:18:40 -07:00
|
|
|
void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
JSContext* cx = m_ScriptInterface.GetContext();
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
switch (JS_TypeOfValue(cx, val))
|
|
|
|
|
{
|
|
|
|
|
case JSTYPE_VOID:
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_NULL: // This type is never actually returned (it's a JS2 feature)
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
if (val.isNull())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedObject obj(cx, &val.toObject());
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// If we've already serialized this object, just output a reference to it
|
|
|
|
|
u32 tag = GetScriptBackrefTag(obj);
|
|
|
|
|
if (tag)
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BACKREF);
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("tag", tag);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-26 14:57:24 -07:00
|
|
|
// Arrays are special cases of Object
|
2010-01-09 11:20:14 -08:00
|
|
|
if (JS_IsArrayObject(cx, obj))
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY);
|
2010-01-09 11:20:14 -08:00
|
|
|
// TODO: probably should have a more efficient storage format
|
2011-10-27 13:56:32 -07:00
|
|
|
|
|
|
|
|
// 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
|
2014-03-28 13:26:32 -07:00
|
|
|
uint length = 0;
|
2011-10-27 13:56:32 -07:00
|
|
|
if (!JS_GetArrayLength(cx, obj, &length))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetArrayLength failed");
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("array length", length);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2014-03-28 13:26:32 -07:00
|
|
|
else if (JS_IsTypedArrayObject(obj))
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_TYPED_ARRAY);
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
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));
|
2013-05-26 14:57:24 -07:00
|
|
|
|
|
|
|
|
// Now handle its array buffer
|
|
|
|
|
// this may be a backref, since ArrayBuffers can be shared by multiple views
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedValue bufferVal(cx, JS::ObjectValue(*JS_GetArrayBufferViewBuffer(cx, obj)));
|
2014-07-31 12:18:40 -07:00
|
|
|
HandleScriptVal(bufferVal);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2014-03-28 13:26:32 -07:00
|
|
|
else if (JS_IsArrayBufferObject(obj))
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
u32 length = JS_GetArrayBufferByteLength(obj);
|
2013-05-26 14:57:24 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("buffer length", length);
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.RawBytes("buffer data", (const u8*)JS_GetArrayBufferData(obj), length);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
else
|
|
|
|
|
{
|
2013-05-26 14:57:24 -07:00
|
|
|
// Find type of object
|
2015-01-24 06:46:52 -08:00
|
|
|
const JSClass* jsclass = JS_GetClass(obj);
|
2013-05-26 14:57:24 -07:00
|
|
|
if (!jsclass)
|
2014-03-28 13:26:32 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetClass failed");
|
2013-05-26 14:57:24 -07:00
|
|
|
JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass);
|
|
|
|
|
|
|
|
|
|
if (protokey == JSProto_Object)
|
|
|
|
|
{
|
|
|
|
|
// Object class - check for user-defined prototype
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedObject proto(cx);
|
2015-01-24 06:46:52 -08:00
|
|
|
JS_GetPrototype(cx, obj, &proto);
|
2013-05-26 14:57:24 -07:00
|
|
|
if (!proto)
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPrototype failed");
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
if (m_SerializablePrototypes->empty() || !IsSerializablePrototype(proto))
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
// Standard Object prototype
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT);
|
|
|
|
|
|
|
|
|
|
// TODO: maybe we should throw an error for unrecognized non-Object prototypes?
|
|
|
|
|
// (requires fixing AI serialization first and excluding component scripts)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// User-defined custom prototype
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_PROTOTYPE);
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
const std::wstring prototypeName = GetPrototypeName(proto);
|
2013-05-26 14:57:24 -07:00
|
|
|
m_Serializer.String("proto name", prototypeName, 0, 256);
|
|
|
|
|
|
|
|
|
|
// Does it have custom Serialize function?
|
|
|
|
|
// if so, we serialize the data it returns, rather than the object's properties directly
|
2015-01-24 06:46:52 -08:00
|
|
|
bool hasCustomSerialize;
|
2013-05-26 14:57:24 -07:00
|
|
|
if (!JS_HasProperty(cx, obj, "Serialize", &hasCustomSerialize))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_HasProperty failed");
|
2014-03-28 13:26:32 -07:00
|
|
|
|
2013-05-26 14:57:24 -07:00
|
|
|
if (hasCustomSerialize)
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue serialize(cx);
|
2016-01-11 12:03:33 -08:00
|
|
|
if (!JS_GetProperty(cx, obj, "Serialize", &serialize))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetProperty failed");
|
2013-05-26 14:57:24 -07:00
|
|
|
|
|
|
|
|
// If serialize is null, so don't serialize anything more
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!serialize.isNull())
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
2014-07-31 12:18:40 -07:00
|
|
|
JS::RootedValue data(cx);
|
|
|
|
|
if (!m_ScriptInterface.CallFunction(val, "Serialize", &data))
|
2013-05-26 14:57:24 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("Prototype Serialize function failed");
|
2014-07-31 12:18:40 -07:00
|
|
|
HandleScriptVal(data);
|
2013-05-26 14:57:24 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (protokey == JSProto_Number)
|
|
|
|
|
{
|
|
|
|
|
// Standard Number object
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_NUMBER);
|
|
|
|
|
// Get primitive value
|
2014-03-28 13:26:32 -07:00
|
|
|
double d;
|
|
|
|
|
if (!JS::ToNumber(cx, val, &d))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ToNumber failed");
|
2013-05-26 14:57:24 -07:00
|
|
|
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
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedString str(cx, JS::ToString(cx, val));
|
2013-05-26 14:57:24 -07:00
|
|
|
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
|
2014-03-28 13:26:32 -07:00
|
|
|
bool b = JS::ToBoolean(val);
|
|
|
|
|
m_Serializer.Bool("value", b);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2014-09-20 10:14:53 -07:00
|
|
|
else if (protokey == JSProto_Map)
|
|
|
|
|
{
|
|
|
|
|
// TODO: There's no C++ API (yet) to work with maps. This code relies on the internal
|
|
|
|
|
// structure of the Iterator object returned by Map.entries(). This is not ideal
|
|
|
|
|
// because the structure could change in the future (and actually does change with v31).
|
|
|
|
|
// Change this code if SpiderMonkey gets such an API.
|
|
|
|
|
u32 mapSize;
|
|
|
|
|
m_ScriptInterface.GetProperty(val, "size", mapSize);
|
|
|
|
|
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP);
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("map size", mapSize);
|
|
|
|
|
|
|
|
|
|
JS::RootedValue keyValueIterator(cx);
|
|
|
|
|
m_ScriptInterface.CallFunction(val, "entries", &keyValueIterator);
|
|
|
|
|
for (u32 i=0; i<mapSize; ++i)
|
|
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedValue currentIterator(cx);
|
2014-09-20 10:14:53 -07:00
|
|
|
JS::RootedValue keyValuePair(cx);
|
2015-01-24 06:46:52 -08:00
|
|
|
ENSURE(m_ScriptInterface.CallFunction(keyValueIterator, "next", ¤tIterator));
|
|
|
|
|
|
|
|
|
|
// the Iterator has a property called "value" that contains the key-value pair of the map
|
|
|
|
|
m_ScriptInterface.GetProperty(currentIterator, "value", &keyValuePair);
|
2014-09-20 10:14:53 -07:00
|
|
|
JS::RootedObject keyValuePairObj(cx, &keyValuePair.toObject());
|
|
|
|
|
|
|
|
|
|
JS::RootedValue key(cx);
|
|
|
|
|
JS::RootedValue value(cx);
|
2015-01-24 06:46:52 -08:00
|
|
|
ENSURE(JS_GetElement(cx, keyValuePairObj, 0, &key));
|
|
|
|
|
ENSURE(JS_GetElement(cx, keyValuePairObj, 1, &value));
|
2014-09-20 10:14:53 -07:00
|
|
|
|
|
|
|
|
HandleScriptVal(key);
|
|
|
|
|
HandleScriptVal(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-05-01 16:04:28 -07:00
|
|
|
else if (protokey == JSProto_Set)
|
|
|
|
|
{
|
|
|
|
|
// TODO: There's no C++ API (yet) to work with sets. This code relies on the internal
|
|
|
|
|
// structure of the Iterator object returned by Set.values(). This is not ideal
|
|
|
|
|
// because the structure could change in the future.
|
|
|
|
|
// Change this code if SpiderMonkey gets such an API.
|
|
|
|
|
u32 setSize;
|
|
|
|
|
m_ScriptInterface.GetProperty(val, "size", setSize);
|
|
|
|
|
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_SET);
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("set size", setSize);
|
|
|
|
|
|
|
|
|
|
JS::RootedValue valueIterator(cx);
|
|
|
|
|
m_ScriptInterface.CallFunction(val, "values", &valueIterator);
|
|
|
|
|
for (u32 i=0; i<setSize; ++i)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedValue currentIterator(cx);
|
|
|
|
|
JS::RootedValue value(cx);
|
|
|
|
|
ENSURE(m_ScriptInterface.CallFunction(valueIterator, "next", ¤tIterator));
|
|
|
|
|
|
|
|
|
|
m_ScriptInterface.GetProperty(currentIterator, "value", &value);
|
|
|
|
|
|
|
|
|
|
HandleScriptVal(value);
|
|
|
|
|
}
|
2015-05-03 18:41:09 -07:00
|
|
|
|
|
|
|
|
break;
|
2015-05-01 16:04:28 -07:00
|
|
|
}
|
2013-05-26 14:57:24 -07:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Unrecognized class
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGERROR("Cannot serialise JS objects with unrecognized class '%s'", jsclass->name);
|
2013-05-26 14:57:24 -07:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find all properties (ordered by insertion time)
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::AutoIdArray ida (cx, JS_Enumerate(cx, obj));
|
|
|
|
|
if (!ida)
|
2010-11-16 15:00:52 -08:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_Enumerate failed");
|
2010-05-25 11:24:12 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("num props", (u32)ida.length());
|
2010-06-27 04:57:00 -07:00
|
|
|
|
2010-11-16 15:00:52 -08:00
|
|
|
for (size_t i = 0; i < ida.length(); ++i)
|
2010-06-27 04:57:00 -07:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedId id(cx, ida[i]);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue idval(cx);
|
|
|
|
|
JS::RootedValue propval(cx);
|
2016-01-11 12:03:33 -08:00
|
|
|
|
|
|
|
|
// Forbid getters, which might delete values and mess things up.
|
|
|
|
|
JS::Rooted<JSPropertyDescriptor> desc(cx);
|
|
|
|
|
if (!JS_GetPropertyDescriptorById(cx, obj, id, &desc))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPropertyDescriptorById failed");
|
|
|
|
|
if (desc.hasGetterObject())
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("Cannot serialize property getters");
|
|
|
|
|
|
2010-05-05 15:36:35 -07:00
|
|
|
// Get the property name as a string
|
2015-01-24 06:46:52 -08:00
|
|
|
if (!JS_IdToValue(cx, id, &idval))
|
2010-05-05 15:36:35 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_IdToValue failed");
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedString idstr(cx, JS::ToString(cx, idval));
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!idstr)
|
2010-05-05 15:36:35 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
ScriptString("prop name", idstr);
|
2010-05-05 15:36:35 -07:00
|
|
|
|
2016-01-11 12:03:33 -08:00
|
|
|
if (!JS_GetPropertyById(cx, obj, id, &propval))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPropertyById failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
HandleScriptVal(propval);
|
|
|
|
|
}
|
2010-05-25 11:24:12 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
|
{
|
2012-02-28 14:12:30 -08:00
|
|
|
// We can't serialise functions, but we can at least name the offender (hopefully)
|
2012-02-29 15:41:23 -08:00
|
|
|
std::wstring funcname(L"(unnamed)");
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedFunction func(cx, JS_ValueToFunction(cx, val));
|
2012-02-28 14:12:30 -08:00
|
|
|
if (func)
|
|
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedString string(cx, JS_GetFunctionId(func));
|
2012-02-28 14:12:30 -08:00
|
|
|
if (string)
|
|
|
|
|
{
|
2012-02-29 15:41:23 -08:00
|
|
|
size_t length;
|
2015-12-18 17:29:55 -08:00
|
|
|
const char16_t* ch = JS_GetStringCharsAndLength(cx, string, &length);
|
2012-02-29 15:41:23 -08:00
|
|
|
if (ch && length > 0)
|
2016-01-11 12:03:28 -08:00
|
|
|
funcname.assign(ch, ch + length);
|
2012-02-28 14:12:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 12:37:38 -08:00
|
|
|
LOGERROR("Cannot serialise JS objects of type 'function': %s", utf8_from_wstring(funcname));
|
2010-01-09 11:20:14 -08:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING);
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedString stringVal(cx, val.toString());
|
|
|
|
|
ScriptString("string", stringVal);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_NUMBER:
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
// 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))
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT);
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberI32_Unbounded("value", integer);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE);
|
|
|
|
|
m_Serializer.NumberDouble_Unbounded("value", d);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN);
|
2016-09-02 09:23:44 -07:00
|
|
|
bool b = val.toBoolean();
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
debug_warn(L"Invalid TypeOfValue");
|
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
void CBinarySerializerScriptImpl::ScriptString(const char* name, JS::HandleString string)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2011-07-16 16:24:14 -07:00
|
|
|
JSContext* cx = m_ScriptInterface.GetContext();
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2011-07-16 16:24:14 -07:00
|
|
|
size_t length;
|
2015-12-18 17:29:55 -08:00
|
|
|
const char16_t* chars = JS_GetStringCharsAndLength(cx, string, &length);
|
2011-07-16 16:24:14 -07:00
|
|
|
|
|
|
|
|
if (!chars)
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetStringCharsAndLength failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2010-05-25 11:07:41 -07:00
|
|
|
#if BYTE_ORDER != LITTLE_ENDIAN
|
|
|
|
|
#error TODO: probably need to convert JS strings to little-endian
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Serialize strings directly as UTF-16, to avoid expensive encoding conversions
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.RawBytes(name, (const u8*)chars, length*2);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
u32 CBinarySerializerScriptImpl::GetScriptBackrefTag(JS::HandleObject obj)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// 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 non-zero tag; when we serialize it a second time we just
|
|
|
|
|
// refer to that tag.
|
|
|
|
|
//
|
|
|
|
|
// The tags are stored in a map. Maybe it'd be more efficient to store it inline in the object
|
|
|
|
|
// somehow? but this works okay for now
|
|
|
|
|
|
|
|
|
|
// If it was already there, return the tag
|
2015-01-24 06:46:52 -08:00
|
|
|
u32 tag;
|
|
|
|
|
if (m_ScriptBackrefs.find(obj, tag))
|
|
|
|
|
return tag;
|
|
|
|
|
|
|
|
|
|
m_ScriptBackrefs.add(m_ScriptInterface.GetContext(), obj, m_ScriptBackrefsNext);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2010-05-25 11:24:12 -07:00
|
|
|
m_ScriptBackrefsNext++;
|
2010-01-09 11:20:14 -08:00
|
|
|
// Return a non-tag number so callers know they need to serialize the object
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-05-26 14:57:24 -07:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
bool CBinarySerializerScriptImpl::IsSerializablePrototype(JS::HandleObject prototype)
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
return m_SerializablePrototypes->has(prototype);
|
2013-05-26 14:57:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
std::wstring CBinarySerializerScriptImpl::GetPrototypeName(JS::HandleObject prototype)
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
std::wstring ret;
|
|
|
|
|
bool found = m_SerializablePrototypes->find(prototype, ret);
|
|
|
|
|
ENSURE(found);
|
|
|
|
|
return ret;
|
2013-05-26 14:57:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
void CBinarySerializerScriptImpl::SetSerializablePrototypes(shared_ptr<ObjectIdCache<std::wstring> > prototypes)
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
m_SerializablePrototypes = prototypes;
|
|
|
|
|
}
|