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_SCRIPTINTERFACE_OBJECT
19 : #define INCLUDED_SCRIPTINTERFACE_OBJECT
20 :
21 : #include "lib/posix/posix_types.h"
22 : #include "ps/CLogger.h"
23 : #include "scriptinterface/Conversions.h"
24 : #include "scriptinterface/Request.h"
25 :
26 : #include <cwchar>
27 : #include <js/Array.h>
28 : #include <js/GCVector.h>
29 : #include <js/Id.h>
30 : #include <js/Object.h>
31 : #include <js/PropertyAndElement.h>
32 : #include <js/PropertyDescriptor.h>
33 : #include <js/RootingAPI.h>
34 : #include <js/TypeDecls.h>
35 : #include <js/Value.h>
36 : #include <jsapi.h>
37 : #include <jsfriendapi.h>
38 : #include <string>
39 : #include <type_traits>
40 : #include <utility>
41 : #include <vector>
42 :
43 : /**
44 : * Wraps SM APIs for manipulating JS objects.
45 : */
46 :
47 : namespace Script
48 : {
49 : /**
50 : * Get the named property on the given object.
51 : */
52 : template<typename PropType>
53 40129 : inline bool GetProperty(const Script::Request& rq, JS::HandleValue obj, PropType name, JS::MutableHandleValue out)
54 : {
55 40129 : if (!obj.isObject())
56 : return false;
57 40126 : JS::RootedObject object(rq.cx, &obj.toObject());
58 : if constexpr (std::is_same_v<int, PropType>)
59 : {
60 6 : JS::RootedId id(rq.cx, JS::PropertyKey::Int(name));
61 6 : return JS_GetPropertyById(rq.cx, object, id, out);
62 6 : }
63 : else if constexpr (std::is_same_v<const char*, PropType>)
64 40120 : return JS_GetProperty(rq.cx, object, name, out);
65 : else
66 : return JS_GetUCProperty(rq.cx, object, name, wcslen(name), out);
67 40129 : }
68 :
69 : template<typename T, typename PropType>
70 39656 : inline bool GetProperty(const Script::Request& rq, JS::HandleValue obj, PropType name, T& out)
71 : {
72 39656 : JS::RootedValue val(rq.cx);
73 39656 : if (!GetProperty<PropType>(rq, obj, name, &val))
74 : return false;
75 39653 : return FromJSVal(rq, val, out);
76 39656 : }
77 : inline bool GetProperty(const Script::Request& rq, JS::HandleValue obj, const char* name, JS::MutableHandleObject out)
78 : {
79 : JS::RootedValue val(rq.cx, JS::ObjectValue(*out.get()));
80 : if (!GetProperty(rq, obj, name, &val))
81 : return false;
82 : out.set(val.toObjectOrNull());
83 : return true;
84 : }
85 :
86 : template<typename T>
87 0 : inline bool GetPropertyInt(const Script::Request& rq, JS::HandleValue obj, int name, T& out)
88 : {
89 0 : return GetProperty(rq, obj, name, out);
90 : }
91 6 : inline bool GetPropertyInt(const Script::Request& rq, JS::HandleValue obj, int name, JS::MutableHandleValue out)
92 : {
93 6 : return GetProperty(rq, obj, name, out);
94 : }
95 : /**
96 : * Check the named property has been defined on the given object.
97 : */
98 463 : inline bool HasProperty(const Script::Request& rq, JS::HandleValue obj, const char* name)
99 : {
100 463 : if (!obj.isObject())
101 : return false;
102 462 : JS::RootedObject object(rq.cx, &obj.toObject());
103 :
104 462 : bool found;
105 462 : if (!JS_HasProperty(rq.cx, object, name, &found))
106 : return false;
107 462 : return found;
108 463 : }
109 :
110 : /**
111 : * Set the named property on the given object.
112 : */
113 : template<typename PropType>
114 8156 : inline bool SetProperty(const Script::Request& rq, JS::HandleValue obj, PropType name, JS::HandleValue value, bool constant = false, bool enumerable = true)
115 : {
116 8156 : unsigned int attrs = 0;
117 8156 : if (constant)
118 7996 : attrs |= JSPROP_READONLY | JSPROP_PERMANENT;
119 8156 : if (enumerable)
120 232 : attrs |= JSPROP_ENUMERATE;
121 :
122 8156 : if (!obj.isObject())
123 : return false;
124 8156 : JS::RootedObject object(rq.cx, &obj.toObject());
125 : if constexpr (std::is_same_v<int, PropType>)
126 : {
127 0 : JS::RootedId id(rq.cx, JS::PropertyKey::Int(name));
128 0 : return JS_DefinePropertyById(rq.cx, object, id, value, attrs);
129 0 : }
130 : else if constexpr (std::is_same_v<const char*, PropType>)
131 8156 : return JS_DefineProperty(rq.cx, object, name, value, attrs);
132 : else
133 : return JS_DefineUCProperty(rq.cx, object, name, value, attrs);
134 8156 : }
135 :
136 : template<typename T, typename PropType>
137 8156 : inline bool SetProperty(const Script::Request& rq, JS::HandleValue obj, PropType name, const T& value, bool constant = false, bool enumerable = true)
138 : {
139 8156 : JS::RootedValue val(rq.cx);
140 8156 : Script::ToJSVal(rq, &val, value);
141 8156 : return SetProperty<PropType>(rq, obj, name, val, constant, enumerable);
142 8156 : }
143 :
144 : template<typename T>
145 0 : inline bool SetPropertyInt(const Script::Request& rq, JS::HandleValue obj, int name, const T& value, bool constant = false, bool enumerable = true)
146 : {
147 0 : return SetProperty<T, int>(rq, obj, name, value, constant, enumerable);
148 : }
149 :
150 : template<typename T>
151 39263 : inline bool GetObjectClassName(const Script::Request& rq, JS::HandleObject obj, T& name)
152 : {
153 78526 : JS::RootedValue constructor(rq.cx, JS::ObjectOrNullValue(JS_GetConstructor(rq.cx, obj)));
154 39263 : return constructor.isObject() && Script::GetProperty(rq, constructor, "name", name);
155 39263 : }
156 :
157 : /**
158 : * Get the name of the object's class. Note that inheritance may lead to unexpected results.
159 : */
160 : template<typename T>
161 1 : inline bool GetObjectClassName(const Script::Request& rq, JS::HandleValue val, T& name)
162 : {
163 1 : JS::RootedObject obj(rq.cx, val.toObjectOrNull());
164 1 : if (!obj)
165 : return false;
166 1 : return GetObjectClassName(rq, obj, name);
167 1 : }
168 :
169 908 : inline bool DeepFreezeObject(const Script::Request& rq, JS::HandleValue objVal)
170 : {
171 908 : if (!objVal.isObject())
172 : {
173 0 : LOGERROR("DeepFreezeObject expected object type!");
174 0 : return false;
175 : }
176 :
177 : // Get all properties and recursively freeze those that are objects
178 908 : JS::RootedObject obj(rq.cx, &objVal.toObject());
179 908 : JS::RootedIdVector props(rq.cx);
180 908 : if (!js::GetPropertyKeys(rq.cx, obj, JSITER_HIDDEN, &props))
181 : return false;
182 :
183 44353 : for (const JS::PropertyKey& id : props)
184 : {
185 43445 : JS::RootedValue val(rq.cx);
186 43445 : if (!JS_IdToValue(rq.cx, id, &val))
187 : return false;
188 :
189 43445 : if (!val.isObject())
190 43445 : continue;
191 :
192 0 : if (!DeepFreezeObject(rq, val))
193 : return false;
194 43445 : }
195 :
196 908 : return JS_FreezeObject(rq.cx, obj);
197 908 : }
198 :
199 : /**
200 : * Returns all properties of the object, both own properties and inherited.
201 : * This is essentially equivalent to calling Object.getOwnPropertyNames()
202 : * and recursing up the prototype chain.
203 : * NB: this does not return properties with symbol or numeric keys, as that would
204 : * require a variant in the vector, and it's not useful for now.
205 : * @param enumerableOnly - only return enumerable properties.
206 : */
207 411 : inline bool EnumeratePropertyNames(const Script::Request& rq, JS::HandleValue objVal, bool enumerableOnly, std::vector<std::string>& out)
208 : {
209 411 : if (!objVal.isObjectOrNull())
210 : {
211 0 : LOGERROR("EnumeratePropertyNames expected object type!");
212 0 : return false;
213 : }
214 :
215 411 : JS::RootedObject obj(rq.cx, &objVal.toObject());
216 411 : JS::RootedIdVector props(rq.cx);
217 : // This recurses up the prototype chain on its own.
218 813 : if (!js::GetPropertyKeys(rq.cx, obj, enumerableOnly ? 0 : JSITER_HIDDEN, &props))
219 : return false;
220 :
221 411 : out.reserve(out.size() + props.length());
222 12030 : for (const JS::PropertyKey& id : props)
223 : {
224 11619 : JS::RootedValue val(rq.cx);
225 11619 : if (!JS_IdToValue(rq.cx, id, &val))
226 : return false;
227 :
228 : // Ignore integer properties for now.
229 : // TODO: is this actually a thing in ECMAScript 6?
230 11619 : if (!val.isString())
231 0 : continue;
232 :
233 11619 : std::string propName;
234 11619 : if (!FromJSVal(rq, val, propName))
235 0 : return false;
236 :
237 11619 : out.emplace_back(std::move(propName));
238 11619 : }
239 :
240 : return true;
241 411 : }
242 :
243 : /**
244 : * Create a plain object (i.e. {}). If it fails, returns undefined.
245 : */
246 122 : inline JS::Value CreateObject(const Script::Request& rq)
247 : {
248 122 : JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
249 122 : if (!obj)
250 0 : return JS::UndefinedValue();
251 122 : return JS::ObjectValue(*obj.get());
252 122 : }
253 :
254 122 : inline bool CreateObject(const Script::Request& rq, JS::MutableHandleValue objectValue)
255 : {
256 227 : objectValue.set(CreateObject(rq));
257 122 : return !objectValue.isNullOrUndefined();
258 : }
259 :
260 : /**
261 : * Sets the given value to a new plain JS::Object, converts the arguments to JS::Values and sets them as properties.
262 : * This is static so that callers like ToJSVal can use it with the JSContext directly instead of having to obtain the instance using GetScriptInterfaceAndCBData.
263 : * Can throw an exception.
264 : */
265 : template<typename T, typename... Args>
266 138 : inline bool CreateObject(const Script::Request& rq, JS::MutableHandleValue objectValue, const char* propertyName, const T& propertyValue, Args const&... args)
267 : {
268 138 : JS::RootedValue val(rq.cx);
269 138 : ToJSVal(rq, &val, propertyValue);
270 138 : return CreateObject(rq, objectValue, args...) && SetProperty(rq, objectValue, propertyName, val, false, true);
271 138 : }
272 :
273 : } // namespace Script
274 :
275 : #endif // INCLUDED_SCRIPTINTERFACE_Object
|