Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/serialization/StdDeserializer.cpp
Show All 22 Lines | |||||
#include "StdSerializer.h" // for DEBUG_SERIALIZER_ANNOTATE | #include "StdSerializer.h" // for DEBUG_SERIALIZER_ANNOTATE | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/ScriptExtraHeaders.h" // for typed arrays | #include "scriptinterface/ScriptExtraHeaders.h" // for typed arrays | ||||
#include "lib/byte_order.h" | #include "lib/byte_order.h" | ||||
CStdDeserializer::CStdDeserializer(const ScriptInterface& scriptInterface, std::istream& stream) : | CStdDeserializer::CStdDeserializer(const ScriptInterface& scriptInterface, std::istream& stream) : | ||||
m_ScriptInterface(scriptInterface), m_Stream(stream), | m_ScriptInterface(scriptInterface), m_Stream(stream) | ||||
m_dummyObject(scriptInterface.GetJSRuntime()) | |||||
{ | { | ||||
JSContext* cx = m_ScriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
JS_AddExtraGCRootsTracer(m_ScriptInterface.GetJSRuntime(), CStdDeserializer::Trace, this); | JS_AddExtraGCRootsTracer(m_ScriptInterface.GetJSRuntime(), CStdDeserializer::Trace, this); | ||||
// Add a dummy tag because the serializer uses the tag 0 to indicate that a value | |||||
// needs to be serialized and then tagged | |||||
m_dummyObject = JS_NewPlainObject(cx); | |||||
m_ScriptBackrefs.push_back(JS::Heap<JSObject*>(m_dummyObject)); | |||||
} | } | ||||
CStdDeserializer::~CStdDeserializer() | CStdDeserializer::~CStdDeserializer() | ||||
{ | { | ||||
JS_RemoveExtraGCRootsTracer(m_ScriptInterface.GetJSRuntime(), CStdDeserializer::Trace, this); | JS_RemoveExtraGCRootsTracer(m_ScriptInterface.GetJSRuntime(), CStdDeserializer::Trace, this); | ||||
} | } | ||||
void CStdDeserializer::Trace(JSTracer *trc, void *data) | void CStdDeserializer::Trace(JSTracer *trc, void *data) | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | if (numBytes > 64*MiB) | ||||
throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream"); | throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream"); | ||||
} | } | ||||
void CStdDeserializer::AddScriptBackref(JS::HandleObject obj) | void CStdDeserializer::AddScriptBackref(JS::HandleObject obj) | ||||
{ | { | ||||
m_ScriptBackrefs.push_back(JS::Heap<JSObject*>(obj)); | m_ScriptBackrefs.push_back(JS::Heap<JSObject*>(obj)); | ||||
} | } | ||||
void CStdDeserializer::GetScriptBackref(u32 tag, JS::MutableHandleObject ret) | void CStdDeserializer::GetScriptBackref(size_t tag, JS::MutableHandleObject ret) | ||||
{ | { | ||||
ENSURE(m_ScriptBackrefs.size() > tag); | ENSURE(m_ScriptBackrefs.size() > tag); | ||||
ret.set(m_ScriptBackrefs[tag]); | ret.set(m_ScriptBackrefs[tag]); | ||||
} | } | ||||
//////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////// | ||||
JS::Value CStdDeserializer::ReadScriptVal(const char* UNUSED(name), JS::HandleObject appendParent) | JS::Value CStdDeserializer::ReadScriptVal(const char* UNUSED(name), JS::HandleObject appendParent) | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | // TODO: Should ask upstream about getting a variant of JS_SetProperty with a length param. | ||||
case SCRIPT_TYPE_BOOLEAN: | case SCRIPT_TYPE_BOOLEAN: | ||||
{ | { | ||||
uint8_t value; | uint8_t value; | ||||
NumberU8("value", value, 0, 1); | NumberU8("value", value, 0, 1); | ||||
return JS::BooleanValue(value ? true : false); | return JS::BooleanValue(value ? true : false); | ||||
} | } | ||||
case SCRIPT_TYPE_BACKREF: | case SCRIPT_TYPE_BACKREF: | ||||
{ | { | ||||
u32 tag; | i32 tag; | ||||
NumberU32_Unbounded("tag", tag); | NumberI32("tag", tag, 0, JSVAL_INT_MAX); | ||||
JS::RootedObject obj(cx); | JS::RootedObject obj(cx); | ||||
GetScriptBackref(tag, &obj); | GetScriptBackref(tag, &obj); | ||||
if (!obj) | if (!obj) | ||||
throw PSERROR_Deserialize_ScriptError("Invalid backref tag"); | throw PSERROR_Deserialize_ScriptError("Invalid backref tag"); | ||||
return JS::ObjectValue(*obj); | return JS::ObjectValue(*obj); | ||||
} | } | ||||
case SCRIPT_TYPE_OBJECT_NUMBER: | case SCRIPT_TYPE_OBJECT_NUMBER: | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 229 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator