LCOV - code coverage report
Current view: top level - gi - fundamental.h (source / functions) Hit Total Coverage
Test: gjs- Code Coverage Lines: 25 26 96.2 %
Date: 2023-09-17 02:39:54 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 6 8 75.0 %

           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                 :            : #ifndef GI_FUNDAMENTAL_H_
       7                 :            : #define GI_FUNDAMENTAL_H_
       8                 :            : 
       9                 :            : #include <config.h>
      10                 :            : 
      11                 :            : #include <girepository.h>
      12                 :            : #include <glib-object.h>
      13                 :            : 
      14                 :            : #include <js/TypeDecls.h>
      15                 :            : 
      16                 :            : #include "gi/cwrapper.h"
      17                 :            : #include "gi/wrapperutils.h"
      18                 :            : #include "gjs/jsapi-util.h"  // for GjsAutoCallableInfo
      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                 :         27 :     explicit FundamentalBase(FundamentalPrototype* proto = nullptr)
      41                 :         27 :         : 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* cx, JS::HandleObject obj, GValue* gvalue);
      54                 :            : };
      55                 :            : 
      56                 :            : class FundamentalPrototype
      57                 :            :     : public GIWrapperPrototype<FundamentalBase, FundamentalPrototype,
      58                 :            :                                 FundamentalInstance> {
      59                 :            :     friend class GIWrapperPrototype<FundamentalBase, FundamentalPrototype,
      60                 :            :                                     FundamentalInstance>;
      61                 :            :     friend class GIWrapperBase<FundamentalBase, FundamentalPrototype,
      62                 :            :                                FundamentalInstance>;
      63                 :            : 
      64                 :            :     GIObjectInfoRefFunction m_ref_function;
      65                 :            :     GIObjectInfoUnrefFunction m_unref_function;
      66                 :            :     GIObjectInfoGetValueFunction m_get_value_function;
      67                 :            :     GIObjectInfoSetValueFunction m_set_value_function;
      68                 :            :     GjsAutoCallableInfo m_constructor_info;
      69                 :            : 
      70                 :            :     explicit FundamentalPrototype(GIObjectInfo* info, GType gtype);
      71                 :            :     ~FundamentalPrototype(void);
      72                 :            : 
      73                 :            :     static constexpr InfoType::Tag info_type_tag = InfoType::Object;
      74                 :            : 
      75                 :            :  public:
      76                 :            :     GJS_JSAPI_RETURN_CONVENTION
      77                 :            :     static FundamentalPrototype* for_gtype(JSContext* cx, GType gtype);
      78                 :            : 
      79                 :            :     // Accessors
      80                 :            : 
      81                 :         31 :     [[nodiscard]] GICallableInfo* constructor_info() const {
      82                 :         31 :         return m_constructor_info;
      83                 :            :     }
      84                 :            : 
      85                 :         17 :     void* call_ref_function(void* ptr) const {
      86         [ -  + ]:         17 :         if (!m_ref_function)
      87                 :          0 :             return ptr;
      88                 :            : 
      89                 :         17 :         return m_ref_function(ptr);
      90                 :            :     }
      91                 :         33 :     void call_unref_function(void* ptr) const {
      92         [ +  - ]:         33 :         if (m_unref_function)
      93                 :         33 :             m_unref_function(ptr);
      94                 :         33 :     }
      95                 :         12 :     [[nodiscard]] bool call_get_value_function(const GValue* value,
      96                 :            :                                                void** ptr_out) const {
      97         [ +  + ]:         12 :         if (!m_get_value_function)
      98                 :          6 :             return false;
      99                 :            : 
     100                 :          6 :         *ptr_out = m_get_value_function(value);
     101                 :          6 :         return true;
     102                 :            :     }
     103                 :         11 :     bool call_set_value_function(GValue* value, void* object) const {
     104         [ +  + ]:         11 :         if (m_set_value_function) {
     105                 :          4 :             m_set_value_function(value, object);
     106                 :          4 :             return true;
     107                 :            :         }
     108                 :            : 
     109                 :          7 :         return false;
     110                 :            :     }
     111                 :            : 
     112                 :            :     // Helper methods
     113                 :            : 
     114                 :            :  private:
     115                 :            :     GJS_JSAPI_RETURN_CONVENTION
     116                 :            :     bool get_parent_proto(JSContext* cx, JS::MutableHandleObject proto) const;
     117                 :            : 
     118                 :            :     [[nodiscard]] unsigned constructor_nargs() const;
     119                 :            : 
     120                 :            :     GJS_JSAPI_RETURN_CONVENTION
     121                 :            :     bool resolve_interface(JSContext* cx, JS::HandleObject obj, bool* resolved,
     122                 :            :                            const char* name);
     123                 :            : 
     124                 :            :     // JSClass operations
     125                 :            : 
     126                 :            :     GJS_JSAPI_RETURN_CONVENTION
     127                 :            :     bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
     128                 :            :                       bool* resolved);
     129                 :            : 
     130                 :            :     // Public API
     131                 :            :  public:
     132                 :            :     GJS_JSAPI_RETURN_CONVENTION
     133                 :            :     static bool define_class(JSContext* cx, JS::HandleObject in_object,
     134                 :            :                              GIObjectInfo* info,
     135                 :            :                              JS::MutableHandleObject constructor);
     136                 :            : };
     137                 :            : 
     138                 :            : class FundamentalInstance
     139                 :            :     : public GIWrapperInstance<FundamentalBase, FundamentalPrototype,
     140                 :            :                                FundamentalInstance> {
     141                 :            :     friend class FundamentalBase;  // for set_value()
     142                 :            :     friend class GIWrapperInstance<FundamentalBase, FundamentalPrototype,
     143                 :            :                                    FundamentalInstance>;
     144                 :            :     friend class GIWrapperBase<FundamentalBase, FundamentalPrototype,
     145                 :            :                                FundamentalInstance>;
     146                 :            : 
     147                 :            :     explicit FundamentalInstance(FundamentalPrototype* prototype,
     148                 :            :                                  JS::HandleObject obj);
     149                 :            :     ~FundamentalInstance(void);
     150                 :            : 
     151                 :            :     // Helper methods
     152                 :            : 
     153                 :            :     GJS_JSAPI_RETURN_CONVENTION
     154                 :            :     bool invoke_constructor(JSContext* cx, JS::HandleObject obj,
     155                 :            :                             const JS::CallArgs& args, GIArgument* rvalue);
     156                 :            : 
     157                 :         17 :     void ref(void) { get_prototype()->call_ref_function(m_ptr); }
     158                 :         17 :     void unref(void) { 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* cx, JSObject* object,
     165                 :            :                                void* gfundamental);
     166                 :            : 
     167                 :            :     // JS constructor
     168                 :            : 
     169                 :            :     GJS_JSAPI_RETURN_CONVENTION
     170                 :            :     bool constructor_impl(JSContext* cx, JS::HandleObject obj,
     171                 :            :                           const JS::CallArgs& args);
     172                 :            : 
     173                 :            :  public:
     174                 :            :     GJS_JSAPI_RETURN_CONVENTION
     175                 :            :     static JSObject* object_for_c_ptr(JSContext* cx, void* gfundamental);
     176                 :            :     GJS_JSAPI_RETURN_CONVENTION
     177                 :            :     static bool object_for_gvalue(JSContext* cx, const GValue* gvalue,
     178                 :            :                                   GType gtype,
     179                 :            :                                   JS::MutableHandleObject object_out);
     180                 :            : 
     181                 :            :     static void* copy_ptr(JSContext* cx, GType gtype, void* gfundamental);
     182                 :            : };
     183                 :            : 
     184                 :            : #endif  // GI_FUNDAMENTAL_H_

Generated by: LCOV version 1.14