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_SCRIPTCONVERSIONS
19 : #define INCLUDED_SCRIPTCONVERSIONS
20 :
21 : #include "lib/code_generation.h"
22 : #include "lib/debug.h"
23 : #include "ps/CStr.h"
24 : #include "scriptinterface/Exceptions.h"
25 : #include "scriptinterface/Request.h"
26 :
27 : #include <js/Array.h>
28 : #include <js/PropertyAndElement.h>
29 : #include <js/RootingAPI.h>
30 : #include <js/TypeDecls.h>
31 : #include <js/Value.h>
32 : #include <js/experimental/TypedData.h>
33 : #include <limits>
34 : #include <optional>
35 : #include <string>
36 : #include <utility>
37 : #include <vector>
38 :
39 : namespace Script
40 : {
41 : /**
42 : * Convert a JS::Value to a C++ type. (This might trigger GC.)
43 : */
44 : template<typename T> bool FromJSVal(const Request& rq, const JS::HandleValue val, T& ret);
45 :
46 : template<typename T>
47 240 : bool FromJSVal(const Request& rq, JS::HandleValue v, std::optional<T>& out)
48 : {
49 240 : if (v.isNullOrUndefined())
50 : {
51 0 : out = std::nullopt;
52 0 : return true;
53 : }
54 240 : T value;
55 240 : if (!FromJSVal(rq, v, value))
56 : return false;
57 240 : out = std::move(value);
58 240 : return true;
59 240 : }
60 :
61 : /**
62 : * Convert a C++ type to a JS::Value. (This might trigger GC. The return
63 : * value must be rooted if you don't want it to be collected.)
64 : * NOTE: We are passing the JS::Value by reference instead of returning it by value.
65 : * The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio.
66 : * Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921
67 : */
68 : template<typename T> void ToJSVal(const Request& rq, JS::MutableHandleValue ret, T const& val);
69 :
70 : template<std::size_t N>
71 9 : void ToJSVal(const Request& rq, JS::MutableHandleValue ret, const char (&val)[N])
72 : {
73 9 : ToJSVal(rq, ret, static_cast<const char*>(val));
74 : }
75 : template<std::size_t N>
76 : void ToJSVal(const Request& rq, JS::MutableHandleValue ret, const wchar_t (&val)[N])
77 : {
78 : ToJSVal(rq, ret, static_cast<const wchar_t*>(val));
79 : }
80 :
81 : template<>
82 19 : inline void ToJSVal<JS::PersistentRootedValue>(const Request&, JS::MutableHandleValue handle,
83 : const JS::PersistentRootedValue& a)
84 : {
85 19 : handle.set(a);
86 : }
87 :
88 : template<>
89 : inline void ToJSVal<JS::Heap<JS::Value> >(const Request&, JS::MutableHandleValue handle,
90 : const JS::Heap<JS::Value>& a)
91 : {
92 : handle.set(a);
93 : }
94 :
95 : template<>
96 20567 : inline void ToJSVal<JS::RootedValue>(const Request&, JS::MutableHandleValue handle,
97 : const JS::RootedValue& a)
98 : {
99 20567 : handle.set(a);
100 : }
101 :
102 : template <>
103 620 : inline void ToJSVal<JS::HandleValue>(const Request&, JS::MutableHandleValue handle,
104 : const JS::HandleValue& a)
105 : {
106 620 : handle.set(a);
107 : }
108 :
109 : /**
110 : * Convert a named property of an object to a C++ type.
111 : */
112 505 : template<typename T> inline bool FromJSProperty(const Request& rq, const JS::HandleValue val, const char* name, T& ret, bool strict = false)
113 : {
114 505 : if (!val.isObject())
115 : return false;
116 :
117 501 : JS::RootedObject obj(rq.cx, &val.toObject());
118 :
119 : bool hasProperty;
120 501 : if (!JS_HasProperty(rq.cx, obj, name, &hasProperty) || !hasProperty)
121 13 : return false;
122 :
123 488 : JS::RootedValue value(rq.cx);
124 488 : if (!JS_GetProperty(rq.cx, obj, name, &value))
125 : return false;
126 :
127 488 : if (strict && value.isNull())
128 : return false;
129 :
130 481 : return FromJSVal(rq, value, ret);
131 505 : }
132 :
133 1356 : template<typename T> inline void ToJSVal_vector(const Request& rq, JS::MutableHandleValue ret, const std::vector<T>& val)
134 : {
135 1356 : JS::RootedObject obj(rq.cx, JS::NewArrayObject(rq.cx, 0));
136 1356 : if (!obj)
137 : {
138 0 : ret.setUndefined();
139 0 : return;
140 : }
141 :
142 1356 : ENSURE(val.size() <= std::numeric_limits<std::uint32_t>::max());
143 2256 : for (std::uint32_t i = 0; i < val.size(); ++i)
144 : {
145 900 : JS::RootedValue el(rq.cx);
146 1152 : ToJSVal<T>(rq, &el, val[i]);
147 900 : JS_SetElement(rq.cx, obj, i, el);
148 : }
149 1356 : ret.setObject(*obj);
150 1356 : }
151 :
152 : #define FAIL(msg) STMT(Exception::Raise(rq, msg); return false)
153 :
154 1326 : template<typename T> inline bool FromJSVal_vector(const Request& rq, JS::HandleValue v, std::vector<T>& out)
155 : {
156 1326 : JS::RootedObject obj(rq.cx);
157 1326 : if (!v.isObject())
158 1326 : FAIL("Argument must be an array");
159 :
160 : bool isArray;
161 1326 : obj = &v.toObject();
162 1326 : if ((!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj))
163 1326 : FAIL("Argument must be an array");
164 :
165 : std::uint32_t length;
166 1326 : if (!JS::GetArrayLength(rq.cx, obj, &length))
167 1326 : FAIL("Failed to get array length");
168 :
169 1326 : out.clear();
170 1326 : out.reserve(length);
171 2653 : for (std::uint32_t i = 0; i < length; ++i)
172 : {
173 1327 : JS::RootedValue el(rq.cx);
174 1327 : if (!JS_GetElement(rq.cx, obj, i, &el))
175 655 : FAIL("Failed to read array element");
176 1327 : T el2{};
177 1327 : if (!FromJSVal<T>(rq, el, el2))
178 0 : return false;
179 1327 : out.push_back(el2);
180 : }
181 : return true;
182 1326 : }
183 :
184 : #undef FAIL
185 :
186 : } // namespace Script
187 :
188 : #define JSVAL_VECTOR(T) \
189 : namespace Script { \
190 : template<> void ToJSVal<std::vector<T> >(const Request& rq, JS::MutableHandleValue ret, const std::vector<T>& val) \
191 : { \
192 : ToJSVal_vector(rq, ret, val); \
193 : } \
194 : template<> bool FromJSVal<std::vector<T> >(const Request& rq, JS::HandleValue v, std::vector<T>& out) \
195 : { \
196 : return FromJSVal_vector(rq, v, out); \
197 : } \
198 : }
199 :
200 : #endif //INCLUDED_SCRIPTCONVERSIONS
|