gperl-i11n-invoke.c 2.93 KB
Newer Older
1 2 3 4 5 6
/* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8; -*- */

static void
prepare_invocation_info (GPerlI11nInvocationInfo *iinfo,
                         GICallableInfo *info)
{
7
	gint orig_n_args;
8 9
	guint i;

10
	dwarn ("%s\n", g_base_info_get_name (info));
11 12 13 14 15 16 17 18 19 20

	iinfo->interface = info;

	iinfo->is_function = GI_IS_FUNCTION_INFO (info);
	iinfo->is_vfunc = GI_IS_VFUNC_INFO (info);
	iinfo->is_callback = (g_base_info_get_type (info) == GI_INFO_TYPE_CALLBACK);
	iinfo->is_signal = GI_IS_SIGNAL_INFO (info);
	dwarn ("  is_function = %d, is_vfunc = %d, is_callback = %d\n",
	       iinfo->is_function, iinfo->is_vfunc, iinfo->is_callback);

21 22 23
	orig_n_args = g_callable_info_get_n_args (info);
	g_assert (orig_n_args >= 0);
	iinfo->n_args = (guint) orig_n_args;
24
	dwarn ("  n_args = %u\n", iinfo->n_args);
25 26

	if (iinfo->n_args) {
27 28
		iinfo->arg_infos = gperl_alloc_temp (sizeof (GITypeInfo) * iinfo->n_args);
		iinfo->arg_types = gperl_alloc_temp (sizeof (GITypeInfo) * iinfo->n_args);
29
		iinfo->aux_args = gperl_alloc_temp (sizeof (GIArgument) * iinfo->n_args);
30 31 32 33 34 35 36
	} else {
		iinfo->arg_infos = NULL;
		iinfo->arg_types = NULL;
		iinfo->aux_args = NULL;
	}

	for (i = 0 ; i < iinfo->n_args ; i++) {
37 38
		g_callable_info_load_arg (info, (gint) i, &(iinfo->arg_infos[i]));
		g_arg_info_load_type (&(iinfo->arg_infos[i]), &(iinfo->arg_types[i]));
39 40
	}

41
	g_callable_info_load_return_type (info, &iinfo->return_type_info);
42
	iinfo->has_return_value =
43 44
		GI_TYPE_TAG_VOID != g_type_info_get_tag (&iinfo->return_type_info);
	iinfo->return_type_ffi = g_type_info_get_ffi_type (&iinfo->return_type_info);
45 46 47 48
	iinfo->return_type_transfer = g_callable_info_get_caller_owns (info);

	iinfo->callback_infos = NULL;
	iinfo->array_infos = NULL;
49

50 51 52 53 54 55 56
	iinfo->free_after_call = NULL;
}

static void
clear_invocation_info (GPerlI11nInvocationInfo *iinfo)
{
	g_slist_free (iinfo->free_after_call);
57
	iinfo->free_after_call = NULL;
58 59 60 61

	/* The actual callback infos might be needed later, so we cannot free
	 * them here. */
	g_slist_free (iinfo->callback_infos);
62
	iinfo->callback_infos = NULL;
63 64 65

	g_slist_foreach (iinfo->array_infos, (GFunc) g_free, NULL);
	g_slist_free (iinfo->array_infos);
66
	iinfo->array_infos = NULL;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

/* ------------------------------------------------------------------------- */

typedef struct {
	GFunc func;
	gpointer data;
} FreeClosure;

static void
free_after_call (GPerlI11nInvocationInfo *iinfo, GFunc func, gpointer data)
{
	FreeClosure *closure = g_new (FreeClosure, 1);
	closure->func = func;
	closure->data = data;
	iinfo->free_after_call
		= g_slist_prepend (iinfo->free_after_call, closure);
}

static void
_invoke_free_closure (FreeClosure *closure)
{
	closure->func (closure->data, NULL);
	g_free (closure);
}

static void
invoke_free_after_call_handlers (GPerlI11nInvocationInfo *iinfo)
{
96 97
	/* We free the FreeClosures themselves directly after invoking them.  The list
	   is freed in clear_invocation_info. */
98 99 100
	g_slist_foreach (iinfo->free_after_call,
	                 (GFunc) _invoke_free_closure, NULL);
}