70d81f8e92a99c5158ff0abeed22804b0430124b
[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   ffi_call (&cif, func, return_value, args);
251
252   if (local_error)
253     {
254       g_propagate_error (error, local_error);
255       success = FALSE;
256     }
257   else
258     {
259       success = TRUE;
260     }
261  out:
262   return success;
263 }
264
265 static ffi_type *
266 value_to_ffi_type (const GValue *gvalue, gpointer *value)
267 {
268   ffi_type *rettype = NULL;
269   GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
270   g_assert (type != G_TYPE_INVALID);
271
272   switch (type)
273     {
274     case G_TYPE_BOOLEAN:
275     case G_TYPE_CHAR:
276     case G_TYPE_INT:
277       rettype = &ffi_type_sint;
278       *value = (gpointer)&(gvalue->data[0].v_int);
279       break;
280     case G_TYPE_UCHAR:
281     case G_TYPE_UINT:
282       rettype = &ffi_type_uint;
283       *value = (gpointer)&(gvalue->data[0].v_uint);
284       break;
285     case G_TYPE_STRING:
286     case G_TYPE_OBJECT:
287     case G_TYPE_BOXED:
288     case G_TYPE_POINTER:
289       rettype = &ffi_type_pointer;
290       *value = (gpointer)&(gvalue->data[0].v_pointer);
291       break;
292     case G_TYPE_FLOAT:
293       rettype = &ffi_type_float;
294       *value = (gpointer)&(gvalue->data[0].v_float);
295       break;
296     case G_TYPE_DOUBLE:
297       rettype = &ffi_type_double;
298       *value = (gpointer)&(gvalue->data[0].v_double);
299       break;
300     case G_TYPE_LONG:
301       rettype = &ffi_type_slong;
302       *value = (gpointer)&(gvalue->data[0].v_long);
303       break;
304     case G_TYPE_ULONG:
305       rettype = &ffi_type_ulong;
306       *value = (gpointer)&(gvalue->data[0].v_ulong);
307       break;
308     case G_TYPE_INT64:
309       rettype = &ffi_type_sint64;
310       *value = (gpointer)&(gvalue->data[0].v_int64);
311       break;
312     case G_TYPE_UINT64:
313       rettype = &ffi_type_uint64;
314       *value = (gpointer)&(gvalue->data[0].v_uint64);
315       break;
316     default:
317       rettype = &ffi_type_pointer;
318       *value = NULL;
319       g_warning ("Unsupported fundamental type: %s", g_type_name (type));
320       break;
321     }
322   return rettype;
323 }
324
325 static void
326 value_from_ffi_type (GValue *gvalue, gpointer *value)
327 {
328   switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
329     {
330     case G_TYPE_INT:
331       g_value_set_int (gvalue, *(gint*)value);
332       break;
333     case G_TYPE_FLOAT:
334       g_value_set_float (gvalue, *(gfloat*)value);
335       break;
336     case G_TYPE_DOUBLE:
337       g_value_set_double (gvalue, *(gdouble*)value);
338       break;
339     case G_TYPE_BOOLEAN:
340       g_value_set_boolean (gvalue, *(gboolean*)value);
341       break;
342     case G_TYPE_STRING:
343       g_value_set_string (gvalue, *(gchar**)value);
344       break;
345     case G_TYPE_CHAR:
346       g_value_set_char (gvalue, *(gchar*)value);
347       break;
348     case G_TYPE_UCHAR:
349       g_value_set_uchar (gvalue, *(guchar*)value);
350       break;
351     case G_TYPE_UINT:
352       g_value_set_uint (gvalue, *(guint*)value);
353       break;
354     case G_TYPE_POINTER:
355       g_value_set_pointer (gvalue, *(gpointer*)value);
356       break;
357     case G_TYPE_LONG:
358       g_value_set_long (gvalue, *(glong*)value);
359       break;
360     case G_TYPE_ULONG:
361       g_value_set_ulong (gvalue, *(gulong*)value);
362       break;
363     case G_TYPE_INT64:
364       g_value_set_int64 (gvalue, *(gint64*)value);
365       break;
366     case G_TYPE_UINT64:
367       g_value_set_uint64 (gvalue, *(guint64*)value);
368       break;
369     case G_TYPE_BOXED:
370       g_value_set_boxed (gvalue, *(gpointer*)value);
371       break;
372     default:
373       g_warning ("Unsupported fundamental type: %s",
374                 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
375     }
376 }
377
378 void
379 gi_cclosure_marshal_generic (GClosure *closure,
380                              GValue *return_gvalue,
381                              guint n_param_values,
382                              const GValue *param_values,
383                              gpointer invocation_hint,
384                              gpointer marshal_data)
385 {
386   ffi_type *rtype;
387   void *rvalue;
388   int n_args;
389   ffi_type **atypes;
390   void **args;
391   int i;
392   ffi_cif cif;
393   GCClosure *cc = (GCClosure*) closure;
394
395   if (return_gvalue && G_VALUE_TYPE (return_gvalue)) 
396     {
397       rtype = value_to_ffi_type (return_gvalue, &rvalue);
398     }
399   else 
400     {
401       rtype = &ffi_type_void;
402     }
403
404   rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
405   
406   n_args = n_param_values + 1;
407   atypes = g_alloca (sizeof (ffi_type *) * n_args);
408   args =  g_alloca (sizeof (gpointer) * n_args);
409
410   if (n_param_values > 0)
411     {
412       if (G_CCLOSURE_SWAP_DATA (closure))
413         {
414           atypes[n_args-1] = value_to_ffi_type (param_values + 0,  
415                                                 &args[n_args-1]);
416           atypes[0] = &ffi_type_pointer;
417           args[0] = &closure->data;
418         }
419       else
420         {
421           atypes[0] = value_to_ffi_type (param_values + 0, &args[0]);
422           atypes[n_args-1] = &ffi_type_pointer;
423           args[n_args-1] = &closure->data;
424         }
425     }
426   else
427     {
428       atypes[0] = &ffi_type_pointer;
429       args[0] = &closure->data;
430     }
431
432   for (i = 1; i < n_args - 1; i++)
433     atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
434
435   if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
436     return;
437
438   ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
439
440   if (return_gvalue && G_VALUE_TYPE (return_gvalue))
441     value_from_ffi_type (return_gvalue, rvalue);
442 }