LCOV - code coverage report
Current view: top level - gi - interface.cpp (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 73.1 % 78 57
Test Date: 2024-04-20 17:42:51 Functions: 85.7 % 7 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60.5 % 38 23

             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                 :             : // SPDX-FileCopyrightText: 2012 Red Hat, Inc.
       5                 :             : 
       6                 :             : #include <config.h>
       7                 :             : 
       8                 :             : #include <girepository.h>
       9                 :             : 
      10                 :             : #include <js/Class.h>
      11                 :             : #include <js/ErrorReport.h>  // for JS_ReportOutOfMemory
      12                 :             : #include <js/GCVector.h>     // for MutableHandleIdVector
      13                 :             : #include <js/Id.h>           // for PropertyKey, jsid
      14                 :             : #include <js/TypeDecls.h>
      15                 :             : #include <js/Utility.h>  // for UniqueChars
      16                 :             : 
      17                 :             : #include "gi/function.h"
      18                 :             : #include "gi/interface.h"
      19                 :             : #include "gi/object.h"
      20                 :             : #include "gi/repo.h"
      21                 :             : #include "gjs/atoms.h"
      22                 :             : #include "gjs/context-private.h"
      23                 :             : #include "gjs/jsapi-util.h"
      24                 :             : #include "gjs/mem-private.h"
      25                 :             : 
      26                 :         148 : InterfacePrototype::InterfacePrototype(GIInterfaceInfo* info, GType gtype)
      27                 :             :     : GIWrapperPrototype(info, gtype),
      28                 :         148 :       m_vtable(
      29                 :         148 :           static_cast<GTypeInterface*>(g_type_default_interface_ref(gtype))) {
      30                 :         148 :     GJS_INC_COUNTER(interface);
      31                 :         148 : }
      32                 :             : 
      33                 :         148 : InterfacePrototype::~InterfacePrototype(void) {
      34         [ +  - ]:         148 :     g_clear_pointer(&m_vtable, g_type_default_interface_unref);
      35                 :         148 :     GJS_DEC_COUNTER(interface);
      36                 :         148 : }
      37                 :             : 
      38                 :          48 : bool InterfacePrototype::new_enumerate_impl(
      39                 :             :     JSContext* cx, JS::HandleObject, JS::MutableHandleIdVector properties,
      40                 :             :     bool only_enumerable [[maybe_unused]]) {
      41         [ +  + ]:          48 :     if (!info())
      42                 :          26 :         return true;
      43                 :             : 
      44                 :          22 :     int n_methods = g_interface_info_get_n_methods(info());
      45         [ -  + ]:          22 :     if (!properties.reserve(properties.length() + n_methods)) {
      46                 :           0 :         JS_ReportOutOfMemory(cx);
      47                 :           0 :         return false;
      48                 :             :     }
      49                 :             : 
      50         [ +  + ]:         478 :     for (int i = 0; i < n_methods; i++) {
      51                 :         456 :         GjsAutoFunctionInfo meth_info = g_interface_info_get_method(info(), i);
      52                 :         456 :         GIFunctionInfoFlags flags = g_function_info_get_flags(meth_info);
      53                 :             : 
      54         [ +  + ]:         456 :         if (flags & GI_FUNCTION_IS_METHOD) {
      55                 :         408 :             const char* name = meth_info.name();
      56                 :         408 :             jsid id = gjs_intern_string_to_id(cx, name);
      57         [ -  + ]:         408 :             if (id.isVoid())
      58                 :           0 :                 return false;
      59                 :         408 :             properties.infallibleAppend(id);
      60                 :             :         }
      61         [ +  - ]:         456 :     }
      62                 :             : 
      63                 :          22 :     return true;
      64                 :             : }
      65                 :             : 
      66                 :             : // See GIWrapperBase::resolve().
      67                 :         999 : bool InterfacePrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
      68                 :             :                                       JS::HandleId id, bool* resolved) {
      69                 :             :     /* If we have no GIRepository information then this interface was defined
      70                 :             :      * from within GJS. In that case, it has no properties that need to be
      71                 :             :      * resolved from within C code, as interfaces cannot inherit. */
      72         [ +  + ]:         999 :     if (!info()) {
      73                 :          21 :         *resolved = false;
      74                 :          21 :         return true;
      75                 :             :     }
      76                 :             : 
      77                 :         978 :     JS::UniqueChars prop_name;
      78         [ -  + ]:         978 :     if (!gjs_get_string_id(context, id, &prop_name))
      79                 :           0 :         return false;
      80         [ -  + ]:         978 :     if (!prop_name) {
      81                 :           0 :         *resolved = false;
      82                 :           0 :         return true;  // not resolved, but no error
      83                 :             :     }
      84                 :             : 
      85                 :             :     GjsAutoFunctionInfo method_info =
      86                 :         978 :         g_interface_info_find_method(m_info, prop_name.get());
      87                 :             : 
      88         [ +  + ]:         978 :     if (method_info) {
      89         [ +  - ]:         309 :         if (g_function_info_get_flags (method_info) & GI_FUNCTION_IS_METHOD) {
      90         [ -  + ]:         309 :             if (!gjs_define_function(context, obj, m_gtype, method_info))
      91                 :           0 :                 return false;
      92                 :             : 
      93                 :         309 :             *resolved = true;
      94                 :             :         } else {
      95                 :           0 :             *resolved = false;
      96                 :             :         }
      97                 :             :     } else {
      98                 :         669 :         *resolved = false;
      99                 :             :     }
     100                 :             : 
     101                 :         978 :     return true;
     102                 :         978 : }
     103                 :             : 
     104                 :             : /*
     105                 :             :  * InterfaceBase::has_instance:
     106                 :             :  *
     107                 :             :  * JSNative implementation of `[Symbol.hasInstance]()`. This method is never
     108                 :             :  * called directly, but instead is called indirectly by the JS engine as part of
     109                 :             :  * an `instanceof` expression.
     110                 :             :  */
     111                 :          19 : bool InterfaceBase::has_instance(JSContext* cx, unsigned argc, JS::Value* vp) {
     112         [ -  + ]:          19 :     GJS_GET_THIS(cx, argc, vp, args, interface_constructor);
     113                 :             : 
     114                 :          19 :     JS::RootedObject interface_proto(cx);
     115                 :          19 :     const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
     116         [ -  + ]:          19 :     if (!gjs_object_require_property(cx, interface_constructor,
     117                 :             :                                      "interface constructor", atoms.prototype(),
     118                 :             :                                      &interface_proto))
     119                 :           0 :         return false;
     120                 :             : 
     121                 :             :     InterfaceBase* priv;
     122         [ -  + ]:          19 :     if (!for_js_typecheck(cx, interface_proto, &priv))
     123                 :           0 :         return false;
     124                 :             : 
     125                 :          19 :     return priv->to_prototype()->has_instance_impl(cx, args);
     126                 :          19 : }
     127                 :             : 
     128                 :             : // See InterfaceBase::has_instance().
     129                 :          19 : bool InterfacePrototype::has_instance_impl(JSContext* cx,
     130                 :             :                                            const JS::CallArgs& args) {
     131                 :             :     // This method is never called directly, so no need for error messages.
     132                 :          19 :     g_assert(args.length() == 1);
     133                 :             : 
     134         [ +  + ]:          19 :     if (!args[0].isObject()) {
     135                 :           6 :         args.rval().setBoolean(false);
     136                 :           6 :         return true;
     137                 :             :     }
     138                 :             : 
     139                 :          13 :     JS::RootedObject instance(cx, &args[0].toObject());
     140                 :          13 :     bool isinstance = ObjectBase::typecheck(cx, instance, nullptr, m_gtype,
     141                 :             :                                             GjsTypecheckNoThrow());
     142                 :          13 :     args.rval().setBoolean(isinstance);
     143                 :          13 :     return true;
     144                 :          13 : }
     145                 :             : 
     146                 :             : // clang-format off
     147                 :             : const struct JSClassOps InterfaceBase::class_ops = {
     148                 :             :     nullptr,  // addProperty
     149                 :             :     nullptr,  // deleteProperty
     150                 :             :     nullptr,  // enumerate
     151                 :             :     &InterfaceBase::new_enumerate,
     152                 :             :     &InterfaceBase::resolve,
     153                 :             :     nullptr,  // mayResolve
     154                 :             :     &InterfaceBase::finalize,
     155                 :             : };
     156                 :             : 
     157                 :             : const struct JSClass InterfaceBase::klass = {
     158                 :             :     "GObject_Interface",
     159                 :             :     JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_BACKGROUND_FINALIZE,
     160                 :             :     &InterfaceBase::class_ops
     161                 :             : };
     162                 :             : 
     163                 :             : JSFunctionSpec InterfaceBase::static_methods[] = {
     164                 :             :     JS_SYM_FN(hasInstance, &InterfaceBase::has_instance, 1, 0),
     165                 :             :     JS_FS_END
     166                 :             : };
     167                 :             : // clang-format on
     168                 :             : 
     169                 :             : bool
     170                 :           0 : gjs_lookup_interface_constructor(JSContext             *context,
     171                 :             :                                  GType                  gtype,
     172                 :             :                                  JS::MutableHandleValue value_p)
     173                 :             : {
     174                 :             :     JSObject *constructor;
     175                 :             : 
     176                 :           0 :     GjsAutoInterfaceInfo interface_info = gjs_lookup_gtype(nullptr, gtype);
     177         [ #  # ]:           0 :     if (!interface_info) {
     178                 :           0 :         gjs_throw(context, "Cannot expose non introspectable interface %s",
     179                 :             :                   g_type_name(gtype));
     180                 :           0 :         return false;
     181                 :             :     }
     182                 :             : 
     183                 :           0 :     constructor = gjs_lookup_generic_constructor(context, interface_info);
     184         [ #  # ]:           0 :     if (G_UNLIKELY(!constructor))
     185                 :           0 :         return false;
     186                 :             : 
     187                 :           0 :     value_p.setObject(*constructor);
     188                 :           0 :     return true;
     189                 :           0 : }
        

Generated by: LCOV version 2.0-1