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 : : #define GET_BLOB(Type, rinfo) ((Type *) &rinfo->typelib->data[rinfo->offset])
39 : :
40 : : /* GICallableInfo functions */
41 : :
42 : : /**
43 : : * GICallableInfo:
44 : : *
45 : : * `GICallableInfo` represents an entity which is callable.
46 : : *
47 : : * Examples of callable are:
48 : : *
49 : : * - functions ([class@GIRepository.FunctionInfo])
50 : : * - virtual functions ([class@GIRepository.VFuncInfo])
51 : : * - callbacks ([class@GIRepository.CallbackInfo]).
52 : : *
53 : : * A callable has a list of arguments ([class@GIRepository.ArgInfo]), a return
54 : : * type, direction and a flag which decides if it returns `NULL`.
55 : : *
56 : : * Since: 2.80
57 : : */
58 : :
59 : : static uint32_t
60 : 47 : signature_offset (GICallableInfo *info)
61 : : {
62 : 47 : GIRealInfo *rinfo = (GIRealInfo*)info;
63 : 47 : int sigoff = -1;
64 : :
65 : 47 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info))
66 : : {
67 : 41 : case GI_INFO_TYPE_FUNCTION:
68 : 41 : sigoff = G_STRUCT_OFFSET (FunctionBlob, signature);
69 : 41 : break;
70 : 2 : case GI_INFO_TYPE_VFUNC:
71 : 2 : sigoff = G_STRUCT_OFFSET (VFuncBlob, signature);
72 : 2 : break;
73 : 1 : case GI_INFO_TYPE_CALLBACK:
74 : 1 : sigoff = G_STRUCT_OFFSET (CallbackBlob, signature);
75 : 1 : break;
76 : 3 : case GI_INFO_TYPE_SIGNAL:
77 : 3 : sigoff = G_STRUCT_OFFSET (SignalBlob, signature);
78 : 3 : break;
79 : 0 : default:
80 : : g_assert_not_reached ();
81 : : }
82 : 47 : if (sigoff >= 0)
83 : 47 : return *(uint32_t *)&rinfo->typelib->data[rinfo->offset + (unsigned) sigoff];
84 : 0 : return 0;
85 : : }
86 : :
87 : : /**
88 : : * gi_callable_info_can_throw_gerror:
89 : : * @info: a #GICallableInfo
90 : : *
91 : : * Whether the callable can throw a [type@GLib.Error]
92 : : *
93 : : * Returns: `TRUE` if this `GICallableInfo` can throw a [type@GLib.Error]
94 : : * Since: 2.80
95 : : */
96 : : gboolean
97 : 8 : gi_callable_info_can_throw_gerror (GICallableInfo *info)
98 : : {
99 : 8 : GIRealInfo *rinfo = (GIRealInfo*)info;
100 : : SignatureBlob *signature;
101 : :
102 : 8 : signature = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
103 : 8 : if (signature->throws)
104 : 6 : return TRUE;
105 : :
106 : : /* Functions and VFuncs store "throws" in their own blobs.
107 : : * This info was additionally added to the SignatureBlob
108 : : * to support the other callables. For Functions and VFuncs,
109 : : * also check their legacy flag for compatibility.
110 : : */
111 : 2 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info)) {
112 : 2 : case GI_INFO_TYPE_FUNCTION:
113 : : {
114 : : FunctionBlob *blob;
115 : 2 : blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
116 : 2 : return blob->throws;
117 : : }
118 : 0 : case GI_INFO_TYPE_VFUNC:
119 : : {
120 : : VFuncBlob *blob;
121 : 0 : blob = (VFuncBlob *)&rinfo->typelib->data[rinfo->offset];
122 : 0 : return blob->throws;
123 : : }
124 : 0 : case GI_INFO_TYPE_CALLBACK:
125 : : case GI_INFO_TYPE_SIGNAL:
126 : 0 : return FALSE;
127 : 0 : default:
128 : : g_assert_not_reached ();
129 : : }
130 : : }
131 : :
132 : : /**
133 : : * gi_callable_info_is_method:
134 : : * @info: a #GICallableInfo
135 : : *
136 : : * Determines if the callable info is a method.
137 : : *
138 : : * For [class@GIRepository.SignalInfo]s, this is always true, and for
139 : : * [class@GIRepository.CallbackInfo]s always false.
140 : : * For [class@GIRepository.FunctionInfo]s this looks at the
141 : : * `GI_FUNCTION_IS_METHOD` flag on the [class@GIRepository.FunctionInfo].
142 : : * For [class@GIRepository.VFuncInfo]s this is true when the virtual function
143 : : * has an instance parameter.
144 : : *
145 : : * Concretely, this function returns whether
146 : : * [method@GIRepository.CallableInfo.get_n_args] matches the number of arguments
147 : : * in the raw C method. For methods, there is one more C argument than is
148 : : * exposed by introspection: the `self` or `this` object.
149 : : *
150 : : * Returns: `TRUE` if @info is a method, `FALSE` otherwise
151 : : * Since: 2.80
152 : : */
153 : : gboolean
154 : 12 : gi_callable_info_is_method (GICallableInfo *info)
155 : : {
156 : 12 : GIRealInfo *rinfo = (GIRealInfo*)info;
157 : 12 : switch (gi_base_info_get_info_type ((GIBaseInfo *) info)) {
158 : 9 : case GI_INFO_TYPE_FUNCTION:
159 : : {
160 : : FunctionBlob *blob;
161 : 9 : blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
162 : 9 : return (!blob->constructor && !blob->is_static);
163 : : }
164 : 1 : case GI_INFO_TYPE_VFUNC:
165 : : {
166 : : VFuncBlob *blob;
167 : 1 : blob = (VFuncBlob *) &rinfo->typelib->data[rinfo->offset];
168 : 1 : return !blob->is_static;
169 : : }
170 : 1 : case GI_INFO_TYPE_SIGNAL:
171 : 1 : return TRUE;
172 : 1 : case GI_INFO_TYPE_CALLBACK:
173 : 1 : return FALSE;
174 : 0 : default:
175 : : g_assert_not_reached ();
176 : : }
177 : : }
178 : :
179 : : /**
180 : : * gi_callable_info_get_return_type:
181 : : * @info: a #GICallableInfo
182 : : *
183 : : * Obtain the return type of a callable item as a [class@GIRepository.TypeInfo].
184 : : *
185 : : * If the callable doesn’t return anything, a [class@GIRepository.TypeInfo] of
186 : : * type [enum@GIRepository.TypeTag.VOID] will be returned.
187 : : *
188 : : * Returns: (transfer full): the [class@GIRepository.TypeInfo]. Free the struct
189 : : * by calling [method@GIRepository.BaseInfo.unref] when done.
190 : : * Since: 2.80
191 : : */
192 : : GITypeInfo *
193 : 8 : gi_callable_info_get_return_type (GICallableInfo *info)
194 : : {
195 : 8 : GIRealInfo *rinfo = (GIRealInfo *)info;
196 : : uint32_t offset;
197 : :
198 : 8 : g_return_val_if_fail (info != NULL, NULL);
199 : 8 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), NULL);
200 : :
201 : 8 : offset = signature_offset (info);
202 : :
203 : 8 : return gi_type_info_new ((GIBaseInfo*)info, rinfo->typelib, offset);
204 : : }
205 : :
206 : : /**
207 : : * gi_callable_info_load_return_type:
208 : : * @info: a #GICallableInfo
209 : : * @type: (out caller-allocates): Initialized with return type of @info
210 : : *
211 : : * Obtain information about a return value of callable; this
212 : : * function is a variant of [method@GIRepository.CallableInfo.get_return_type]
213 : : * designed for stack allocation.
214 : : *
215 : : * The initialized @type must not be referenced after @info is deallocated.
216 : : *
217 : : * Once you are done with @type, it must be cleared using
218 : : * [method@GIRepository.BaseInfo.clear].
219 : : *
220 : : * Since: 2.80
221 : : */
222 : : void
223 : 1 : gi_callable_info_load_return_type (GICallableInfo *info,
224 : : GITypeInfo *type)
225 : : {
226 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
227 : : uint32_t offset;
228 : :
229 : 1 : g_return_if_fail (info != NULL);
230 : 1 : g_return_if_fail (GI_IS_CALLABLE_INFO (info));
231 : :
232 : 1 : offset = signature_offset (info);
233 : :
234 : 1 : gi_type_info_init (type, (GIBaseInfo*)info, rinfo->typelib, offset);
235 : : }
236 : :
237 : : /**
238 : : * gi_callable_info_may_return_null:
239 : : * @info: a #GICallableInfo
240 : : *
241 : : * See if a callable could return `NULL`.
242 : : *
243 : : * Returns: `TRUE` if callable could return `NULL`
244 : : * Since: 2.80
245 : : */
246 : : gboolean
247 : 1 : gi_callable_info_may_return_null (GICallableInfo *info)
248 : : {
249 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
250 : : SignatureBlob *blob;
251 : :
252 : 1 : g_return_val_if_fail (info != NULL, FALSE);
253 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), FALSE);
254 : :
255 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
256 : :
257 : 1 : return blob->may_return_null;
258 : : }
259 : :
260 : : /**
261 : : * gi_callable_info_skip_return:
262 : : * @info: a #GICallableInfo
263 : : *
264 : : * See if a callable’s return value is only useful in C.
265 : : *
266 : : * Returns: `TRUE` if return value is only useful in C.
267 : : * Since: 2.80
268 : : */
269 : : gboolean
270 : 1 : gi_callable_info_skip_return (GICallableInfo *info)
271 : : {
272 : 1 : GIRealInfo *rinfo = (GIRealInfo *)info;
273 : : SignatureBlob *blob;
274 : :
275 : 1 : g_return_val_if_fail (info != NULL, FALSE);
276 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), FALSE);
277 : :
278 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
279 : :
280 : 1 : return blob->skip_return;
281 : : }
282 : :
283 : : /**
284 : : * gi_callable_info_get_caller_owns:
285 : : * @info: a #GICallableInfo
286 : : *
287 : : * See whether the caller owns the return value of this callable.
288 : : *
289 : : * [type@GIRepository.Transfer] contains a list of possible transfer values.
290 : : *
291 : : * Returns: the transfer mode for the return value of the callable
292 : : * Since: 2.80
293 : : */
294 : : GITransfer
295 : 1 : gi_callable_info_get_caller_owns (GICallableInfo *info)
296 : : {
297 : 1 : GIRealInfo *rinfo = (GIRealInfo*) info;
298 : : SignatureBlob *blob;
299 : :
300 : 1 : g_return_val_if_fail (info != NULL, GI_TRANSFER_NOTHING);
301 : 1 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), GI_TRANSFER_NOTHING);
302 : :
303 : 1 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
304 : :
305 : 1 : if (blob->caller_owns_return_value)
306 : 0 : return GI_TRANSFER_EVERYTHING;
307 : 1 : else if (blob->caller_owns_return_container)
308 : 0 : return GI_TRANSFER_CONTAINER;
309 : : else
310 : 1 : return GI_TRANSFER_NOTHING;
311 : : }
312 : :
313 : : /**
314 : : * gi_callable_info_get_instance_ownership_transfer:
315 : : * @info: a #GICallableInfo
316 : : *
317 : : * Obtains the ownership transfer for the instance argument.
318 : : *
319 : : * [type@GIRepository.Transfer] contains a list of possible transfer values.
320 : : *
321 : : * Returns: the transfer mode of the instance argument
322 : : * Since: 2.80
323 : : */
324 : : GITransfer
325 : 3 : gi_callable_info_get_instance_ownership_transfer (GICallableInfo *info)
326 : : {
327 : 3 : GIRealInfo *rinfo = (GIRealInfo*) info;
328 : : SignatureBlob *blob;
329 : :
330 : 3 : g_return_val_if_fail (info != NULL, GI_TRANSFER_NOTHING);
331 : 3 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), GI_TRANSFER_NOTHING);
332 : :
333 : 3 : blob = (SignatureBlob *)&rinfo->typelib->data[signature_offset (info)];
334 : :
335 : 3 : if (blob->instance_transfer_ownership)
336 : 1 : return GI_TRANSFER_EVERYTHING;
337 : : else
338 : 2 : return GI_TRANSFER_NOTHING;
339 : : }
340 : :
341 : : /**
342 : : * gi_callable_info_get_n_args:
343 : : * @info: a #GICallableInfo
344 : : *
345 : : * Obtain the number of arguments (both ‘in’ and ‘out’) for this callable.
346 : : *
347 : : * Returns: The number of arguments this callable expects.
348 : : * Since: 2.80
349 : : */
350 : : unsigned int
351 : 7 : gi_callable_info_get_n_args (GICallableInfo *info)
352 : : {
353 : 7 : GIRealInfo *rinfo = (GIRealInfo *)info;
354 : : uint32_t offset;
355 : : SignatureBlob *blob;
356 : :
357 : 7 : g_return_val_if_fail (info != NULL, 0);
358 : 7 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), 0);
359 : :
360 : 7 : offset = signature_offset (info);
361 : 7 : blob = (SignatureBlob *)&rinfo->typelib->data[offset];
362 : :
363 : 7 : return blob->n_arguments;
364 : : }
365 : :
366 : : /**
367 : : * gi_callable_info_get_arg:
368 : : * @info: a #GICallableInfo
369 : : * @n: the argument index to fetch
370 : : *
371 : : * Obtain information about a particular argument of this callable.
372 : : *
373 : : * Returns: (transfer full): the [class@GIRepository.ArgInfo]. Free it with
374 : : * [method@GIRepository.BaseInfo.unref] when done.
375 : : * Since: 2.80
376 : : */
377 : : GIArgInfo *
378 : 10 : gi_callable_info_get_arg (GICallableInfo *info,
379 : : unsigned int n)
380 : : {
381 : 10 : GIRealInfo *rinfo = (GIRealInfo *)info;
382 : : Header *header;
383 : : uint32_t offset;
384 : :
385 : 10 : g_return_val_if_fail (info != NULL, NULL);
386 : 10 : g_return_val_if_fail (GI_IS_CALLABLE_INFO (info), NULL);
387 : 10 : g_return_val_if_fail (n <= G_MAXUINT16, NULL);
388 : :
389 : 10 : offset = signature_offset (info);
390 : 10 : header = (Header *)rinfo->typelib->data;
391 : :
392 : 10 : return (GIArgInfo *) gi_base_info_new (GI_INFO_TYPE_ARG, (GIBaseInfo*)info, rinfo->typelib,
393 : 10 : offset + header->signature_blob_size + n * header->arg_blob_size);
394 : : }
395 : :
396 : : /**
397 : : * gi_callable_info_load_arg:
398 : : * @info: a #GICallableInfo
399 : : * @n: the argument index to fetch
400 : : * @arg: (out caller-allocates): Initialize with argument number @n
401 : : *
402 : : * Obtain information about a particular argument of this callable; this
403 : : * function is a variant of [method@GIRepository.CallableInfo.get_arg] designed
404 : : * for stack allocation.
405 : : *
406 : : * The initialized @arg must not be referenced after @info is deallocated.
407 : : *
408 : : * Once you are done with @arg, it must be cleared using
409 : : * [method@GIRepository.BaseInfo.clear].
410 : : *
411 : : * Since: 2.80
412 : : */
413 : : void
414 : 5 : gi_callable_info_load_arg (GICallableInfo *info,
415 : : unsigned int n,
416 : : GIArgInfo *arg)
417 : : {
418 : 5 : GIRealInfo *rinfo = (GIRealInfo *)info;
419 : : Header *header;
420 : : uint32_t offset;
421 : :
422 : 5 : g_return_if_fail (info != NULL);
423 : 5 : g_return_if_fail (GI_IS_CALLABLE_INFO (info));
424 : 5 : g_return_if_fail (n <= G_MAXUINT16);
425 : :
426 : 5 : offset = signature_offset (info);
427 : 5 : header = (Header *)rinfo->typelib->data;
428 : :
429 : 5 : gi_info_init ((GIRealInfo*)arg, GI_TYPE_ARG_INFO, rinfo->repository, (GIBaseInfo*)info, rinfo->typelib,
430 : 5 : offset + header->signature_blob_size + n * header->arg_blob_size);
431 : : }
432 : :
433 : : /**
434 : : * gi_callable_info_get_return_attribute:
435 : : * @info: a #GICallableInfo
436 : : * @name: a freeform string naming an attribute
437 : : *
438 : : * Retrieve an arbitrary attribute associated with the return value.
439 : : *
440 : : * Returns: (nullable): The value of the attribute, or `NULL` if no such
441 : : * attribute exists
442 : : * Since: 2.80
443 : : */
444 : : const char *
445 : 1 : gi_callable_info_get_return_attribute (GICallableInfo *info,
446 : : const char *name)
447 : : {
448 : 1 : GIAttributeIter iter = GI_ATTRIBUTE_ITER_INIT;
449 : : const char *curname, *curvalue;
450 : 1 : while (gi_callable_info_iterate_return_attributes (info, &iter, &curname, &curvalue))
451 : : {
452 : 0 : if (g_strcmp0 (name, curname) == 0)
453 : 0 : return (const char*) curvalue;
454 : : }
455 : :
456 : 1 : return NULL;
457 : : }
458 : :
459 : : /**
460 : : * gi_callable_info_iterate_return_attributes:
461 : : * @info: a #GICallableInfo
462 : : * @iterator: (inout): a [type@GIRepository.AttributeIter] structure, must be
463 : : * initialized; see below
464 : : * @name: (out) (transfer none): Returned name, must not be freed
465 : : * @value: (out) (transfer none): Returned name, must not be freed
466 : : *
467 : : * Iterate over all attributes associated with the return value.
468 : : *
469 : : * The iterator structure is typically stack allocated, and must have its
470 : : * first member initialized to `NULL`.
471 : : *
472 : : * Both the @name and @value should be treated as constants
473 : : * and must not be freed.
474 : : *
475 : : * See [method@GIRepository.BaseInfo.iterate_attributes] for an example of how
476 : : * to use a similar API.
477 : : *
478 : : * Returns: `TRUE` if there are more attributes
479 : : * Since: 2.80
480 : : */
481 : : gboolean
482 : 2 : gi_callable_info_iterate_return_attributes (GICallableInfo *info,
483 : : GIAttributeIter *iterator,
484 : : const char **name,
485 : : const char **value)
486 : : {
487 : 2 : GIRealInfo *rinfo = (GIRealInfo *)info;
488 : 2 : Header *header = (Header *)rinfo->typelib->data;
489 : : AttributeBlob *next, *after;
490 : : uint32_t blob_offset;
491 : :
492 : 2 : after = (AttributeBlob *) &rinfo->typelib->data[header->attributes +
493 : 2 : header->n_attributes * header->attribute_blob_size];
494 : :
495 : 2 : blob_offset = signature_offset (info);
496 : :
497 : 2 : if (iterator->data != NULL)
498 : 0 : next = (AttributeBlob *) iterator->data;
499 : : else
500 : 2 : next = _attribute_blob_find_first ((GIBaseInfo *) info, blob_offset);
501 : :
502 : 2 : if (next == NULL || next->offset != blob_offset || next >= after)
503 : 2 : return FALSE;
504 : :
505 : 0 : *name = gi_typelib_get_string (rinfo->typelib, next->name);
506 : 0 : *value = gi_typelib_get_string (rinfo->typelib, next->value);
507 : 0 : iterator->data = next + 1;
508 : :
509 : 0 : return TRUE;
510 : : }
511 : :
512 : : /**
513 : : * gi_type_tag_extract_ffi_return_value:
514 : : * @return_tag: [type@GIRepository.TypeTag] of the return value
515 : : * @interface_type: [type@GObject.Type] of the underlying interface type
516 : : * @ffi_value: pointer to [type@GIRepository.FFIReturnValue] union containing
517 : : * the return value from `ffi_call()`
518 : : * @arg: (out caller-allocates): pointer to an allocated
519 : : * [class@GIRepository.Argument]
520 : : *
521 : : * Extract the correct bits from an `ffi_arg` return value into
522 : : * [class@GIRepository.Argument].
523 : : *
524 : : * See: https://bugzilla.gnome.org/show_bug.cgi?id=665152
525 : : *
526 : : * Also see [`ffi_call()`](man:ffi_call(3)): the storage requirements for return
527 : : * values are ‘special’.
528 : : *
529 : : * The @interface_type argument only applies if @return_tag is
530 : : * `GI_TYPE_TAG_INTERFACE`. Otherwise it is ignored.
531 : : *
532 : : * Since: 2.80
533 : : */
534 : : void
535 : 0 : gi_type_tag_extract_ffi_return_value (GITypeTag return_tag,
536 : : GType interface_type,
537 : : GIFFIReturnValue *ffi_value,
538 : : GIArgument *arg)
539 : : {
540 : 0 : switch (return_tag) {
541 : 0 : case GI_TYPE_TAG_INT8:
542 : 0 : arg->v_int8 = (int8_t) ffi_value->v_long;
543 : 0 : break;
544 : 0 : case GI_TYPE_TAG_UINT8:
545 : 0 : arg->v_uint8 = (uint8_t) ffi_value->v_ulong;
546 : 0 : break;
547 : 0 : case GI_TYPE_TAG_INT16:
548 : 0 : arg->v_int16 = (int16_t) ffi_value->v_long;
549 : 0 : break;
550 : 0 : case GI_TYPE_TAG_UINT16:
551 : 0 : arg->v_uint16 = (uint16_t) ffi_value->v_ulong;
552 : 0 : break;
553 : 0 : case GI_TYPE_TAG_INT32:
554 : 0 : arg->v_int32 = (int32_t) ffi_value->v_long;
555 : 0 : break;
556 : 0 : case GI_TYPE_TAG_UINT32:
557 : : case GI_TYPE_TAG_BOOLEAN:
558 : : case GI_TYPE_TAG_UNICHAR:
559 : 0 : arg->v_uint32 = (uint32_t) ffi_value->v_ulong;
560 : 0 : break;
561 : 0 : case GI_TYPE_TAG_INT64:
562 : 0 : arg->v_int64 = (int64_t) ffi_value->v_int64;
563 : 0 : break;
564 : 0 : case GI_TYPE_TAG_UINT64:
565 : 0 : arg->v_uint64 = (uint64_t) ffi_value->v_uint64;
566 : 0 : break;
567 : 0 : case GI_TYPE_TAG_FLOAT:
568 : 0 : arg->v_float = ffi_value->v_float;
569 : 0 : break;
570 : 0 : case GI_TYPE_TAG_DOUBLE:
571 : 0 : arg->v_double = ffi_value->v_double;
572 : 0 : break;
573 : 0 : case GI_TYPE_TAG_INTERFACE:
574 : 0 : if (interface_type == GI_TYPE_ENUM_INFO ||
575 : 0 : interface_type == GI_TYPE_FLAGS_INFO)
576 : 0 : arg->v_int32 = (int32_t) ffi_value->v_long;
577 : : else
578 : 0 : arg->v_pointer = (void *) ffi_value->v_pointer;
579 : 0 : break;
580 : 0 : default:
581 : 0 : arg->v_pointer = (void *) ffi_value->v_pointer;
582 : 0 : break;
583 : : }
584 : 0 : }
585 : :
586 : : /**
587 : : * gi_type_info_extract_ffi_return_value:
588 : : * @return_info: [type@GIRepository.TypeInfo] describing the return type
589 : : * @ffi_value: pointer to [type@GIRepository.FFIReturnValue] union containing
590 : : * the return value from `ffi_call()`
591 : : * @arg: (out caller-allocates): pointer to an allocated
592 : : * [class@GIRepository.Argument]
593 : : *
594 : : * Extract the correct bits from an `ffi_arg` return value into
595 : : * [class@GIRepository.Argument].
596 : : *
597 : : * See: https://bugzilla.gnome.org/show_bug.cgi?id=665152
598 : : *
599 : : * Also see [`ffi_call()`](man:ffi_call(3)): the storage requirements for return
600 : : * values are ‘special’.
601 : : *
602 : : * Since: 2.80
603 : : */
604 : : void
605 : 0 : gi_type_info_extract_ffi_return_value (GITypeInfo *return_info,
606 : : GIFFIReturnValue *ffi_value,
607 : : GIArgument *arg)
608 : : {
609 : 0 : GITypeTag return_tag = gi_type_info_get_tag (return_info);
610 : 0 : GType interface_type = G_TYPE_INVALID;
611 : :
612 : 0 : if (return_tag == GI_TYPE_TAG_INTERFACE)
613 : : {
614 : 0 : GIBaseInfo *interface_info = gi_type_info_get_interface (return_info);
615 : 0 : interface_type = G_TYPE_FROM_INSTANCE (interface_info);
616 : 0 : gi_base_info_unref (interface_info);
617 : : }
618 : :
619 : 0 : gi_type_tag_extract_ffi_return_value (return_tag, interface_type,
620 : : ffi_value, arg);
621 : 0 : }
622 : :
623 : : /**
624 : : * gi_callable_info_invoke:
625 : : * @info: a #GICallableInfo
626 : : * @function: function pointer to call
627 : : * @in_args: (array length=n_in_args): array of ‘in’ arguments
628 : : * @n_in_args: number of arguments in @in_args
629 : : * @out_args: (array length=n_out_args): array of ‘out’ arguments allocated by
630 : : * the caller, to be populated with outputted values
631 : : * @n_out_args: number of arguments in @out_args
632 : : * @return_value: (out caller-allocates) (not optional) (nullable): return
633 : : * location for the return value from the callable; `NULL` may be returned if
634 : : * the callable returns that
635 : : * @error: return location for a [type@GLib.Error], or `NULL`
636 : : *
637 : : * Invoke the given `GICallableInfo` by calling the given @function pointer.
638 : : *
639 : : * The set of arguments passed to @function will be constructed according to the
640 : : * introspected type of the `GICallableInfo`, using @in_args, @out_args
641 : : * and @error.
642 : : *
643 : : * Returns: `TRUE` if the callable was executed successfully and didn’t throw
644 : : * a [type@GLib.Error]; `FALSE` if @error is set
645 : : * Since: 2.80
646 : : */
647 : : gboolean
648 : 1 : gi_callable_info_invoke (GICallableInfo *info,
649 : : void *function,
650 : : const GIArgument *in_args,
651 : : size_t n_in_args,
652 : : GIArgument *out_args,
653 : : size_t n_out_args,
654 : : GIArgument *return_value,
655 : : GError **error)
656 : : {
657 : : ffi_cif cif;
658 : : ffi_type *rtype;
659 : : ffi_type **atypes;
660 : : GITypeInfo *tinfo;
661 : : GITypeInfo *rinfo;
662 : : GITypeTag rtag;
663 : : GIArgInfo *ainfo;
664 : : unsigned int n_args, n_invoke_args, in_pos, out_pos, i;
665 : : void **args;
666 : 1 : gboolean success = FALSE;
667 : 1 : GError *local_error = NULL;
668 : 1 : void *error_address = &local_error;
669 : : GIFFIReturnValue ffi_return_value;
670 : : void *return_value_p; /* Will point inside the union return_value */
671 : : gboolean is_method, throws;
672 : :
673 : 1 : rinfo = gi_callable_info_get_return_type ((GICallableInfo *)info);
674 : 1 : rtype = gi_type_info_get_ffi_type (rinfo);
675 : 1 : rtag = gi_type_info_get_tag(rinfo);
676 : 1 : is_method = gi_callable_info_is_method (info);
677 : 1 : throws = gi_callable_info_can_throw_gerror (info);
678 : :
679 : 1 : in_pos = 0;
680 : 1 : out_pos = 0;
681 : :
682 : 1 : n_args = gi_callable_info_get_n_args ((GICallableInfo *)info);
683 : 1 : if (is_method)
684 : : {
685 : 0 : if (n_in_args == 0)
686 : : {
687 : 0 : g_set_error (error,
688 : : GI_INVOKE_ERROR,
689 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
690 : : "Too few \"in\" arguments (handling this)");
691 : 0 : goto out;
692 : : }
693 : 0 : n_invoke_args = n_args+1;
694 : 0 : in_pos++;
695 : : }
696 : : else
697 : 1 : n_invoke_args = n_args;
698 : :
699 : 1 : if (throws)
700 : : /* Add an argument for the GError */
701 : 1 : n_invoke_args ++;
702 : :
703 : 1 : atypes = g_alloca (sizeof (ffi_type*) * n_invoke_args);
704 : 1 : args = g_alloca (sizeof (void *) * n_invoke_args);
705 : :
706 : 1 : if (is_method)
707 : : {
708 : 0 : atypes[0] = &ffi_type_pointer;
709 : 0 : args[0] = (void *) &in_args[0];
710 : : }
711 : 2 : for (i = 0; i < n_args; i++)
712 : : {
713 : 1 : size_t offset = (is_method ? 1 : 0);
714 : 1 : ainfo = gi_callable_info_get_arg ((GICallableInfo *)info, i);
715 : 1 : switch (gi_arg_info_get_direction (ainfo))
716 : : {
717 : 1 : case GI_DIRECTION_IN:
718 : 1 : tinfo = gi_arg_info_get_type_info (ainfo);
719 : 1 : atypes[i+offset] = gi_type_info_get_ffi_type (tinfo);
720 : 1 : gi_base_info_unref ((GIBaseInfo *)ainfo);
721 : 1 : gi_base_info_unref ((GIBaseInfo *)tinfo);
722 : :
723 : 1 : if (in_pos >= n_in_args)
724 : : {
725 : 0 : g_set_error (error,
726 : : GI_INVOKE_ERROR,
727 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
728 : : "Too few \"in\" arguments (handling in)");
729 : 0 : goto out;
730 : : }
731 : :
732 : 1 : args[i+offset] = (void *)&in_args[in_pos];
733 : 1 : in_pos++;
734 : :
735 : 1 : break;
736 : 0 : case GI_DIRECTION_OUT:
737 : 0 : atypes[i+offset] = &ffi_type_pointer;
738 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
739 : :
740 : 0 : if (out_pos >= n_out_args)
741 : : {
742 : 0 : g_set_error (error,
743 : : GI_INVOKE_ERROR,
744 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
745 : : "Too few \"out\" arguments (handling out)");
746 : 0 : goto out;
747 : : }
748 : :
749 : 0 : args[i+offset] = (void *)&out_args[out_pos];
750 : 0 : out_pos++;
751 : 0 : break;
752 : 0 : case GI_DIRECTION_INOUT:
753 : 0 : atypes[i+offset] = &ffi_type_pointer;
754 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
755 : :
756 : 0 : if (in_pos >= n_in_args)
757 : : {
758 : 0 : g_set_error (error,
759 : : GI_INVOKE_ERROR,
760 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
761 : : "Too few \"in\" arguments (handling inout)");
762 : 0 : goto out;
763 : : }
764 : :
765 : 0 : if (out_pos >= n_out_args)
766 : : {
767 : 0 : g_set_error (error,
768 : : GI_INVOKE_ERROR,
769 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
770 : : "Too few \"out\" arguments (handling inout)");
771 : 0 : goto out;
772 : : }
773 : :
774 : 0 : args[i+offset] = (void *)&in_args[in_pos];
775 : 0 : in_pos++;
776 : 0 : out_pos++;
777 : 0 : break;
778 : 0 : default:
779 : 0 : gi_base_info_unref ((GIBaseInfo *)ainfo);
780 : : g_assert_not_reached ();
781 : : }
782 : : }
783 : :
784 : 1 : if (throws)
785 : : {
786 : 1 : args[n_invoke_args - 1] = &error_address;
787 : 1 : atypes[n_invoke_args - 1] = &ffi_type_pointer;
788 : : }
789 : :
790 : 1 : if (in_pos < n_in_args)
791 : : {
792 : 0 : g_set_error (error,
793 : : GI_INVOKE_ERROR,
794 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
795 : : "Too many \"in\" arguments (at end)");
796 : 0 : goto out;
797 : : }
798 : 1 : if (out_pos < n_out_args)
799 : : {
800 : 0 : g_set_error (error,
801 : : GI_INVOKE_ERROR,
802 : : GI_INVOKE_ERROR_ARGUMENT_MISMATCH,
803 : : "Too many \"out\" arguments (at end)");
804 : 0 : goto out;
805 : : }
806 : :
807 : 1 : if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) != FFI_OK)
808 : 0 : goto out;
809 : :
810 : 1 : g_return_val_if_fail (return_value, FALSE);
811 : : /* See comment for GIFFIReturnValue above */
812 : 1 : switch (rtag)
813 : : {
814 : 0 : case GI_TYPE_TAG_FLOAT:
815 : 0 : return_value_p = &ffi_return_value.v_float;
816 : 0 : break;
817 : 0 : case GI_TYPE_TAG_DOUBLE:
818 : 0 : return_value_p = &ffi_return_value.v_double;
819 : 0 : break;
820 : 0 : case GI_TYPE_TAG_INT64:
821 : : case GI_TYPE_TAG_UINT64:
822 : 0 : return_value_p = &ffi_return_value.v_uint64;
823 : 0 : break;
824 : 1 : default:
825 : 1 : return_value_p = &ffi_return_value.v_long;
826 : : }
827 : 1 : ffi_call (&cif, function, return_value_p, args);
828 : :
829 : 1 : if (local_error)
830 : : {
831 : 1 : g_propagate_error (error, local_error);
832 : 1 : success = FALSE;
833 : : }
834 : : else
835 : : {
836 : 0 : gi_type_info_extract_ffi_return_value (rinfo, &ffi_return_value, return_value);
837 : 0 : success = TRUE;
838 : : }
839 : 1 : out:
840 : 1 : gi_base_info_unref ((GIBaseInfo *)rinfo);
841 : 1 : return success;
842 : : }
843 : :
844 : : void
845 : 8 : gi_callable_info_class_init (gpointer g_class,
846 : : gpointer class_data)
847 : : {
848 : 8 : GIBaseInfoClass *info_class = g_class;
849 : :
850 : 8 : info_class->info_type = GI_INFO_TYPE_CALLABLE;
851 : 8 : }
852 : :
853 : : static GICallableInfo *
854 : 9 : get_method_callable_info_for_index (GIBaseInfo *rinfo,
855 : : unsigned int index)
856 : : {
857 : 9 : GIBaseInfo *container = rinfo->container;
858 : :
859 : 9 : g_assert (container);
860 : :
861 : 9 : if (GI_IS_OBJECT_INFO (container))
862 : 0 : return (GICallableInfo *) gi_object_info_get_method ((GIObjectInfo *) container, index);
863 : :
864 : 9 : if (GI_IS_INTERFACE_INFO (container))
865 : 9 : return (GICallableInfo *) gi_interface_info_get_method ((GIInterfaceInfo *) container,
866 : : index);
867 : :
868 : 0 : return NULL;
869 : : }
870 : :
871 : : static GICallableInfo *
872 : 9 : get_callable_info_for_index (GIBaseInfo *rinfo,
873 : : unsigned int index)
874 : : {
875 : 9 : if (!rinfo->container)
876 : : {
877 : 0 : GIBaseInfo *info = gi_info_from_entry (rinfo->repository, rinfo->typelib, index);
878 : :
879 : 0 : g_assert (GI_IS_CALLABLE_INFO (info));
880 : :
881 : 0 : return (GICallableInfo *) info;
882 : : }
883 : :
884 : 9 : return get_method_callable_info_for_index (rinfo, index);
885 : : }
886 : :
887 : : /**
888 : : * gi_callable_info_get_async_function
889 : : * @info: a callable info structure
890 : : *
891 : : * Gets the callable info for the callable's asynchronous version
892 : : *
893 : : * Returns: (transfer full) (nullable): a [class@GIRepository.CallableInfo] for the
894 : : * async function or `NULL` if not defined.
895 : : * Since: 2.84
896 : : */
897 : : GICallableInfo *
898 : 5 : gi_callable_info_get_async_function (GICallableInfo *info)
899 : : {
900 : 5 : GIBaseInfo *rinfo = (GIBaseInfo *) info;
901 : :
902 : 5 : switch (gi_base_info_get_info_type (rinfo))
903 : : {
904 : 5 : case GI_INFO_TYPE_FUNCTION:
905 : : {
906 : 5 : FunctionBlob *blob = GET_BLOB (FunctionBlob, rinfo);
907 : 5 : if (blob->is_async || blob->sync_or_async == ASYNC_SENTINEL)
908 : 1 : return NULL;
909 : :
910 : 4 : return get_callable_info_for_index (rinfo, blob->sync_or_async);
911 : : }
912 : 0 : case GI_INFO_TYPE_VFUNC:
913 : : {
914 : 0 : VFuncBlob *blob = GET_BLOB (VFuncBlob, rinfo);
915 : 0 : if (blob->is_async || blob->sync_or_async == ASYNC_SENTINEL)
916 : 0 : return NULL;
917 : :
918 : 0 : return get_method_callable_info_for_index (rinfo, blob->sync_or_async);
919 : : }
920 : 0 : case GI_INFO_TYPE_CALLBACK:
921 : : case GI_INFO_TYPE_SIGNAL:
922 : 0 : return NULL;
923 : 0 : default:
924 : : g_assert_not_reached ();
925 : : }
926 : : }
927 : :
928 : : /**
929 : : * gi_callable_info_get_sync_function
930 : : * @info: a callable info structure
931 : : *
932 : : * Gets the callable info for the callable's synchronous version
933 : : *
934 : : * Returns: (transfer full) (nullable): a [class@GIRepository.CallableInfo] for the
935 : : * sync function or `NULL` if not defined.
936 : : * Since: 2.84
937 : : */
938 : : GICallableInfo *
939 : 5 : gi_callable_info_get_sync_function (GICallableInfo *info)
940 : : {
941 : 5 : GIBaseInfo *rinfo = (GIBaseInfo *) info;
942 : :
943 : 5 : switch (gi_base_info_get_info_type (rinfo))
944 : : {
945 : 5 : case GI_INFO_TYPE_FUNCTION:
946 : : {
947 : 5 : FunctionBlob *blob = GET_BLOB (FunctionBlob, rinfo);
948 : 5 : if (!blob->is_async || blob->sync_or_async == ASYNC_SENTINEL)
949 : 2 : return NULL;
950 : :
951 : 3 : return get_callable_info_for_index (rinfo, blob->sync_or_async);
952 : : }
953 : 0 : case GI_INFO_TYPE_VFUNC:
954 : : {
955 : 0 : VFuncBlob *blob = GET_BLOB (VFuncBlob, rinfo);
956 : 0 : if (!blob->is_async || blob->sync_or_async == ASYNC_SENTINEL)
957 : 0 : return NULL;
958 : :
959 : 0 : return get_method_callable_info_for_index (rinfo, blob->sync_or_async);
960 : : }
961 : 0 : case GI_INFO_TYPE_CALLBACK:
962 : : case GI_INFO_TYPE_SIGNAL:
963 : 0 : return NULL;
964 : 0 : default:
965 : : g_assert_not_reached ();
966 : : }
967 : : }
968 : :
969 : : /**
970 : : * gi_callable_info_get_finish_function
971 : : * @info: a callable info structure
972 : : *
973 : : * Gets the info for an async function's corresponding finish function
974 : : *
975 : : * Returns: (transfer full) (nullable): a [class@GIRepository.CallableInfo] for the
976 : : * finish function or `NULL` if not defined.
977 : : * Since: 2.84
978 : : */
979 : : GICallableInfo *
980 : 4 : gi_callable_info_get_finish_function (GICallableInfo *info)
981 : : {
982 : 4 : GIBaseInfo *rinfo = (GIBaseInfo *) info;
983 : :
984 : 4 : switch (gi_base_info_get_info_type (rinfo))
985 : : {
986 : 4 : case GI_INFO_TYPE_FUNCTION:
987 : : {
988 : 4 : FunctionBlob *blob = GET_BLOB (FunctionBlob, rinfo);
989 : 4 : if (!blob->is_async || blob->finish == ASYNC_SENTINEL)
990 : 2 : return NULL;
991 : :
992 : 2 : return get_callable_info_for_index (rinfo, blob->finish);
993 : : }
994 : 0 : case GI_INFO_TYPE_VFUNC:
995 : : {
996 : 0 : VFuncBlob *blob = GET_BLOB (VFuncBlob, rinfo);
997 : 0 : if (!blob->is_async || blob->finish == ASYNC_SENTINEL)
998 : 0 : return NULL;
999 : :
1000 : 0 : return get_method_callable_info_for_index (rinfo, blob->finish);
1001 : : }
1002 : 0 : case GI_INFO_TYPE_CALLBACK:
1003 : : case GI_INFO_TYPE_SIGNAL:
1004 : 0 : return NULL;
1005 : 0 : default:
1006 : : g_assert_not_reached ();
1007 : : }
1008 : : }
1009 : :
1010 : : /**
1011 : : * gi_callable_info_is_async
1012 : : * @info: a callable info structure
1013 : : *
1014 : : * Gets whether a callable is ‘async’. Async callables have a
1015 : : * [type@Gio.AsyncReadyCallback] parameter and user data.
1016 : : *
1017 : : * Returns: true if the callable is async
1018 : : * Since: 2.84
1019 : : */
1020 : : gboolean
1021 : 1 : gi_callable_info_is_async (GICallableInfo *callable_info)
1022 : : {
1023 : 1 : GIBaseInfo *info = (GIBaseInfo *) callable_info;
1024 : 1 : switch (gi_base_info_get_info_type ((GIBaseInfo *) callable_info))
1025 : : {
1026 : 1 : case GI_INFO_TYPE_FUNCTION:
1027 : : {
1028 : 1 : return GET_BLOB (FunctionBlob, info)->is_async;
1029 : : }
1030 : 0 : case GI_INFO_TYPE_VFUNC:
1031 : : {
1032 : 0 : return GET_BLOB (VFuncBlob, info)->is_async;
1033 : : }
1034 : 0 : case GI_INFO_TYPE_CALLBACK:
1035 : : case GI_INFO_TYPE_SIGNAL:
1036 : 0 : return FALSE;
1037 : 0 : default:
1038 : : g_assert_not_reached ();
1039 : : }
1040 : : }
1041 : :
|