-
Notifications
You must be signed in to change notification settings - Fork 4
/
gobject_gobject_class.c
488 lines (376 loc) · 14.1 KB
/
gobject_gobject_class.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Alexey Zakhlestin <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <php.h>
#include <zend_interfaces.h>
#include <zend_exceptions.h>
#include <ext/spl/spl_exceptions.h>
#include "php_gobject_private.h"
zend_class_entry *gobject_ce_gobject;
zend_object_handlers *php_gobject_gobject_handlers;
void unregister_gobject_closure(gpointer _zobject, GClosure *closure)
{
gobject_gobject_object *zobject = (gobject_gobject_object *) _zobject;
zobject->closures = g_slist_remove(zobject->closures, closure);
}
void register_gobject_closure(zval *zval_object, GClosure *closure TSRMLS_DC)
{
gobject_gobject_object *zobject = __php_objstore_object(zval_object);
if (g_slist_find(zobject->closures, closure) != NULL) {
// registered already
return;
}
zobject->closures = g_slist_prepend(zobject->closures, closure);
g_closure_add_invalidate_notifier(closure, zobject, unregister_gobject_closure);
}
void php_gobject_invalidate_gvalue(GValue **gvalue)
{
g_value_unset(*gvalue);
g_free(*gvalue);
*gvalue = NULL;
}
void gobject_gobject_free_storage(gobject_gobject_object *intern TSRMLS_DC)
{
// php_printf("gobject_gobject_free_storage()\n");
if (intern->gobject) {
g_object_set_data(intern->gobject, "gobject-for-php", NULL);
g_object_unref(intern->gobject);
}
GSList *tmp = intern->closures;
while (tmp) {
GClosure *closure = (GClosure *) tmp->data;
// Save the pointer, because phpg_gobject_unwatch_closure() will remove
// the closure from the list.
tmp = tmp->next;
if (closure) {
g_closure_invalidate(closure);
}
}
g_slist_free(intern->closures);
intern->closures = NULL;
if (intern->std.guards) {
zend_hash_destroy(intern->std.guards);
FREE_HASHTABLE(intern->std.guards);
}
if (intern->std.properties) {
zend_hash_destroy(intern->std.properties);
FREE_HASHTABLE(intern->std.properties);
}
efree(intern);
// php_printf("<= DONE\n");
}
zend_object_value gobject_gobject_object_new(zend_class_entry *ce TSRMLS_DC)
{
// php_printf("gobject_gobject_object_new()\n");
zend_object_value retval;
gobject_gobject_object *object;
object = emalloc(sizeof(gobject_gobject_object));
object->std.ce = ce;
object->std.guards = NULL;
ALLOC_HASHTABLE(object->std.properties);
zend_hash_init(object->std.properties, zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
zval *tmp;
zend_hash_copy(
object->std.properties,
&ce->default_properties,
(copy_ctor_func_t) zval_add_ref,
(void *) &tmp,
sizeof(zval *)
);
object->closures = g_slist_alloc();
object->gobject = NULL; // this is later assigned either in constructor or from external source
retval.handle = zend_objects_store_put(
object,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t) gobject_gobject_free_storage,
NULL
TSRMLS_CC
);
retval.handlers = php_gobject_gobject_handlers;
return retval;
}
// Zend property handlers
HashTable *php_gobject_gobject_get_properties(zval *object TSRMLS_DC)
{
// php_printf("getting properties (zend)\n");
GObject *gobject = __php_gobject_ptr(object);
GObjectClass *gobject_class = G_OBJECT_GET_CLASS(gobject);
guint n_properties = 0;
GParamSpec **params = g_object_class_list_properties(gobject_class, &n_properties);
HashTable *ht = NULL;
zend_hash_init(ht, 20, NULL, ZVAL_PTR_DTOR, 0);
for (guint i = 0; i < n_properties; i++) {
// zend_hash_next_index_insert(ht, pData, sizeof(zval), NULL);
}
return NULL;
}
zval *php_gobject_gobject_read_property(zval *zobject, zval *prop, int type TSRMLS_DC)
{
// php_printf("reading property (zend)\n");
GObject *gobject = __php_gobject_ptr(zobject);
char *property_name = Z_STRVAL_P(prop);
GValue gvalue = {0,};
GObjectClass *oclass = G_OBJECT_GET_CLASS(gobject);
GParamSpec *pspec = g_object_class_find_property(oclass, property_name);
g_value_init(&gvalue, pspec->value_type);
g_object_get_property(gobject, property_name, &gvalue);
zval *retval = NULL;
MAKE_STD_ZVAL(retval);
gvalue_with_gtype_to_zval(G_PARAM_SPEC_TYPE(pspec), &gvalue, retval TSRMLS_CC);
g_value_unset(&gvalue);
Z_DELREF_P(retval); // setting refcount to 0. receiving side is responsible for adding ref
return retval;
}
void php_gobject_gobject_write_property(zval *zobject, zval *prop, zval *value TSRMLS_DC)
{
// php_printf("writing property (zend)\n");
GObject *gobject = __php_gobject_ptr(zobject);
char *property_name = Z_STRVAL_P(prop);
GValue gvalue = {0,};
GObjectClass *oclass = G_OBJECT_GET_CLASS(gobject);
GParamSpec *pspec = g_object_class_find_property(oclass, property_name);
if (NULL == pspec) {
php_error(E_WARNING, "property %s is not found", property_name);
return;
}
zval_with_gtype_to_gvalue(G_PARAM_SPEC_TYPE(pspec), value, &gvalue, TRUE TSRMLS_CC);
g_object_set_property(gobject, property_name, &gvalue);
g_value_unset(&gvalue);
}
zval **php_gobject_gobject_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC)
{
// we don't want to provide direct access to underlying properties
return NULL;
}
// GLib property handlers
void php_gobject_gobject_init(PhpGObject *self, gpointer g_class)
{
// php_printf("php_gobject_gobject_init()\n");
ALLOC_HASHTABLE(self->glib_properties);
zend_hash_init(self->glib_properties, 10, NULL, (void (*)(void *))php_gobject_invalidate_gvalue, 0);
}
void php_gobject_gobject_finalize(PhpGObject *self)
{
// php_printf("php_gobject_gobject_finalize()\n");
zend_hash_destroy(self->glib_properties);
FREE_HASHTABLE(self->glib_properties);
self->glib_properties = NULL;
}
void php_gobject_gobject_set_glib_property(PhpGObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
property_id--;
// php_printf("writing property (glib)\n");
GValue *tmp = g_new0(GValue, 1);
g_value_init(tmp, G_VALUE_TYPE(value));
g_value_copy(value, tmp);
zend_hash_index_update(object->glib_properties, property_id, &tmp, sizeof(GValue *), NULL);
// g_value_array_insert(gobject_class->properties, property_id, value);
}
void php_gobject_gobject_get_glib_property(PhpGObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
property_id--;
// php_printf("reading property (glib)\n");
GValue **tmp = NULL;
if (FAILURE == zend_hash_index_find(object->glib_properties, property_id, (void **)&tmp)) {
php_error(E_WARNING, "didn't find property %d", property_id);
return;
}
g_value_copy(*tmp, value);
}
PHP_METHOD(Glib_GObject_GObject, __construct)
{
zval *self = getThis();
gobject_gobject_object *object = (gobject_gobject_object *)zend_object_store_get_object(self TSRMLS_CC);
zend_class_entry *ce = object->std.ce;
{
GType new_gtype = g_type_from_phpname(ce->name TSRMLS_CC);
if (new_gtype == 0 || new_gtype == 4) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "This class doesn't have known GObject counterpart: %s (or it couldn't be activated)", ce->name);
return;
}
if (G_TYPE_IS_ABSTRACT(new_gtype)) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Can not create instance of abstract class %s", ce->name);
return;
}
object->gobject = g_object_new(new_gtype, NULL);
g_object_set_data(object->gobject, "gobject-for-php", self);
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_GObject__signal_connect, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 2)
ZEND_ARG_INFO(0, signal_name)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, after)
ZEND_ARG_INFO(0, extra_param1)
ZEND_ARG_INFO(0, ...)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_GObject, signal_connect)
{
int signal_name_len = 0;
char *signal_name = NULL;
zend_bool after = FALSE;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
int params_count = 0;
zval ***params = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf|b*", &signal_name, &signal_name_len, &fci, &fci_cache, &after, ¶ms, ¶ms_count) == FAILURE) {
return;
}
zval *zval_object = getThis();
GObject *gobject = __php_gobject_ptr(zval_object);
GType gtype = G_OBJECT_TYPE(gobject);
guint signal_id;
GQuark signal_detail;
if (!g_signal_parse_name(signal_name, gtype, &signal_id, &signal_detail, TRUE)) {
php_error(E_WARNING, "%s signal name is invalid", signal_name);
if (params) {
efree(params);
}
return;
/* TODO , should we throw exception here? */
}
GClosure *closure = php_gobject_closure_new(gobject, fci, fci_cache, params, params_count TSRMLS_CC);
if (params) {
efree(params);
}
if (!closure) {
php_error(E_WARNING, "Couldn't create new closure");
return;
}
gulong handler_id = g_signal_connect_closure_by_id(gobject, signal_id, signal_detail, closure, after);
register_gobject_closure(zval_object, closure TSRMLS_CC);
RETURN_LONG((long)handler_id);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_GObject__signal_disconnect, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, handler_id)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_GObject, signal_disconnect)
{
long handler_id = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &handler_id) == FAILURE) {
return;
}
GObject *gobject = __php_gobject_ptr(getThis());
g_signal_handler_disconnect(gobject, (gulong)handler_id);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_GObject__notify, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, property_name)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_GObject, notify)
{
int property_name_len;
char *property_name;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &property_name, &property_name_len) == FAILURE) {
return;
}
GObject *gobject = __php_gobject_ptr(getThis());
g_object_notify(gobject, property_name);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_GObject__emit, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, signal_name)
ZEND_ARG_INFO(0, param1)
ZEND_ARG_INFO(0, ...)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_GObject, emit)
{
int signal_name_len = 0;
char *signal_name = NULL;
int params_len = 0;
zval ***params = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s*", &signal_name, &signal_name_len, ¶ms, ¶ms_len) == FAILURE) {
return;
}
GObject *gobject = __php_gobject_ptr(getThis());
GType gtype = G_OBJECT_TYPE(gobject);
guint signal_id = g_signal_lookup(signal_name, gtype);
if (signal_id == 0) {
php_error(E_WARNING, "Signal \"%s\" does not exist", signal_name);
if (params_len) {
efree(params);
}
return;
}
GValue *signal_params = ecalloc(1 + params_len, sizeof(GValue));
// signal_params[0] is reserved for instance
g_value_init(signal_params + 0, G_TYPE_OBJECT);
g_value_set_instance(signal_params + 0, G_OBJECT(gobject));
for (int i = 0; i < params_len; i++) {
zval **param = params[i];
if (zval_to_gvalue(*param, (signal_params + i + 1), 1 TSRMLS_CC) == FALSE) {
efree(signal_params);
php_error(E_WARNING, "conversion of param %d failed", i+1);
if (params_len) {
efree(params);
}
return;
}
}
GSignalQuery sig_query;
g_signal_query(signal_id, &sig_query);
if (sig_query.signal_id == 0) {
php_error(E_WARNING, "invalid signal");
return;
}
if (G_TYPE_NONE == sig_query.return_type) {
g_signal_emitv(signal_params, signal_id, 0, NULL);
} else {
GValue *retval = g_new0(GValue, 1);
g_value_init(retval, sig_query.return_type);
// php_printf("g_signal_emitv(%p, %d, 0, %p)\n", signal_params, signal_id, retval);
g_signal_emitv(signal_params, signal_id, 0, retval);
gvalue_to_zval(retval, return_value TSRMLS_CC);
g_free(retval);
}
for (int i = 0; i <= params_len; i++) {
g_value_unset(signal_params + i);
}
efree(signal_params);
if (params_len) {
efree(params);
}
}
const zend_function_entry gobject_gobject_methods[] = {
// public
PHP_ME(Glib_GObject_GObject, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Glib_GObject_GObject, signal_connect, arginfo_Glib_GObject_GObject__signal_connect, ZEND_ACC_PUBLIC)
PHP_ME(Glib_GObject_GObject, notify, arginfo_Glib_GObject_GObject__notify, ZEND_ACC_PUBLIC)
PHP_ME(Glib_GObject_GObject, emit, arginfo_Glib_GObject_GObject__emit, ZEND_ACC_PUBLIC)
PHP_ME(Glib_GObject_GObject, signal_disconnect, arginfo_Glib_GObject_GObject__signal_disconnect, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
PHP_MINIT_FUNCTION(gobject_gobject)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, GOBJECT_NAMESPACE, "Object", gobject_gobject_methods);
gobject_ce_gobject = zend_register_internal_class(&ce TSRMLS_CC);
gobject_ce_gobject->create_object = gobject_gobject_object_new;
// standard handlers + overriding
php_gobject_gobject_handlers = malloc(sizeof(zend_object_handlers));
memcpy(php_gobject_gobject_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_gobject_gobject_handlers->write_property = php_gobject_gobject_write_property;
php_gobject_gobject_handlers->read_property = php_gobject_gobject_read_property;
// php_gobject_gobject_handlers->get_properties = php_gobject_gobject_get_properties;
php_gobject_gobject_handlers->get_property_ptr_ptr = php_gobject_gobject_get_property_ptr_ptr;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(gobject_gobject)
{
free(php_gobject_gobject_handlers);
return SUCCESS;
}