LCOV - code coverage report
Current view: top level - gi - fundamental.h (source / functions) Coverage Total Hit
Test: gjs-1.86.0 Code Coverage Lines: 96.2 % 26 25
Test Date: 2025-11-16 18:28:38 Functions: 100.0 % 9 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 75.0 % 8 6

             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: 2013 Intel Corporation
       4                 :             : // SPDX-FileCopyrightText: 2008-2010 litl, LLC
       5                 :             : 
       6                 :             : #pragma once
       7                 :             : 
       8                 :             : #include <config.h>
       9                 :             : 
      10                 :             : #include <girepository/girepository.h>
      11                 :             : #include <glib-object.h>
      12                 :             : 
      13                 :             : #include <js/TypeDecls.h>
      14                 :             : #include <mozilla/Maybe.h>
      15                 :             : 
      16                 :             : #include "gi/cwrapper.h"
      17                 :             : #include "gi/info.h"
      18                 :             : #include "gi/wrapperutils.h"
      19                 :             : #include "gjs/macros.h"
      20                 :             : #include "util/log.h"
      21                 :             : 
      22                 :             : class FundamentalPrototype;
      23                 :             : class FundamentalInstance;
      24                 :             : namespace JS { class CallArgs; }
      25                 :             : 
      26                 :             : /* To conserve memory, we have two different kinds of private data for JS
      27                 :             :  * wrappers for fundamental types: FundamentalInstance, and
      28                 :             :  * FundamentalPrototype. Both inherit from FundamentalBase for their common
      29                 :             :  * functionality. For more information, see the notes in wrapperutils.h.
      30                 :             :  */
      31                 :             : 
      32                 :             : class FundamentalBase
      33                 :             :     : public GIWrapperBase<FundamentalBase, FundamentalPrototype,
      34                 :             :                            FundamentalInstance> {
      35                 :             :     friend class CWrapperPointerOps<FundamentalBase>;
      36                 :             :     friend class GIWrapperBase<FundamentalBase, FundamentalPrototype,
      37                 :             :                                FundamentalInstance>;
      38                 :             : 
      39                 :             :  protected:
      40                 :          37 :     explicit FundamentalBase(FundamentalPrototype* proto = nullptr)
      41                 :          37 :         : GIWrapperBase(proto) {}
      42                 :             : 
      43                 :             :     static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_GFUNDAMENTAL;
      44                 :             :     static constexpr const char* DEBUG_TAG = "fundamental";
      45                 :             : 
      46                 :             :     static const struct JSClassOps class_ops;
      47                 :             :     static const struct JSClass klass;
      48                 :             : 
      49                 :             :     // Public API
      50                 :             : 
      51                 :             :  public:
      52                 :             :     GJS_JSAPI_RETURN_CONVENTION
      53                 :             :     static bool to_gvalue(JSContext*, JS::HandleObject, GValue*);
      54                 :             : };
      55                 :             : 
      56                 :             : class FundamentalPrototype
      57                 :             :     : public GIWrapperPrototype<FundamentalBase, FundamentalPrototype,
      58                 :             :                                 FundamentalInstance, GI::AutoObjectInfo,
      59                 :             :                                 GI::ObjectInfo> {
      60                 :             :     friend class GIWrapperPrototype<FundamentalBase, FundamentalPrototype,
      61                 :             :                                     FundamentalInstance, GI::AutoObjectInfo,
      62                 :             :                                     GI::ObjectInfo>;
      63                 :             :     friend class GIWrapperBase<FundamentalBase, FundamentalPrototype,
      64                 :             :                                FundamentalInstance>;
      65                 :             : 
      66                 :             :     GIObjectInfoRefFunction m_ref_function;
      67                 :             :     GIObjectInfoUnrefFunction m_unref_function;
      68                 :             :     GIObjectInfoGetValueFunction m_get_value_function;
      69                 :             :     GIObjectInfoSetValueFunction m_set_value_function;
      70                 :             :     mozilla::Maybe<GI::AutoFunctionInfo> m_constructor_info;
      71                 :             : 
      72                 :             :     explicit FundamentalPrototype(const GI::ObjectInfo, GType);
      73                 :             :     ~FundamentalPrototype();
      74                 :             : 
      75                 :             :  public:
      76                 :             :     GJS_JSAPI_RETURN_CONVENTION
      77                 :             :     static FundamentalPrototype* for_gtype(JSContext*, GType);
      78                 :             : 
      79                 :             :     // Accessors
      80                 :             : 
      81                 :             :     [[nodiscard]]
      82                 :          44 :     mozilla::Maybe<const GI::FunctionInfo> constructor_info() const {
      83                 :          44 :         return m_constructor_info;
      84                 :             :     }
      85                 :             : 
      86                 :          26 :     void* call_ref_function(void* ptr) const {
      87         [ -  + ]:          26 :         if (!m_ref_function)
      88                 :           0 :             return ptr;
      89                 :             : 
      90                 :          26 :         return m_ref_function(ptr);
      91                 :             :     }
      92                 :          49 :     void call_unref_function(void* ptr) const {
      93         [ +  - ]:          49 :         if (m_unref_function)
      94                 :          49 :             m_unref_function(ptr);
      95                 :          49 :     }
      96                 :          13 :     [[nodiscard]] bool call_get_value_function(const GValue* value,
      97                 :             :                                                void** ptr_out) const {
      98         [ +  + ]:          13 :         if (!m_get_value_function)
      99                 :           6 :             return false;
     100                 :             : 
     101                 :           7 :         *ptr_out = m_get_value_function(value);
     102                 :           7 :         return true;
     103                 :             :     }
     104                 :          11 :     bool call_set_value_function(GValue* value, void* object) const {
     105         [ +  + ]:          11 :         if (m_set_value_function) {
     106                 :           4 :             m_set_value_function(value, object);
     107                 :           4 :             return true;
     108                 :             :         }
     109                 :             : 
     110                 :           7 :         return false;
     111                 :             :     }
     112                 :             : 
     113                 :             :     // Helper methods
     114                 :             : 
     115                 :             :  private:
     116                 :             :     GJS_JSAPI_RETURN_CONVENTION
     117                 :             :     bool get_parent_proto(JSContext*, JS::MutableHandleObject proto) const;
     118                 :             : 
     119                 :             :     [[nodiscard]] unsigned constructor_nargs() const;
     120                 :             : 
     121                 :             :     GJS_JSAPI_RETURN_CONVENTION
     122                 :             :     bool resolve_interface(JSContext*, JS::HandleObject, bool* resolved,
     123                 :             :                            const char* name);
     124                 :             : 
     125                 :             :     // JSClass operations
     126                 :             : 
     127                 :             :     GJS_JSAPI_RETURN_CONVENTION
     128                 :             :     bool resolve_impl(JSContext*, JS::HandleObject, JS::HandleId,
     129                 :             :                       bool* resolved);
     130                 :             : 
     131                 :             :     // Public API
     132                 :             :  public:
     133                 :             :     GJS_JSAPI_RETURN_CONVENTION
     134                 :             :     static bool define_class(JSContext*, JS::HandleObject in_object,
     135                 :             :                              const GI::ObjectInfo,
     136                 :             :                              JS::MutableHandleObject constructor);
     137                 :             : };
     138                 :             : 
     139                 :             : class FundamentalInstance
     140                 :             :     : public GIWrapperInstance<FundamentalBase, FundamentalPrototype,
     141                 :             :                                FundamentalInstance> {
     142                 :             :     friend class FundamentalBase;  // for set_value()
     143                 :             :     friend class GIWrapperInstance<FundamentalBase, FundamentalPrototype,
     144                 :             :                                    FundamentalInstance>;
     145                 :             :     friend class GIWrapperBase<FundamentalBase, FundamentalPrototype,
     146                 :             :                                FundamentalInstance>;
     147                 :             : 
     148                 :             :     explicit FundamentalInstance(FundamentalPrototype*, JS::HandleObject);
     149                 :             :     ~FundamentalInstance();
     150                 :             : 
     151                 :             :     // Helper methods
     152                 :             : 
     153                 :             :     GJS_JSAPI_RETURN_CONVENTION
     154                 :             :     bool invoke_constructor(JSContext*, JS::HandleObject, const JS::CallArgs&,
     155                 :             :                             GIArgument* rvalue);
     156                 :             : 
     157                 :          25 :     void ref() { get_prototype()->call_ref_function(m_ptr); }
     158                 :          25 :     void unref() { get_prototype()->call_unref_function(m_ptr); }
     159                 :          11 :     [[nodiscard]] bool set_value(GValue* gvalue) const {
     160                 :          11 :         return get_prototype()->call_set_value_function(gvalue, m_ptr);
     161                 :             :     }
     162                 :             : 
     163                 :             :     GJS_JSAPI_RETURN_CONVENTION
     164                 :             :     bool associate_js_instance(JSContext*, JSObject*, void* gfundamental);
     165                 :             : 
     166                 :             :     // JS constructor
     167                 :             : 
     168                 :             :     GJS_JSAPI_RETURN_CONVENTION
     169                 :             :     bool constructor_impl(JSContext*, JS::HandleObject, const JS::CallArgs&);
     170                 :             : 
     171                 :             :  public:
     172                 :             :     GJS_JSAPI_RETURN_CONVENTION
     173                 :             :     static JSObject* object_for_c_ptr(JSContext*, void* gfundamental);
     174                 :             :     GJS_JSAPI_RETURN_CONVENTION
     175                 :             :     static bool object_for_gvalue(JSContext*, const GValue*, GType,
     176                 :             :                                   JS::MutableHandleObject);
     177                 :             : 
     178                 :             :     static void* copy_ptr(JSContext*, GType, void* gfundamental);
     179                 :             : };
        

Generated by: LCOV version 2.0-1