Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Conversions.h
Go to the documentation of this file.
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"
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
39namespace Script
40{
41/**
42 * Convert a JS::Value to a C++ type. (This might trigger GC.)
43 */
44template<typename T> bool FromJSVal(const Request& rq, const JS::HandleValue val, T& ret);
45
46template<typename T>
47bool FromJSVal(const Request& rq, JS::HandleValue v, std::optional<T>& out)
48{
49 if (v.isNullOrUndefined())
50 {
51 out = std::nullopt;
52 return true;
53 }
54 T value;
55 if (!FromJSVal(rq, v, value))
56 return false;
57 out = std::move(value);
58 return true;
59}
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 */
68template<typename T> void ToJSVal(const Request& rq, JS::MutableHandleValue ret, T const& val);
69
70template<std::size_t N>
71void ToJSVal(const Request& rq, JS::MutableHandleValue ret, const char (&val)[N])
72{
73 ToJSVal(rq, ret, static_cast<const char*>(val));
74}
75template<std::size_t N>
76void 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
81template<>
82inline void ToJSVal<JS::PersistentRootedValue>(const Request&, JS::MutableHandleValue handle,
83 const JS::PersistentRootedValue& a)
84{
85 handle.set(a);
86}
87
88template<>
89inline 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
95template<>
96inline void ToJSVal<JS::RootedValue>(const Request&, JS::MutableHandleValue handle,
97 const JS::RootedValue& a)
98{
99 handle.set(a);
100}
101
102template <>
103inline void ToJSVal<JS::HandleValue>(const Request&, JS::MutableHandleValue handle,
104 const JS::HandleValue& a)
105{
106 handle.set(a);
107}
108
109/**
110 * Convert a named property of an object to a C++ type.
111 */
112template<typename T> inline bool FromJSProperty(const Request& rq, const JS::HandleValue val, const char* name, T& ret, bool strict = false)
113{
114 if (!val.isObject())
115 return false;
116
117 JS::RootedObject obj(rq.cx, &val.toObject());
118
119 bool hasProperty;
120 if (!JS_HasProperty(rq.cx, obj, name, &hasProperty) || !hasProperty)
121 return false;
122
123 JS::RootedValue value(rq.cx);
124 if (!JS_GetProperty(rq.cx, obj, name, &value))
125 return false;
126
127 if (strict && value.isNull())
128 return false;
129
130 return FromJSVal(rq, value, ret);
131}
132
133template<typename T> inline void ToJSVal_vector(const Request& rq, JS::MutableHandleValue ret, const std::vector<T>& val)
134{
135 JS::RootedObject obj(rq.cx, JS::NewArrayObject(rq.cx, 0));
136 if (!obj)
137 {
138 ret.setUndefined();
139 return;
140 }
141
142 ENSURE(val.size() <= std::numeric_limits<std::uint32_t>::max());
143 for (std::uint32_t i = 0; i < val.size(); ++i)
144 {
145 JS::RootedValue el(rq.cx);
146 ToJSVal<T>(rq, &el, val[i]);
147 JS_SetElement(rq.cx, obj, i, el);
148 }
149 ret.setObject(*obj);
150}
151
152#define FAIL(msg) STMT(Exception::Raise(rq, msg); return false)
153
154template<typename T> inline bool FromJSVal_vector(const Request& rq, JS::HandleValue v, std::vector<T>& out)
155{
156 JS::RootedObject obj(rq.cx);
157 if (!v.isObject())
158 FAIL("Argument must be an array");
159
160 bool isArray;
161 obj = &v.toObject();
162 if ((!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj))
163 FAIL("Argument must be an array");
164
165 std::uint32_t length;
166 if (!JS::GetArrayLength(rq.cx, obj, &length))
167 FAIL("Failed to get array length");
168
169 out.clear();
170 out.reserve(length);
171 for (std::uint32_t i = 0; i < length; ++i)
172 {
173 JS::RootedValue el(rq.cx);
174 if (!JS_GetElement(rq.cx, obj, i, &el))
175 FAIL("Failed to read array element");
176 T el2{};
177 if (!FromJSVal<T>(rq, el, el2))
178 return false;
179 out.push_back(el2);
180 }
181 return true;
182}
183
184#undef FAIL
185
186} // namespace Script
187
188#define JSVAL_VECTOR(T) \
189namespace Script { \
190template<> void ToJSVal<std::vector<T> >(const Request& rq, JS::MutableHandleValue ret, const std::vector<T>& val) \
191{ \
192 ToJSVal_vector(rq, ret, val); \
193} \
194template<> 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
#define FAIL(msg)
Definition: Conversions.h:152
Definition: Request.h:51
JSContext * cx
Definition: Request.h:83
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
bool FromJSVal_vector(const Request &rq, JS::HandleValue v, std::vector< T > &out)
Definition: Conversions.h:154
bool FromJSVal(const Request &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
void ToJSVal(const Request &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
bool FromJSProperty(const Request &rq, const JS::HandleValue val, const char *name, T &ret, bool strict=false)
Convert a named property of an object to a C++ type.
Definition: Conversions.h:112
void ToJSVal_vector(const Request &rq, JS::MutableHandleValue ret, const std::vector< T > &val)
Definition: Conversions.h:133
#define T(string_literal)
Definition: secure_crt.cpp:81
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407