6 #include <girepository.h>
9 main (int argc, char *argv[])
11 const gchar *testfns = "./testfns.la";
15 GIFunctionInfo *function;
17 GArgument out_args[3];
27 rep = g_irepository_get_default ();
29 g_print ("before dlopening %s: %d infos in the repository\n",
31 g_irepository_get_n_infos (rep, "test"));
35 handle = g_module_open (testfns, 0);
38 g_error ("module open failed: %s\n", g_module_error ());
44 name = g_irepository_register_file (rep, "test", &error);
47 g_error ("Unable to load metadata 'test': %s", error->message);
50 g_print ("Loaded %s from test.gmeta\n", name);
53 g_print ("after dlopening %s: %d infos in the repository\n",
55 g_irepository_get_n_infos (rep, "test"));
57 /* test1 calculates x + 4,
58 * taking x as an in parameter
59 * and returning the result
61 info = g_irepository_find_by_name (rep, "test", "test1");
62 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
63 function = (GIFunctionInfo *)info;
67 if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
68 g_print ("Invokation of %s failed: %s\n",
69 g_base_info_get_name (info),
72 g_assert (retval.v_int == 8);
73 g_base_info_unref (info);
75 /* test2 calculates x + 4,
76 * taking x as an in parameter
77 * and storing the result in an out parameter
79 info = g_irepository_find_by_name (rep, "test", "test2");
80 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
81 function = (GIFunctionInfo *)info;
85 out_args[0].v_pointer = &res;
86 if (!g_function_info_invoke (function, in_args, 1, out_args, 1, &retval, &error))
87 g_print ("Invokation of %s failed: %s\n",
88 g_base_info_get_name (info),
92 g_base_info_unref (info);
94 /* test3 calculates x + 4,
95 * taking x as an inout parameter
96 * and storing the result in the same parameter
98 info = g_irepository_find_by_name (rep, "test", "test3");
99 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
100 function = (GIFunctionInfo *)info;
103 in_args[0].v_pointer = out_args[0].v_pointer = &res;
104 if (!g_function_info_invoke (function, in_args, 1, out_args, 1, &retval, &error))
105 g_print ("Invokation of %s failed: %s\n",
106 g_base_info_get_name (info),
109 g_assert (res == 10);
110 g_base_info_unref (info);
112 /* test4 prints out a string
114 info = g_irepository_find_by_name (rep, "test", "test4");
115 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
116 function = (GIFunctionInfo *)info;
118 in_args[0].v_pointer = "hello world\n";
119 if (!g_function_info_invoke (function, in_args, 1, NULL, 0, NULL, &error))
120 g_print ("Invokation of %s failed: %s\n",
121 g_base_info_get_name (info),
124 g_base_info_unref (info);
126 /* test5 returns a string and a length
128 info = g_irepository_find_by_name (rep, "test", "test5");
129 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
130 function = (GIFunctionInfo *)info;
134 out_args[0].v_pointer = &blurb;
135 out_args[1].v_pointer = &len;
136 if (!g_function_info_invoke (function, NULL, 0, out_args, 2, NULL, &error))
137 g_print ("Invokation of %s failed: %s\n",
138 g_base_info_get_name (info),
141 g_assert (strcmp (blurb, "hey there") == 0);
142 g_assert (len == strlen (blurb));
143 g_base_info_unref (info);
148 info = g_irepository_find_by_name (rep, "test", "test6");
149 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
150 function = (GIFunctionInfo *)info;
154 list = g_list_prepend (list, GINT_TO_POINTER(1));
155 list = g_list_prepend (list, GINT_TO_POINTER(2));
157 in_args[0].v_pointer = out_args[0].v_pointer = list;
158 if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
159 g_print ("Invokation of %s failed: %s\n",
160 g_base_info_get_name (info),
163 g_print("returned %d", retval.v_int);
164 g_assert (retval.v_int == 2);
168 g_base_info_unref (info);
169 g_clear_error (&error);
171 /* test GList more, transfer ownership*/
173 info = g_irepository_find_by_name (rep, "test", "test7");
174 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
175 function = (GIFunctionInfo *)info;
179 list = g_list_prepend (list, g_strdup("there..."));
180 list = g_list_prepend (list, g_strdup("Hey "));
181 retval.v_pointer = NULL;
182 in_args[0].v_pointer = out_args[0].v_pointer = list;
183 if (!g_function_info_invoke (function, in_args, 1, NULL, 0, &retval, &error))
184 g_print ("Invokation of %s failed: %s\n",
185 g_base_info_get_name (info),
188 g_print("returned %s", retval.v_pointer);
189 g_assert (strcmp(retval.v_pointer, "Hey there...")==0);
190 g_list_foreach (list, (GFunc) g_free, NULL);
193 g_assert (g_callable_info_get_caller_owns ((GICallableInfo *)function) ==
194 GI_TRANSFER_EVERYTHING);
196 g_free (retval.v_pointer);
199 g_base_info_unref (info);
200 g_clear_error (&error);
202 /* test error handling */
205 /* "broken" is in the metadata but not in the .so*/
206 info = g_irepository_find_by_name (rep, "test", "broken");
207 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
208 function = (GIFunctionInfo *)info;
210 if (!g_function_info_invoke (function, NULL, 0, NULL, 0, NULL, &error))
211 g_print ("Invokation of %s failed: %s\n",
212 g_base_info_get_name (info),
215 g_base_info_unref (info);
216 g_clear_error (&error);
219 /* too few in arguments */
220 info = g_irepository_find_by_name (rep, "test", "test2");
221 g_assert (g_base_info_get_type (info) == GI_INFO_TYPE_FUNCTION);
222 function = (GIFunctionInfo *)info;
224 if (!g_function_info_invoke (function, NULL, 0, NULL, 0, NULL, &error))
225 g_print ("Invokation of %s failed: %s\n",
226 g_base_info_get_name (info),
229 g_clear_error (&error);
231 /* too few out arguments */
232 if (!g_function_info_invoke (function, in_args, 1, NULL, 0, NULL, &error))
233 g_print ("Invokation of %s failed: %s\n",
234 g_base_info_get_name (info),
237 g_clear_error (&error);
239 /* too many in arguments */
240 if (!g_function_info_invoke (function, in_args, 2, out_args, 1, NULL, &error))
241 g_print ("Invokation of %s failed: %s\n",
242 g_base_info_get_name (info),
245 g_clear_error (&error);
247 /* too many out arguments */
248 if (!g_function_info_invoke (function, in_args, 1, out_args, 2, NULL, &error))
249 g_print ("Invokation of %s failed: %s\n",
250 g_base_info_get_name (info),
253 g_clear_error (&error);