Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2008 litl, LLC
4 : :
5 : : #include <config.h>
6 : :
7 : : #include <string.h>
8 : :
9 : : #include <string>
10 : : #include <vector>
11 : :
12 : : #include <girepository.h>
13 : : #include <glib.h>
14 : :
15 : : #include <js/CallArgs.h>
16 : : #include <js/Class.h>
17 : : #include <js/ComparisonOperators.h>
18 : : #include <js/ErrorReport.h> // for JS_ReportOutOfMemory
19 : : #include <js/GCVector.h> // for MutableHandleIdVector
20 : : #include <js/Id.h>
21 : : #include <js/PropertyDescriptor.h> // for JSPROP_READONLY
22 : : #include <js/PropertySpec.h>
23 : : #include <js/RootingAPI.h>
24 : : #include <js/TypeDecls.h>
25 : : #include <js/Utility.h> // for UniqueChars
26 : : #include <jsapi.h> // for JS_NewObjectWithGivenProto
27 : :
28 : : #include "gi/cwrapper.h"
29 : : #include "gi/info.h"
30 : : #include "gi/ns.h"
31 : : #include "gi/repo.h"
32 : : #include "gjs/atoms.h"
33 : : #include "gjs/auto.h"
34 : : #include "gjs/context-private.h"
35 : : #include "gjs/global.h"
36 : : #include "gjs/jsapi-util.h"
37 : : #include "gjs/macros.h"
38 : : #include "gjs/mem-private.h"
39 : : #include "util/log.h"
40 : :
41 : : #if GLIB_CHECK_VERSION(2, 79, 2)
42 : : # include "gjs/deprecation.h"
43 : : #endif // GLib >= 2.79.2
44 : :
45 : 10032 : [[nodiscard]] static bool type_is_enumerable(GIInfoType info_type) {
46 [ + + ]: 10032 : switch (info_type) {
47 : 10012 : case GI_INFO_TYPE_BOXED:
48 : : case GI_INFO_TYPE_STRUCT:
49 : : case GI_INFO_TYPE_UNION:
50 : : case GI_INFO_TYPE_OBJECT:
51 : : case GI_INFO_TYPE_ENUM:
52 : : case GI_INFO_TYPE_FLAGS:
53 : : case GI_INFO_TYPE_INTERFACE:
54 : : case GI_INFO_TYPE_FUNCTION:
55 : : case GI_INFO_TYPE_CONSTANT:
56 : 10012 : return true;
57 : : // Don't enumerate types which GJS doesn't define on namespaces.
58 : : // See gjs_define_info
59 : 20 : case GI_INFO_TYPE_INVALID:
60 : : case GI_INFO_TYPE_INVALID_0:
61 : : case GI_INFO_TYPE_CALLBACK:
62 : : case GI_INFO_TYPE_VALUE:
63 : : case GI_INFO_TYPE_SIGNAL:
64 : : case GI_INFO_TYPE_VFUNC:
65 : : case GI_INFO_TYPE_PROPERTY:
66 : : case GI_INFO_TYPE_FIELD:
67 : : case GI_INFO_TYPE_ARG:
68 : : case GI_INFO_TYPE_TYPE:
69 : : case GI_INFO_TYPE_UNRESOLVED:
70 : : default:
71 : 20 : return false;
72 : : }
73 : : }
74 : :
75 : : class Ns : private Gjs::AutoChar, public CWrapper<Ns> {
76 : : friend CWrapperPointerOps<Ns>;
77 : : friend CWrapper<Ns>;
78 : :
79 : : #if GLIB_CHECK_VERSION(2, 79, 2)
80 : : bool m_is_gio_or_glib : 1;
81 : : #endif // GLib >= 2.79.2
82 : :
83 : : static constexpr auto PROTOTYPE_SLOT = GjsGlobalSlot::PROTOTYPE_ns;
84 : : static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_GNAMESPACE;
85 : :
86 : 251 : explicit Ns(const char* ns_name)
87 : 251 : : Gjs::AutoChar(const_cast<char*>(ns_name), Gjs::TakeOwnership{}) {
88 : 251 : GJS_INC_COUNTER(ns);
89 : : #if GLIB_CHECK_VERSION(2, 79, 2)
90 : 251 : m_is_gio_or_glib =
91 [ + + + + ]: 251 : strcmp(ns_name, "Gio") == 0 || strcmp(ns_name, "GLib") == 0;
92 : : #endif // GLib >= 2.79.2
93 : 251 : }
94 : :
95 : 247 : ~Ns() { GJS_DEC_COUNTER(ns); }
96 : :
97 : : #if GLIB_CHECK_VERSION(2, 79, 2)
98 : : // helper function
99 : 10280 : void platform_specific_warning(JSContext* cx, const char* prefix,
100 : : const char* platform,
101 : : const char* resolved_name,
102 : : const char** exceptions = nullptr) {
103 [ + + ]: 10280 : if (!g_str_has_prefix(resolved_name, prefix))
104 : 10279 : return;
105 : :
106 : 7 : const char* base_name = resolved_name + strlen(prefix);
107 : : Gjs::AutoChar old_name{
108 : 7 : g_strdup_printf("%s.%s", this->get(), resolved_name)};
109 [ + - ]: 7 : if (exceptions) {
110 [ + + ]: 24 : for (const char** exception = exceptions; *exception; exception++) {
111 [ + + ]: 23 : if (strcmp(old_name, *exception) == 0)
112 : 6 : return;
113 : : }
114 : : }
115 : :
116 : : Gjs::AutoChar new_name{
117 : 1 : g_strdup_printf("%s%s.%s", this->get(), platform, base_name)};
118 [ + + ]: 7 : _gjs_warn_deprecated_once_per_callsite(
119 : : cx, GjsDeprecationMessageId::PlatformSpecificTypelib,
120 : 2 : {old_name.get(), new_name.get()});
121 [ + + ]: 7 : }
122 : : #endif // GLib >= 2.79.2
123 : :
124 : : // JSClass operations
125 : :
126 : : // The *resolved out parameter, on success, should be false to indicate that
127 : : // id was not resolved; and true if id was resolved.
128 : : GJS_JSAPI_RETURN_CONVENTION
129 : 13760 : bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
130 : : bool* resolved) {
131 [ + + ]: 13760 : if (!id.isString()) {
132 : 31 : *resolved = false;
133 : 31 : return true; // not resolved, but no error
134 : : }
135 : :
136 : : // let Object.prototype resolve these
137 : 13729 : const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
138 [ + + - + : 13729 : if (id == atoms.to_string() || id == atoms.value_of()) {
+ + ]
139 : 14 : *resolved = false;
140 : 14 : return true;
141 : : }
142 : :
143 : 13715 : JS::UniqueChars name;
144 [ - + ]: 13715 : if (!gjs_get_string_id(cx, id, &name))
145 : 0 : return false;
146 [ - + ]: 13715 : if (!name) {
147 : 0 : *resolved = false;
148 : 0 : return true; // not resolved, but no error
149 : : }
150 : :
151 : : GI::AutoBaseInfo info{
152 : 13715 : g_irepository_find_by_name(nullptr, get(), name.get())};
153 [ + + ]: 13715 : if (!info) {
154 : 4410 : *resolved = false; // No property defined, but no error either
155 : 4410 : return true;
156 : : }
157 : :
158 : 9305 : gjs_debug(GJS_DEBUG_GNAMESPACE,
159 : : "Found info type %s for '%s' in namespace '%s'",
160 : : gjs_info_type_name(info.type()), info.name(), info.ns());
161 : :
162 : : #if GLIB_CHECK_VERSION(2, 79, 2)
163 : : static const char* unix_types_exceptions[] = {
164 : : "Gio.UnixConnection",
165 : : "Gio.UnixCredentialsMessage",
166 : : "Gio.UnixFDList",
167 : : "Gio.UnixSocketAddress",
168 : : "Gio.UnixSocketAddressType",
169 : : nullptr};
170 : :
171 [ + + ]: 9305 : if (m_is_gio_or_glib) {
172 : 2570 : platform_specific_warning(cx, "Unix", "Unix", name.get(),
173 : : unix_types_exceptions);
174 : 2570 : platform_specific_warning(cx, "unix_", "Unix", name.get());
175 : 2570 : platform_specific_warning(cx, "Win32", "Win32", name.get());
176 : 2570 : platform_specific_warning(cx, "win32_", "Win32", name.get());
177 : : }
178 : : #endif // GLib >= 2.79.2
179 : :
180 : : bool defined;
181 [ - + ]: 9305 : if (!gjs_define_info(cx, obj, info, &defined)) {
182 : 0 : gjs_debug(GJS_DEBUG_GNAMESPACE, "Failed to define info '%s'",
183 : : info.name());
184 : 0 : return false;
185 : : }
186 : :
187 : : // we defined the property in this object?
188 : 9305 : *resolved = defined;
189 : 9305 : return true;
190 : 13715 : }
191 : :
192 : : GJS_JSAPI_RETURN_CONVENTION
193 : 4 : bool new_enumerate_impl(JSContext* cx,
194 : : JS::HandleObject obj [[maybe_unused]],
195 : : JS::MutableHandleIdVector properties,
196 : : bool only_enumerable [[maybe_unused]]) {
197 : 4 : int n = g_irepository_get_n_infos(nullptr, get());
198 [ - + ]: 4 : if (!properties.reserve(properties.length() + n)) {
199 : 0 : JS_ReportOutOfMemory(cx);
200 : 0 : return false;
201 : : }
202 : :
203 [ + + ]: 10036 : for (int k = 0; k < n; k++) {
204 : 10032 : GI::AutoBaseInfo info{g_irepository_get_info(nullptr, get(), k)};
205 : 10032 : GIInfoType info_type = g_base_info_get_type(info);
206 [ + + ]: 10032 : if (!type_is_enumerable(info_type))
207 : 20 : continue;
208 : :
209 : 10012 : const char* name = info.name();
210 : :
211 : 10012 : jsid id = gjs_intern_string_to_id(cx, name);
212 [ - + ]: 10012 : if (id.isVoid())
213 : 0 : return false;
214 : 10012 : properties.infallibleAppend(id);
215 [ + + - ]: 10032 : }
216 : :
217 : 4 : return true;
218 : : }
219 : :
220 : 247 : static void finalize_impl(JS::GCContext*, Ns* priv) {
221 : 247 : g_assert(priv && "Finalize called on wrong object");
222 [ + - ]: 247 : delete priv;
223 : 247 : }
224 : :
225 : : // Properties and methods
226 : :
227 : : GJS_JSAPI_RETURN_CONVENTION
228 : 2 : static bool get_name(JSContext* cx, unsigned argc, JS::Value* vp) {
229 [ - + - + ]: 2 : GJS_CHECK_WRAPPER_PRIV(cx, argc, vp, args, this_obj, Ns, priv);
230 : 2 : return gjs_string_from_utf8(cx, priv->get(), args.rval());
231 : 2 : }
232 : :
233 : : GJS_JSAPI_RETURN_CONVENTION
234 : 4 : static bool get_version(JSContext* cx, unsigned argc, JS::Value* vp) {
235 [ - + - + ]: 4 : GJS_CHECK_WRAPPER_PRIV(cx, argc, vp, args, this_obj, Ns, priv);
236 : 4 : const char *version = g_irepository_get_version(nullptr, priv->get());
237 : 4 : return gjs_string_from_utf8(cx, version, args.rval());
238 : 4 : }
239 : :
240 : : static constexpr JSClassOps class_ops = {
241 : : nullptr, // addProperty
242 : : nullptr, // deleteProperty
243 : : nullptr, // enumerate
244 : : &Ns::new_enumerate,
245 : : &Ns::resolve,
246 : : nullptr, // mayResolve
247 : : &Ns::finalize,
248 : : };
249 : :
250 : : // clang-format off
251 : : static constexpr JSPropertySpec proto_props[] = {
252 : : JS_STRING_SYM_PS(toStringTag, "GIRepositoryNamespace", JSPROP_READONLY),
253 : : JS_PSG("__name__", &Ns::get_name, GJS_MODULE_PROP_FLAGS),
254 : : JS_PSG("__version__", &Ns::get_version, GJS_MODULE_PROP_FLAGS & ~JSPROP_ENUMERATE),
255 : : JS_PS_END};
256 : : // clang-format on
257 : :
258 : : static constexpr js::ClassSpec class_spec = {
259 : : nullptr, // createConstructor
260 : : nullptr, // createPrototype
261 : : nullptr, // constructorFunctions
262 : : nullptr, // constructorProperties
263 : : nullptr, // prototypeFunctions
264 : : Ns::proto_props,
265 : : nullptr, // finishInit
266 : : js::ClassSpec::DontDefineConstructor};
267 : :
268 : : static constexpr JSClass klass = {
269 : : "GIRepositoryNamespace",
270 : : JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_FOREGROUND_FINALIZE,
271 : : &Ns::class_ops, &Ns::class_spec};
272 : :
273 : : public:
274 : : GJS_JSAPI_RETURN_CONVENTION
275 : 251 : static JSObject* create(JSContext* cx, const char* ns_name) {
276 : 251 : JS::RootedObject proto(cx, Ns::create_prototype(cx));
277 [ - + ]: 251 : if (!proto)
278 : 0 : return nullptr;
279 : :
280 : : JS::RootedObject ns(cx,
281 : 251 : JS_NewObjectWithGivenProto(cx, &Ns::klass, proto));
282 [ - + ]: 251 : if (!ns)
283 : 0 : return nullptr;
284 : :
285 : 251 : auto* priv = new Ns(ns_name);
286 : 251 : Ns::init_private(ns, priv);
287 : :
288 : : gjs_debug_lifecycle(GJS_DEBUG_GNAMESPACE,
289 : : "ns constructor, obj %p priv %p", ns.get(), priv);
290 : :
291 : 251 : return ns;
292 : 251 : }
293 : : };
294 : :
295 : : JSObject*
296 : 251 : gjs_create_ns(JSContext *context,
297 : : const char *ns_name)
298 : : {
299 : 251 : return Ns::create(context, ns_name);
300 : : }
|