-
Notifications
You must be signed in to change notification settings - Fork 2
/
php_midgard_gobject_closures.c
345 lines (265 loc) · 9.22 KB
/
php_midgard_gobject_closures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "php_midgard.h"
#include "php_midgard__helpers.h"
#include "php_midgard_gobject.h"
typedef struct {
GClosure closure;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
zval *args;
zval *zval_array;
guint argc;
guint type;
zval *zobject;
zval *connected;
} php_mgd_closure;
static void php_midgard_closure_invalidate(gpointer data, GClosure *closure)
{
TSRMLS_FETCH();
if (MGDG(midgard_memory_debug)) {
printf("[%p] php_midgard_closure_invalidate(data = %p)\n", closure, data);
}
php_mgd_closure *mgdclosure = (php_mgd_closure *) closure;
zval_ptr_dtor(&(mgdclosure->fci.function_name));
if (mgdclosure->args != NULL) {
zval_ptr_dtor(&mgdclosure->args);
mgdclosure->args = NULL;
}
if (MGDG(midgard_memory_debug)) {
printf("[%p] ----> done with args\n", closure);
}
mgdclosure->zobject = NULL;
mgdclosure->connected = NULL;
if (MGDG(midgard_memory_debug)) {
printf("[%p] <= php_midgard_closure_invalidate()\n", closure);
}
}
/* CLASS CLOSURES */
static GHashTable *__classes_hash = NULL;
void php_midgard_gobject_closure_hash_new()
{
if (__classes_hash == NULL)
__classes_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
}
static php_mgd_closure * __class_closure_lookup(GType class_type, guint signal_id)
{
if (signal_id == 0)
return NULL;
if (__classes_hash == NULL)
return NULL;
GHashTable *closures_hash;
closures_hash = g_hash_table_lookup(__classes_hash, g_type_name(class_type));
if (!closures_hash)
return NULL;
gchar *sname = g_strdup(g_signal_name(signal_id));
g_strdelimit (sname, G_STR_DELIMITERS ":^", '_');
php_mgd_closure *pmc = g_hash_table_lookup(closures_hash, sname);
g_free(sname);
return pmc;
}
static void __php_midgard_closure_free(gpointer data)
{
TSRMLS_FETCH();
if (MGDG(midgard_memory_debug)) {
printf("[%p] __php_midgard_closure_free()\n", data);
}
GClosure *closure = (GClosure *) data;
if (closure) {
g_closure_unref(closure);
}
}
void __free_hash_foreach(gpointer key, gpointer val, gpointer ud)
{
GHashTable *hash = (GHashTable *) val;
if (hash)
g_hash_table_destroy(hash);
}
void php_midgard_gobject_closure_hash_free()
{
if (__classes_hash != NULL) {
g_hash_table_foreach(__classes_hash, __free_hash_foreach, NULL);
g_hash_table_destroy(__classes_hash);
__classes_hash = NULL;
}
return;
}
static void __register_class_closure(const gchar *class_name, const gchar *signal, php_mgd_closure *closure)
{
if (__classes_hash == NULL)
return;
gchar *sname = g_strdup(signal);
g_strdelimit (sname, G_STR_DELIMITERS ":^", '_'); /* FIXME, it should be fast, so no conversion here */
guint signal_id = g_signal_lookup(sname, g_type_from_name(class_name));
if (signal_id == 0) {
php_error(E_WARNING, "'%s' is not registered as event for '%s'", sname, class_name);
g_free(sname);
return;
}
GHashTable *closures_hash =
g_hash_table_lookup(__classes_hash, class_name);
if (!closures_hash) {
closures_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, __php_midgard_closure_free);
}
g_hash_table_insert(closures_hash, (gpointer) sname, closure);
g_hash_table_insert(__classes_hash, (gpointer) g_strdup(class_name), closures_hash);
}
static void php_midgard_closure_default_marshal(GClosure *closure,
GValue *return_value, guint n_param_values,
const GValue *param_values, gpointer invocation_hint,
gpointer marshal_data)
{
php_mgd_closure *mgdclosure = (php_mgd_closure *) closure;
TSRMLS_FETCH();
if (MGDG(can_deliver_signals) == 0)
return; // engine is not in sane state
if (MGDG(midgard_memory_debug)) {
printf("[%p] php_midgard_closure_default_marshal(args = %p)\n", closure, mgdclosure->args);
}
zval *params = NULL;
MAKE_STD_ZVAL(params);
array_init(params);
// "10000" on next line is a HACK. Just took a number which is slightly above normal, but still doesn't look like invalid
if (mgdclosure->zobject != NULL && Z_REFCOUNT_P(mgdclosure->zobject) < 10000) {
if (MGDG(midgard_memory_debug)) {
printf("[%p] ---> zobject (%p) refcount = %d\n", closure, mgdclosure->zobject, Z_REFCOUNT_P(mgdclosure->zobject));
}
Z_ADDREF_P(mgdclosure->zobject);
zend_hash_next_index_insert(Z_ARRVAL_P(params), &(mgdclosure->zobject), sizeof(zval *), NULL);
} else {
zval *dummy = NULL;
MAKE_STD_ZVAL(dummy);
ZVAL_NULL(dummy);
zend_hash_next_index_insert(Z_ARRVAL_P(params), &dummy, sizeof(zval *), NULL);
}
if (mgdclosure->args != NULL) {
// + params
HashTable *args_hash = Z_ARRVAL_P(mgdclosure->args);
zend_uint argc = 1 + zend_hash_num_elements(args_hash);
zend_hash_internal_pointer_reset(args_hash);
size_t i;
for (i = 1; i < argc; i++) {
zval **ptr;
zend_hash_get_current_data(args_hash, (void **)&ptr);
zend_hash_move_forward(args_hash);
if (MGDG(midgard_memory_debug)) {
printf("[%p] ----> got ptr = %p, *ptr = %p [refcount = %d]\n", closure, ptr, *ptr, Z_REFCOUNT_P(*ptr));
}
Z_ADDREF_PP(ptr);
zend_hash_next_index_insert(Z_ARRVAL_P(params), ptr, sizeof(zval *), NULL);
}
}
zval *retval = NULL;
zend_fcall_info_args(&(mgdclosure->fci), params TSRMLS_CC);
mgdclosure->fci.retval_ptr_ptr = &retval;
zend_call_function(&(mgdclosure->fci), &(mgdclosure->fci_cache) TSRMLS_CC);
zend_fcall_info_args_clear(&(mgdclosure->fci), 1);
if (retval)
zval_ptr_dtor(&retval);
zval_ptr_dtor(¶ms);
}
GClosure *php_midgard_closure_new_default(zend_fcall_info fci, zend_fcall_info_cache fci_cache, zval *zobject, zval *zval_array TSRMLS_DC)
{
GClosure *closure;
if (zobject == NULL || !g_type_from_name(php_class_name_to_g_class_name(Z_OBJCE_P(zobject)->name))) {
closure = g_closure_new_simple(sizeof(php_mgd_closure), NULL);
} else {
closure = g_closure_new_object(sizeof(php_mgd_closure), __php_gobject_ptr(zobject));
}
if (!closure) {
php_error(E_ERROR, "Couldn't create new closure");
return NULL;
}
if (MGDG(midgard_memory_debug)) {
printf("[%p] php_midgard_closure_new_default(zobject = %p, zval_array = %p)\n", closure, zobject, zval_array);
}
php_mgd_closure *mgdclosure = (php_mgd_closure*) closure;
Z_ADDREF_P(fci.function_name);
mgdclosure->fci = fci;
mgdclosure->fci_cache = fci_cache;
mgdclosure->zobject = zobject; // we do not add reference here, as closure would be destroyed when object destroyed
mgdclosure->args = NULL;
if (zval_array) {
Z_ADDREF_P(zval_array);
mgdclosure->args = zval_array;
}
g_closure_add_invalidate_notifier(closure, NULL, php_midgard_closure_invalidate);
g_closure_set_marshal((GClosure *)mgdclosure, php_midgard_closure_default_marshal);
return (GClosure *)mgdclosure;
}
void php_midgard_object_class_connect_default(INTERNAL_FUNCTION_PARAMETERS)
{
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
char *sname = NULL, *class_name = NULL;
int sname_length, class_name_length;
zval *zval_array = NULL;
/* Keep '!' as passed object parameter ( or params array ) can be NULL */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssf|a!",
&class_name, &class_name_length,
&sname, &sname_length,
&fci, &fci_cache, &zval_array)
== FAILURE)
{
return;
}
GType class_type = g_type_from_name((const gchar *)class_name);
if (class_type == 0) {
php_error(E_WARNING, "Class %s is not registered in GType system", class_name);
return;
}
guint signal_id;
GQuark signal_detail;
if (!g_signal_parse_name(sname, class_type, &signal_id, &signal_detail, TRUE)) {
php_error(E_WARNING, "%s signal name is invalid", sname);
return;
/* TODO , should we handle exception here? */
}
GClosure *closure = php_midgard_closure_new_default(fci, fci_cache, NULL, zval_array TSRMLS_CC);
if (!closure) {
php_error(E_WARNING, "Can not create new closure");
return;
}
php_mgd_closure *dclosure = (php_mgd_closure *) closure;
dclosure->zval_array = zval_array;
__register_class_closure(class_name, sname, dclosure);
}
void php_midgard_object_connect_class_closures(GObject *object, zval *zobject TSRMLS_DC)
{
/* TODO, add error handling , IS_OBJECT , etc */
if (zobject == NULL) {
php_error(E_WARNING, "Connect to class closure: failed to get zend object");
return;
}
if (object == NULL) {
php_error(E_WARNING, "Connect to class closure: failed to get underlying object");
return;
}
if (MGDG(midgard_memory_debug)) {
printf("[%p] php_midgard_object_connect_class_closures(zobject = %p)\n", object, zobject);
}
/* Use MIDGARD_TYPE_OBJECT type explicitly!
* Ancestor type is not taken into account in GLib's list_ids! */
guint n_ids;
guint *ids = g_signal_list_ids(MIDGARD_TYPE_OBJECT, &n_ids);
if (n_ids == 0)
return;
guint i = 0;
for (i = 0; i < n_ids; i++) {
php_mgd_closure *closure = __class_closure_lookup(G_OBJECT_TYPE(object), ids[i]);
if (closure) {
if (MGDG(midgard_memory_debug)) {
printf("[%p] ----> found \"default\" closure = %p\n", object, closure);
}
php_mgd_closure *dclosure = (php_mgd_closure *)
php_midgard_closure_new_default(closure->fci, closure->fci_cache, zobject, closure->zval_array TSRMLS_CC);
if (MGDG(midgard_memory_debug)) {
printf("[%p] ----> created closure = %p\n", object, dclosure);
}
g_signal_connect_closure(object, g_signal_name(ids[i]), (GClosure *)dclosure, FALSE);
}
}
g_free(ids);
if (MGDG(midgard_memory_debug)) {
printf("[%p] <= php_midgard_object_connect_class_closures(zobject = %p)\n", object, zobject);
}
return;
}