LCOV - code coverage report
Current view: top level - source/simulation2/serialization - SerializedScriptTypes.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 15 19 78.9 %
Date: 2023-01-19 00:18:29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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             : #ifndef INCLUDED_SERIALIZEDSCRIPTTYPES
      19             : #define INCLUDED_SERIALIZEDSCRIPTTYPES
      20             : 
      21             : enum
      22             : {
      23             :     SCRIPT_TYPE_VOID = 0,
      24             :     SCRIPT_TYPE_NULL = 1,
      25             :     SCRIPT_TYPE_ARRAY = 2,
      26             :     SCRIPT_TYPE_OBJECT = 3,             // standard Object prototype
      27             :     SCRIPT_TYPE_STRING = 4,
      28             :     SCRIPT_TYPE_INT = 5,
      29             :     SCRIPT_TYPE_DOUBLE = 6,
      30             :     SCRIPT_TYPE_BOOLEAN = 7,
      31             :     SCRIPT_TYPE_BACKREF = 8,
      32             :     SCRIPT_TYPE_TYPED_ARRAY = 9,        // ArrayBufferView subclasses - see below
      33             :     SCRIPT_TYPE_ARRAY_BUFFER = 10,      // ArrayBuffer containing actual typed array data (may be shared by multiple views)
      34             :     SCRIPT_TYPE_OBJECT_PROTOTYPE = 11,  // User-defined prototype - see GetPrototypeInfo
      35             :     SCRIPT_TYPE_OBJECT_NUMBER = 12,     // standard Number class
      36             :     SCRIPT_TYPE_OBJECT_STRING = 13,     // standard String class
      37             :     SCRIPT_TYPE_OBJECT_BOOLEAN = 14,    // standard Boolean class
      38             :     SCRIPT_TYPE_OBJECT_MAP = 15,        // Map class
      39             :     SCRIPT_TYPE_OBJECT_SET = 16         // Set class
      40             : };
      41             : 
      42             : // ArrayBufferView subclasses (to avoid relying directly on the JSAPI enums)
      43             : enum
      44             : {
      45             :     SCRIPT_TYPED_ARRAY_INT8 = 0,
      46             :     SCRIPT_TYPED_ARRAY_UINT8 = 1,
      47             :     SCRIPT_TYPED_ARRAY_INT16 = 2,
      48             :     SCRIPT_TYPED_ARRAY_UINT16 = 3,
      49             :     SCRIPT_TYPED_ARRAY_INT32 = 4,
      50             :     SCRIPT_TYPED_ARRAY_UINT32 = 5,
      51             :     SCRIPT_TYPED_ARRAY_FLOAT32 = 6,
      52             :     SCRIPT_TYPED_ARRAY_FLOAT64 = 7,
      53             :     SCRIPT_TYPED_ARRAY_UINT8_CLAMPED = 8
      54             : };
      55             : 
      56         128 : struct SPrototypeSerialization
      57             : {
      58             :     std::string name = "";
      59             :     bool hasCustomSerialize = false;
      60             :     bool hasCustomDeserialize = false;
      61             :     bool hasNullSerialize = false;
      62             : };
      63             : 
      64          64 : inline SPrototypeSerialization GetPrototypeInfo(const ScriptRequest& rq, JS::HandleObject prototype)
      65             : {
      66          64 :     SPrototypeSerialization ret;
      67             : 
      68          64 :     if (!Script::GetObjectClassName(rq, prototype, ret.name))
      69           0 :         throw PSERROR_Serialize_ScriptError("Could not get constructor name.");
      70             : 
      71             :     // Nothing to do for basic Object objects.
      72          64 :     if (ret.name == "Object")
      73          31 :         return ret;
      74             : 
      75          66 :     if (!JS_HasProperty(rq.cx, prototype, "Serialize", &ret.hasCustomSerialize) ||
      76          33 :          !JS_HasProperty(rq.cx, prototype, "Deserialize", &ret.hasCustomDeserialize))
      77           0 :         throw PSERROR_Serialize_ScriptError("JS_HasProperty failed");
      78             : 
      79          33 :     if (ret.hasCustomSerialize)
      80             :     {
      81          32 :         JS::RootedValue serialize(rq.cx);
      82          16 :         if (!JS_GetProperty(rq.cx, prototype, "Serialize", &serialize))
      83           0 :             throw PSERROR_Serialize_ScriptError("JS_GetProperty failed");
      84             : 
      85          16 :         if (serialize.isNull())
      86           6 :             ret.hasNullSerialize = true;
      87          10 :         else if (!ret.hasCustomDeserialize)
      88             :         {
      89             :             // Don't throw for this error: mods might need updating and this crashes as exceptions are not correctly handled.
      90           0 :             LOGERROR("Error serializing object '%s': non-null Serialize() but no matching Deserialize().", ret.name);
      91             :         }
      92             :     }
      93          33 :     return ret;
      94             : }
      95             : 
      96             : #endif // INCLUDED_SERIALIZEDSCRIPTTYPES

Generated by: LCOV version 1.13