Debian Codegraph @

gi/pygi-cache.h

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
5 * Copyright (C) 2013 Simon Feltman <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __PYGI_CACHE_H__
22 #define __PYGI_CACHE_H__
23
24
25 /* Workaround for FFI_GO_CLOSURES not being defined on macOS
26 * See also: https://github.com/openjdk/jdk17u-dev/pull/741
27 */
28 #ifndef FFI_GO_CLOSURES
29 #define FFI_GO_CLOSURES 0
30 #endif
31
32 #include <girepository/girepository.h>
33 #include <girepository/girffi.h>
34
35 #include "pygi-invoke-state-struct.h"
36
37 G_BEGIN_DECLS
38
39 typedef struct _PyGIArgCache PyGIArgCache;
40 typedef struct _PyGICallableCache PyGICallableCache;
41 typedef struct _PyGIFunctionCache PyGIFunctionCache;
42 typedef struct _PyGIVFuncCache PyGIVFuncCache;
43
44 typedef PyGIFunctionCache PyGICCallbackCache;
45 typedef PyGIFunctionCache PyGIConstructorCache;
46 typedef PyGIFunctionCache PyGIFunctionWithInstanceCache;
47 typedef PyGIFunctionCache PyGIMethodCache;
48 typedef PyGICallableCache PyGIClosureCache;
49
50 typedef gboolean (*PyGIMarshalFromPyFunc) (PyGIInvokeState *state,
51 PyGICallableCache *callable_cache,
52 PyGIArgCache *arg_cache,
53 PyObject *py_arg, GIArgument *arg,
54 gpointer *cleanup_data);
55
56 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState *state,
57 PyGICallableCache *callable_cache,
58 PyGIArgCache *arg_cache,
59 GIArgument *arg,
60 gpointer *cleanup_data);
61
62 typedef void (*PyGIMarshalFromPyCleanupFunc) (PyGIInvokeState *state,
63 PyGIArgCache *arg_cache,
64 PyObject *py_arg, gpointer data,
65 gboolean was_processed);
66
67 typedef void (*PyGIMarshalToPyCleanupFunc) (PyGIInvokeState *state,
68 PyGIArgCache *arg_cache,
69 gpointer cleanup_data,
70 gpointer data,
71 gboolean was_processed);
72
73 /* Argument meta types denote how we process the argument:
74 * - PYGI_META_ARG_TYPE_PARENT - parents may or may not have children
75 * but are always processed via the normal marshaller for their
76 * actual GI type. If they have children the marshaller will
77 * also handle marshalling the children.
78 * - PYGI_META_ARG_TYPE_CHILD - Children without python argument are
79 * ignored by the marshallers and handled directly by their parents
80 * marshaller.
81 * - Children with pyargs (PYGI_META_ARG_TYPE_CHILD_WITH_PYARG) are processed
82 * the same as other child args but also have an index into the
83 * python parameters passed to the invoker
84 */
85 typedef enum {
86 PYGI_META_ARG_TYPE_PARENT,
87 PYGI_META_ARG_TYPE_CHILD,
88 PYGI_META_ARG_TYPE_CHILD_WITH_PYARG,
89 PYGI_META_ARG_TYPE_CLOSURE,
90 } PyGIMetaArgType;
91
92 /*
93 * Argument direction types denotes how we marshal,
94 * e.g. to Python or from Python or both.
95 */
96 typedef enum {
97 PYGI_DIRECTION_TO_PYTHON = 1 << 0,
98 PYGI_DIRECTION_FROM_PYTHON = 1 << 1,
99 PYGI_DIRECTION_BIDIRECTIONAL = PYGI_DIRECTION_TO_PYTHON
100 | PYGI_DIRECTION_FROM_PYTHON
101 } PyGIDirection;
102
103 /*
104 * In PyGI IN and OUT arguments mean different things depending on the context
105 * of the callable, e.g. is it a callback that is being called from C or a
106 * function that is being called from Python.
107 */
108 typedef enum {
109 PYGI_CALLING_CONTEXT_IS_FROM_C,
110 PYGI_CALLING_CONTEXT_IS_FROM_PY
111 } PyGICallingContext;
112
113 typedef enum {
114 PYGI_ASYNC_CONTEXT_NONE = 0,
115 PYGI_ASYNC_CONTEXT_CALLBACK,
116 PYGI_ASYNC_CONTEXT_CANCELLABLE,
117 } PyGIAsyncContext;
118
119 struct _PyGIArgCache {
120 GITypeInfo *type_info;
121 GIArgInfo *arg_info;
122
123 PyGIMetaArgType meta_type;
124 PyGIAsyncContext async_context;
125 gboolean is_pointer;
126
127 PyGIDirection direction;
128 GITransfer transfer;
129 GITypeTag type_tag;
130
131 PyGIMarshalFromPyFunc from_py_marshaller;
132 PyGIMarshalToPyFunc to_py_marshaller;
133
134 PyGIMarshalFromPyCleanupFunc from_py_cleanup;
135 PyGIMarshalToPyCleanupFunc to_py_cleanup;
136
137 GDestroyNotify destroy_notify;
138
139 gssize c_arg_index;
140 gssize py_arg_index;
141 };
142
143 typedef struct _PyGISequenceCache {
144 PyGIArgCache arg_cache;
145 PyGIArgCache *item_cache;
146 } PyGISequenceCache;
147
148 typedef struct _PyGIArgGArray {
149 PyGISequenceCache seq_cache;
150 PyGIArgCache *len_arg_cache;
151 unsigned int len_arg_index;
152 gboolean has_len_arg;
153 gsize item_size;
154 } PyGIArgGArray;
155
156 typedef struct _PyGIInterfaceCache {
157 PyGIArgCache arg_cache;
158 gboolean is_foreign;
159 GType g_type;
160 PyObject *py_type;
161 GIRegisteredTypeInfo *interface_info;
162 gchar *type_name;
163 } PyGIInterfaceCache;
164
165 struct _PyGICallableCache {
166 GIBaseInfo *info;
167
168 PyGICallingContext calling_context;
169
170 PyGIArgCache *return_cache;
171 GPtrArray *args_cache;
172 GSList *to_py_args;
173 GHashTable *arg_name_hash;
174
175 /* Index of user_data arg passed to a callable. */
176 unsigned int user_data_index;
177 gboolean has_user_data;
178
179 /* A user_data arg that can eat variable args passed to a callable. */
180 PyGIArgCache *user_data_varargs_arg;
181
182 /* Number of args already added */
183 unsigned int args_offset;
184
185 /* Number of out args passed to gi_function_info_invoke.
186 * This is used for the length of PyGIInvokeState.out_values */
187 gssize n_to_py_args;
188
189 /* If the callable return value gets used */
190 gboolean has_return;
191
192 /* The type used for returning multiple values or NULL */
193 PyTypeObject *resulttuple_type;
194
195 /* Number of out args for gi_function_info_invoke that will be skipped
196 * when marshaling to Python due to them being implicitly available
197 * (list/array length).
198 */
199 gssize n_to_py_child_args;
200
201 /* Number of Python arguments expected for invoking the gi function. */
202 gssize n_py_args;
203
204 void (*deinit) (PyGICallableCache *callable_cache);
205 };
206
207 struct _PyGIFunctionCache {
208 PyGICallableCache callable_cache;
209
210 /* Information about async functions. */
211 PyObject *async_finish;
212 PyGIArgCache *async_callback;
213 PyGIArgCache *async_cancellable;
214
215 /* An invoker with ffi_cif already setup */
216 GIFunctionInvoker invoker;
217
218 PyObject *(*invoke) (PyGIFunctionCache *function_cache,
219 PyGIInvokeState *state, PyObject *const *py_args,
220 size_t py_nargsf, PyObject *py_kwnames);
221 };
222
223 struct _PyGIVFuncCache {
224 PyGIFunctionWithInstanceCache fwi_cache;
225 };
226
227 typedef struct _PyGICallbackCache {
228 PyGIArgCache arg_cache;
229 unsigned int user_data_index;
230 gboolean has_user_data;
231 unsigned int destroy_notify_index;
232 gboolean has_destroy_notify;
233 GIScopeType scope;
234 GIBaseInfo *interface_info;
235 PyGIClosureCache *closure_cache;
236 } PyGICallbackCache;
237
238 const char *pygi_arg_cache_get_name (PyGIArgCache *cache);
239
240 gboolean pygi_arg_cache_allow_none (PyGIArgCache *cache);
241
242 gboolean pygi_arg_cache_is_caller_allocates (PyGIArgCache *cache);
243
244 void pygi_callable_cache_free (PyGICallableCache *cache);
245
246 gchar *pygi_callable_cache_get_full_name (PyGICallableCache *cache);
247
248 gboolean pygi_callable_cache_skip_return (PyGICallableCache *cache);
249
250 gboolean pygi_callable_cache_can_throw_gerror (PyGICallableCache *cache);
251
252 PyGIFunctionCache *pygi_function_cache_new (GICallableInfo *info);
253
254 PyObject *pygi_function_cache_invoke (PyGIFunctionCache *function_cache,
255 PyObject *const *py_args,
256 size_t py_nargsf, PyObject *py_kwnames);
257
258 PyGIFunctionCache *pygi_ccallback_cache_new (GICallableInfo *info,
259 GCallback function_ptr);
260
261 PyObject *pygi_ccallback_cache_invoke (PyGIFunctionCache *function_cache,
262 PyObject *const *py_args,
263 size_t py_nargsf, PyObject *py_kwnames,
264 gpointer user_data);
265
266 PyGIFunctionCache *pygi_constructor_cache_new (GICallableInfo *info);
267
268 PyGIFunctionCache *pygi_method_cache_new (GICallableInfo *info);
269
270 PyGIFunctionCache *pygi_vfunc_cache_new (GICallableInfo *info);
271
272 PyGIClosureCache *pygi_closure_cache_new (GICallableInfo *info);
273
274 static inline guint
275 _pygi_callable_cache_args_len (PyGICallableCache *cache)
276 {
277 return cache->args_cache->len;
278 }
279
280 static inline PyGIArgCache *
281 _pygi_callable_cache_get_arg (PyGICallableCache *cache, guint index)
282 {
283 return (PyGIArgCache *)g_ptr_array_index (cache->args_cache, index);
284 }
285
286 static inline void
287 _pygi_callable_cache_set_arg (PyGICallableCache *cache, guint index,
288 PyGIArgCache *arg_cache)
289 {
290 g_assert (index < cache->args_cache->len);
291 cache->args_cache->pdata[index] = arg_cache;
292 }
293
294 G_END_DECLS
295
296 #endif /* __PYGI_CACHE_H__ */