1 /* GObject introspection: Invoke functionality
3 * Copyright (C) 2005 Matthias Clasen
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.
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.
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.
24 #include <glib-object.h>
26 #include "girepository.h"
32 g_invoke_error_quark (void)
34 static GQuark quark = 0;
36 quark = g_quark_from_static_string ("g-invoke-error-quark");
43 get_ffi_type (GITypeInfo *info)
45 if (g_type_info_is_pointer (info))
46 return &ffi_type_pointer;
48 return g_ir_ffi_get_ffi_type (g_type_info_get_tag (info));
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
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
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
65 * @error: return location for detailed error information, or %NULL
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.
74 * Returns: %TRUE if the function has been invoked, %FALSE if an
78 g_function_info_invoke (GIFunctionInfo *info,
79 const GArgument *in_args,
81 const GArgument *out_args,
83 GArgument *return_value,
95 gint n_args, n_invoke_args, in_pos, out_pos, i;
97 gboolean success = FALSE;
98 GError *local_error = NULL;
99 gpointer error_address = &local_error;
101 symbol = g_function_info_get_symbol (info);
103 if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
108 G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
109 "Could not locate %s: %s", symbol, g_module_error ());
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;
118 tinfo = g_callable_info_get_return_type ((GICallableInfo *)info);
119 rtype = get_ffi_type (tinfo);
120 g_base_info_unref ((GIBaseInfo *)tinfo);
125 n_args = g_callable_info_get_n_args ((GICallableInfo *)info);
132 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
133 "Too few \"in\" arguments (handling this)");
136 n_invoke_args = n_args+1;
140 n_invoke_args = n_args;
143 /* Add an argument for the GError */
146 atypes = g_alloca (sizeof (ffi_type*) * n_invoke_args);
147 args = g_alloca (sizeof (gpointer) * n_invoke_args);
151 atypes[0] = &ffi_type_pointer;
152 args[0] = (gpointer) &in_args[0];
154 for (i = 0; i < n_args; i++)
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))
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);
165 if (in_pos >= n_in_args)
169 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
170 "Too few \"in\" arguments (handling in)");
174 args[i+offset] = (gpointer)&in_args[in_pos];
178 case GI_DIRECTION_OUT:
179 atypes[i+offset] = &ffi_type_pointer;
181 if (out_pos >= n_out_args)
185 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
186 "Too few \"out\" arguments (handling out)");
190 args[i+offset] = (gpointer)&out_args[out_pos];
193 case GI_DIRECTION_INOUT:
194 atypes[i+offset] = &ffi_type_pointer;
196 if (in_pos >= n_in_args)
200 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
201 "Too few \"in\" arguments (handling inout)");
205 if (out_pos >= n_out_args)
209 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
210 "Too few \"out\" arguments (handling inout)");
214 args[i+offset] = (gpointer)&in_args[in_pos];
219 g_assert_not_reached ();
221 g_base_info_unref ((GIBaseInfo *)ainfo);
226 args[n_invoke_args - 1] = &error_address;
227 atypes[n_invoke_args - 1] = &ffi_type_pointer;
230 if (in_pos < n_in_args)
234 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
235 "Too many \"in\" arguments (at end)");
238 if (out_pos < n_out_args)
242 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
243 "Too many \"out\" arguments (at end)");
247 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) != FFI_OK)
250 ffi_call (&cif, func, return_value, args);
254 g_propagate_error (error, local_error);
266 value_to_ffi_type (const GValue *gvalue, gpointer *value)
268 ffi_type *rettype = NULL;
269 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
270 g_assert (type != G_TYPE_INVALID);
277 rettype = &ffi_type_sint;
278 *value = (gpointer)&(gvalue->data[0].v_int);
282 rettype = &ffi_type_uint;
283 *value = (gpointer)&(gvalue->data[0].v_uint);
289 rettype = &ffi_type_pointer;
290 *value = (gpointer)&(gvalue->data[0].v_pointer);
293 rettype = &ffi_type_float;
294 *value = (gpointer)&(gvalue->data[0].v_float);
297 rettype = &ffi_type_double;
298 *value = (gpointer)&(gvalue->data[0].v_double);
301 rettype = &ffi_type_slong;
302 *value = (gpointer)&(gvalue->data[0].v_long);
305 rettype = &ffi_type_ulong;
306 *value = (gpointer)&(gvalue->data[0].v_ulong);
309 rettype = &ffi_type_sint64;
310 *value = (gpointer)&(gvalue->data[0].v_int64);
313 rettype = &ffi_type_uint64;
314 *value = (gpointer)&(gvalue->data[0].v_uint64);
317 rettype = &ffi_type_pointer;
319 g_warning ("Unsupported fundamental type: %s", g_type_name (type));
326 value_from_ffi_type (GValue *gvalue, gpointer *value)
328 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
331 g_value_set_int (gvalue, *(gint*)value);
334 g_value_set_float (gvalue, *(gfloat*)value);
337 g_value_set_double (gvalue, *(gdouble*)value);
340 g_value_set_boolean (gvalue, *(gboolean*)value);
343 g_value_set_string (gvalue, *(gchar**)value);
346 g_value_set_char (gvalue, *(gchar*)value);
349 g_value_set_uchar (gvalue, *(guchar*)value);
352 g_value_set_uint (gvalue, *(guint*)value);
355 g_value_set_pointer (gvalue, *(gpointer*)value);
358 g_value_set_long (gvalue, *(glong*)value);
361 g_value_set_ulong (gvalue, *(gulong*)value);
364 g_value_set_int64 (gvalue, *(gint64*)value);
367 g_value_set_uint64 (gvalue, *(guint64*)value);
370 g_value_set_boxed (gvalue, *(gpointer*)value);
373 g_warning ("Unsupported fundamental type: %s",
374 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
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)
393 GCClosure *cc = (GCClosure*) closure;
395 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
397 rtype = value_to_ffi_type (return_gvalue, &rvalue);
401 rtype = &ffi_type_void;
404 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
406 n_args = n_param_values + 1;
407 atypes = g_alloca (sizeof (ffi_type *) * n_args);
408 args = g_alloca (sizeof (gpointer) * n_args);
410 if (n_param_values > 0)
412 if (G_CCLOSURE_SWAP_DATA (closure))
414 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
416 atypes[0] = &ffi_type_pointer;
417 args[0] = &closure->data;
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;
428 atypes[0] = &ffi_type_pointer;
429 args[0] = &closure->data;
432 for (i = 1; i < n_args - 1; i++)
433 atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
435 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
438 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
440 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
441 value_from_ffi_type (return_gvalue, rvalue);