Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : * GObject introspection: Callable implementation
3 : : *
4 : : * Copyright (C) 2005 Matthias Clasen
5 : : * Copyright (C) 2008,2009 Red Hat, Inc.
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General Public
20 : : * License along with this library; if not, write to the
21 : : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 : : * Boston, MA 02111-1307, USA.
23 : : */
24 : :
25 : : #include "config.h"
26 : :
27 : : #include <stdlib.h>
28 : :
29 : : #include <glib.h>
30 : :
31 : : #include <girepository/girepository.h>
32 : : #include "gibaseinfo-private.h"
33 : : #include "girepository-private.h"
34 : : #include "gitypelib-internal.h"
35 : : #include "girffi.h"
36 : : #include "gicallableinfo.h"
37 : :
38 : : /* GICallableInfo functions */
39 : :
40 : : /**
41 : : * GICallableInfo:
42 : : *
43 : : * `GICallableInfo` represents an entity which is callable.
44 : : *
45 : : * Examples of callable are:
46 : : *
47 : : * - functions ([class@GIRepository.FunctionInfo])
48 : : * - virtual functions ([class@GIRepository.VFuncInfo])
49 : : * - callbacks ([class@GIRepository.CallbackInfo]).
50 : : *
51 : : * A callable has a list of arguments ([class@GIRepository.ArgInfo]), a return
52 : : * type, direction and a flag which decides if it returns `NULL`.
53 : : *
54 : : * Since: 2.80
55 : : */
56 : :
57 : : static uint32_t
58 : 47 : signature_offset (GICallableInfo *info)
59 : : {
60 : 47 : GIRealInfo *rinfo = (GIRealInfo*)info;
61 : 47 : int sigoff = -1;
62 : :
63 [ + + + + : 47 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info))
- ]
64 : : {
65 : 41 : case GI_INFO_TYPE_FUNCTION:
66 : 41 : sigoff = G_STRUCT_OFFSET (FunctionBlob, signature);
67 : 41 : break;
68 : 2 : case GI_INFO_TYPE_VFUNC:
69 : 2 : sigoff = G_STRUCT_OFFSET (VFuncBlob, signature);
70 : 2 : break;
71 : 1 : case GI_INFO_TYPE_CALLBACK:
72 : 1 : sigoff = G_STRUCT_OFFSET (CallbackBlob, signature);
73 : 1 : break;
74 : 3 : case GI_INFO_TYPE_SIGNAL:
75 : 3 : sigoff = G_STRUCT_OFFSET (SignalBlob, signature);
76 : 3 : break;
77 : 0 : default:
78 : : g_assert_not_reached ();
79 : : }
80 [ + - ]: 47 : if (sigoff >= 0)
81 : 47 : return *(uint32_t *)&rinfo->typelib->data[rinfo->offset + sigoff];
82 : 0 : return 0;
83 : : }
84 : :
85 : : /**
86 : : * gi_callable_info_can_throw_gerror:
87 : : * @info: a #GICallableInfo
88 : : *
89 : : * Whether the callable can throw a [type@GLib.Error]
90 : : *
91 : : * Returns: `TRUE` if this `GICallableInfo` can throw a [type@GLib.Error]
92 : : * Since: 2.80
93 : : */
94 : : gboolean
95 : 8 : gi_callable_info_can_throw_gerror (GICallableInfo *info)
96 : : {
97 : 8 : GIRealInfo *rinfo = (GIRealInfo*)info;
98 : : SignatureBlob *signature;
99 : :
100 : 8 : signature = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
101 [ + + ]: 8 : if (signature->throws)
102 : 6 : return TRUE;
103 : :
104 : : /* Functions and VFuncs store "throws" in their own blobs.
105 : : * This info was additionally added to the SignatureBlob
106 : : * to support the other callables. For Functions and VFuncs,
107 : : * also check their legacy flag for compatibility.
108 : : */
109 [ + - - - ]: 2 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info)) {
110 : 2 : case GI_INFO_TYPE_FUNCTION:
111 : : {
112 : : FunctionBlob *blob;
113 : 2 : blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
114 : 2 : return blob->throws;
115 : : }
116 : 0 : case GI_INFO_TYPE_VFUNC:
117 : : {
118 : : VFuncBlob *blob;
119 : 0 : blob = (VFuncBlob *)&rinfo->typelib->data[rinfo->offset];
120 : 0 : return blob->throws;
121 : : }
122 : 0 : case GI_INFO_TYPE_CALLBACK:
123 : : case GI_INFO_TYPE_SIGNAL:
124 : 0 : return FALSE;
125 : 0 : default:
126 : : g_assert_not_reached ();
127 : : }
128 : : }
129 : :
130 : : /**
131 : : * gi_callable_info_is_method:
132 : : * @info: a #GICallableInfo
133 : : *
134 : : * Determines if the callable info is a method.
135 : : *
136 : : * For [class@GIRepository.VFuncInfo]s, [class@GIRepository.CallbackInfo]s, and
137 : : * [class@GIRepository.SignalInfo]s, this is always true. Otherwise, this looks
138 : : * at the `GI_FUNCTION_IS_METHOD` flag on the [class@GIRepository.FunctionInfo].
139 : : *
140 : : * Concretely, this function returns whether
141 : : * [method@GIRepository.CallableInfo.get_n_args] matches the number of arguments
142 : : * in the raw C method. For methods, there is one more C argument than is
143 : : * exposed by introspection: the `self` or `this` object.
144 : : *
145 : : * Returns: `TRUE` if @info is a method, `FALSE` otherwise
146 : : * Since: 2.80
147 : : */
148 : : gboolean
149 : 6 : gi_callable_info_is_method (GICallableInfo *info)
150 : : {
151 : 6 : GIRealInfo *rinfo = (GIRealInfo*)info;
152 [ + - - - ]: 6 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info)) {
153 : 6 : case GI_INFO_TYPE_FUNCTION:
154 : : {
155 : : FunctionBlob *blob;
156 : 6 : blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
157 [ + - + + ]: 6 : return (!blob->constructor && !blob->is_static);
158 : : }
159 : 0 : case GI_INFO_TYPE_VFUNC:
160 : : case GI_INFO_TYPE_SIGNAL:
161 : 0 : return TRUE;
162 : 0 : case GI_INFO_TYPE_CALLBACK:
163 : 0 : return FALSE;
164 : 0 : default:
165 : : g_assert_not_reached ();
166 : : }
167 : : }
168 : :
169 : : /**
170 : : * gi_callable_info_get_return_type:
171 : : * @info: a #GICallableInfo
172 : : *
173 : : * Obtain the return type of a callable item as a [class@GIRepository.TypeInfo].
174 : : *
175 : : * If the callable doesn’t return anything, a [class@GIRepository.TypeInfo] of
176 : : * type [enum@GIRepository.TypeTag.VOID] will be returned.
177 : : *
178 : : * Returns: (transfer full): the [class@GIRepository.TypeInfo]. Free the struct
179 : : * by calling [method@GIRepository.BaseInfo.unref] when done.
180 : : * Since: 2.80
181 : : */
182 : : GITypeInfo *
183 : 8 : gi_callable_info_get_return_type (GICallableInfo *info)
184 : : {
185 : 8 : GIRealInfo *rinfo = (GIRealInfo *)info;
186 : : uint32_t offset;
187 : :
188 : 8 : g_return_val_if_fail (info != NULL, NULL);
189 : 8 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), NULL);
190 : :
191 : 8 : offset = signature_offset (info);
192 : :
193 : 8 : return gi_type_info_new ((GIBaseInfo*)info, rinfo->typelib, offset);
194 : : }
195 : :
196 : : /**
197 : : * gi_callable_info_load_return_type:
198 : : * @info: a #GICallableInfo
199 : : * @type: (out caller-allocates): Initialized with return type of @info
200 : : *
201 : : * Obtain information about a return value of callable; this
202 : : * function is a variant of [method@GIRepository.CallableInfo.get_return_type]
203 : : * designed for stack allocation.
204 : : *
205 : : * The initialized @type must not be referenced after @info is deallocated.
206 : : *
207 : : * Once you are done with @type, it must be cleared using
208 : : * [method@GIRepository.BaseInfo.clear].
209 : : *
210 : : * Since: 2.80
211 : : */
212 : : void
213 : 1 : gi_callable_info_load_return_type (GICallableInfo *info,
214 : : GITypeInfo *type)
215 : : {
216 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
217 : : uint32_t offset;
218 : :
219 : 1 : g_return_if_fail (info != NULL);
220 : 1 : g_return_if_fail (GI_IS_CALLABLE_INFO (info));
221 : :
222 : 1 : offset = signature_offset (info);
223 : :
224 : 1 : gi_type_info_init (type, (GIBaseInfo*)info, rinfo->typelib, offset);
225 : : }
226 : :
227 : : /**
228 : : * gi_callable_info_may_return_null:
229 : : * @info: a #GICallableInfo
230 : : *
231 : : * See if a callable could return `NULL`.
232 : : *
233 : : * Returns: `TRUE` if callable could return `NULL`
234 : : * Since: 2.80
235 : : */
236 : : gboolean
237 : 1 : gi_callable_info_may_return_null (GICallableInfo *info)
238 : : {
239 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
240 : : SignatureBlob *blob;
241 : :
242 : 1 : g_return_val_if_fail (info != NULL, FALSE);
243 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), FALSE);
244 : :
245 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
246 : :
247 : 1 : return blob->may_return_null;
248 : : }
249 : :
250 : : /**
251 : : * gi_callable_info_skip_return:
252 : : * @info: a #GICallableInfo
253 : : *
254 : : * See if a callable’s return value is only useful in C.
255 : : *
256 : : * Returns: `TRUE` if return value is only useful in C.
257 : : * Since: 2.80
258 : : */
259 : : gboolean
260 : 1 : gi_callable_info_skip_return (GICallableInfo *info)
261 : : {
262 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
263 : : SignatureBlob *blob;
264 : :
265 : 1 : g_return_val_if_fail (info != NULL, FALSE);
266 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), FALSE);
267 : :
268 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
269 : :
270 : 1 : return blob->skip_return;
271 : : }
272 : :
273 : : /**
274 : : * gi_callable_info_get_caller_owns:
275 : : * @info: a #GICallableInfo
276 : : *
277 : : * See whether the caller owns the return value of this callable.
278 : : *
279 : : * [type@GIRepository.Transfer] contains a list of possible transfer values.
280 : : *
281 : : * Returns: the transfer mode for the return value of the callable
282 : : * Since: 2.80
283 : : */
284 : : GITransfer
285 : 1 : gi_callable_info_get_caller_owns (GICallableInfo *info)
286 : : {
287 : 1 : GIRealInfo *rinfo = (GIRealInfo*) info;
288 : : SignatureBlob *blob;
289 : :
290 : 1 : g_return_val_if_fail (info != NULL, -1);
291 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), -1);
292 : :
293 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
294 : :
295 [ - + ]: 1 : if (blob->caller_owns_return_value)
296 : 0 : return GI_TRANSFER_EVERYTHING;
297 [ - + ]: 1 : else if (blob->caller_owns_return_container)
298 : 0 : return GI_TRANSFER_CONTAINER;
299 : : else
300 : 1 : return GI_TRANSFER_NOTHING;
301 : : }
302 : :
303 : : /**
304 : : * gi_callable_info_get_instance_ownership_transfer:
305 : : * @info: a #GICallableInfo
306 : : *
307 : : * Obtains the ownership transfer for the instance argument.
308 : : *
309 : : * [type@GIRepository.Transfer] contains a list of possible transfer values.
310 : : *
311 : : * Returns: the transfer mode of the instance argument
312 : : * Since: 2.80
313 : : */
314 : : GITransfer
315 : 3 : gi_callable_info_get_instance_ownership_transfer (GICallableInfo *info)
316 : : {
317 : 3 : GIRealInfo *rinfo = (GIRealInfo*) info;
318 : : SignatureBlob *blob;
319 : :
320 : 3 : g_return_val_if_fail (info != NULL, -1);
321 : 3 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), -1);
322 : :
323 : 3 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
324 : :
325 [ + + ]: 3 : if (blob->instance_transfer_ownership)
326 : 1 : return GI_TRANSFER_EVERYTHING;
327 : : else
328 : 2 : return GI_TRANSFER_NOTHING;
329 : : }
330 : :
331 : : /**
332 : : * gi_callable_info_get_n_args:
333 : : * @info: a #GICallableInfo
334 : : *
335 : : * Obtain the number of arguments (both ‘in’ and ‘out’) for this callable.
336 : : *
337 : : * Returns: The number of arguments this callable expects.
338 : : * Since: 2.80
339 : : */
340 : : unsigned int
341 : 7 : gi_callable_info_get_n_args (GICallableInfo *info)
342 : : {
343 : 7 : GIRealInfo *rinfo = (GIRealInfo *)info;
344 : : uint32_t offset;
345 : : SignatureBlob *blob;
346 : :
347 : 7 : g_return_val_if_fail (info != NULL, -1);
348 : 7 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), -1);
349 : :
350 : 7 : offset = signature_offset (info);
351 : 7 : blob = (SignatureBlob *)&rinfo->typelib->data[offset];
352 : :
353 : 7 : return blob->n_arguments;
354 : : }
355 : :
356 : : /**
357 : : * gi_callable_info_get_arg:
358 : : * @info: a #GICallableInfo
359 : : * @n: the argument index to fetch
360 : : *
361 : : * Obtain information about a particular argument of this callable.
362 : : *
363 : : * Returns: (transfer full): the [class@GIRepository.ArgInfo]. Free it with
364 : : * [method@GIRepository.BaseInfo.unref] when done.
365 : : * Since: 2.80
366 : : */
367 : : GIArgInfo *
368 : 10 : gi_callable_info_get_arg (GICallableInfo *info,
369 : : unsigned int n)
370 : : {
371 : 10 : GIRealInfo *rinfo = (GIRealInfo *)info;
372 : : Header *header;
373 : : uint32_t offset;
374 : :
375 : 10 : g_return_val_if_fail (info != NULL, NULL);
376 : 10 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), NULL);
377 : 10 : g_return_val_if_fail (n <= G_MAXUINT16, NULL);
378 : :
379 : 10 : offset = signature_offset (info);
380 : 10 : header = (Header *)rinfo->typelib->data;
381 : :
382 : 10 : return (GIArgInfo *) gi_base_info_new (GI_INFO_TYPE_ARG, (GIBaseInfo*)info, rinfo->typelib,
383 : 10 : offset + header->signature_blob_size + n * header->arg_blob_size);
384 : : }
385 : :
386 : : /**
387 : : * gi_callable_info_load_arg:
388 : : * @info: a #GICallableInfo
389 : : * @n: the argument index to fetch
390 : : * @arg: (out caller-allocates): Initialize with argument number @n
391 : : *
392 : : * Obtain information about a particular argument of this callable; this
393 : : * function is a variant of [method@GIRepository.CallableInfo.get_arg] designed
394 : : * for stack allocation.
395 : : *
396 : : * The initialized @arg must not be referenced after @info is deallocated.
397 : : *
398 : : * Once you are done with @arg, it must be cleared using
399 : : * [method@GIRepository.BaseInfo.clear].
400 : : *
401 : : * Since: 2.80
402 : : */
403 : : void
404 : 5 : gi_callable_info_load_arg (GICallableInfo *info,
405 : : unsigned int n,
406 : : GIArgInfo *arg)
407 : : {
408 : 5 : GIRealInfo *rinfo = (GIRealInfo *)info;
409 : : Header *header;
410 : : uint32_t offset;
411 : :
412 : 5 : g_return_if_fail (info != NULL);
413 : 5 : g_return_if_fail (GI_IS_CALLABLE_INFO (info));
414 : 5 : g_return_if_fail (n <= G_MAXUINT16);
415 : :
416 : 5 : offset = signature_offset (info);
417 : 5 : header = (Header *)rinfo->typelib->data;
418 : :
419 : 5 : gi_info_init ((GIRealInfo*)arg, GI_TYPE_ARG_INFO, rinfo->repository, (GIBaseInfo*)info, rinfo->typelib,
420 : 5 : offset + header->signature_blob_size + n * header->arg_blob_size);
421 : : }
422 : :
423 : : /**
424 : : * gi_callable_info_get_return_attribute:
425 : : * @info: a #GICallableInfo
426 : : * @name: a freeform string naming an attribute
427 : : *
428 : : * Retrieve an arbitrary attribute associated with the return value.
429 : : *
430 : : * Returns: (nullable): The value of the attribute, or `NULL` if no such
431 : : * attribute exists
432 : : * Since: 2.80
433 : : */
434 : : const char *
435 : 1 : gi_callable_info_get_return_attribute (GICallableInfo *info,
436 : : const char *name)
437 : : {
438 : 1 : GIAttributeIter iter = GI_ATTRIBUTE_ITER_INIT;
439 : : const char *curname, *curvalue;
440 [ - + ]: 1 : while (gi_callable_info_iterate_return_attributes (info, &iter, &curname, &curvalue))
441 : : {
442 [ # # ]: 0 : if (g_strcmp0 (name, curname) == 0)
443 : 0 : return (const char*) curvalue;
444 : : }
445 : :
446 : 1 : return NULL;
447 : : }
448 : :
449 : : /**
450 : : * gi_callable_info_iterate_return_attributes:
451 : : * @info: a #GICallableInfo
452 : : * @iterator: (inout): a [type@GIRepository.AttributeIter] structure, must be
453 : : * initialized; see below
454 : : * @name: (out) (transfer none): Returned name, must not be freed
455 : : * @value: (out) (transfer none): Returned name, must not be freed
456 : : *
457 : : * Iterate over all attributes associated with the return value.
458 : : *
459 : : * The iterator structure is typically stack allocated, and must have its
460 : : * first member initialized to `NULL`.
461 : : *
462 : : * Both the @name and @value should be treated as constants
463 : : * and must not be freed.
464 : : *
465 : : * See [method@GIRepository.BaseInfo.iterate_attributes] for an example of how
466 : : * to use a similar API.
467 : : *
468 : : * Returns: `TRUE` if there are more attributes
469 : : * Since: 2.80
470 : : */
471 : : gboolean
472 : 2 : gi_callable_info_iterate_return_attributes (GICallableInfo *info,
473 : : GIAttributeIter *iterator,
474 : : const char **name,
475 : : const char **value)
476 : : {
477 : 2 : GIRealInfo *rinfo = (GIRealInfo *)info;
478 : 2 : Header *header = (Header *)rinfo->typelib->data;
479 : : AttributeBlob *next, *after;
480 : : uint32_t blob_offset;
481 : :
482 : 2 : after = (AttributeBlob *) &rinfo->typelib->data[header->attributes +
483 : 2 : header->n_attributes * header->attribute_blob_size];
484 : :
485 : 2 : blob_offset = signature_offset (info);
486 : :
487 [ - + ]: 2 : if (iterator->data != NULL)
488 : 0 : next = (AttributeBlob *) iterator->data;
489 : : else
490 : 2 : next = _attribute_blob_find_first ((GIBaseInfo *) info, blob_offset);
491 : :
492 [ - + - - : 2 : if (next == NULL || next->offset != blob_offset || next >= after)
- - ]
493 : 2 : return FALSE;
494 : :
495 : 0 : *name = gi_typelib_get_string (rinfo->typelib, next->name);
496 : 0 : *value = gi_typelib_get_string (rinfo->typelib, next->value);
497 : 0 : iterator->data = next + 1;
498 : :
499 : 0 : return TRUE;
500 : : }
501 : :
502 : : /**
503 : : * gi_type_tag_extract_ffi_return_value:
504 : : * @return_tag: [type@GIRepository.TypeTag] of the return value
505 : : * @interface_type: [type@GObject.Type] of the underlying interface type
506 : : * @ffi_value: pointer to [type@GIRepository.FFIReturnValue] union containing
507 : : * the return value from `ffi_call()`
508 : : * @arg: (out caller-allocates): pointer to an allocated
509 : : * [class@GIRepository.Argument]
510 : : *
511 : : * Extract the correct bits from an `ffi_arg` return value into
512 : : * [class@GIRepository.Argument].
513 : : *
514 : : * See: https://bugzilla.gnome.org/show_bug.cgi?id=665152
515 : : *
516 : : * Also see [`ffi_call()`](man:ffi_call(3)): the storage requirements for return
517 : : * values are ‘special’.
518 : : *
519 : : * The @interface_type argument only applies if @return_tag is
520 : : * `GI_TYPE_TAG_INTERFACE`. Otherwise it is ignored.
521 : : *
522 : : * Since: 2.80
523 : : */
524 : : void
525 : 0 : gi_type_tag_extract_ffi_return_value (GITypeTag return_tag,
526 : : GType interface_type,
527 : : GIFFIReturnValue *ffi_value,
528 : : GIArgument *arg)
529 : : {
530 [ # # # # : 0 : switch (return_tag) {
# # # # #
# # # ]
531 : 0 : case GI_TYPE_TAG_INT8:
532 : 0 : arg->v_int8 = (int8_t) ffi_value->v_long;
533 : 0 : break;
534 : 0 : case GI_TYPE_TAG_UINT8:
535 : 0 : arg->v_uint8 = (uint8_t) ffi_value->v_ulong;
536 : 0 : break;
537 : 0 : case GI_TYPE_TAG_INT16:
538 : 0 : arg->v_int16 = (int16_t) ffi_value->v_long;
539 : 0 : break;
540 : 0 : case GI_TYPE_TAG_UINT16:
541 : 0 : arg->v_uint16 = (uint16_t) ffi_value->v_ulong;
542 : 0 : break;
543 : 0 : case GI_TYPE_TAG_INT32:
544 : 0 : arg->v_int32 = (int32_t) ffi_value->v_long;
545 : 0 : break;
546 : 0 : case GI_TYPE_TAG_UINT32:
547 : : case GI_TYPE_TAG_BOOLEAN:
548 : : case GI_TYPE_TAG_UNICHAR:
549 : 0 : arg->v_uint32 = (uint32_t) ffi_value->v_ulong;
550 : 0 : break;
551 : 0 : case GI_TYPE_TAG_INT64:
552 : 0 : arg->v_int64 = (int64_t) ffi_value->v_int64;
553 : 0 : break;
554 : 0 : case GI_TYPE_TAG_UINT64:
555 : 0 : arg->v_uint64 = (uint64_t) ffi_value->v_uint64;
556 : 0 : break;
557 : 0 : case GI_TYPE_TAG_FLOAT:
558 : 0 : arg->v_float = ffi_value->v_float;
559 : 0 : break;
560 : 0 : case GI_TYPE_TAG_DOUBLE:
561 : 0 : arg->v_double = ffi_value->v_double;
562 : 0 : break;
563 : 0 : case GI_TYPE_TAG_INTERFACE:
564 [ # # # # ]: 0 : if (interface_type == GI_TYPE_ENUM_INFO ||
565 : 0 : interface_type == GI_TYPE_FLAGS_INFO)
566 : 0 : arg->v_int32 = (int32_t) ffi_value->v_long;
567 : : else
568 : 0 : arg->v_pointer = (void *) ffi_value->v_pointer;
569 : 0 : break;
570 : 0 : default:
571 : 0 : arg->v_pointer = (void *) ffi_value->v_pointer;
572 : 0 : break;
573 : : }
574 : 0 : }
575 : :
576 : : /**
577 : : * gi_type_info_extract_ffi_return_value:
578 : : * @return_info: [type@GIRepository.TypeInfo] describing the return type
579 : : * @ffi_value: pointer to [type@GIRepository.FFIReturnValue] union containing
580 : : * the return value from `ffi_call()`
581 : : * @arg: (out caller-allocates): pointer to an allocated
582 : : * [class@GIRepository.Argument]
583 : : *
584 : : * Extract the correct bits from an `ffi_arg` return value into
585 : : * [class@GIRepository.Argument].
586 : : *
587 : : * See: https://bugzilla.gnome.org/show_bug.cgi?id=665152
588 : : *
589 : : * Also see [`ffi_call()`](man:ffi_call(3)): the storage requirements for return
590 : : * values are ‘special’.
591 : : *
592 : : * Since: 2.80
593 : : */
594 : : void
595 : 0 : gi_type_info_extract_ffi_return_value (GITypeInfo *return_info,
596 : : GIFFIReturnValue *ffi_value,
597 : : GIArgument *arg)
598 : : {
599 : 0 : GITypeTag return_tag = gi_type_info_get_tag (return_info);
600 : 0 : GType interface_type = G_TYPE_INVALID;
601 : :
602 [ # # ]: 0 : if (return_tag == GI_TYPE_TAG_INTERFACE)
603 : : {
604 : 0 : GIBaseInfo *interface_info = gi_type_info_get_interface (return_info);
605 : 0 : interface_type = G_TYPE_FROM_INSTANCE (interface_info);
606 : 0 : gi_base_info_unref (interface_info);
607 : : }
608 : :
609 : 0 : gi_type_tag_extract_ffi_return_value (return_tag, interface_type,
610 : : ffi_value, arg);
611 : 0 : }
612 : :
613 : : /**
614 : : * gi_callable_info_invoke:
615 : : * @info: a #GICallableInfo
616 : : * @function: function pointer to call
617 : : * @in_args: (array length=n_in_args): array of ‘in’ arguments
618 : : * @n_in_args: number of arguments in @in_args
619 : : * @out_args: (array length=n_out_args): array of ‘out’ arguments allocated by
620 : : * the caller, to be populated with outputted values
621 : : * @n_out_args: number of arguments in @out_args
622 : : * @return_value: (out caller-allocates) (not optional) (nullable): return
623 : : * location for the return value from the callable; `NULL` may be returned if
624 : : * the callable returns that
625 : : * @error: return location for a [type@GLib.Error], or `NULL`
626 : : *
627 : : * Invoke the given `GICallableInfo` by calling the given @function pointer.
628 : : *
629 : : * The set of arguments passed to @function will be constructed according to the
630 : : * introspected type of the `GICallableInfo`, using @in_args, @out_args
631 : : * and @error.
632 : : *
633 : : * Returns: `TRUE` if the callable was executed successfully and didn’t throw
634 : : * a [type@GLib.Error]; `FALSE` if @error is set
635 : : * Since: 2.80
636 : : */
637 : : gboolean
638 : 1 : gi_callable_info_invoke (GICallableInfo *info,
639 : : void *function,
640 : : const GIArgument *in_args,
641 : : size_t n_in_args,
642 : : GIArgument *out_args,
643 : : size_t n_out_args,
644 : : GIArgument *return_value,
645 : : GError **error)
646 : : {
647 : : ffi_cif cif;
648 : : ffi_type *rtype;
649 : : ffi_type **atypes;
650 : : GITypeInfo *tinfo;
651 : : GITypeInfo *rinfo;
652 : : GITypeTag rtag;
653 : : GIArgInfo *ainfo;
654 : : unsigned int n_args, n_invoke_args, in_pos, out_pos, i;
655 : : void **args;
656 : 1 : gboolean success = FALSE;
657 : 1 : GError *local_error = NULL;
658 : 1 : void *error_address = &local_error;
659 : : GIFFIReturnValue ffi_return_value;
660 : : void *return_value_p; /* Will point inside the union return_value */
661 : : gboolean is_method, throws;
662 : :
663 : 1 : rinfo = gi_callable_info_get_return_type ((GICallableInfo *)info);
664 : 1 : rtype = gi_type_info_get_ffi_type (rinfo);
665 : 1 : rtag = gi_type_info_get_tag(rinfo);
666 : 1 : is_method = gi_callable_info_is_method (info);
667 : 1 : throws = gi_callable_info_can_throw_gerror (info);
668 : :
669 : 1 : in_pos = 0;
670 : 1 : out_pos = 0;
671 : :
672 : 1 : n_args = gi_callable_info_get_n_args ((GICallableInfo *)info);
673 [ - + ]: 1 : if (is_method)
674 : : {
675 [ # # ]: 0 : if (n_in_args == 0)
676 : : {
677 : 0 : g_set_error (error,
678 : : GI_INVOKE_ERROR,
679 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
680 : : "Too few \"in\" arguments (handling this)");
681 : 0 : goto out;
682 : : }
683 : 0 : n_invoke_args = n_args+1;
684 : 0 : in_pos++;
685 : : }
686 : : else
687 : 1 : n_invoke_args = n_args;
688 : :
689 [ + - ]: 1 : if (throws)
690 : : /* Add an argument for the GError */
691 : 1 : n_invoke_args ++;
692 : :
693 : 1 : atypes = g_alloca (sizeof (ffi_type*) * n_invoke_args);
694 : 1 : args = g_alloca (sizeof (void *) * n_invoke_args);
695 : :
696 [ - + ]: 1 : if (is_method)
697 : : {
698 : 0 : atypes[0] = &ffi_type_pointer;
699 : 0 : args[0] = (void *) &in_args[0];
700 : : }
701 [ + + ]: 2 : for (i = 0; i < n_args; i++)
702 : : {
703 : 1 : int offset = (is_method ? 1 : 0);
704 : 1 : ainfo = gi_callable_info_get_arg ((GICallableInfo *)info, i);
705 [ + - - - ]: 1 : switch (gi_arg_info_get_direction (ainfo))
706 : : {
707 : 1 : case GI_DIRECTION_IN:
708 : 1 : tinfo = gi_arg_info_get_type_info (ainfo);
709 : 1 : atypes[i+offset] = gi_type_info_get_ffi_type (tinfo);
710 : 1 : gi_base_info_unref ((GIBaseInfo *)ainfo);
711 : 1 : gi_base_info_unref ((GIBaseInfo *)tinfo);
712 : :
713 [ - + ]: 1 : if (in_pos >= n_in_args)
714 : : {
715 : 0 : g_set_error (error,
716 : : GI_INVOKE_ERROR,
717 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
718 : : "Too few \"in\" arguments (handling in)");
719 : 0 : goto out;
720 : : }
721 : :
722 : 1 : args[i+offset] = (void *)&in_args[in_pos];
723 : 1 : in_pos++;
724 : :
725 : 1 : break;
726 : 0 : case GI_DIRECTION_OUT:
727 : 0 : atypes[i+offset] = &ffi_type_pointer;
728 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
729 : :
730 [ # # ]: 0 : if (out_pos >= n_out_args)
731 : : {
732 : 0 : g_set_error (error,
733 : : GI_INVOKE_ERROR,
734 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
735 : : "Too few \"out\" arguments (handling out)");
736 : 0 : goto out;
737 : : }
738 : :
739 : 0 : args[i+offset] = (void *)&out_args[out_pos];
740 : 0 : out_pos++;
741 : 0 : break;
742 : 0 : case GI_DIRECTION_INOUT:
743 : 0 : atypes[i+offset] = &ffi_type_pointer;
744 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
745 : :
746 [ # # ]: 0 : if (in_pos >= n_in_args)
747 : : {
748 : 0 : g_set_error (error,
749 : : GI_INVOKE_ERROR,
750 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
751 : : "Too few \"in\" arguments (handling inout)");
752 : 0 : goto out;
753 : : }
754 : :
755 [ # # ]: 0 : if (out_pos >= n_out_args)
756 : : {
757 : 0 : g_set_error (error,
758 : : GI_INVOKE_ERROR,
759 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
760 : : "Too few \"out\" arguments (handling inout)");
761 : 0 : goto out;
762 : : }
763 : :
764 : 0 : args[i+offset] = (void *)&in_args[in_pos];
765 : 0 : in_pos++;
766 : 0 : out_pos++;
767 : 0 : break;
768 : 0 : default:
769 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
770 : : g_assert_not_reached ();
771 : : }
772 : : }
773 : :
774 [ + - ]: 1 : if (throws)
775 : : {
776 : 1 : args[n_invoke_args - 1] = &error_address;
777 : 1 : atypes[n_invoke_args - 1] = &ffi_type_pointer;
778 : : }
779 : :
780 [ - + ]: 1 : if (in_pos < n_in_args)
781 : : {
782 : 0 : g_set_error (error,
783 : : GI_INVOKE_ERROR,
784 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
785 : : "Too many \"in\" arguments (at end)");
786 : 0 : goto out;
787 : : }
788 [ - + ]: 1 : if (out_pos < n_out_args)
789 : : {
790 : 0 : g_set_error (error,
791 : : GI_INVOKE_ERROR,
792 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
793 : : "Too many \"out\" arguments (at end)");
794 : 0 : goto out;
795 : : }
796 : :
797 [ - + ]: 1 : if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) != FFI_OK)
798 : 0 : goto out;
799 : :
800 : 1 : g_return_val_if_fail (return_value, FALSE);
801 : : /* See comment for GIFFIReturnValue above */
802 [ - - - + ]: 1 : switch (rtag)
803 : : {
804 : 0 : case GI_TYPE_TAG_FLOAT:
805 : 0 : return_value_p = &ffi_return_value.v_float;
806 : 0 : break;
807 : 0 : case GI_TYPE_TAG_DOUBLE:
808 : 0 : return_value_p = &ffi_return_value.v_double;
809 : 0 : break;
810 : 0 : case GI_TYPE_TAG_INT64:
811 : : case GI_TYPE_TAG_UINT64:
812 : 0 : return_value_p = &ffi_return_value.v_uint64;
813 : 0 : break;
814 : 1 : default:
815 : 1 : return_value_p = &ffi_return_value.v_long;
816 : : }
817 : 1 : ffi_call (&cif, function, return_value_p, args);
818 : :
819 [ + - ]: 1 : if (local_error)
820 : : {
821 : 1 : g_propagate_error (error, local_error);
822 : 1 : success = FALSE;
823 : : }
824 : : else
825 : : {
826 : 0 : gi_type_info_extract_ffi_return_value (rinfo, &ffi_return_value, return_value);
827 : 0 : success = TRUE;
828 : : }
829 : 1 : out:
830 : 1 : gi_base_info_unref ((GIBaseInfo *)rinfo);
831 : 1 : return success;
832 : : }
833 : :
834 : : void
835 : 7 : gi_callable_info_class_init (gpointer g_class,
836 : : gpointer class_data)
837 : : {
838 : 7 : GIBaseInfoClass *info_class = g_class;
839 : :
840 : 7 : info_class->info_type = GI_INFO_TYPE_CALLABLE;
841 : 7 : }
|