Fix compilation warning in ginvoke.c
[gnome.gobject-introspection] / girepository / ginvoke.c
1 /* GObject introspection: Invoke functionality
2  *
3  * Copyright (C) 2005 Matthias Clasen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdlib.h>
22
23 #include <glib.h>
24 #include <glib-object.h>
25
26 #include "girepository.h"
27 #include "girffi.h"
28 #include "gtypelib.h"
29 #include "config.h"
30
31 GQuark
32 g_invoke_error_quark (void)
33 {
34   static GQuark quark = 0;
35   if (quark == 0)
36     quark = g_quark_from_static_string ("g-invoke-error-quark");
37   return quark;
38 }
39
40 #include "ffi.h"
41
42 static ffi_type *
43 get_ffi_type (GITypeInfo *info)
44 {
45   if (g_type_info_is_pointer (info))
46     return &ffi_type_pointer;
47   else
48     return g_ir_ffi_get_ffi_type (g_type_info_get_tag (info));
49 }
50
51 /**
52  * g_function_info_invoke:
53  * @info: a #GIFunctionInfo describing the function to invoke
54  * @in_args: an array of #GArgument<!-- -->s, one for each in 
55  *    parameter of @info. If there are no in parameter, @in_args
56  *    can be %NULL
57  * @n_in_args: the length of the @in_args array
58  * @out_args: an array of #GArgument<!-- -->s, one for each out
59  *    parameter of @info. If there are no out parameters, @out_args
60  *    may be %NULL 
61  * @n_out_args: the length of the @out_args array
62  * @return_value: return location for the return value of the 
63  *    function. If the function returns void, @return_value may be
64  *    %NULL
65  * @error: return location for detailed error information, or %NULL
66  *
67  * Invokes the function described in @info with the given 
68  * arguments. Note that inout parameters must appear in both
69  * argument lists. This function uses dlsym() to obtain a pointer
70  * to the function, so the library or shared object containing the 
71  * described function must either be linked to the caller, or must 
72  * have been dlopen()<!-- -->ed before calling this function.
73  *
74  * Returns: %TRUE if the function has been invoked, %FALSE if an
75  *   error occurred. 
76  */
77 gboolean 
78 g_function_info_invoke (GIFunctionInfo *info, 
79                         const GArgument  *in_args,
80                         int               n_in_args,
81                         const GArgument  *out_args,
82                         int               n_out_args,
83                         GArgument        *return_value,
84                         GError          **error)
85 {
86   ffi_cif cif;
87   ffi_type *rtype;
88   ffi_type **atypes;
89   const gchar *symbol;
90   gpointer func;
91   GITypeInfo *tinfo;
92   GIArgInfo *ainfo;
93   gboolean is_method;
94   gboolean throws;
95   gint n_args, n_invoke_args, in_pos, out_pos, i;
96   gpointer *args;
97   gboolean success = FALSE;
98   GError *local_error = NULL;
99   gpointer error_address = &local_error;
100
101   symbol = g_function_info_get_symbol (info);
102
103   if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
104                          symbol, &func))
105     {
106       g_set_error (error,
107                    G_INVOKE_ERROR,
108                    G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
109                    "Could not locate %s: %s", symbol, g_module_error ());
110
111       return FALSE;
112     }
113
114   is_method = (g_function_info_get_flags (info) & GI_FUNCTION_IS_METHOD) != 0
115     && (g_function_info_get_flags (info) & GI_FUNCTION_IS_CONSTRUCTOR) == 0;
116   throws = g_function_info_get_flags (info) & GI_FUNCTION_THROWS;
117
118   tinfo = g_callable_info_get_return_type ((GICallableInfo *)info);
119   rtype = get_ffi_type (tinfo);
120   g_base_info_unref ((GIBaseInfo *)tinfo);
121
122   in_pos = 0;
123   out_pos = 0;
124
125   n_args = g_callable_info_get_n_args ((GICallableInfo *)info);
126   if (is_method)
127     {
128       if (n_in_args == 0)
129         {
130           g_set_error (error,
131                        G_INVOKE_ERROR,
132                        G_INVOKE_ERROR_ARGUMENT_MISMATCH,
133                        "Too few \"in\" arguments (handling this)");
134           goto out;
135         }
136       n_invoke_args = n_args+1;
137       in_pos++;
138     }
139   else
140     n_invoke_args = n_args;
141
142   if (throws)
143     /* Add an argument for the GError */
144     n_invoke_args ++;
145
146   atypes = g_alloca (sizeof (ffi_type*) * n_invoke_args);
147   args = g_alloca (sizeof (gpointer) * n_invoke_args);
148   
149   if (is_method)
150     {
151       atypes[0] = &ffi_type_pointer;
152       args[0] = (gpointer) &in_args[0];
153     }
154   for (i = 0; i < n_args; i++)
155     {
156       int offset = (is_method ? 1 : 0);
157       ainfo = g_callable_info_get_arg ((GICallableInfo *)info, i);
158       switch (g_arg_info_get_direction (ainfo))
159         {
160         case GI_DIRECTION_IN:
161           tinfo = g_arg_info_get_type (ainfo);
162           atypes[i+offset] = get_ffi_type (tinfo);
163           g_base_info_unref ((GIBaseInfo *)tinfo);
164
165           if (in_pos >= n_in_args)
166             {
167               g_set_error (error,
168                            G_INVOKE_ERROR,
169                            G_INVOKE_ERROR_ARGUMENT_MISMATCH,
170                            "Too few \"in\" arguments (handling in)");
171               goto out;
172             }
173
174           args[i+offset] = (gpointer)&in_args[in_pos];
175           in_pos++;
176           
177           break;
178         case GI_DIRECTION_OUT:
179           atypes[i+offset] = &ffi_type_pointer;
180
181           if (out_pos >= n_out_args)
182             {
183               g_set_error (error,
184                            G_INVOKE_ERROR,
185                            G_INVOKE_ERROR_ARGUMENT_MISMATCH,
186                            "Too few \"out\" arguments (handling out)");       
187               goto out;
188             }
189
190           args[i+offset] = (gpointer)&out_args[out_pos];
191           out_pos++;      
192           break;
193         case GI_DIRECTION_INOUT:
194           atypes[i+offset] = &ffi_type_pointer;
195
196           if (in_pos >= n_in_args)
197             {
198               g_set_error (error,
199                            G_INVOKE_ERROR,
200                            G_INVOKE_ERROR_ARGUMENT_MISMATCH,
201                            "Too few \"in\" arguments (handling inout)");
202               goto out;
203             }
204
205           if (out_pos >= n_out_args)
206             {
207               g_set_error (error,
208                            G_INVOKE_ERROR,
209                            G_INVOKE_ERROR_ARGUMENT_MISMATCH,
210                            "Too few \"out\" arguments (handling inout)");             
211               goto out;
212             }
213           
214           args[i+offset] = (gpointer)&in_args[in_pos];
215           in_pos++;       
216           out_pos++;      
217           break;
218         default:
219           g_assert_not_reached ();
220         }
221       g_base_info_unref ((GIBaseInfo *)ainfo);
222     }
223
224   if (throws)
225     {
226       args[n_invoke_args - 1] = &error_address;
227       atypes[n_invoke_args - 1] = &ffi_type_pointer;
228     }
229
230   if (in_pos < n_in_args)
231     {
232       g_set_error (error,
233                    G_INVOKE_ERROR,
234                    G_INVOKE_ERROR_ARGUMENT_MISMATCH,
235                    "Too many \"in\" arguments (at end)");
236       goto out;
237     }
238   if (out_pos < n_out_args)
239     {
240       g_set_error (error,
241                    G_INVOKE_ERROR,
242                    G_INVOKE_ERROR_ARGUMENT_MISMATCH,
243                    "Too many \"out\" arguments (at end)");            
244       goto out;
245     }
246
247   if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) != FFI_OK)
248     goto out;
249
250   g_return_val_if_fail (return_value, FALSE);
251   ffi_call (&cif, func, return_value, args);
252
253   if (local_error)
254     {
255       g_propagate_error (error, local_error);
256       success = FALSE;
257     }
258   else
259     {
260       success = TRUE;
261     }
262  out:
263   return success;
264 }
265
266 static ffi_type *
267 value_to_ffi_type (const GValue *gvalue, gpointer *value)
268 {
269   ffi_type *rettype = NULL;
270   GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
271   g_assert (type != G_TYPE_INVALID);
272
273   switch (type)
274     {
275     case G_TYPE_BOOLEAN:
276     case G_TYPE_CHAR:
277     case G_TYPE_INT:
278       rettype = &ffi_type_sint;
279       *value = (gpointer)&(gvalue->data[0].v_int);
280       break;
281     case G_TYPE_UCHAR:
282     case G_TYPE_UINT:
283       rettype = &ffi_type_uint;
284       *value = (gpointer)&(gvalue->data[0].v_uint);
285       break;
286     case G_TYPE_STRING:
287     case G_TYPE_OBJECT:
288     case G_TYPE_BOXED:
289     case G_TYPE_POINTER:
290       rettype = &ffi_type_pointer;
291       *value = (gpointer)&(gvalue->data[0].v_pointer);
292       break;
293     case G_TYPE_FLOAT:
294       rettype = &ffi_type_float;
295       *value = (gpointer)&(gvalue->data[0].v_float);
296       break;
297     case G_TYPE_DOUBLE:
298       rettype = &ffi_type_double;
299       *value = (gpointer)&(gvalue->data[0].v_double);
300       break;
301     case G_TYPE_LONG:
302       rettype = &ffi_type_slong;
303       *value = (gpointer)&(gvalue->data[0].v_long);
304       break;
305     case G_TYPE_ULONG:
306       rettype = &ffi_type_ulong;
307       *value = (gpointer)&(gvalue->data[0].v_ulong);
308       break;
309     case G_TYPE_INT64:
310       rettype = &ffi_type_sint64;
311       *value = (gpointer)&(gvalue->data[0].v_int64);
312       break;
313     case G_TYPE_UINT64:
314       rettype = &ffi_type_uint64;
315       *value = (gpointer)&(gvalue->data[0].v_uint64);
316       break;
317     default:
318       rettype = &ffi_type_pointer;
319       *value = NULL;
320       g_warning ("Unsupported fundamental type: %s", g_type_name (type));
321       break;
322     }
323   return rettype;
324 }
325
326 static void
327 value_from_ffi_type (GValue *gvalue, gpointer *value)
328 {
329   switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
330     {
331     case G_TYPE_INT:
332       g_value_set_int (gvalue, *(gint*)value);
333       break;
334     case G_TYPE_FLOAT:
335       g_value_set_float (gvalue, *(gfloat*)value);
336       break;
337     case G_TYPE_DOUBLE:
338       g_value_set_double (gvalue, *(gdouble*)value);
339       break;
340     case G_TYPE_BOOLEAN:
341       g_value_set_boolean (gvalue, *(gboolean*)value);
342       break;
343     case G_TYPE_STRING:
344       g_value_set_string (gvalue, *(gchar**)value);
345       break;
346     case G_TYPE_CHAR:
347       g_value_set_char (gvalue, *(gchar*)value);
348       break;
349     case G_TYPE_UCHAR:
350       g_value_set_uchar (gvalue, *(guchar*)value);
351       break;
352     case G_TYPE_UINT:
353       g_value_set_uint (gvalue, *(guint*)value);
354       break;
355     case G_TYPE_POINTER:
356       g_value_set_pointer (gvalue, *(gpointer*)value);
357       break;
358     case G_TYPE_LONG:
359       g_value_set_long (gvalue, *(glong*)value);
360       break;
361     case G_TYPE_ULONG:
362       g_value_set_ulong (gvalue, *(gulong*)value);
363       break;
364     case G_TYPE_INT64:
365       g_value_set_int64 (gvalue, *(gint64*)value);
366       break;
367     case G_TYPE_UINT64:
368       g_value_set_uint64 (gvalue, *(guint64*)value);
369       break;
370     case G_TYPE_BOXED:
371       g_value_set_boxed (gvalue, *(gpointer*)value);
372       break;
373     default:
374       g_warning ("Unsupported fundamental type: %s",
375                 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
376     }
377 }
378
379 void
380 gi_cclosure_marshal_generic (GClosure *closure,
381                              GValue *return_gvalue,
382                              guint n_param_values,
383                              const GValue *param_values,
384                              gpointer invocation_hint,
385                              gpointer marshal_data)
386 {
387   ffi_type *rtype;
388   void *rvalue;
389   int n_args;
390   ffi_type **atypes;
391   void **args;
392   int i;
393   ffi_cif cif;
394   GCClosure *cc = (GCClosure*) closure;
395
396   if (return_gvalue && G_VALUE_TYPE (return_gvalue)) 
397     {
398       rtype = value_to_ffi_type (return_gvalue, &rvalue);
399     }
400   else 
401     {
402       rtype = &ffi_type_void;
403     }
404
405   rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
406   
407   n_args = n_param_values + 1;
408   atypes = g_alloca (sizeof (ffi_type *) * n_args);
409   args =  g_alloca (sizeof (gpointer) * n_args);
410
411   if (n_param_values > 0)
412     {
413       if (G_CCLOSURE_SWAP_DATA (closure))
414         {
415           atypes[n_args-1] = value_to_ffi_type (param_values + 0,  
416                                                 &args[n_args-1]);
417           atypes[0] = &ffi_type_pointer;
418           args[0] = &closure->data;
419         }
420       else
421         {
422           atypes[0] = value_to_ffi_type (param_values + 0, &args[0]);
423           atypes[n_args-1] = &ffi_type_pointer;
424           args[n_args-1] = &closure->data;
425         }
426     }
427   else
428     {
429       atypes[0] = &ffi_type_pointer;
430       args[0] = &closure->data;
431     }
432
433   for (i = 1; i < n_args - 1; i++)
434     atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
435
436   if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
437     return;
438
439   g_return_if_fail (rvalue != NULL);
440   ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
441
442   if (return_gvalue && G_VALUE_TYPE (return_gvalue))
443     value_from_ffi_type (return_gvalue, rvalue);
444 }