1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5  * Copyright (C) 2013 Simon Feltman <sfeltman@gnome.org>
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 #include <Python.h>
25 #include <girepository.h>
26 #include <girffi.h>
27 
28 #include "pygi-invoke-state-struct.h"
29 
30 G_BEGIN_DECLS
31 
32 typedef struct _PyGIArgCache PyGIArgCache;
33 typedef struct _PyGICallableCache PyGICallableCache;
34 typedef struct _PyGIFunctionCache PyGIFunctionCache;
35 typedef struct _PyGIVFuncCache PyGIVFuncCache;
36 
37 typedef PyGIFunctionCache PyGICCallbackCache;
38 typedef PyGIFunctionCache PyGIConstructorCache;
39 typedef PyGIFunctionCache PyGIFunctionWithInstanceCache;
40 typedef PyGIFunctionCache PyGIMethodCache;
41 typedef PyGICallableCache PyGIClosureCache;
42 
43 typedef gboolean (*PyGIMarshalFromPyFunc) (PyGIInvokeState   *state,
44                                            PyGICallableCache *callable_cache,
45                                            PyGIArgCache      *arg_cache,
46                                            PyObject          *py_arg,
47                                            GIArgument        *arg,
48                                            gpointer          *cleanup_data);
49 
50 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState   *state,
51                                           PyGICallableCache *callable_cache,
52                                           PyGIArgCache      *arg_cache,
53                                           GIArgument        *arg,
54                                           gpointer          *cleanup_data);
55 
56 typedef void (*PyGIMarshalCleanupFunc) (PyGIInvokeState *state,
57                                         PyGIArgCache    *arg_cache,
58                                         PyObject        *py_arg,
59                                         gpointer         data,
60                                         gboolean         was_processed);
61 
62 typedef void (*PyGIMarshalToPyCleanupFunc) (PyGIInvokeState *state,
63                                             PyGIArgCache    *arg_cache,
64                                             gpointer         cleanup_data,
65                                             gpointer         data,
66                                             gboolean         was_processed);
67 
68 /* Argument meta types denote how we process the argument:
69  *  - PYGI_META_ARG_TYPE_PARENT - parents may or may not have children
70  *    but are always processed via the normal marshaller for their
71  *    actual GI type.  If they have children the marshaller will
72  *    also handle marshalling the children.
73  *  - PYGI_META_ARG_TYPE_CHILD - Children without python argument are
74  *    ignored by the marshallers and handled directly by their parents
75  *    marshaller.
76  *  - Children with pyargs (PYGI_META_ARG_TYPE_CHILD_WITH_PYARG) are processed
77  *    the same as other child args but also have an index into the
78  *    python parameters passed to the invoker
79  */
80 typedef enum {
81     PYGI_META_ARG_TYPE_PARENT,
82     PYGI_META_ARG_TYPE_CHILD,
83     PYGI_META_ARG_TYPE_CHILD_WITH_PYARG,
84     PYGI_META_ARG_TYPE_CLOSURE,
85 } PyGIMetaArgType;
86 
87 /*
88  * Argument direction types denotes how we marshal,
89  * e.g. to Python or from Python or both.
90  */
91 typedef enum {
92     PYGI_DIRECTION_TO_PYTHON     = 1 << 0,
93     PYGI_DIRECTION_FROM_PYTHON   = 1 << 1,
94     PYGI_DIRECTION_BIDIRECTIONAL = PYGI_DIRECTION_TO_PYTHON | PYGI_DIRECTION_FROM_PYTHON
95  } PyGIDirection;
96 
97 /*
98  * In PyGI IN and OUT arguments mean different things depending on the context
99  * of the callable, e.g. is it a callback that is being called from C or a
100  * function that is being called from Python.
101  */
102 typedef enum {
103     PYGI_CALLING_CONTEXT_IS_FROM_C,
104     PYGI_CALLING_CONTEXT_IS_FROM_PY
105 } PyGICallingContext;
106 
107 
108 struct _PyGIArgCache
109 {
110     const gchar *arg_name;
111 
112     PyGIMetaArgType meta_type;
113     gboolean is_pointer;
114     gboolean is_caller_allocates;
115     gboolean is_skipped;
116     gboolean allow_none;
117     gboolean has_default;
118 
119     PyGIDirection direction;
120     GITransfer transfer;
121     GITypeTag type_tag;
122     GITypeInfo *type_info;
123 
124     PyGIMarshalFromPyFunc from_py_marshaller;
125     PyGIMarshalToPyFunc to_py_marshaller;
126 
127     PyGIMarshalCleanupFunc from_py_cleanup;
128     PyGIMarshalToPyCleanupFunc to_py_cleanup;
129 
130     GDestroyNotify destroy_notify;
131 
132     gssize c_arg_index;
133     gssize py_arg_index;
134 
135     /* Set when has_default is true. */
136     GIArgument default_value;
137 };
138 
139 typedef struct _PyGISequenceCache
140 {
141     PyGIArgCache arg_cache;
142     PyGIArgCache *item_cache;
143 } PyGISequenceCache;
144 
145 typedef struct _PyGIArgGArray
146 {
147     PyGISequenceCache seq_cache;
148     gssize fixed_size;
149     gssize len_arg_index;
150     gboolean is_zero_terminated;
151     gsize item_size;
152     GIArrayType array_type;
153 } PyGIArgGArray;
154 
155 typedef struct _PyGIInterfaceCache
156 {
157     PyGIArgCache arg_cache;
158     gboolean is_foreign;
159     GType g_type;
160     PyObject *py_type;
161     GIInterfaceInfo *interface_info;
162     gchar *type_name;
163 } PyGIInterfaceCache;
164 
165 struct _PyGICallableCache
166 {
167     const gchar *name;
168     const gchar *container_name;
169     const gchar *namespace;
170 
171     PyGICallingContext calling_context;
172 
173     PyGIArgCache *return_cache;
174     GPtrArray *args_cache;
175     GSList *to_py_args;
176     GSList *arg_name_list; /* for keyword arg matching */
177     GHashTable *arg_name_hash;
178     gboolean throws;
179 
180     /* Index of user_data arg passed to a callable. */
181     gssize user_data_index;
182 
183     /* Index of user_data arg that can eat variable args passed to a callable. */
184     gssize user_data_varargs_index;
185 
186     /* Number of args already added */
187     gssize args_offset;
188 
189     /* Number of out args passed to g_function_info_invoke.
190      * This is used for the length of PyGIInvokeState.out_values */
191     gssize n_to_py_args;
192 
193     /* If the callable return value gets used */
194     gboolean has_return;
195 
196     /* The type used for returning multiple values or NULL */
197     PyTypeObject* resulttuple_type;
198 
199     /* Number of out args for g_function_info_invoke that will be skipped
200      * when marshaling to Python due to them being implicitly available
201      * (list/array length).
202      */
203     gssize n_to_py_child_args;
204 
205     /* Number of Python arguments expected for invoking the gi function. */
206     gssize n_py_args;
207 
208     /* Minimum number of args required to call the callable from Python.
209      * This count does not include args with defaults. */
210     gssize n_py_required_args;
211 
212     void     (*deinit)              (PyGICallableCache *callable_cache);
213 
214     gboolean (*generate_args_cache) (PyGICallableCache *callable_cache,
215                                      GICallableInfo *callable_info);
216 };
217 
218 struct _PyGIFunctionCache {
219     PyGICallableCache callable_cache;
220 
221     /* An invoker with ffi_cif already setup */
222     GIFunctionInvoker invoker;
223 
224     PyObject *(*invoke) (PyGIFunctionCache *function_cache,
225                          PyGIInvokeState *state,
226                          PyObject *py_args,
227                          PyObject *py_kwargs);
228 } ;
229 
230 struct _PyGIVFuncCache {
231     PyGIFunctionWithInstanceCache fwi_cache;
232 
233     GIBaseInfo *info;
234 };
235 
236 
237 gboolean
238 pygi_arg_base_setup      (PyGIArgCache *arg_cache,
239                           GITypeInfo   *type_info,
240                           GIArgInfo    *arg_info,  /* may be NULL for return arguments */
241                           GITransfer    transfer,
242                           PyGIDirection direction);
243 
244 gboolean
245 pygi_arg_interface_setup (PyGIInterfaceCache *iface_cache,
246                           GITypeInfo         *type_info,
247                           GIArgInfo          *arg_info,  /* may be NULL for return arguments */
248                           GITransfer          transfer,
249                           PyGIDirection       direction,
250                           GIInterfaceInfo    *iface_info);
251 
252 gboolean
253 pygi_arg_sequence_setup  (PyGISequenceCache  *sc,
254                           GITypeInfo         *type_info,
255                           GIArgInfo          *arg_info,    /* may be NULL for return arguments */
256                           GITransfer          transfer,
257                           PyGIDirection       direction,
258                           PyGICallableCache  *callable_cache);
259 
260 PyGIArgCache *
261 pygi_arg_interface_new_from_info (GITypeInfo         *type_info,
262                                   GIArgInfo          *arg_info,     /* may be NULL for return arguments */
263                                   GITransfer          transfer,
264                                   PyGIDirection       direction,
265                                   GIInterfaceInfo    *iface_info);
266 
267 PyGIArgCache *
268 pygi_arg_cache_alloc     (void);
269 
270 PyGIArgCache *
271 pygi_arg_cache_new       (GITypeInfo *type_info,
272                           GIArgInfo *arg_info,
273                           GITransfer transfer,
274                           PyGIDirection direction,
275                           PyGICallableCache *callable_cache,
276                           /* will be removed */
277                           gssize c_arg_index,
278                           gssize py_arg_index);
279 
280 void
281 pygi_arg_cache_free      (PyGIArgCache *cache);
282 
283 void
284 pygi_callable_cache_free    (PyGICallableCache *cache);
285 
286 gchar *
287 pygi_callable_cache_get_full_name (PyGICallableCache *cache);
288 
289 PyGIFunctionCache *
290 pygi_function_cache_new     (GICallableInfo *info);
291 
292 PyObject *
293 pygi_function_cache_invoke  (PyGIFunctionCache *function_cache,
294                              PyObject *py_args,
295                              PyObject *py_kwargs);
296 
297 PyGIFunctionCache *
298 pygi_ccallback_cache_new    (GICallableInfo *info,
299                              GCallback function_ptr);
300 
301 PyObject *
302 pygi_ccallback_cache_invoke (PyGIFunctionCache *function_cache,
303                              PyObject *py_args,
304                              PyObject *py_kwargs,
305                              gpointer user_data);
306 
307 PyGIFunctionCache *
308 pygi_constructor_cache_new  (GICallableInfo *info);
309 
310 PyGIFunctionCache *
311 pygi_method_cache_new       (GICallableInfo *info);
312 
313 PyGIFunctionCache *
314 pygi_vfunc_cache_new        (GICallableInfo *info);
315 
316 PyGIClosureCache *
317 pygi_closure_cache_new      (GICallableInfo *info);
318 
319 inline static guint
_pygi_callable_cache_args_len(PyGICallableCache * cache)320 _pygi_callable_cache_args_len (PyGICallableCache *cache) {
321     return ((cache)->args_cache)->len;
322 }
323 
324 inline static PyGIArgCache *
_pygi_callable_cache_get_arg(PyGICallableCache * cache,guint index)325 _pygi_callable_cache_get_arg (PyGICallableCache *cache, guint index) {
326     return (PyGIArgCache *) g_ptr_array_index (cache->args_cache, index);
327 }
328 
329 inline static void
_pygi_callable_cache_set_arg(PyGICallableCache * cache,guint index,PyGIArgCache * arg_cache)330 _pygi_callable_cache_set_arg (PyGICallableCache *cache, guint index, PyGIArgCache *arg_cache) {
331     cache->args_cache->pdata[index] = arg_cache;
332 }
333 
334 G_END_DECLS
335 
336 #endif /* __PYGI_CACHE_H__ */
337