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 g_return_val_if_fail (return_value, FALSE);
251 ffi_call (&cif, func, return_value, args);
255 g_propagate_error (error, local_error);
267 value_to_ffi_type (const GValue *gvalue, gpointer *value)
269 ffi_type *rettype = NULL;
270 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
271 g_assert (type != G_TYPE_INVALID);
278 rettype = &ffi_type_sint;
279 *value = (gpointer)&(gvalue->data[0].v_int);
283 rettype = &ffi_type_uint;
284 *value = (gpointer)&(gvalue->data[0].v_uint);
290 rettype = &ffi_type_pointer;
291 *value = (gpointer)&(gvalue->data[0].v_pointer);
294 rettype = &ffi_type_float;
295 *value = (gpointer)&(gvalue->data[0].v_float);
298 rettype = &ffi_type_double;
299 *value = (gpointer)&(gvalue->data[0].v_double);
302 rettype = &ffi_type_slong;
303 *value = (gpointer)&(gvalue->data[0].v_long);
306 rettype = &ffi_type_ulong;
307 *value = (gpointer)&(gvalue->data[0].v_ulong);
310 rettype = &ffi_type_sint64;
311 *value = (gpointer)&(gvalue->data[0].v_int64);
314 rettype = &ffi_type_uint64;
315 *value = (gpointer)&(gvalue->data[0].v_uint64);
318 rettype = &ffi_type_pointer;
320 g_warning ("Unsupported fundamental type: %s", g_type_name (type));
327 value_from_ffi_type (GValue *gvalue, gpointer *value)
329 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
332 g_value_set_int (gvalue, *(gint*)value);
335 g_value_set_float (gvalue, *(gfloat*)value);
338 g_value_set_double (gvalue, *(gdouble*)value);
341 g_value_set_boolean (gvalue, *(gboolean*)value);
344 g_value_set_string (gvalue, *(gchar**)value);
347 g_value_set_char (gvalue, *(gchar*)value);
350 g_value_set_uchar (gvalue, *(guchar*)value);
353 g_value_set_uint (gvalue, *(guint*)value);
356 g_value_set_pointer (gvalue, *(gpointer*)value);
359 g_value_set_long (gvalue, *(glong*)value);
362 g_value_set_ulong (gvalue, *(gulong*)value);
365 g_value_set_int64 (gvalue, *(gint64*)value);
368 g_value_set_uint64 (gvalue, *(guint64*)value);
371 g_value_set_boxed (gvalue, *(gpointer*)value);
374 g_warning ("Unsupported fundamental type: %s",
375 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
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)
394 GCClosure *cc = (GCClosure*) closure;
396 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
398 rtype = value_to_ffi_type (return_gvalue, &rvalue);
402 rtype = &ffi_type_void;
405 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
407 n_args = n_param_values + 1;
408 atypes = g_alloca (sizeof (ffi_type *) * n_args);
409 args = g_alloca (sizeof (gpointer) * n_args);
411 if (n_param_values > 0)
413 if (G_CCLOSURE_SWAP_DATA (closure))
415 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
417 atypes[0] = &ffi_type_pointer;
418 args[0] = &closure->data;
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;
429 atypes[0] = &ffi_type_pointer;
430 args[0] = &closure->data;
433 for (i = 1; i < n_args - 1; i++)
434 atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
436 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
439 g_return_if_fail (rvalue != NULL);
440 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
442 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
443 value_from_ffi_type (return_gvalue, rvalue);