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