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 : #include "precompiled.h"
19 :
20 : #include "graphics/Color.h"
21 : #include "lib/code_generation.h"
22 : #include "maths/Fixed.h"
23 : #include "maths/FixedVector2D.h"
24 : #include "maths/FixedVector3D.h"
25 : #include "maths/NUSpline.h"
26 : #include "ps/CLogger.h"
27 : #include "ps/CStr.h"
28 : #include "scriptinterface/Object.h"
29 : #include "scriptinterface/Conversions.h"
30 : #include "scriptinterface/Exceptions.h"
31 : #include "scriptinterface/Interface.h"
32 : #include "scriptinterface/Request.h"
33 : #include "simulation2/helpers/CinemaPath.h"
34 : #include "simulation2/helpers/Grid.h"
35 : #include "simulation2/system/Component.h"
36 :
37 : #include <cstring>
38 : #include <js/Array.h>
39 : #include <js/CallAndConstruct.h>
40 : #include <js/Conversions.h>
41 : #include <js/GCAPI.h>
42 : #include <js/PropertyAndElement.h>
43 : #include <js/RootingAPI.h>
44 : #include <js/TypeDecls.h>
45 : #include <js/Value.h>
46 : #include <js/ValueArray.h>
47 : #include <js/experimental/TypedData.h>
48 : #include <string>
49 : #include <vector>
50 : #include <optional>
51 :
52 : #define FAIL(msg) STMT(LOGERROR(msg); return false)
53 : #define FAIL_VOID(msg) STMT(Script::Exception::Raise(rq, msg); return)
54 :
55 110164 : template<> void Script::ToJSVal<IComponent*>(const Script::Request&, JS::MutableHandleValue ret,
56 : IComponent* const& val)
57 : {
58 110164 : if (val == NULL)
59 : {
60 17407 : ret.setNull();
61 110164 : return;
62 : }
63 :
64 92757 : JS::HandleValue instance(val->GetJSInstance());
65 92757 : if (instance.isNull())
66 : {
67 : // Report as an error, since scripts really shouldn't try to use unscriptable interfaces
68 0 : LOGERROR("IComponent does not have a scriptable interface");
69 0 : ret.setUndefined();
70 0 : return;
71 : }
72 :
73 92757 : ret.set(instance);
74 : }
75 :
76 4000 : template<> void Script::ToJSVal<CParamNode>(const Script::Request& rq, JS::MutableHandleValue ret, CParamNode const& val)
77 : {
78 3966 : val.ToJSVal(rq, true, ret);
79 3966 : }
80 :
81 34 : template<> void Script::ToJSVal<const CParamNode*>(const Script::Request& rq, JS::MutableHandleValue ret, const CParamNode* const& val)
82 : {
83 34 : if (val)
84 34 : ToJSVal(rq, ret, *val);
85 : else
86 0 : ret.setUndefined();
87 34 : }
88 :
89 354 : template<> bool Script::FromJSVal<CColor>(const Script::Request& rq, JS::HandleValue v, CColor& out)
90 : {
91 354 : if (!v.isObject())
92 0 : FAIL("CColor has to be an object");
93 :
94 354 : JS::RootedObject obj(rq.cx, &v.toObject());
95 :
96 354 : JS::RootedValue r(rq.cx);
97 354 : JS::RootedValue g(rq.cx);
98 354 : JS::RootedValue b(rq.cx);
99 354 : JS::RootedValue a(rq.cx);
100 354 : if (!JS_GetProperty(rq.cx, obj, "r", &r) || !FromJSVal(rq, r, out.r))
101 0 : FAIL("Failed to get property CColor.r");
102 354 : if (!JS_GetProperty(rq.cx, obj, "g", &g) || !FromJSVal(rq, g, out.g))
103 0 : FAIL("Failed to get property CColor.g");
104 354 : if (!JS_GetProperty(rq.cx, obj, "b", &b) || !FromJSVal(rq, b, out.b))
105 0 : FAIL("Failed to get property CColor.b");
106 354 : if (!JS_GetProperty(rq.cx, obj, "a", &a) || !FromJSVal(rq, a, out.a))
107 0 : FAIL("Failed to get property CColor.a");
108 :
109 : return true;
110 354 : }
111 :
112 0 : template<> void Script::ToJSVal<CColor>(const Script::Request& rq, JS::MutableHandleValue ret, CColor const& val)
113 : {
114 0 : Script::CreateObject(
115 : rq,
116 : ret,
117 0 : "r", val.r,
118 0 : "g", val.g,
119 0 : "b", val.b,
120 0 : "a", val.a);
121 0 : }
122 :
123 14556 : template<> bool Script::FromJSVal<fixed>(const Script::Request& rq, JS::HandleValue v, fixed& out)
124 : {
125 14556 : double ret;
126 29112 : if (!JS::ToNumber(rq.cx, v, &ret))
127 : return false;
128 14556 : out = fixed::FromDouble(ret);
129 : // double can precisely represent the full range of fixed, so this is a non-lossy conversion
130 :
131 14556 : return true;
132 : }
133 :
134 27102 : template<> void Script::ToJSVal<fixed>(const Script::Request&, JS::MutableHandleValue ret, const fixed& val)
135 : {
136 27102 : ret.set(JS::NumberValue(val.ToDouble()));
137 27102 : }
138 :
139 356 : template<> bool Script::FromJSVal<CFixedVector3D>(const Script::Request& rq, JS::HandleValue v, CFixedVector3D& out)
140 : {
141 356 : if (!v.isObject())
142 : return false; // TODO: report type error
143 :
144 356 : JS::RootedObject obj(rq.cx, &v.toObject());
145 356 : JS::RootedValue p(rq.cx);
146 :
147 356 : if (!JS_GetProperty(rq.cx, obj, "x", &p)) return false; // TODO: report type errors
148 356 : if (!FromJSVal(rq, p, out.X)) return false;
149 :
150 356 : if (!JS_GetProperty(rq.cx, obj, "y", &p)) return false;
151 356 : if (!FromJSVal(rq, p, out.Y)) return false;
152 :
153 356 : if (!JS_GetProperty(rq.cx, obj, "z", &p)) return false;
154 356 : if (!FromJSVal(rq, p, out.Z)) return false;
155 :
156 : return true;
157 356 : }
158 :
159 2149 : template<> void Script::ToJSVal<CFixedVector3D>(const Script::Request& rq, JS::MutableHandleValue ret, const CFixedVector3D& val)
160 : {
161 2149 : JS::RootedObject global(rq.cx, rq.glob);
162 2149 : JS::RootedValue valueVector3D(rq.cx);
163 2149 : if (!Script::Interface::GetGlobalProperty(rq, "Vector3D", &valueVector3D))
164 0 : FAIL_VOID("Failed to get Vector3D constructor");
165 :
166 2149 : JS::RootedValueArray<3> args(rq.cx);
167 2149 : args[0].setNumber(val.X.ToDouble());
168 2149 : args[1].setNumber(val.Y.ToDouble());
169 2149 : args[2].setNumber(val.Z.ToDouble());
170 :
171 2149 : JS::RootedObject objVec(rq.cx);
172 2149 : if (!JS::Construct(rq.cx, valueVector3D, args, &objVec))
173 0 : FAIL_VOID("Failed to construct Vector3D object");
174 :
175 2149 : ret.setObject(*objVec);
176 2149 : }
177 :
178 59 : template<> bool Script::FromJSVal<CFixedVector2D>(const Script::Request& rq, JS::HandleValue v, CFixedVector2D& out)
179 : {
180 59 : if (!v.isObject())
181 : return false; // TODO: report type error
182 59 : JS::RootedObject obj(rq.cx, &v.toObject());
183 :
184 59 : JS::RootedValue p(rq.cx);
185 :
186 59 : if (!JS_GetProperty(rq.cx, obj, "x", &p)) return false; // TODO: report type errors
187 59 : if (!FromJSVal(rq, p, out.X)) return false;
188 :
189 59 : if (!JS_GetProperty(rq.cx, obj, "y", &p)) return false;
190 59 : if (!FromJSVal(rq, p, out.Y)) return false;
191 :
192 : return true;
193 59 : }
194 :
195 610 : template<> void Script::ToJSVal<CFixedVector2D>(const Script::Request& rq, JS::MutableHandleValue ret, const CFixedVector2D& val)
196 : {
197 610 : JS::RootedObject global(rq.cx, rq.glob);
198 610 : JS::RootedValue valueVector2D(rq.cx);
199 610 : if (!Script::Interface::GetGlobalProperty(rq, "Vector2D", &valueVector2D))
200 0 : FAIL_VOID("Failed to get Vector2D constructor");
201 :
202 610 : JS::RootedValueArray<2> args(rq.cx);
203 610 : args[0].setNumber(val.X.ToDouble());
204 610 : args[1].setNumber(val.Y.ToDouble());
205 :
206 610 : JS::RootedObject objVec(rq.cx);
207 610 : if (!JS::Construct(rq.cx, valueVector2D, args, &objVec))
208 0 : FAIL_VOID("Failed to construct Vector2D object");
209 :
210 610 : ret.setObject(*objVec);
211 610 : }
212 :
213 0 : template<> bool Script::FromJSVal<std::optional<CFixedVector2D>>(const Script::Request& rq, JS::HandleValue v, std::optional<CFixedVector2D>& out)
214 : {
215 0 : if (v.isNullOrUndefined())
216 : {
217 0 : out = std::nullopt;
218 0 : return true;
219 : }
220 :
221 0 : CFixedVector2D vec;
222 0 : if (!FromJSVal(rq, v, vec))
223 : return false;
224 :
225 0 : out = vec;
226 : return true;
227 : }
228 :
229 0 : template<> void Script::ToJSVal<std::optional<CFixedVector2D>>(const Script::Request& rq, JS::MutableHandleValue ret, const std::optional<CFixedVector2D>& val)
230 : {
231 0 : if (!val.has_value())
232 0 : ret.setNull();
233 : else
234 0 : ToJSVal(rq, ret, val.value());
235 0 : }
236 :
237 3 : template<> void Script::ToJSVal<Grid<std::uint8_t>>(const Script::Request& rq, JS::MutableHandleValue ret,
238 : const Grid<std::uint8_t>& val)
239 : {
240 3 : std::uint32_t length = static_cast<std::uint32_t>(val.m_W * val.m_H);
241 3 : std::uint32_t nbytes = static_cast<std::uint32_t>(length * sizeof(std::uint8_t));
242 3 : JS::RootedObject objArr(rq.cx, JS_NewUint8Array(rq.cx, length));
243 : // Copy the array data and then remove the no-GC check to allow further changes to the JS data
244 3 : {
245 3 : JS::AutoCheckCannotGC nogc;
246 3 : bool sharedMemory;
247 3 : memcpy((void*)JS_GetUint8ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes);
248 : }
249 :
250 3 : JS::RootedValue data(rq.cx, JS::ObjectValue(*objArr));
251 3 : Script::CreateObject(
252 : rq,
253 : ret,
254 3 : "width", val.m_W,
255 3 : "height", val.m_H,
256 : "data", data);
257 3 : }
258 :
259 3 : template<> void Script::ToJSVal<Grid<std::uint16_t>>(const Script::Request& rq, JS::MutableHandleValue ret,
260 : const Grid<std::uint16_t>& val)
261 : {
262 3 : std::uint32_t length = static_cast<std::uint32_t>(val.m_W * val.m_H);
263 3 : std::uint32_t nbytes = static_cast<std::uint32_t>(length * sizeof(std::uint16_t));
264 3 : JS::RootedObject objArr(rq.cx, JS_NewUint16Array(rq.cx, length));
265 : // Copy the array data and then remove the no-GC check to allow further changes to the JS data
266 3 : {
267 3 : JS::AutoCheckCannotGC nogc;
268 3 : bool sharedMemory;
269 3 : memcpy((void*)JS_GetUint16ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes);
270 : }
271 :
272 3 : JS::RootedValue data(rq.cx, JS::ObjectValue(*objArr));
273 3 : Script::CreateObject(
274 : rq,
275 : ret,
276 3 : "width", val.m_W,
277 3 : "height", val.m_H,
278 : "data", data);
279 3 : }
280 :
281 0 : template<> bool Script::FromJSVal<TNSpline>(const Script::Request& rq, JS::HandleValue v, TNSpline& out)
282 : {
283 0 : if (!v.isObject())
284 0 : FAIL("Argument must be an object");
285 :
286 0 : JS::RootedObject obj(rq.cx, &v.toObject());
287 0 : bool isArray;
288 0 : if (!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray)
289 0 : FAIL("Argument must be an array");
290 :
291 0 : std::uint32_t numberOfNodes = 0;
292 0 : if (!JS::GetArrayLength(rq.cx, obj, &numberOfNodes))
293 0 : FAIL("Failed to get array length");
294 :
295 0 : for (std::uint32_t i = 0; i < numberOfNodes; ++i)
296 : {
297 0 : JS::RootedValue node(rq.cx);
298 0 : if (!JS_GetElement(rq.cx, obj, i, &node))
299 0 : FAIL("Failed to read array element");
300 :
301 0 : fixed deltaTime;
302 0 : if (!FromJSProperty(rq, node, "deltaTime", deltaTime))
303 0 : FAIL("Failed to read Spline.deltaTime property");
304 :
305 0 : CFixedVector3D position;
306 0 : if (!FromJSProperty(rq, node, "position", position))
307 0 : FAIL("Failed to read Spline.position property");
308 :
309 0 : out.AddNode(position, CFixedVector3D(), deltaTime);
310 0 : }
311 :
312 0 : if (out.GetAllNodes().empty())
313 0 : FAIL("Spline must contain at least one node");
314 :
315 : return true;
316 0 : }
317 :
318 0 : template<> bool Script::FromJSVal<CCinemaPath>(const Script::Request& rq, JS::HandleValue v, CCinemaPath& out)
319 : {
320 0 : if (!v.isObject())
321 0 : FAIL("Argument must be an object");
322 :
323 0 : JS::RootedObject obj(rq.cx, &v.toObject());
324 :
325 0 : CCinemaData pathData;
326 0 : TNSpline positionSpline, targetSpline;
327 :
328 0 : if (!FromJSProperty(rq, v, "name", pathData.m_Name))
329 0 : FAIL("Failed to get CCinemaPath.name property");
330 :
331 0 : if (!FromJSProperty(rq, v, "orientation", pathData.m_Orientation))
332 0 : FAIL("Failed to get CCinemaPath.orientation property");
333 :
334 0 : if (!FromJSProperty(rq, v, "positionNodes", positionSpline))
335 0 : FAIL("Failed to get CCinemaPath.positionNodes property");
336 :
337 0 : if (pathData.m_Orientation == L"target" && !FromJSProperty(rq, v, "targetNodes", targetSpline))
338 0 : FAIL("Failed to get CCinemaPath.targetNodes property");
339 :
340 : // Other properties are not necessary to be defined
341 0 : if (!FromJSProperty(rq, v, "timescale", pathData.m_Timescale))
342 0 : pathData.m_Timescale = fixed::FromInt(1);
343 :
344 0 : if (!FromJSProperty(rq, v, "mode", pathData.m_Mode))
345 0 : pathData.m_Mode = L"ease_inout";
346 :
347 0 : if (!FromJSProperty(rq, v, "style", pathData.m_Style))
348 0 : pathData.m_Style = L"default";
349 :
350 0 : out = CCinemaPath(pathData, positionSpline, targetSpline);
351 :
352 0 : return true;
353 0 : }
354 :
355 : // define vectors
356 0 : JSVAL_VECTOR(CFixedVector2D)
357 :
358 : #undef FAIL
359 : #undef FAIL_VOID
|