LCOV - code coverage report
Current view: top level - source/simulation2/serialization - StdDeserializer.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 279 322 86.6 %
Date: 2026-12-03 01:19:39 Functions: 12 16 75.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "StdDeserializer.h"
      21             : 
      22             : #include "lib/alignment.h"
      23             : #include "lib/byte_order.h"
      24             : #include "lib/debug.h"
      25             : #include "lib/utf8.h"
      26             : #include "ps/CStr.h"
      27             : #include "scriptinterface/FunctionWrapper.h"
      28             : #include "scriptinterface/Interface.h"
      29             : #include "scriptinterface/Request.h"
      30             : #include "simulation2/serialization/ISerializer.h"
      31             : #include "simulation2/serialization/SerializedScriptTypes.h"
      32             : #include "simulation2/serialization/StdSerializer.h"
      33             : 
      34             : #include <cstdint>
      35             : #include <cstdlib>
      36             : #include <istream>
      37             : #include <js/Array.h>
      38             : #include <js/ArrayBuffer.h>
      39             : #include <js/CallAndConstruct.h>
      40             : #include <js/ComparisonOperators.h>
      41             : #include <js/GCAPI.h>
      42             : #include <js/MapAndSet.h>
      43             : #include <js/PropertyAndElement.h>
      44             : #include <js/RootingAPI.h>
      45             : #include <js/String.h>
      46             : #include <js/TracingAPI.h>
      47             : #include <js/ValueArray.h>
      48             : #include <js/experimental/TypedData.h>
      49             : #include <jsapi.h>
      50             : #include <jspubtd.h>
      51             : 
      52             : class JSObject;
      53             : 
      54          96 : CStdDeserializer::CStdDeserializer(const Script::Interface& scriptInterface, std::istream& stream) :
      55          96 :     m_ScriptInterface(scriptInterface), m_Stream(stream)
      56             : {
      57          96 :     Script::Request rq(m_ScriptInterface);
      58          96 :     JS_AddExtraGCRootsTracer(rq.cx, CStdDeserializer::Trace, this);
      59          96 :     m_SerializePropId = JS::PropertyKey::fromPinnedString(JS_AtomizeAndPinString(rq.cx, "Serialize"));
      60          96 :     m_DeserializePropId = JS::PropertyKey::fromPinnedString(JS_AtomizeAndPinString(rq.cx, "Deserialize"));
      61             : 
      62             :     // Insert a dummy object in front, as valid tags start at 1.
      63          96 :     m_ScriptBackrefs.emplace_back(nullptr);
      64          96 : }
      65             : 
      66          96 : CStdDeserializer::~CStdDeserializer()
      67             : {
      68          96 :     JS_RemoveExtraGCRootsTracer(Script::Request(m_ScriptInterface).cx, CStdDeserializer::Trace, this);
      69          96 : }
      70             : 
      71           0 : void CStdDeserializer::Trace(JSTracer *trc, void *data)
      72             : {
      73           0 :     reinterpret_cast<CStdDeserializer*>(data)->TraceMember(trc);
      74           0 : }
      75             : 
      76           0 : void CStdDeserializer::TraceMember(JSTracer *trc)
      77             : {
      78           0 :     for (JS::Heap<JSObject*>& backref : m_ScriptBackrefs)
      79           0 :         JS::TraceEdge(trc, &backref, "StdDeserializer::m_ScriptBackrefs");
      80           0 : }
      81             : 
      82        1443 : void CStdDeserializer::Get([[maybe_unused]] const char* name, std::uint8_t* data, size_t len)
      83             : {
      84             : #if DEBUG_SERIALIZER_ANNOTATE
      85             :     std::string strName;
      86             :     char c = m_Stream.get();
      87             :     ENSURE(c == '<');
      88             :     while (1)
      89             :     {
      90             :         c = m_Stream.get();
      91             :         if (c == '>')
      92             :             break;
      93             :         else
      94             :             strName += c;
      95             :     }
      96             :     ENSURE(strName == name);
      97             : #endif
      98        1443 :     m_Stream.read((char*)data, (std::streamsize)len);
      99        1443 :     if (!m_Stream.good())
     100             :     {
     101             :         // hit eof before len, or other errors
     102             :         // NOTE: older libc++ versions incorrectly set eofbit on the last char; test gcount as a workaround
     103             :         // see https://llvm.org/bugs/show_bug.cgi?id=9335
     104           0 :         if (m_Stream.bad() || m_Stream.fail() || (m_Stream.eof() && m_Stream.gcount() != (std::streamsize)len))
     105           0 :             throw PSERROR_Deserialize_ReadFailed();
     106             :     }
     107        1443 : }
     108             : 
     109           0 : std::istream& CStdDeserializer::GetStream()
     110             : {
     111           0 :     return m_Stream;
     112             : }
     113             : 
     114         223 : void CStdDeserializer::RequireBytesInStream(size_t numBytes)
     115             : {
     116             :     // It would be nice to do:
     117             : //  if (numBytes > (size_t)m_Stream.rdbuf()->in_avail())
     118             : //      throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream");
     119             :     // but that doesn't work (at least on MSVC) since in_avail isn't
     120             :     // guaranteed to return the actual number of bytes available; see e.g.
     121             :     // http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/13009a88-933f-4be7-bf3d-150e425e66a6#70ea562d-8605-4742-8851-1bae431ce6ce
     122             : 
     123             :     // Instead we'll just verify that it's not an extremely large number:
     124         223 :     if (numBytes > 64*MiB)
     125           0 :         throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream");
     126         223 : }
     127             : 
     128         163 : void CStdDeserializer::AddScriptBackref(JS::HandleObject obj)
     129             : {
     130         163 :     m_ScriptBackrefs.push_back(JS::Heap<JSObject*>(obj));
     131         163 : }
     132             : 
     133          17 : void CStdDeserializer::GetScriptBackref(size_t tag, JS::MutableHandleObject ret)
     134             : {
     135          17 :     ENSURE(m_ScriptBackrefs.size() > tag);
     136          17 :     ret.set(m_ScriptBackrefs[tag]);
     137          17 : }
     138             : 
     139             : ////////////////////////////////////////////////////////////////
     140             : 
     141         324 : JS::Value CStdDeserializer::ReadScriptVal(const char* /*name*/, JS::HandleObject preexistingObject)
     142             : {
     143         324 :     Script::Request rq(m_ScriptInterface);
     144             : 
     145         324 :     uint8_t type;
     146         324 :     NumberU8_Unbounded("type", type);
     147         324 :     switch (type)
     148             :     {
     149           2 :     case SCRIPT_TYPE_VOID:
     150           2 :         return JS::UndefinedValue();
     151             : 
     152           2 :     case SCRIPT_TYPE_NULL:
     153           2 :         return JS::NullValue();
     154             : 
     155          75 :     case SCRIPT_TYPE_ARRAY:
     156          75 :     case SCRIPT_TYPE_OBJECT:
     157          75 :     case SCRIPT_TYPE_OBJECT_PROTOTYPE:
     158          75 :     {
     159          75 :         JS::RootedObject obj(rq.cx);
     160          75 :         if (type == SCRIPT_TYPE_ARRAY)
     161             :         {
     162          31 :             std::uint32_t length;
     163          31 :             NumberU32_Unbounded("array length", length);
     164          31 :             obj.set(JS::NewArrayObject(rq.cx, length));
     165             :         }
     166          44 :         else if (type == SCRIPT_TYPE_OBJECT)
     167             :         {
     168          28 :             obj.set(JS_NewPlainObject(rq.cx));
     169             :         }
     170             :         else // SCRIPT_TYPE_OBJECT_PROTOTYPE
     171             :         {
     172          16 :             CStrW prototypeName;
     173          16 :             String("proto", prototypeName, 0, 256);
     174             : 
     175             :             // If an object was passed, no need to construct a new one.
     176          16 :             if (preexistingObject != nullptr)
     177           5 :                 obj.set(preexistingObject);
     178             :             else
     179             :             {
     180          11 :                 JS::RootedValue constructor(rq.cx);
     181          11 :                 if (!Script::Interface::GetGlobalProperty(rq, prototypeName.ToUTF8(), &constructor))
     182           0 :                     throw PSERROR_Deserialize_ScriptError("Deserializer failed to get constructor object");
     183             : 
     184          11 :                 JS::RootedObject newObj(rq.cx);
     185          11 :                 if (!JS::Construct(rq.cx, constructor, JS::HandleValueArray::empty(), &newObj))
     186           0 :                     throw PSERROR_Deserialize_ScriptError("Deserializer failed to construct object");
     187          11 :                 obj.set(newObj);
     188          11 :             }
     189             : 
     190          16 :             JS::RootedObject prototype(rq.cx);
     191          16 :             JS_GetPrototype(rq.cx, obj, &prototype);
     192          16 :             SPrototypeSerialization info = GetPrototypeInfo(rq, prototype, m_SerializePropId, m_DeserializePropId);
     193             : 
     194          16 :             if (preexistingObject != nullptr && prototypeName != info.name)
     195           0 :                 throw PSERROR_Deserialize_ScriptError("Deserializer failed: incorrect pre-existing object");
     196             : 
     197             : 
     198          16 :             if (info.hasCustomDeserialize)
     199             :             {
     200           8 :                 AddScriptBackref(obj);
     201             : 
     202             :                 // If Serialize is null, we'll still call Deserialize but with undefined argument
     203           8 :                 JS::RootedValue data(rq.cx);
     204           8 :                 if (!info.hasNullSerialize)
     205           5 :                     ScriptVal("data", &data);
     206             : 
     207           8 :                 JS::RootedValue objVal(rq.cx, JS::ObjectValue(*obj));
     208           8 :                 Script::Function::CallVoid(rq, objVal, "Deserialize", data);
     209             : 
     210           8 :                 return JS::ObjectValue(*obj);
     211           8 :             }
     212           8 :             else if (info.hasNullSerialize)
     213             :             {
     214             :                 // If we serialized null, this means we're pretty much a default-constructed object.
     215             :                 // Nothing to do.
     216           0 :                 AddScriptBackref(obj);
     217           8 :                 return JS::ObjectValue(*obj);
     218             :             }
     219          32 :         }
     220             : 
     221          67 :         if (!obj)
     222           0 :             throw PSERROR_Deserialize_ScriptError("Deserializer failed to create new object");
     223             : 
     224          67 :         AddScriptBackref(obj);
     225             : 
     226          67 :         uint32_t numProps;
     227          67 :         NumberU32_Unbounded("num props", numProps);
     228             :         bool isLatin1;
     229         217 :         for (uint32_t i = 0; i < numProps; ++i)
     230             :         {
     231         150 :             Bool("isLatin1", isLatin1);
     232         150 :             if (isLatin1)
     233             :             {
     234         150 :                 std::vector<JS::Latin1Char> propname;
     235         150 :                 ReadStringLatin1("prop name", propname);
     236         150 :                 JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr));
     237             : 
     238         300 :                 std::u16string prp(propname.begin(), propname.end());;
     239             : // TODO: Should ask upstream about getting a variant of JS_SetProperty with a length param.
     240         150 :                 if (!JS_SetUCProperty(rq.cx, obj, (const char16_t*)prp.data(), prp.length(), propval))
     241           0 :                     throw PSERROR_Deserialize_ScriptError();
     242         300 :             }
     243             :             else
     244             :             {
     245           0 :                 std::u16string propname;
     246           0 :                 ReadStringUTF16("prop name", propname);
     247           0 :                 JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr));
     248             : 
     249           0 :                 if (!JS_SetUCProperty(rq.cx, obj, (const char16_t*)propname.data(), propname.length(), propval))
     250           0 :                     throw PSERROR_Deserialize_ScriptError();
     251           0 :             }
     252             :         }
     253             : 
     254          75 :         return JS::ObjectValue(*obj);
     255          75 :     }
     256          37 :     case SCRIPT_TYPE_STRING:
     257          37 :     {
     258          37 :         JS::RootedString str(rq.cx);
     259          37 :         ScriptString("string", &str);
     260          37 :         return JS::StringValue(str);
     261          37 :     }
     262          83 :     case SCRIPT_TYPE_INT:
     263          83 :     {
     264          83 :         int32_t value;
     265          83 :         NumberI32("value", value, JSVAL_INT_MIN, JSVAL_INT_MAX);
     266          83 :         return JS::NumberValue(value);
     267             :     }
     268          14 :     case SCRIPT_TYPE_DOUBLE:
     269          14 :     {
     270          14 :         double value;
     271          14 :         NumberDouble_Unbounded("value", value);
     272          28 :         JS::RootedValue rval(rq.cx, JS::NumberValue(value));
     273          14 :         if (rval.isNull())
     274           0 :             throw PSERROR_Deserialize_ScriptError("JS_NewNumberValue failed");
     275          14 :         return rval;
     276          14 :     }
     277           6 :     case SCRIPT_TYPE_BOOLEAN:
     278           6 :     {
     279           6 :         uint8_t value;
     280           6 :         NumberU8("value", value, 0, 1);
     281           6 :         return JS::BooleanValue(value ? true : false);
     282             :     }
     283          17 :     case SCRIPT_TYPE_BACKREF:
     284          17 :     {
     285          17 :         std::int32_t tag;
     286          17 :         NumberI32("tag", tag, 0, JSVAL_INT_MAX);
     287          17 :         JS::RootedObject obj(rq.cx);
     288          17 :         GetScriptBackref(tag, &obj);
     289          17 :         if (!obj)
     290           0 :             throw PSERROR_Deserialize_ScriptError("Invalid backref tag");
     291          17 :         return JS::ObjectValue(*obj);
     292          17 :     }
     293           4 :     case SCRIPT_TYPE_OBJECT_NUMBER:
     294           4 :     {
     295           4 :         double value;
     296           4 :         NumberDouble_Unbounded("value", value);
     297           8 :         JS::RootedValue val(rq.cx, JS::NumberValue(value));
     298             : 
     299           4 :         JS::RootedObject ctorobj(rq.cx);
     300           4 :         if (!JS_GetClassObject(rq.cx, JSProto_Number, &ctorobj))
     301           0 :             throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed");
     302           8 :         JS::RootedValue protoval(rq.cx, JS::ObjectOrNullValue(ctorobj));
     303             : 
     304           4 :         JS::RootedObject obj(rq.cx);
     305           4 :         if (!JS::Construct(rq.cx, protoval, JS::HandleValueArray(val), &obj))
     306           0 :             throw PSERROR_Deserialize_ScriptError("JS_New failed");
     307           4 :         AddScriptBackref(obj);
     308           4 :         return JS::ObjectValue(*obj);
     309           4 :     }
     310           4 :     case SCRIPT_TYPE_OBJECT_STRING:
     311           4 :     {
     312           4 :         JS::RootedString str(rq.cx);
     313           4 :         ScriptString("value", &str);
     314           4 :         if (!str)
     315           0 :             throw PSERROR_Deserialize_ScriptError();
     316           4 :         JS::RootedValue val(rq.cx, JS::StringValue(str));
     317             : 
     318           4 :         JS::RootedObject ctorobj(rq.cx);
     319           4 :         if (!JS_GetClassObject(rq.cx, JSProto_String, &ctorobj))
     320           0 :             throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed");
     321           8 :         JS::RootedValue protoval(rq.cx, JS::ObjectOrNullValue(ctorobj));
     322             : 
     323           4 :         JS::RootedObject obj(rq.cx);
     324           4 :         if (!JS::Construct(rq.cx, protoval, JS::HandleValueArray(val), &obj))
     325           0 :             throw PSERROR_Deserialize_ScriptError("JS_New failed");
     326           4 :         AddScriptBackref(obj);
     327           4 :         return JS::ObjectValue(*obj);
     328           4 :     }
     329           4 :     case SCRIPT_TYPE_OBJECT_BOOLEAN:
     330           4 :     {
     331           4 :         bool value;
     332           4 :         Bool("value", value);
     333           4 :         JS::RootedValue val(rq.cx, JS::BooleanValue(value));
     334             : 
     335           4 :         JS::RootedObject ctorobj(rq.cx);
     336           4 :         if (!JS_GetClassObject(rq.cx, JSProto_Boolean, &ctorobj))
     337           0 :             throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed");
     338           8 :         JS::RootedValue protoval(rq.cx, JS::ObjectOrNullValue(ctorobj));
     339             : 
     340           4 :         JS::RootedObject obj(rq.cx);
     341           4 :         if (!JS::Construct(rq.cx, protoval, JS::HandleValueArray(val), &obj))
     342           0 :             throw PSERROR_Deserialize_ScriptError("JS_New failed");
     343           4 :         AddScriptBackref(obj);
     344           4 :         return JS::ObjectValue(*obj);
     345           4 :     }
     346          32 :     case SCRIPT_TYPE_TYPED_ARRAY:
     347          32 :     {
     348          32 :         std::uint8_t arrayType;
     349          32 :         std::uint32_t byteOffset, length;
     350          32 :         NumberU8_Unbounded("array type", arrayType);
     351          32 :         NumberU32_Unbounded("byte offset", byteOffset);
     352          32 :         NumberU32_Unbounded("length", length);
     353             : 
     354             :         // To match the serializer order, we reserve the typed array's backref tag here
     355          32 :         JS::RootedObject arrayObj(rq.cx);
     356          32 :         AddScriptBackref(arrayObj);
     357             : 
     358             :         // Get buffer object
     359          32 :         JS::RootedValue bufferVal(rq.cx, ReadScriptVal("buffer", nullptr));
     360             : 
     361          32 :         if (!bufferVal.isObject())
     362           0 :             throw PSERROR_Deserialize_ScriptError();
     363             : 
     364          32 :         JS::RootedObject bufferObj(rq.cx, &bufferVal.toObject());
     365          32 :         if (!JS::IsArrayBufferObject(bufferObj))
     366           0 :             throw PSERROR_Deserialize_ScriptError("js_IsArrayBuffer failed");
     367             : 
     368          32 :         switch(arrayType)
     369             :         {
     370           4 :         case SCRIPT_TYPED_ARRAY_INT8:
     371           4 :             arrayObj = JS_NewInt8ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     372           4 :             break;
     373           4 :         case SCRIPT_TYPED_ARRAY_UINT8:
     374           4 :             arrayObj = JS_NewUint8ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     375           4 :             break;
     376           6 :         case SCRIPT_TYPED_ARRAY_INT16:
     377           6 :             arrayObj = JS_NewInt16ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     378           6 :             break;
     379           4 :         case SCRIPT_TYPED_ARRAY_UINT16:
     380           4 :             arrayObj = JS_NewUint16ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     381           4 :             break;
     382           4 :         case SCRIPT_TYPED_ARRAY_INT32:
     383           4 :             arrayObj = JS_NewInt32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     384           4 :             break;
     385           4 :         case SCRIPT_TYPED_ARRAY_UINT32:
     386           4 :             arrayObj = JS_NewUint32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     387           4 :             break;
     388           2 :         case SCRIPT_TYPED_ARRAY_FLOAT32:
     389           2 :             arrayObj = JS_NewFloat32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     390           2 :             break;
     391           2 :         case SCRIPT_TYPED_ARRAY_FLOAT64:
     392           2 :             arrayObj = JS_NewFloat64ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     393           2 :             break;
     394           2 :         case SCRIPT_TYPED_ARRAY_UINT8_CLAMPED:
     395           2 :             arrayObj = JS_NewUint8ClampedArrayWithBuffer(rq.cx, bufferObj, byteOffset, length);
     396           2 :             break;
     397           0 :         default:
     398           0 :             throw PSERROR_Deserialize_ScriptError("Failed to deserialize unrecognized typed array view");
     399             :         }
     400          32 :         if (!arrayObj)
     401           0 :             throw PSERROR_Deserialize_ScriptError("js_CreateTypedArrayWithBuffer failed");
     402             : 
     403          32 :         return JS::ObjectValue(*arrayObj);
     404          32 :     }
     405          24 :     case SCRIPT_TYPE_ARRAY_BUFFER:
     406          24 :     {
     407          24 :         std::uint32_t length;
     408          24 :         NumberU32_Unbounded("buffer length", length);
     409             : 
     410             : #if BYTE_ORDER != LITTLE_ENDIAN
     411             : #error TODO: need to convert JS ArrayBuffer data from little-endian
     412             : #endif
     413          24 :         void* bufferData = js_malloc(length);
     414          24 :         ENSURE(bufferData);
     415          24 :         RawBytes("buffer data", reinterpret_cast<std::uint8_t*>(bufferData), length);
     416             : 
     417          48 :         mozilla::UniquePtr<void, JS::FreePolicy> contents{ bufferData };
     418          48 :         JS::RootedObject bufferObj(rq.cx, JS::NewArrayBufferWithContents(rq.cx, length, std::move(contents)));
     419          24 :         AddScriptBackref(bufferObj);
     420             : 
     421          24 :         return JS::ObjectValue(*bufferObj);
     422             :     }
     423          12 :     case SCRIPT_TYPE_OBJECT_MAP:
     424          12 :     {
     425          12 :         JS::RootedObject obj(rq.cx, JS::NewMapObject(rq.cx));
     426          12 :         AddScriptBackref(obj);
     427             : 
     428          12 :         std::uint32_t mapSize;
     429          12 :         NumberU32_Unbounded("map size", mapSize);
     430             : 
     431          30 :         for (std::uint32_t i=0; i<mapSize; ++i)
     432             :         {
     433          18 :             JS::RootedValue key(rq.cx, ReadScriptVal("map key", nullptr));
     434          18 :             JS::RootedValue value(rq.cx, ReadScriptVal("map value", nullptr));
     435          18 :             JS::MapSet(rq.cx, obj, key, value);
     436          18 :         }
     437             : 
     438          12 :         return JS::ObjectValue(*obj);
     439          12 :     }
     440           8 :     case SCRIPT_TYPE_OBJECT_SET:
     441           8 :     {
     442           8 :         JS::RootedObject obj(rq.cx, JS::NewSetObject(rq.cx));
     443           8 :         AddScriptBackref(obj);
     444             : 
     445           8 :         std::uint32_t setSize;
     446           8 :         NumberU32_Unbounded("set size", setSize);
     447             : 
     448          16 :         for (std::uint32_t i=0; i<setSize; ++i)
     449             :         {
     450           8 :             JS::RootedValue value(rq.cx, ReadScriptVal("set value", nullptr));
     451           8 :             JS::SetAdd(rq.cx, obj, value);
     452           8 :         }
     453             : 
     454           8 :         return JS::ObjectValue(*obj);
     455           8 :     }
     456           0 :     default:
     457           0 :         throw PSERROR_Deserialize_OutOfBounds();
     458             :     }
     459         324 : }
     460             : 
     461         185 : void CStdDeserializer::ReadStringLatin1(const char* name, std::vector<JS::Latin1Char>& str)
     462             : {
     463         185 :     uint32_t len;
     464         185 :     NumberU32_Unbounded("string length", len);
     465         185 :     RequireBytesInStream(len);
     466         185 :     str.resize(len);
     467         185 :     Get(name, reinterpret_cast<std::uint8_t*>(str.data()), len);
     468         185 : }
     469             : 
     470           6 : void CStdDeserializer::ReadStringUTF16(const char* name, std::u16string& str)
     471             : {
     472           6 :     uint32_t len;
     473           6 :     NumberU32_Unbounded("string length", len);
     474           6 :     RequireBytesInStream(len*2);
     475           6 :     str.resize(len);
     476           6 :     Get(name, reinterpret_cast<std::uint8_t*>(str.data()), len*2);
     477           6 : }
     478             : 
     479          41 : void CStdDeserializer::ScriptString(const char* name, JS::MutableHandleString out)
     480             : {
     481             : #if BYTE_ORDER != LITTLE_ENDIAN
     482             : #error TODO: probably need to convert JS strings from little-endian
     483             : #endif
     484             : 
     485          41 :     Script::Request rq(m_ScriptInterface);
     486             : 
     487          41 :     bool isLatin1;
     488          41 :     Bool("isLatin1", isLatin1);
     489          41 :     if (isLatin1)
     490             :     {
     491          35 :         std::vector<JS::Latin1Char> str;
     492          35 :         ReadStringLatin1(name, str);
     493             : 
     494          35 :         out.set(JS_NewStringCopyN(rq.cx, (const char*)str.data(), str.size()));
     495          35 :         if (!out)
     496           0 :             throw PSERROR_Deserialize_ScriptError("JS_NewStringCopyN failed");
     497          35 :     }
     498             :     else
     499             :     {
     500           6 :         std::u16string str;
     501           6 :         ReadStringUTF16(name, str);
     502             : 
     503           6 :         out.set(JS_NewUCStringCopyN(rq.cx, (const char16_t*)str.data(), str.length()));
     504           6 :         if (!out)
     505           0 :             throw PSERROR_Deserialize_ScriptError("JS_NewUCStringCopyN failed");
     506           6 :     }
     507          41 : }
     508             : 
     509          93 : void CStdDeserializer::ScriptVal(const char* name, JS::MutableHandleValue out)
     510             : {
     511          93 :     out.set(ReadScriptVal(name, nullptr));
     512          93 : }
     513             : 
     514           5 : void CStdDeserializer::ScriptObjectAssign(const char* name, JS::HandleValue objVal)
     515             : {
     516           5 :     Script::Request rq(m_ScriptInterface);
     517             : 
     518           5 :     if (!objVal.isObject())
     519           0 :         throw PSERROR_Deserialize_ScriptError();
     520             : 
     521           5 :     JS::RootedObject obj(rq.cx, &objVal.toObject());
     522           5 :     ReadScriptVal(name, obj);
     523           5 : }

Generated by: LCOV version 1.16