LCOV - code coverage report
Current view: top level - glib/girepository - gifunctioninfo.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 40 66 60.6 %
Date: 2024-05-07 05:15:23 Functions: 6 8 75.0 %
Branches: 12 24 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2                 :            :  * GObject introspection: Function implementation
       3                 :            :  *
       4                 :            :  * Copyright (C) 2005 Matthias Clasen
       5                 :            :  * Copyright (C) 2008,2009 Red Hat, Inc.
       6                 :            :  *
       7                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       8                 :            :  *
       9                 :            :  * This library is free software; you can redistribute it and/or
      10                 :            :  * modify it under the terms of the GNU Lesser General Public
      11                 :            :  * License as published by the Free Software Foundation; either
      12                 :            :  * version 2 of the License, or (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This library is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :  * Lesser General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU Lesser General Public
      20                 :            :  * License along with this library; if not, write to the
      21                 :            :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      22                 :            :  * Boston, MA 02111-1307, USA.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include "config.h"
      26                 :            : 
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include <glib.h>
      30                 :            : 
      31                 :            : #include <girepository/girepository.h>
      32                 :            : #include "gibaseinfo-private.h"
      33                 :            : #include "girepository-private.h"
      34                 :            : #include "gitypelib-internal.h"
      35                 :            : #include "gifunctioninfo.h"
      36                 :            : 
      37                 :            : /**
      38                 :            :  * GIFunctionInfo:
      39                 :            :  *
      40                 :            :  * `GIFunctionInfo` represents a function, method or constructor.
      41                 :            :  *
      42                 :            :  * To find out what kind of entity a `GIFunctionInfo` represents, call
      43                 :            :  * [method@GIRepository.FunctionInfo.get_flags].
      44                 :            :  *
      45                 :            :  * See also [class@GIRepository.CallableInfo] for information on how to retrieve
      46                 :            :  * arguments and other metadata.
      47                 :            :  *
      48                 :            :  * Since: 2.80
      49                 :            :  */
      50                 :            : 
      51                 :            : GIFunctionInfo *
      52                 :         22 : gi_base_info_find_method (GIBaseInfo  *base,
      53                 :            :                           uint32_t     offset,
      54                 :            :                           uint16_t     n_methods,
      55                 :            :                           const char  *name)
      56                 :            : {
      57                 :            :   /* FIXME hash */
      58                 :         22 :   GIRealInfo *rinfo = (GIRealInfo*)base;
      59                 :         22 :   Header *header = (Header *)rinfo->typelib->data;
      60                 :            : 
      61         [ +  + ]:        246 :   for (uint16_t i = 0; i < n_methods; i++)
      62                 :            :     {
      63                 :        242 :       FunctionBlob *fblob = (FunctionBlob *)&rinfo->typelib->data[offset];
      64                 :        242 :       const char *fname = (const char *)&rinfo->typelib->data[fblob->name];
      65                 :            : 
      66         [ +  + ]:        242 :       if (strcmp (name, fname) == 0)
      67                 :         18 :         return (GIFunctionInfo *) gi_base_info_new (GI_INFO_TYPE_FUNCTION, base,
      68                 :            :                                                     rinfo->typelib, offset);
      69                 :            : 
      70                 :        224 :       offset += header->function_blob_size;
      71                 :            :     }
      72                 :            : 
      73                 :          4 :   return NULL;
      74                 :            : }
      75                 :            : 
      76                 :            : /**
      77                 :            :  * gi_function_info_get_symbol:
      78                 :            :  * @info: a #GIFunctionInfo
      79                 :            :  *
      80                 :            :  * Obtain the symbol of the function.
      81                 :            :  *
      82                 :            :  * The symbol is the name of the exported function, suitable to be used as an
      83                 :            :  * argument to [method@GModule.Module.symbol].
      84                 :            :  *
      85                 :            :  * Returns: the symbol
      86                 :            :  * Since: 2.80
      87                 :            :  */
      88                 :            : const char *
      89                 :          5 : gi_function_info_get_symbol (GIFunctionInfo *info)
      90                 :            : {
      91                 :            :   GIRealInfo *rinfo;
      92                 :            :   FunctionBlob *blob;
      93                 :            : 
      94                 :          5 :   g_return_val_if_fail (info != NULL, NULL);
      95                 :          5 :   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
      96                 :            : 
      97                 :          5 :   rinfo = (GIRealInfo *)info;
      98                 :          5 :   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
      99                 :            : 
     100                 :          5 :   return gi_typelib_get_string (rinfo->typelib, blob->symbol);
     101                 :            : }
     102                 :            : 
     103                 :            : /**
     104                 :            :  * gi_function_info_get_flags:
     105                 :            :  * @info: a #GIFunctionInfo
     106                 :            :  *
     107                 :            :  * Obtain the [type@GIRepository.FunctionInfoFlags] for the @info.
     108                 :            :  *
     109                 :            :  * Returns: the flags
     110                 :            :  * Since: 2.80
     111                 :            :  */
     112                 :            : GIFunctionInfoFlags
     113                 :          2 : gi_function_info_get_flags (GIFunctionInfo *info)
     114                 :            : {
     115                 :            :   GIFunctionInfoFlags flags;
     116                 :            :   GIRealInfo *rinfo;
     117                 :            :   FunctionBlob *blob;
     118                 :            : 
     119                 :          2 :   g_return_val_if_fail (info != NULL, -1);
     120                 :          2 :   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), -1);
     121                 :            : 
     122                 :          2 :   rinfo = (GIRealInfo *)info;
     123                 :          2 :   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
     124                 :            : 
     125                 :          2 :   flags = 0;
     126                 :            : 
     127                 :            :   /* Make sure we don't flag Constructors as methods */
     128   [ +  -  -  + ]:          2 :   if (!blob->constructor && !blob->is_static)
     129                 :          0 :     flags = flags | GI_FUNCTION_IS_METHOD;
     130                 :            : 
     131         [ -  + ]:          2 :   if (blob->constructor)
     132                 :          0 :     flags = flags | GI_FUNCTION_IS_CONSTRUCTOR;
     133                 :            : 
     134         [ -  + ]:          2 :   if (blob->getter)
     135                 :          0 :     flags = flags | GI_FUNCTION_IS_GETTER;
     136                 :            : 
     137         [ -  + ]:          2 :   if (blob->setter)
     138                 :          0 :     flags = flags | GI_FUNCTION_IS_SETTER;
     139                 :            : 
     140         [ -  + ]:          2 :   if (blob->wraps_vfunc)
     141                 :          0 :     flags = flags | GI_FUNCTION_WRAPS_VFUNC;
     142                 :            : 
     143                 :          2 :   return flags;
     144                 :            : }
     145                 :            : 
     146                 :            : /**
     147                 :            :  * gi_function_info_get_property:
     148                 :            :  * @info: a #GIFunctionInfo
     149                 :            :  *
     150                 :            :  * Obtain the property associated with this `GIFunctionInfo`.
     151                 :            :  *
     152                 :            :  * Only `GIFunctionInfo`s with the flag `GI_FUNCTION_IS_GETTER` or
     153                 :            :  * `GI_FUNCTION_IS_SETTER` have a property set. For other cases,
     154                 :            :  * `NULL` will be returned.
     155                 :            :  *
     156                 :            :  * Returns: (transfer full) (nullable): The property or `NULL` if not set. Free
     157                 :            :  *   it with [method@GIRepository.BaseInfo.unref] when done.
     158                 :            :  * Since: 2.80
     159                 :            :  */
     160                 :            : GIPropertyInfo *
     161                 :          0 : gi_function_info_get_property (GIFunctionInfo *info)
     162                 :            : {
     163                 :            :   GIRealInfo *rinfo;
     164                 :            :   FunctionBlob *blob;
     165                 :            : 
     166                 :          0 :   g_return_val_if_fail (info != NULL, NULL);
     167                 :          0 :   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
     168                 :            : 
     169                 :          0 :   rinfo = (GIRealInfo *)info;
     170                 :          0 :   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
     171                 :            : 
     172         [ #  # ]:          0 :   if (gi_base_info_get_info_type ((GIBaseInfo *) rinfo->container) == GI_INFO_TYPE_INTERFACE)
     173                 :            :     {
     174                 :          0 :       GIInterfaceInfo *container = (GIInterfaceInfo *)rinfo->container;
     175                 :            : 
     176                 :          0 :       return gi_interface_info_get_property (container, blob->index);
     177                 :            :     }
     178         [ #  # ]:          0 :   else if (gi_base_info_get_info_type ((GIBaseInfo *) rinfo->container) == GI_INFO_TYPE_OBJECT)
     179                 :            :     {
     180                 :          0 :       GIObjectInfo *container = (GIObjectInfo *)rinfo->container;
     181                 :            : 
     182                 :          0 :       return gi_object_info_get_property (container, blob->index);
     183                 :            :     }
     184                 :            :   else
     185                 :          0 :     return NULL;
     186                 :            : }
     187                 :            : 
     188                 :            : /**
     189                 :            :  * gi_function_info_get_vfunc:
     190                 :            :  * @info: a #GIFunctionInfo
     191                 :            :  *
     192                 :            :  * Obtain the virtual function associated with this `GIFunctionInfo`.
     193                 :            :  *
     194                 :            :  * Only `GIFunctionInfo`s with the flag `GI_FUNCTION_WRAPS_VFUNC` have
     195                 :            :  * a virtual function set. For other cases, `NULL` will be returned.
     196                 :            :  *
     197                 :            :  * Returns: (transfer full) (nullable): The virtual function or `NULL` if not
     198                 :            :  *   set. Free it by calling [method@GIRepository.BaseInfo.unref] when done.
     199                 :            :  * Since: 2.80
     200                 :            :  */
     201                 :            : GIVFuncInfo *
     202                 :          0 : gi_function_info_get_vfunc (GIFunctionInfo *info)
     203                 :            : {
     204                 :            :   GIRealInfo *rinfo;
     205                 :            :   FunctionBlob *blob;
     206                 :            :   GIInterfaceInfo *container;
     207                 :            : 
     208                 :          0 :   g_return_val_if_fail (info != NULL, NULL);
     209                 :          0 :   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
     210                 :            : 
     211                 :          0 :   rinfo = (GIRealInfo *)info;
     212                 :          0 :   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
     213                 :          0 :   container = (GIInterfaceInfo *)rinfo->container;
     214                 :            : 
     215                 :          0 :   return gi_interface_info_get_vfunc (container, blob->index);
     216                 :            : }
     217                 :            : 
     218                 :            : /**
     219                 :            :  * gi_invoke_error_quark:
     220                 :            :  *
     221                 :            :  * Get the error quark which represents [type@GIRepository.InvokeError].
     222                 :            :  *
     223                 :            :  * Returns: error quark
     224                 :            :  * Since: 2.80
     225                 :            :  */
     226                 :            : GQuark
     227                 :          1 : gi_invoke_error_quark (void)
     228                 :            : {
     229                 :            :   static GQuark quark = 0;
     230         [ +  - ]:          1 :   if (quark == 0)
     231                 :          1 :     quark = g_quark_from_static_string ("gi-invoke-error-quark");
     232                 :          1 :   return quark;
     233                 :            : }
     234                 :            : 
     235                 :            : /**
     236                 :            :  * gi_function_info_invoke: (skip)
     237                 :            :  * @info: a #GIFunctionInfo describing the function to invoke
     238                 :            :  * @in_args: (array length=n_in_args) (nullable): An array of
     239                 :            :  *   [type@GIRepository.Argument]s, one for each ‘in’ parameter of @info. If
     240                 :            :  *   there are no ‘in’ parameters, @in_args can be `NULL`.
     241                 :            :  * @n_in_args: the length of the @in_args array
     242                 :            :  * @out_args: (array length=n_out_args) (nullable): An array of
     243                 :            :  *   [type@GIRepository.Argument]s, one for each ‘out’ parameter of @info. If
     244                 :            :  *   there are no ‘out’ parameters, @out_args may be `NULL`.
     245                 :            :  * @n_out_args: the length of the @out_args array
     246                 :            :  * @return_value: (out caller-allocates) (not optional): return location for the
     247                 :            :  *   return value of the function.
     248                 :            :  * @error: return location for detailed error information, or `NULL`
     249                 :            :  *
     250                 :            :  * Invokes the function described in @info with the given
     251                 :            :  * arguments.
     252                 :            :  *
     253                 :            :  * Note that ‘inout’ parameters must appear in both argument lists. This
     254                 :            :  * function uses [`dlsym()`](man:dlsym(3)) to obtain a pointer to the function,
     255                 :            :  * so the library or shared object containing the described function must either
     256                 :            :  * be linked to the caller, or must have been loaded with
     257                 :            :  * [method@GModule.Module.symbol] before calling this function.
     258                 :            :  *
     259                 :            :  * Returns: `TRUE` if the function has been invoked, `FALSE` if an
     260                 :            :  *   error occurred.
     261                 :            :  * Since: 2.80
     262                 :            :  */
     263                 :            : gboolean
     264                 :          1 : gi_function_info_invoke (GIFunctionInfo    *info,
     265                 :            :                          const GIArgument  *in_args,
     266                 :            :                          size_t             n_in_args,
     267                 :            :                          GIArgument        *out_args,
     268                 :            :                          size_t             n_out_args,
     269                 :            :                          GIArgument        *return_value,
     270                 :            :                          GError           **error)
     271                 :            : {
     272                 :            :   const char *symbol;
     273                 :            :   void *func;
     274                 :            : 
     275                 :          1 :   symbol = gi_function_info_get_symbol (info);
     276                 :            : 
     277         [ -  + ]:          1 :   if (!gi_typelib_symbol (gi_base_info_get_typelib ((GIBaseInfo *) info),
     278                 :            :                           symbol, &func))
     279                 :            :     {
     280                 :          0 :       g_set_error (error,
     281                 :            :                    GI_INVOKE_ERROR,
     282                 :            :                    GI_INVOKE_ERROR_SYMBOL_NOT_FOUND,
     283                 :            :                    "Could not locate %s: %s", symbol, g_module_error ());
     284                 :            : 
     285                 :          0 :       return FALSE;
     286                 :            :     }
     287                 :            : 
     288                 :          1 :   return gi_callable_info_invoke ((GICallableInfo*) info,
     289                 :            :                                   func,
     290                 :            :                                   in_args,
     291                 :            :                                   n_in_args,
     292                 :            :                                   out_args,
     293                 :            :                                   n_out_args,
     294                 :            :                                   return_value,
     295                 :            :                                   error);
     296                 :            : }
     297                 :            : 
     298                 :            : void
     299                 :          7 : gi_function_info_class_init (gpointer g_class,
     300                 :            :                              gpointer class_data)
     301                 :            : {
     302                 :          7 :   GIBaseInfoClass *info_class = g_class;
     303                 :            : 
     304                 :          7 :   info_class->info_type = GI_INFO_TYPE_FUNCTION;
     305                 :          7 : }

Generated by: LCOV version 1.14