Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
SerializedScriptTypes.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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
22
23enum
24{
28 SCRIPT_TYPE_OBJECT = 3, // standard Object prototype
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)
45enum
46{
56};
57
59{
60 std::string name = "";
61 bool hasCustomSerialize = false;
63 bool hasNullSerialize = false;
64};
65
66inline SPrototypeSerialization GetPrototypeInfo(const ScriptRequest& rq, JS::HandleObject prototype)
67{
69
70 if (!Script::GetObjectClassName(rq, prototype, ret.name))
71 throw PSERROR_Serialize_ScriptError("Could not get constructor name.");
72
73 // Nothing to do for basic Object objects.
74 if (ret.name == "Object")
75 return ret;
76
77 if (!JS_HasProperty(rq.cx, prototype, "Serialize", &ret.hasCustomSerialize) ||
78 !JS_HasProperty(rq.cx, prototype, "Deserialize", &ret.hasCustomDeserialize))
79 throw PSERROR_Serialize_ScriptError("JS_HasProperty failed");
80
81 if (ret.hasCustomSerialize)
82 {
83 JS::RootedValue serialize(rq.cx);
84 if (!JS_GetProperty(rq.cx, prototype, "Serialize", &serialize))
85 throw PSERROR_Serialize_ScriptError("JS_GetProperty failed");
86
87 if (serialize.isNull())
88 ret.hasNullSerialize = true;
89 else if (!ret.hasCustomDeserialize)
90 {
91 // Don't throw for this error: mods might need updating and this crashes as exceptions are not correctly handled.
92 LOGERROR("Error serializing object '%s': non-null Serialize() but no matching Deserialize().", ret.name);
93 }
94 }
95 return ret;
96}
97
98#endif // INCLUDED_SERIALIZEDSCRIPTTYPES
#define LOGERROR(...)
Definition: CLogger.h:37
@ SCRIPT_TYPED_ARRAY_INT16
Definition: SerializedScriptTypes.h:49
@ SCRIPT_TYPED_ARRAY_INT32
Definition: SerializedScriptTypes.h:51
@ SCRIPT_TYPED_ARRAY_FLOAT64
Definition: SerializedScriptTypes.h:54
@ SCRIPT_TYPED_ARRAY_INT8
Definition: SerializedScriptTypes.h:47
@ SCRIPT_TYPED_ARRAY_UINT8
Definition: SerializedScriptTypes.h:48
@ SCRIPT_TYPED_ARRAY_FLOAT32
Definition: SerializedScriptTypes.h:53
@ SCRIPT_TYPED_ARRAY_UINT32
Definition: SerializedScriptTypes.h:52
@ SCRIPT_TYPED_ARRAY_UINT8_CLAMPED
Definition: SerializedScriptTypes.h:55
@ SCRIPT_TYPED_ARRAY_UINT16
Definition: SerializedScriptTypes.h:50
SPrototypeSerialization GetPrototypeInfo(const ScriptRequest &rq, JS::HandleObject prototype)
Definition: SerializedScriptTypes.h:66
@ SCRIPT_TYPE_TYPED_ARRAY
Definition: SerializedScriptTypes.h:34
@ SCRIPT_TYPE_NULL
Definition: SerializedScriptTypes.h:26
@ SCRIPT_TYPE_OBJECT_PROTOTYPE
Definition: SerializedScriptTypes.h:36
@ SCRIPT_TYPE_OBJECT_NUMBER
Definition: SerializedScriptTypes.h:37
@ SCRIPT_TYPE_OBJECT_STRING
Definition: SerializedScriptTypes.h:38
@ SCRIPT_TYPE_OBJECT_MAP
Definition: SerializedScriptTypes.h:40
@ SCRIPT_TYPE_ARRAY
Definition: SerializedScriptTypes.h:27
@ SCRIPT_TYPE_OBJECT_SET
Definition: SerializedScriptTypes.h:41
@ SCRIPT_TYPE_OBJECT
Definition: SerializedScriptTypes.h:28
@ SCRIPT_TYPE_OBJECT_BOOLEAN
Definition: SerializedScriptTypes.h:39
@ SCRIPT_TYPE_VOID
Definition: SerializedScriptTypes.h:25
@ SCRIPT_TYPE_BACKREF
Definition: SerializedScriptTypes.h:33
@ SCRIPT_TYPE_STRING
Definition: SerializedScriptTypes.h:29
@ SCRIPT_TYPE_INT
Definition: SerializedScriptTypes.h:30
@ SCRIPT_TYPE_ARRAY_BUFFER
Definition: SerializedScriptTypes.h:35
@ SCRIPT_TYPE_DOUBLE
Definition: SerializedScriptTypes.h:31
@ SCRIPT_TYPE_BOOLEAN
Definition: SerializedScriptTypes.h:32
Definition: Errors.cpp:51
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
JSContext * cx
Definition: ScriptRequest.h:92
bool GetObjectClassName(const ScriptRequest &rq, JS::HandleObject obj, T &name)
Definition: Object.h:135
Definition: SerializedScriptTypes.h:59
bool hasCustomSerialize
Definition: SerializedScriptTypes.h:61
bool hasNullSerialize
Definition: SerializedScriptTypes.h:63
bool hasCustomDeserialize
Definition: SerializedScriptTypes.h:62
std::string name
Definition: SerializedScriptTypes.h:60