2014-08-03 21:32:39 +02:00
|
|
|
/* Copyright (C) 2014 Wildfire Games.
|
2010-01-09 20:20:14 +01: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 15:22:46 +02:00
|
|
|
#include "lib/alignment.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2013-05-26 23:57:24 +02:00
|
|
|
#include "scriptinterface/ScriptExtraHeaders.h" // for JSDOUBLE_IS_INT32, typed arrays
|
2014-03-28 21:26:32 +01:00
|
|
|
#include "SerializedScriptTypes.h"
|
2013-05-26 23:57:24 +02:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
static u8 GetArrayType(JSArrayBufferViewType arrayType)
|
2013-05-26 23:57:24 +02:00
|
|
|
{
|
|
|
|
switch(arrayType)
|
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_INT8:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT8;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_UINT8:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_INT16:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT16;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_UINT16:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT16;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_INT32:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT32;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_UINT32:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT32;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_FLOAT32:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT32;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_FLOAT64:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT64;
|
2014-03-28 21:26:32 +01:00
|
|
|
case js::ArrayBufferView::TYPE_UINT8_CLAMPED:
|
2013-05-26 23:57:24 +02:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8_CLAMPED;
|
|
|
|
default:
|
|
|
|
LOGERROR(L"Cannot serialize unrecognized typed array view: %d", arrayType);
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 20:44:33 +02:00
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(ScriptInterface& scriptInterface, ISerializer& serializer) :
|
2010-05-25 21:24:14 +02:00
|
|
|
m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_Rooter(m_ScriptInterface),
|
2013-09-30 03:22:44 +02:00
|
|
|
m_ScriptBackrefsArena(1 * MiB), m_ScriptBackrefs(backrefs_t::key_compare(), ScriptBackrefsAlloc(m_ScriptBackrefsArena)), m_ScriptBackrefsNext(1)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-31 21:18:40 +02:00
|
|
|
void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
JSContext* cx = m_ScriptInterface.GetContext();
|
2014-03-28 21:26:32 +01:00
|
|
|
JSAutoRequest rq(cx);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
switch (JS_TypeOfValue(cx, val))
|
|
|
|
{
|
|
|
|
case JSTYPE_VOID:
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID);
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSTYPE_NULL: // This type is never actually returned (it's a JS2 feature)
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
if (val.isNull())
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
JS::RootedObject obj(cx, &val.toObject());
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
// If we've already serialized this object, just output a reference to it
|
|
|
|
u32 tag = GetScriptBackrefTag(obj);
|
|
|
|
if (tag)
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BACKREF);
|
|
|
|
m_Serializer.NumberU32_Unbounded("tag", tag);
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-26 23:57:24 +02:00
|
|
|
// Arrays are special cases of Object
|
2010-01-09 20:20:14 +01:00
|
|
|
if (JS_IsArrayObject(cx, obj))
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY);
|
2010-01-09 20:20:14 +01:00
|
|
|
// TODO: probably should have a more efficient storage format
|
2011-10-27 22:56:32 +02: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 21:26:32 +01:00
|
|
|
uint length = 0;
|
2011-10-27 22:56:32 +02:00
|
|
|
if (!JS_GetArrayLength(cx, obj, &length))
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetArrayLength failed");
|
|
|
|
m_Serializer.NumberU32_Unbounded("array length", length);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2014-03-28 21:26:32 +01:00
|
|
|
else if (JS_IsTypedArrayObject(obj))
|
2013-05-26 23:57:24 +02:00
|
|
|
{
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_TYPED_ARRAY);
|
|
|
|
|
2014-03-28 21:26:32 +01: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 23:57:24 +02:00
|
|
|
|
|
|
|
// Now handle its array buffer
|
|
|
|
// this may be a backref, since ArrayBuffers can be shared by multiple views
|
2014-07-31 21:18:40 +02:00
|
|
|
JS::RootedValue bufferVal(cx, JS::ObjectValue(*JS_GetArrayBufferViewBuffer(obj)));
|
|
|
|
HandleScriptVal(bufferVal);
|
2013-05-26 23:57:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-03-28 21:26:32 +01:00
|
|
|
else if (JS_IsArrayBufferObject(obj))
|
2013-05-26 23:57:24 +02: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 21:26:32 +01:00
|
|
|
u32 length = JS_GetArrayBufferByteLength(obj);
|
2013-05-26 23:57:24 +02:00
|
|
|
m_Serializer.NumberU32_Unbounded("buffer length", length);
|
2014-03-28 21:26:32 +01:00
|
|
|
m_Serializer.RawBytes("buffer data", (const u8*)JS_GetArrayBufferData(obj), length);
|
2013-05-26 23:57:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
else
|
|
|
|
{
|
2013-05-26 23:57:24 +02:00
|
|
|
// Find type of object
|
2014-03-28 21:26:32 +01:00
|
|
|
JSClass* jsclass = JS_GetClass(obj);
|
2013-05-26 23:57:24 +02:00
|
|
|
if (!jsclass)
|
2014-03-28 21:26:32 +01:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetClass failed");
|
2013-05-26 23:57:24 +02:00
|
|
|
JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass);
|
|
|
|
|
|
|
|
if (protokey == JSProto_Object)
|
|
|
|
{
|
|
|
|
// Object class - check for user-defined prototype
|
2014-03-28 21:26:32 +01:00
|
|
|
JS::RootedObject proto(cx);
|
|
|
|
JS_GetPrototype(cx, obj, proto.address());
|
2013-05-26 23:57:24 +02:00
|
|
|
if (!proto)
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPrototype failed");
|
|
|
|
|
|
|
|
if (m_SerializablePrototypes.empty() || !IsSerializablePrototype(proto))
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
const std::wstring& prototypeName = GetPrototypeName(proto);
|
|
|
|
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
|
|
|
|
JSBool hasCustomSerialize;
|
|
|
|
if (!JS_HasProperty(cx, obj, "Serialize", &hasCustomSerialize))
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_HasProperty failed");
|
2014-03-28 21:26:32 +01:00
|
|
|
|
2013-05-26 23:57:24 +02:00
|
|
|
if (hasCustomSerialize)
|
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
JS::RootedValue serialize(cx);
|
|
|
|
if (!JS_LookupProperty(cx, obj, "Serialize", serialize.address()))
|
2013-05-26 23:57:24 +02:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_LookupProperty failed");
|
|
|
|
|
|
|
|
// If serialize is null, so don't serialize anything more
|
2014-03-28 21:26:32 +01:00
|
|
|
if (!serialize.isNull())
|
2013-05-26 23:57:24 +02:00
|
|
|
{
|
2014-07-31 21:18:40 +02:00
|
|
|
JS::RootedValue data(cx);
|
|
|
|
if (!m_ScriptInterface.CallFunction(val, "Serialize", &data))
|
2013-05-26 23:57:24 +02:00
|
|
|
throw PSERROR_Serialize_ScriptError("Prototype Serialize function failed");
|
2014-07-31 21:18:40 +02:00
|
|
|
HandleScriptVal(data);
|
2013-05-26 23:57:24 +02: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 21:26:32 +01:00
|
|
|
double d;
|
|
|
|
if (!JS::ToNumber(cx, val, &d))
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ToNumber failed");
|
2013-05-26 23:57:24 +02: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
|
|
|
|
JSString* str = JS_ValueToString(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
|
2014-03-28 21:26:32 +01:00
|
|
|
bool b = JS::ToBoolean(val);
|
|
|
|
m_Serializer.Bool("value", b);
|
2013-05-26 23:57:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-09-20 19:14:53 +02: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)
|
|
|
|
{
|
|
|
|
JS::RootedValue keyValuePair(cx);
|
|
|
|
ENSURE(m_ScriptInterface.CallFunction(keyValueIterator, "next", &keyValuePair));
|
|
|
|
|
|
|
|
JS::RootedObject keyValuePairObj(cx, &keyValuePair.toObject());
|
|
|
|
|
|
|
|
JS::RootedValue key(cx);
|
|
|
|
JS::RootedValue value(cx);
|
|
|
|
ENSURE(JS_GetElement(cx, keyValuePairObj, 0, key.address()));
|
|
|
|
ENSURE(JS_GetElement(cx, keyValuePairObj, 1, value.address()));
|
|
|
|
|
|
|
|
HandleScriptVal(key);
|
|
|
|
HandleScriptVal(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-05-26 23:57:24 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unrecognized class
|
|
|
|
LOGERROR(L"Cannot serialise JS objects with unrecognized class '%hs'", jsclass->name);
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find all properties (ordered by insertion time)
|
|
|
|
|
2010-05-25 20:24:12 +02:00
|
|
|
// (Note that we don't do any rooting, because we assume nothing is going to trigger GC.
|
|
|
|
// I'm not absolute certain that's necessarily a valid assumption.)
|
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
JS::AutoIdArray ida (cx, JS_Enumerate(cx, obj));
|
|
|
|
if (!ida)
|
2010-11-17 00:00:52 +01:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_Enumerate failed");
|
2010-05-25 20:24:12 +02:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
m_Serializer.NumberU32_Unbounded("num props", (u32)ida.length());
|
2010-06-27 13:57:00 +02:00
|
|
|
|
2010-11-17 00:00:52 +01:00
|
|
|
for (size_t i = 0; i < ida.length(); ++i)
|
2010-06-27 13:57:00 +02:00
|
|
|
{
|
2010-11-17 00:00:52 +01:00
|
|
|
jsid id = ida[i];
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2014-03-28 21:26:32 +01:00
|
|
|
JS::RootedValue idval(cx);
|
|
|
|
JS::RootedValue propval(cx);
|
|
|
|
|
2010-05-06 00:36:35 +02:00
|
|
|
// Get the property name as a string
|
2014-03-28 21:26:32 +01:00
|
|
|
if (!JS_IdToValue(cx, id, idval.address()))
|
2010-05-06 00:36:35 +02:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_IdToValue failed");
|
2014-03-28 21:26:32 +01:00
|
|
|
JSString* idstr = JS_ValueToString(cx, idval.get());
|
2010-01-09 20:20:14 +01:00
|
|
|
if (!idstr)
|
2010-05-06 00:36:35 +02:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
ScriptString("prop name", idstr);
|
2010-05-06 00:36:35 +02:00
|
|
|
|
2010-05-25 20:24:12 +02:00
|
|
|
// Use LookupProperty instead of GetProperty to avoid the danger of getters
|
|
|
|
// (they might delete values and trigger GC)
|
2014-03-28 21:26:32 +01:00
|
|
|
if (!JS_LookupPropertyById(cx, obj, id, propval.address()))
|
2010-05-25 20:24:12 +02:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_LookupPropertyById failed");
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
HandleScriptVal(propval);
|
|
|
|
}
|
2010-05-25 20:24:12 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
2012-02-28 23:12:30 +01:00
|
|
|
// We can't serialise functions, but we can at least name the offender (hopefully)
|
2012-03-01 00:41:23 +01:00
|
|
|
std::wstring funcname(L"(unnamed)");
|
2012-02-28 23:12:30 +01:00
|
|
|
JSFunction* func = JS_ValueToFunction(cx, val);
|
|
|
|
if (func)
|
|
|
|
{
|
|
|
|
JSString* string = JS_GetFunctionId(func);
|
|
|
|
if (string)
|
|
|
|
{
|
2012-03-01 00:41:23 +01:00
|
|
|
size_t length;
|
|
|
|
const jschar* ch = JS_GetStringCharsAndLength(cx, string, &length);
|
|
|
|
if (ch && length > 0)
|
|
|
|
funcname = std::wstring(ch, ch + length);
|
2012-02-28 23:12:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 00:41:23 +01:00
|
|
|
LOGERROR(L"Cannot serialise JS objects of type 'function': %ls", funcname.c_str());
|
2010-01-09 20:20:14 +01:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
}
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING);
|
2014-03-28 21:26:32 +01:00
|
|
|
ScriptString("string", val.toString());
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSTYPE_NUMBER:
|
|
|
|
{
|
2014-03-28 21:26:32 +01: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 20:20:14 +01:00
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT);
|
2014-03-28 21:26:32 +01:00
|
|
|
m_Serializer.NumberI32_Unbounded("value", integer);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE);
|
|
|
|
m_Serializer.NumberDouble_Unbounded("value", d);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
{
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN);
|
2014-03-28 21:26:32 +01:00
|
|
|
bool b = JSVAL_TO_BOOLEAN(val);
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0);
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
debug_warn(L"Invalid TypeOfValue");
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
void CBinarySerializerScriptImpl::ScriptString(const char* name, JSString* string)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-07-17 01:24:14 +02:00
|
|
|
JSContext* cx = m_ScriptInterface.GetContext();
|
2014-03-28 21:26:32 +01:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
2011-07-17 01:24:14 +02:00
|
|
|
size_t length;
|
|
|
|
const jschar* chars = JS_GetStringCharsAndLength(cx, string, &length);
|
|
|
|
|
|
|
|
if (!chars)
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetStringCharsAndLength failed");
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-25 20:07:41 +02: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 21:26:32 +01:00
|
|
|
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
|
2010-05-25 21:01:30 +02:00
|
|
|
m_Serializer.RawBytes(name, (const u8*)chars, length*2);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
u32 CBinarySerializerScriptImpl::GetScriptBackrefTag(JSObject* obj)
|
2010-01-09 20:20:14 +01: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
|
|
|
|
|
2010-05-25 20:24:12 +02:00
|
|
|
std::pair<backrefs_t::iterator, bool> it = m_ScriptBackrefs.insert(std::make_pair(obj, m_ScriptBackrefsNext));
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
// If it was already there, return the tag
|
|
|
|
if (!it.second)
|
|
|
|
return it.first->second;
|
|
|
|
|
|
|
|
// If it was newly inserted, we need to make sure it gets rooted
|
|
|
|
// for the duration that it's in m_ScriptBackrefs
|
2010-05-25 20:24:12 +02:00
|
|
|
m_Rooter.Push(it.first->first);
|
|
|
|
m_ScriptBackrefsNext++;
|
2010-01-09 20:20:14 +01:00
|
|
|
// Return a non-tag number so callers know they need to serialize the object
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-26 23:57:24 +02:00
|
|
|
|
|
|
|
bool CBinarySerializerScriptImpl::IsSerializablePrototype(JSObject* prototype)
|
|
|
|
{
|
|
|
|
return m_SerializablePrototypes.find(prototype) != m_SerializablePrototypes.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring CBinarySerializerScriptImpl::GetPrototypeName(JSObject* prototype)
|
|
|
|
{
|
|
|
|
std::map<JSObject*, std::wstring>::iterator it = m_SerializablePrototypes.find(prototype);
|
|
|
|
ENSURE(it != m_SerializablePrototypes.end());
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBinarySerializerScriptImpl::SetSerializablePrototypes(std::map<JSObject*, std::wstring>& prototypes)
|
|
|
|
{
|
|
|
|
m_SerializablePrototypes = prototypes;
|
|
|
|
}
|