LCOV - code coverage report
Current view: top level - gi - gerror.h (source / functions) Hit Total Coverage
Test: gjs- Code Coverage Lines: 6 8 75.0 %
Date: 2023-09-17 02:39:54 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 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: 2008 litl, LLC
       4                 :            : 
       5                 :            : #ifndef GI_GERROR_H_
       6                 :            : #define GI_GERROR_H_
       7                 :            : 
       8                 :            : #include <config.h>
       9                 :            : 
      10                 :            : #include <girepository.h>
      11                 :            : #include <glib-object.h>
      12                 :            : #include <glib.h>
      13                 :            : 
      14                 :            : #include <js/PropertySpec.h>
      15                 :            : #include <js/TypeDecls.h>
      16                 :            : 
      17                 :            : #include "gi/cwrapper.h"
      18                 :            : #include "gi/wrapperutils.h"
      19                 :            : #include "gjs/jsapi-util.h"  // for GjsAutoPointer operators
      20                 :            : #include "gjs/macros.h"
      21                 :            : #include "util/log.h"
      22                 :            : 
      23                 :            : class ErrorPrototype;
      24                 :            : class ErrorInstance;
      25                 :            : namespace JS {
      26                 :            : class CallArgs;
      27                 :            : }
      28                 :            : 
      29                 :            : /* To conserve memory, we have two different kinds of private data for GError
      30                 :            :  * JS wrappers: ErrorInstance, and ErrorPrototype. Both inherit from ErrorBase
      31                 :            :  * for their common functionality. For more information, see the notes in
      32                 :            :  * wrapperutils.h.
      33                 :            :  *
      34                 :            :  * ErrorPrototype, unlike the other GIWrapperPrototype subclasses, represents a
      35                 :            :  * single error domain instead of a single GType. All Errors have a GType of
      36                 :            :  * G_TYPE_ERROR.
      37                 :            :  *
      38                 :            :  * Note that in some situations GError structs can show up as BoxedInstance
      39                 :            :  * instead of ErrorInstance. We have some special cases in this code to deal
      40                 :            :  * with that.
      41                 :            :  */
      42                 :            : 
      43                 :            : class ErrorBase
      44                 :            :     : public GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance> {
      45                 :            :     friend class CWrapperPointerOps<ErrorBase>;
      46                 :            :     friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
      47                 :            : 
      48                 :            :  protected:
      49                 :         48 :     explicit ErrorBase(ErrorPrototype* proto = nullptr)
      50                 :         48 :         : GIWrapperBase(proto) {}
      51                 :            : 
      52                 :            :     static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_GERROR;
      53                 :            :     static constexpr const char* DEBUG_TAG = "gerror";
      54                 :            : 
      55                 :            :     static const struct JSClassOps class_ops;
      56                 :            :     static const struct JSClass klass;
      57                 :            :     static JSPropertySpec proto_properties[];
      58                 :            :     static JSFunctionSpec static_methods[];
      59                 :            : 
      60                 :            :     // Accessors
      61                 :            : 
      62                 :            :  public:
      63                 :            :     [[nodiscard]] GQuark domain(void) const;
      64                 :            : 
      65                 :            :     // Property getters
      66                 :            : 
      67                 :            :  protected:
      68                 :            :     GJS_JSAPI_RETURN_CONVENTION
      69                 :            :     static bool get_domain(JSContext* cx, unsigned argc, JS::Value* vp);
      70                 :            :     GJS_JSAPI_RETURN_CONVENTION
      71                 :            :     static bool get_message(JSContext* cx, unsigned argc, JS::Value* vp);
      72                 :            :     GJS_JSAPI_RETURN_CONVENTION
      73                 :            :     static bool get_code(JSContext* cx, unsigned argc, JS::Value* vp);
      74                 :            : 
      75                 :            :     // JS methods
      76                 :            : 
      77                 :            :     GJS_JSAPI_RETURN_CONVENTION
      78                 :            :     static bool value_of(JSContext* cx, unsigned argc, JS::Value* vp);
      79                 :            : 
      80                 :            :  public:
      81                 :            :     GJS_JSAPI_RETURN_CONVENTION
      82                 :            :     static bool to_string(JSContext* cx, unsigned argc, JS::Value* vp);
      83                 :            : 
      84                 :            :     // Helper methods
      85                 :            : 
      86                 :            :     GJS_JSAPI_RETURN_CONVENTION
      87                 :            :     static GError* to_c_ptr(JSContext* cx, JS::HandleObject obj);
      88                 :            : 
      89                 :            :     GJS_JSAPI_RETURN_CONVENTION
      90                 :            :     static bool transfer_to_gi_argument(JSContext* cx, JS::HandleObject obj,
      91                 :            :                                         GIArgument* arg,
      92                 :            :                                         GIDirection transfer_direction,
      93                 :            :                                         GITransfer transfer_ownership);
      94                 :            : 
      95                 :            :     GJS_JSAPI_RETURN_CONVENTION
      96                 :            :     static bool typecheck(JSContext* cx, JS::HandleObject obj);
      97                 :            :     [[nodiscard]] static bool typecheck(JSContext* cx, JS::HandleObject obj,
      98                 :            :                                         GjsTypecheckNoThrow);
      99                 :            : };
     100                 :            : 
     101                 :            : class ErrorPrototype : public GIWrapperPrototype<ErrorBase, ErrorPrototype,
     102                 :            :                                                  ErrorInstance, GIEnumInfo> {
     103                 :            :     friend class GIWrapperPrototype<ErrorBase, ErrorPrototype, ErrorInstance,
     104                 :            :                                     GIEnumInfo>;
     105                 :            :     friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
     106                 :            : 
     107                 :            :     GQuark m_domain;
     108                 :            : 
     109                 :            :     static constexpr InfoType::Tag info_type_tag = InfoType::Enum;
     110                 :            : 
     111                 :            :     explicit ErrorPrototype(GIEnumInfo* info, GType gtype);
     112                 :            :     ~ErrorPrototype(void);
     113                 :            : 
     114                 :            :     GJS_JSAPI_RETURN_CONVENTION
     115                 :            :     bool get_parent_proto(JSContext* cx, JS::MutableHandleObject proto) const;
     116                 :            : 
     117                 :            :  public:
     118                 :         25 :     [[nodiscard]] GQuark domain(void) const { return m_domain; }
     119                 :            : 
     120                 :            :     GJS_JSAPI_RETURN_CONVENTION
     121                 :            :     static bool define_class(JSContext* cx, JS::HandleObject in_object,
     122                 :            :                              GIEnumInfo* info);
     123                 :            : };
     124                 :            : 
     125                 :            : class ErrorInstance : public GIWrapperInstance<ErrorBase, ErrorPrototype,
     126                 :            :                                                ErrorInstance, GError> {
     127                 :            :     friend class GIWrapperInstance<ErrorBase, ErrorPrototype, ErrorInstance,
     128                 :            :                                    GError>;
     129                 :            :     friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
     130                 :            : 
     131                 :            :     explicit ErrorInstance(ErrorPrototype* prototype, JS::HandleObject obj);
     132                 :            :     ~ErrorInstance(void);
     133                 :            : 
     134                 :            :  public:
     135                 :         28 :     void copy_gerror(GError* other) { m_ptr = g_error_copy(other); }
     136                 :            :     GJS_JSAPI_RETURN_CONVENTION
     137                 :          0 :     static GError* copy_ptr(JSContext*, GType, void* ptr) {
     138                 :          0 :         return g_error_copy(static_cast<GError*>(ptr));
     139                 :            :     }
     140                 :            : 
     141                 :            :     // Accessors
     142                 :            : 
     143                 :         11 :     [[nodiscard]] const char* message(void) const { return m_ptr->message; }
     144                 :          6 :     [[nodiscard]] int code(void) const { return m_ptr->code; }
     145                 :            : 
     146                 :            :     // JS constructor
     147                 :            : 
     148                 :            :  private:
     149                 :            :     GJS_JSAPI_RETURN_CONVENTION
     150                 :            :     bool constructor_impl(JSContext* cx, JS::HandleObject obj,
     151                 :            :                           const JS::CallArgs& args);
     152                 :            : 
     153                 :            :     // Public API
     154                 :            : 
     155                 :            :  public:
     156                 :            :     GJS_JSAPI_RETURN_CONVENTION
     157                 :            :     static JSObject* object_for_c_ptr(JSContext* cx, GError* gerror);
     158                 :            : };
     159                 :            : 
     160                 :            : GJS_JSAPI_RETURN_CONVENTION
     161                 :            : GError* gjs_gerror_make_from_thrown_value(JSContext* cx);
     162                 :            : 
     163                 :            : GJS_JSAPI_RETURN_CONVENTION
     164                 :            : bool gjs_define_error_properties(JSContext* cx, JS::HandleObject obj);
     165                 :            : 
     166                 :            : bool gjs_throw_gerror(JSContext* cx, GjsAutoError const&);
     167                 :            : 
     168                 :            : #endif  // GI_GERROR_H_

Generated by: LCOV version 1.14