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");
41 * g_function_info_invoke:
42 * @info: a #GIFunctionInfo describing the function to invoke
43 * @in_args: an array of #GArgument<!-- -->s, one for each in
44 * parameter of @info. If there are no in parameter, @in_args
46 * @n_in_args: the length of the @in_args array
47 * @out_args: an array of #GArgument<!-- -->s, one for each out
48 * parameter of @info. If there are no out parameters, @out_args
50 * @n_out_args: the length of the @out_args array
51 * @return_value: return location for the return value of the
52 * function. If the function returns void, @return_value may be
54 * @error: return location for detailed error information, or %NULL
56 * Invokes the function described in @info with the given
57 * arguments. Note that inout parameters must appear in both
58 * argument lists. This function uses dlsym() to obtain a pointer
59 * to the function, so the library or shared object containing the
60 * described function must either be linked to the caller, or must
61 * have been dlopen()<!-- -->ed before calling this function.
63 * Returns: %TRUE if the function has been invoked, %FALSE if an
67 g_function_info_invoke (GIFunctionInfo *info,
68 const GArgument *in_args,
70 const GArgument *out_args,
72 GArgument *return_value,
84 gint n_args, n_invoke_args, in_pos, out_pos, i;
86 gboolean success = FALSE;
87 GError *local_error = NULL;
88 gpointer error_address = &local_error;
90 symbol = g_function_info_get_symbol (info);
92 if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
97 G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
98 "Could not locate %s: %s", symbol, g_module_error ());
103 is_method = (g_function_info_get_flags (info) & GI_FUNCTION_IS_METHOD) != 0
104 && (g_function_info_get_flags (info) & GI_FUNCTION_IS_CONSTRUCTOR) == 0;
105 throws = g_function_info_get_flags (info) & GI_FUNCTION_THROWS;
107 tinfo = g_callable_info_get_return_type ((GICallableInfo *)info);
108 rtype = g_type_info_get_ffi_type (tinfo);
109 g_base_info_unref ((GIBaseInfo *)tinfo);
114 n_args = g_callable_info_get_n_args ((GICallableInfo *)info);
121 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
122 "Too few \"in\" arguments (handling this)");
125 n_invoke_args = n_args+1;
129 n_invoke_args = n_args;
132 /* Add an argument for the GError */
135 atypes = g_alloca (sizeof (ffi_type*) * n_invoke_args);
136 args = g_alloca (sizeof (gpointer) * n_invoke_args);
140 atypes[0] = &ffi_type_pointer;
141 args[0] = (gpointer) &in_args[0];
143 for (i = 0; i < n_args; i++)
145 int offset = (is_method ? 1 : 0);
146 ainfo = g_callable_info_get_arg ((GICallableInfo *)info, i);
147 switch (g_arg_info_get_direction (ainfo))
149 case GI_DIRECTION_IN:
150 tinfo = g_arg_info_get_type (ainfo);
151 atypes[i+offset] = g_type_info_get_ffi_type (tinfo);
152 g_base_info_unref ((GIBaseInfo *)tinfo);
154 if (in_pos >= n_in_args)
158 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
159 "Too few \"in\" arguments (handling in)");
163 args[i+offset] = (gpointer)&in_args[in_pos];
167 case GI_DIRECTION_OUT:
168 atypes[i+offset] = &ffi_type_pointer;
170 if (out_pos >= n_out_args)
174 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
175 "Too few \"out\" arguments (handling out)");
179 args[i+offset] = (gpointer)&out_args[out_pos];
182 case GI_DIRECTION_INOUT:
183 atypes[i+offset] = &ffi_type_pointer;
185 if (in_pos >= n_in_args)
189 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
190 "Too few \"in\" arguments (handling inout)");
194 if (out_pos >= n_out_args)
198 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
199 "Too few \"out\" arguments (handling inout)");
203 args[i+offset] = (gpointer)&in_args[in_pos];
208 g_assert_not_reached ();
210 g_base_info_unref ((GIBaseInfo *)ainfo);
215 args[n_invoke_args - 1] = &error_address;
216 atypes[n_invoke_args - 1] = &ffi_type_pointer;
219 if (in_pos < n_in_args)
223 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
224 "Too many \"in\" arguments (at end)");
227 if (out_pos < n_out_args)
231 G_INVOKE_ERROR_ARGUMENT_MISMATCH,
232 "Too many \"out\" arguments (at end)");
236 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) != FFI_OK)
239 g_return_val_if_fail (return_value, FALSE);
240 ffi_call (&cif, func, return_value, args);
244 g_propagate_error (error, local_error);
256 value_to_ffi_type (const GValue *gvalue, gpointer *value)
258 ffi_type *rettype = NULL;
259 GType type = g_type_fundamental (G_VALUE_TYPE (gvalue));
260 g_assert (type != G_TYPE_INVALID);
267 rettype = &ffi_type_sint;
268 *value = (gpointer)&(gvalue->data[0].v_int);
272 rettype = &ffi_type_uint;
273 *value = (gpointer)&(gvalue->data[0].v_uint);
279 rettype = &ffi_type_pointer;
280 *value = (gpointer)&(gvalue->data[0].v_pointer);
283 rettype = &ffi_type_float;
284 *value = (gpointer)&(gvalue->data[0].v_float);
287 rettype = &ffi_type_double;
288 *value = (gpointer)&(gvalue->data[0].v_double);
291 rettype = &ffi_type_slong;
292 *value = (gpointer)&(gvalue->data[0].v_long);
295 rettype = &ffi_type_ulong;
296 *value = (gpointer)&(gvalue->data[0].v_ulong);
299 rettype = &ffi_type_sint64;
300 *value = (gpointer)&(gvalue->data[0].v_int64);
303 rettype = &ffi_type_uint64;
304 *value = (gpointer)&(gvalue->data[0].v_uint64);
307 rettype = &ffi_type_pointer;
309 g_warning ("Unsupported fundamental type: %s", g_type_name (type));
316 value_from_ffi_type (GValue *gvalue, gpointer *value)
318 switch (g_type_fundamental (G_VALUE_TYPE (gvalue)))
321 g_value_set_int (gvalue, *(gint*)value);
324 g_value_set_float (gvalue, *(gfloat*)value);
327 g_value_set_double (gvalue, *(gdouble*)value);
330 g_value_set_boolean (gvalue, *(gboolean*)value);
333 g_value_set_string (gvalue, *(gchar**)value);
336 g_value_set_char (gvalue, *(gchar*)value);
339 g_value_set_uchar (gvalue, *(guchar*)value);
342 g_value_set_uint (gvalue, *(guint*)value);
345 g_value_set_pointer (gvalue, *(gpointer*)value);
348 g_value_set_long (gvalue, *(glong*)value);
351 g_value_set_ulong (gvalue, *(gulong*)value);
354 g_value_set_int64 (gvalue, *(gint64*)value);
357 g_value_set_uint64 (gvalue, *(guint64*)value);
360 g_value_set_boxed (gvalue, *(gpointer*)value);
363 g_warning ("Unsupported fundamental type: %s",
364 g_type_name (g_type_fundamental (G_VALUE_TYPE (gvalue))));
369 gi_cclosure_marshal_generic (GClosure *closure,
370 GValue *return_gvalue,
371 guint n_param_values,
372 const GValue *param_values,
373 gpointer invocation_hint,
374 gpointer marshal_data)
383 GCClosure *cc = (GCClosure*) closure;
385 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
387 rtype = value_to_ffi_type (return_gvalue, &rvalue);
391 rtype = &ffi_type_void;
394 rvalue = g_alloca (MAX (rtype->size, sizeof (ffi_arg)));
396 n_args = n_param_values + 1;
397 atypes = g_alloca (sizeof (ffi_type *) * n_args);
398 args = g_alloca (sizeof (gpointer) * n_args);
400 if (n_param_values > 0)
402 if (G_CCLOSURE_SWAP_DATA (closure))
404 atypes[n_args-1] = value_to_ffi_type (param_values + 0,
406 atypes[0] = &ffi_type_pointer;
407 args[0] = &closure->data;
411 atypes[0] = value_to_ffi_type (param_values + 0, &args[0]);
412 atypes[n_args-1] = &ffi_type_pointer;
413 args[n_args-1] = &closure->data;
418 atypes[0] = &ffi_type_pointer;
419 args[0] = &closure->data;
422 for (i = 1; i < n_args - 1; i++)
423 atypes[i] = value_to_ffi_type (param_values + i, &args[i]);
425 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_args, rtype, atypes) != FFI_OK)
428 ffi_call (&cif, marshal_data ? marshal_data : cc->callback, rvalue, args);
430 if (return_gvalue && G_VALUE_TYPE (return_gvalue))
431 value_from_ffi_type (return_gvalue, rvalue);