Pyrogenesis  trunk
SerializedScriptTypes.h
Go to the documentation of this file.
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 {
26  SCRIPT_TYPE_OBJECT = 3, // standard Object prototype
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 {
54 };
55 
57 {
58  std::string name = "";
59  bool hasCustomSerialize = false;
60  bool hasCustomDeserialize = false;
61  bool hasNullSerialize = false;
62 };
63 
64 inline SPrototypeSerialization GetPrototypeInfo(const ScriptRequest& rq, JS::HandleObject prototype)
65 {
67 
68  if (!Script::GetObjectClassName(rq, prototype, ret.name))
69  throw PSERROR_Serialize_ScriptError("Could not get constructor name.");
70 
71  // Nothing to do for basic Object objects.
72  if (ret.name == "Object")
73  return ret;
74 
75  if (!JS_HasProperty(rq.cx, prototype, "Serialize", &ret.hasCustomSerialize) ||
76  !JS_HasProperty(rq.cx, prototype, "Deserialize", &ret.hasCustomDeserialize))
77  throw PSERROR_Serialize_ScriptError("JS_HasProperty failed");
78 
79  if (ret.hasCustomSerialize)
80  {
81  JS::RootedValue serialize(rq.cx);
82  if (!JS_GetProperty(rq.cx, prototype, "Serialize", &serialize))
83  throw PSERROR_Serialize_ScriptError("JS_GetProperty failed");
84 
85  if (serialize.isNull())
86  ret.hasNullSerialize = true;
87  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  LOGERROR("Error serializing object '%s': non-null Serialize() but no matching Deserialize().", ret.name);
91  }
92  }
93  return ret;
94 }
95 
96 #endif // INCLUDED_SERIALIZEDSCRIPTTYPES
JSContext * cx
Definition: ScriptRequest.h:92
Definition: SerializedScriptTypes.h:27
#define LOGERROR(...)
Definition: CLogger.h:36
Definition: SerializedScriptTypes.h:52
Definition: SerializedScriptTypes.h:38
Definition: SerializedScriptTypes.h:34
std::string name
Definition: SerializedScriptTypes.h:58
bool hasNullSerialize
Definition: SerializedScriptTypes.h:61
Definition: SerializedScriptTypes.h:30
bool hasCustomSerialize
Definition: SerializedScriptTypes.h:59
Definition: SerializedScriptTypes.h:23
Definition: SerializedScriptTypes.h:35
Definition: SerializedScriptTypes.h:51
Definition: SerializedScriptTypes.h:46
Definition: SerializedScriptTypes.h:48
Definition: SerializedScriptTypes.h:33
Definition: SerializedScriptTypes.h:26
Definition: SerializedScriptTypes.h:31
Definition: SerializedScriptTypes.h:24
Definition: SerializedScriptTypes.h:56
Definition: SerializedScriptTypes.h:53
Definition: SerializedScriptTypes.h:50
Definition: SerializedScriptTypes.h:47
Definition: SerializedScriptTypes.h:25
Definition: SerializedScriptTypes.h:29
Definition: Errors.cpp:51
bool GetObjectClassName(const ScriptRequest &rq, JS::HandleObject obj, T &name)
Definition: Object.h:135
bool hasCustomDeserialize
Definition: SerializedScriptTypes.h:60
Definition: SerializedScriptTypes.h:45
Definition: SerializedScriptTypes.h:39
Definition: SerializedScriptTypes.h:37
Definition: SerializedScriptTypes.h:32
Definition: SerializedScriptTypes.h:49
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
SPrototypeSerialization GetPrototypeInfo(const ScriptRequest &rq, JS::HandleObject prototype)
Definition: SerializedScriptTypes.h:64
Definition: SerializedScriptTypes.h:36
Definition: SerializedScriptTypes.h:28