LCOV - code coverage report
Current view: top level - gi - ns.cpp (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 87.3 % 79 69
Test Date: 2024-04-20 17:42:51 Functions: 100.0 % 10 10
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 67.4 % 43 29

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

Generated by: LCOV version 2.0-1