-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgobject_type_class.c
525 lines (401 loc) · 15.4 KB
/
gobject_type_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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
+----------------------------------------------------------------------+
| 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_array.h>
#include <ext/spl/spl_exceptions.h>
#include "php_gobject_private.h"
zend_class_entry *gobject_ce_type;
static zend_object_handlers *php_gobject_type_handlers;
#define PHP_GOBJ_INIT_ARRAYOBJ(var) { \
MAKE_STD_ZVAL(var); \
object_init_ex(var, spl_ce_ArrayObject); \
zend_call_method_with_0_params(&var, spl_ce_ArrayObject, &spl_ce_ArrayObject->constructor, "__construct", NULL); \
}
zend_bool php_gobject_store_class_closure(GClosure *class_closure TSRMLS_DC)
{
HashTable *ht = &GOBJECT_G(class_closure_hash);
if (SUCCESS == zend_hash_next_index_insert(ht, (void*)&class_closure, sizeof(GClosure *), NULL))
return TRUE;
return FALSE;
}
void php_gobject_invalidate_class_closure(GClosure **class_closure)
{
g_closure_invalidate(*class_closure);
}
void gobject_type_free_storage(gobject_type_object *intern TSRMLS_DC)
{
if (intern->gtype) {
// g_param_spec_unref(intern->gtype);
}
zval_ptr_dtor(&intern->name);
zval_ptr_dtor(&intern->properties);
zval_ptr_dtor(&intern->signals);
zval_ptr_dtor(&intern->interfaces);
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);
}
zend_object_value gobject_type_object_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
gobject_type_object *object;
object = emalloc(sizeof(gobject_type_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->gtype = 0;
object->parent = 0;
object->is_registered = 0;
MAKE_STD_ZVAL(object->name);
PHP_GOBJ_INIT_ARRAYOBJ(object->properties);
PHP_GOBJ_INIT_ARRAYOBJ(object->signals);
PHP_GOBJ_INIT_ARRAYOBJ(object->interfaces);
retval.handle = zend_objects_store_put(
object,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t) gobject_type_free_storage,
NULL
TSRMLS_CC
);
retval.handlers = php_gobject_type_handlers;
return retval;
}
zval *php_gobject_type_read_property(zval *zobject, zval *prop, int type TSRMLS_DC)
{
const char *propname = Z_STRVAL_P(prop);
int proplen = Z_STRLEN_P(prop);
gobject_type_object *object = (gobject_type_object *)zend_objects_get_address(zobject TSRMLS_CC);
if ( proplen == 4 && strncmp(propname, "name", 4) == 0) {
return object->name;
} else if (proplen == 6 && strncmp(propname, "parent", 6) == 0) {
zval *retval = NULL;
MAKE_STD_ZVAL(retval);
ZVAL_STRING(retval, g_type_name(object->parent), 1);
Z_DELREF_P(retval); // setting refcount to 0. receiving side is responsible for adding ref
return retval;
} else if (proplen == 7 && strncmp(propname, "signals", 7) == 0) {
return object->signals;
} else if (proplen == 10 && strncmp(propname, "properties", 10) == 0) {
return object->properties;
} else if (proplen == 10 && strncmp(propname, "interfaces", 10) == 0) {
return object->interfaces;
} else {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "No way to get this property");
return NULL;
}
}
void php_gobject_type_write_property(zval *zobject, zval *prop, zval *value TSRMLS_DC)
{
const char *propname = Z_STRVAL_P(prop);
int proplen = Z_STRLEN_P(prop);
gobject_type_object *object = (gobject_type_object *)zend_objects_get_address(zobject TSRMLS_CC);
if (proplen == 4 && strncmp(propname, "name", 4) == 0) {
zval_ptr_dtor(&object->name); // free old one
Z_ADDREF_P(value);
object->name = value;
} else if (proplen == 6 && strncmp(propname, "parent", 6) == 0) {
convert_to_string(value);
const gchar *parent_name = Z_STRVAL_P(value);
GType parent_gtype = g_type_from_phpname(parent_name TSRMLS_CC);
if (0 == parent_gtype) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Unknown type name: %s", parent_name);
return;
}
object->parent = parent_gtype;
} else {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "No way to set this property");
return;
}
}
zval **php_gobject_type_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC)
{
// we don't want to provide direct access to underlying properties
return NULL;
}
zend_bool glib_gobject_type_import_class(const char *glib_name TSRMLS_DC)
{
GType type = g_type_from_name(glib_name);
if (type == 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class \"%s\" is not registered in GLib", glib_name);
return FALSE;
}
if (!G_TYPE_IS_OBJECT(type)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Glib-type \"%s\" is not a class", glib_name);
return FALSE;
}
GType parent = g_type_parent(type);
const gchar *parent_name = NULL;
if (parent != 0) {
parent_name = g_type_name(parent);
glib_gobject_type_import_class(parent_name TSRMLS_CC);
}
php_printf("Importing %s class\n", glib_name);
char *php_name = phpname_from_gtype(type);
zend_class_entry *ce = zend_fetch_class(php_name, strlen(php_name), ZEND_FETCH_CLASS_NO_AUTOLOAD TSRMLS_CC);
if (ce) {
efree(php_name);
php_printf("=> (was already loaded)\n");
return TRUE;
}
{
char *php_parent_name = phpname_from_gtype(parent);
zend_class_entry *parent_ce = zend_fetch_class(php_parent_name, strlen(php_parent_name), ZEND_FETCH_CLASS_NO_AUTOLOAD TSRMLS_CC);
efree(php_parent_name);
zend_class_entry ce;
INIT_CLASS_ENTRY_EX(ce, php_name, strlen(php_name), NULL);
zend_class_entry *target = zend_register_internal_class_ex(&ce, parent_ce, NULL TSRMLS_CC);
target->create_object = gobject_gobject_object_new;
}
efree(php_name);
return TRUE;
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_Type__from, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, classname_or_instance)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_Type, from)
{
zval *input = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &input) == FAILURE) {
return;
}
char *classname = NULL;
zend_uint classname_len = 0;
if (Z_TYPE_P(input) == IS_STRING) {
classname = Z_STRVAL_P(input);
classname_len = Z_STRLEN_P(input);
} else if (Z_TYPE_P(input) == IS_OBJECT) {
int need_to_copy = zend_get_object_classname(input, &classname, &classname_len TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "this method accepts only strings and objects");
return;
}
php_printf("MAGIC\b");
return;
// TODO: try to get class from gobject-hierarchy
zval *zobject;
MAKE_STD_ZVAL(zobject);
object_init_ex(zobject, gobject_ce_type);
gobject_type_object *object = (gobject_type_object *)zend_objects_get_address(zobject TSRMLS_CC);
object->is_registered = 1;
zend_call_method_with_0_params(&zobject, gobject_ce_type, &gobject_ce_type->constructor, "__construct", NULL);
RETURN_ZVAL(zobject, 0, 1);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_Glib_GObject_Type__import, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, glib_class_name)
ZEND_END_ARG_INFO()
PHP_METHOD(Glib_GObject_Type, import)
{
char *glib_name = NULL;
int glib_name_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &glib_name, &glib_name_len) == FAILURE) {
return;
}
zend_bool result = glib_gobject_type_import_class(glib_name TSRMLS_CC);
RETURN_BOOL(result);
}
// dummy constructor
PHP_METHOD(Glib_GObject_Type, __construct)
{
}
static int glib_gobject_type_register_signal(zend_object_iterator *iter, gobject_type_object **puser TSRMLS_DC)
{
zval **signal_p = NULL;
zend_user_it_get_current_data(iter, &signal_p TSRMLS_CC);
char *key;
uint key_len;
zend_user_it_get_current_key(iter, &key, &key_len, NULL TSRMLS_CC);
gobject_signal_object *gsignal = (gobject_signal_object *)zend_objects_get_address(*signal_p TSRMLS_CC);
if (gsignal->signal_id > 0) {
php_error(E_ERROR, "This signal associated with \"%s\" name is already registered. Clone object before reuse", key);
efree(key);
return ZEND_HASH_APPLY_STOP;
}
gobject_type_object *object = *puser;
GType *param_types = NULL;
guint param_count = 0;
if (gsignal->param_types) {
HashTable *hash = HASH_OF(gsignal->param_types);
param_count = zend_hash_num_elements(hash);
param_types = ecalloc(param_count, sizeof(GType));
ulong i;
for (i = 0; i < param_count; i++) {
void *pdata = NULL;
if (zend_hash_index_find(hash, i, &pdata) == FAILURE) {
php_error(E_WARNING, "Couldn't fetch parameter #%ld of signal", i);
continue;
}
// zval *param = *((zval **) pdata);
zval **param_p = (zval **) pdata;
param_types[i] = g_type_from_phpname(Z_STRVAL_PP(param_p) TSRMLS_CC);
}
}
GClosure *class_closure = NULL;
if (!callback_is_empty(&gsignal->class_closure_fci)) {
class_closure = php_gobject_closure_new_class(gsignal->class_closure_fci, gsignal->class_closure_fci_cache TSRMLS_CC);
php_gobject_store_class_closure(class_closure TSRMLS_CC);
}
GSignalAccumulator accu = NULL;
if (!callback_is_empty(&gsignal->accumulator_fci)) {
accu = php_gobject_closure_accumulator;
}
gsignal->signal_id = g_signal_newv(
key, object->gtype, gsignal->flags, // name, class, flags
class_closure, // default closure
accu, NULL, // accu + data
php_gobject_closure_marshal, // marshaller
gsignal->return_type, param_count, param_types // return + parameters
);
php_gobject_store_signal_association(*signal_p TSRMLS_CC);
if (param_types) {
efree(param_types);
}
efree(key);
return ZEND_HASH_APPLY_KEEP;
}
static int glib_gobject_type_register_property(zend_object_iterator *iter, GObjectClass **type_class_ptr TSRMLS_DC)
{
zval **pspec_p = NULL;
zend_user_it_get_current_data(iter, &pspec_p TSRMLS_CC);
char *key = NULL;
uint key_len;
ulong int_key;
zend_user_it_get_current_key(iter, &key, &key_len, &int_key TSRMLS_CC);
GObjectClass *type_class = *type_class_ptr;
gobject_paramspec_object *gpspec = (gobject_paramspec_object *)zend_objects_get_address(*pspec_p TSRMLS_CC);
GParamSpec *pspec = gpspec->paramspec;
g_object_class_install_property(type_class, int_key + 1, pspec);
if (key) {
efree(key);
}
return ZEND_HASH_APPLY_KEEP;
}
PHP_METHOD(Glib_GObject_Type, generate)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
gobject_type_object *object = (gobject_type_object *)zend_objects_get_address(getThis() TSRMLS_CC);
const char *class_name = Z_STRVAL_P(object->name);
GType new_gtype = g_type_from_name(class_name);
if (0 != new_gtype) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "This class is already registered: %s", class_name);
return;
}
// 1. register gobject class
new_gtype = g_type_register_static_simple(
object->parent, class_name,
sizeof(GObjectClass), NULL,
sizeof(PhpGObject), (GInstanceInitFunc)php_gobject_gobject_init,
0
);
object->gtype = new_gtype;
if (0 == new_gtype) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Failed to initialise type: %s", class_name);
return;
}
// 2. register php class
{
const char *parent_name = g_type_name(object->parent);
char *php_parent_name = phpname_from_gtype(object->parent);
zend_class_entry *parent_ce = zend_fetch_class(php_parent_name, strlen(php_parent_name), ZEND_FETCH_CLASS_NO_AUTOLOAD TSRMLS_CC);
if (NULL == parent_ce) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Parent PHP class (%s) is not registered", php_parent_name);
efree(php_parent_name);
return;
}
efree(php_parent_name);
zend_class_entry ce;
char *php_class_name = phpname_from_gtype(new_gtype);
INIT_CLASS_ENTRY_EX(ce, php_class_name, strlen(php_class_name), NULL);
efree(php_class_name);
zend_class_entry *target = zend_register_internal_class_ex(&ce, parent_ce, NULL TSRMLS_CC);
target->create_object = gobject_gobject_object_new;
}
// 3. register signals
{
spl_iterator_apply(object->signals, (spl_iterator_apply_func_t)glib_gobject_type_register_signal, (void *)&object TSRMLS_CC);
}
// 4. register properties
{
GObjectClass *type_class = g_type_class_ref(new_gtype);
type_class->get_property = (GObjectGetPropertyFunc)php_gobject_gobject_get_glib_property;
type_class->set_property = (GObjectSetPropertyFunc)php_gobject_gobject_set_glib_property;
type_class->finalize = (GObjectFinalizeFunc)php_gobject_gobject_finalize;
spl_iterator_apply(object->properties, (spl_iterator_apply_func_t)glib_gobject_type_register_property, (void *)&type_class TSRMLS_CC);
g_type_class_unref(type_class); // not needed anymore
}
// object->is_registered = 1;
RETURN_TRUE;
}
PHP_RINIT_FUNCTION(gobject_type)
{
zend_hash_init(&GOBJECT_G(class_closure_hash), 50, NULL, (void (*)(void *))php_gobject_invalidate_class_closure, 0);
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(gobject_type)
{
zend_hash_graceful_destroy(&GOBJECT_G(class_closure_hash));
return SUCCESS;
}
const zend_function_entry gobject_type_methods[] = {
// public
PHP_ME(Glib_GObject_Type, from, arginfo_Glib_GObject_Type__from, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Glib_GObject_Type, import, arginfo_Glib_GObject_Type__import, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Glib_GObject_Type, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Glib_GObject_Type, generate, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
PHP_MINIT_FUNCTION(gobject_type)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, GOBJECT_NAMESPACE, "Type", gobject_type_methods);
gobject_ce_type = zend_register_internal_class(&ce TSRMLS_CC);
gobject_ce_type->create_object = gobject_type_object_new;
// standard handlers + overriding
php_gobject_type_handlers = malloc(sizeof(zend_object_handlers));
memcpy(php_gobject_type_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_gobject_type_handlers->write_property = php_gobject_type_write_property;
php_gobject_type_handlers->read_property = php_gobject_type_read_property;
php_gobject_type_handlers->get_property_ptr_ptr = php_gobject_type_get_property_ptr_ptr;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(gobject_type)
{
free(php_gobject_type_handlers);
return SUCCESS;
}