X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pq_object.c;fp=src%2Fphp_pq_object.c;h=c7e04d63a52149b480eace075ae3d989d06c1d0b;hp=bfab4101402fb98bdd50bd7aadeb0804ddf94dea;hb=cb69b6218d9d6e789527b3bd62c3cc37686547a3;hpb=6e28a8741be3fccdaca960e492c31bc26837a4ed diff --git a/src/php_pq_object.c b/src/php_pq_object.c index bfab410..c7e04d6 100644 --- a/src/php_pq_object.c +++ b/src/php_pq_object.c @@ -18,6 +18,29 @@ #include "php_pq_object.h" +void *php_pq_object_create(zend_class_entry *ce, void *intern, size_t obj_size, zend_object_handlers *oh, HashTable *ph) +{ + php_pq_object_t *o = ecalloc(1, obj_size + zend_object_properties_size(ce)); + + zend_object_std_init(&o->zo, ce); + object_properties_init(&o->zo, ce); + o->zo.handlers = oh; + o->intern = intern; + o->prophandler = ph; + + zend_hash_init(&o->gc, 0, NULL, NULL, 0); + + return o; +} + +void php_pq_object_dtor(zend_object *o) +{ + php_pq_object_t *obj = PHP_PQ_OBJ(NULL, o); + + zend_hash_destroy(&obj->gc); + zend_object_std_dtor(o); +} + void php_pq_object_to_zval(void *o, zval *zv) { php_pq_object_t *obj = o; @@ -39,29 +62,44 @@ void php_pq_object_addref(void *o) ++GC_REFCOUNT(&obj->zo); } -void php_pq_object_delref(void *o TSRMLS_DC) +void php_pq_object_delref(void *o) { php_pq_object_t *obj = o; - zend_objects_store_del(&obj->zo); + zval tmp; + + /* this should gc immediately */ + ZVAL_OBJ(&tmp, &obj->zo); + zval_ptr_dtor(&tmp); } struct apply_pi_to_ht_arg { HashTable *ht; zval *object; php_pq_object_t *pq_obj; - unsigned addref:1; + unsigned gc:1; }; static int apply_pi_to_ht(zval *p, void *a) { zend_property_info *pi = Z_PTR_P(p); struct apply_pi_to_ht_arg *arg = a; - zval tmp_prop, *property = zend_read_property(arg->pq_obj->zo.ce, arg->object, pi->name->val, pi->name->len, 0, &tmp_prop); - if (arg->addref) { - Z_TRY_ADDREF_P(property); + if (arg->gc) { + php_pq_object_prophandler_t *handler; + + if ((handler = zend_hash_find_ptr(arg->pq_obj->prophandler, pi->name)) && handler->gc) { + zval member, return_value; + + ZVAL_STR(&member, pi->name); + ZVAL_ARR(&return_value, arg->ht); + handler->gc(arg->object, arg->pq_obj, &return_value); + } + } else { + zval tmp_prop, *property = NULL; + + property = zend_read_property(arg->pq_obj->zo.ce, arg->object, pi->name->val, pi->name->len, 0, &tmp_prop); + zend_hash_update(arg->ht, pi->name, property); } - zend_hash_update(arg->ht, pi->name, property); return ZEND_HASH_APPLY_KEEP; } @@ -76,7 +114,7 @@ HashTable *php_pq_object_debug_info(zval *object, int *temp) arg.object = object; arg.pq_obj = PHP_PQ_OBJ(object, NULL); - arg.addref = 1; + arg.gc = 0; zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg); @@ -90,10 +128,29 @@ HashTable *php_pq_object_properties(zval *object) arg.ht = zend_get_std_object_handlers()->get_properties(object); arg.object = object; arg.pq_obj = PHP_PQ_OBJ(object, NULL); - arg.addref = 1; + arg.gc = 0; + + zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg); + + return arg.ht; +} + +HashTable *php_pq_object_get_gc(zval *object, zval **table, int *n) +{ + struct apply_pi_to_ht_arg arg = {NULL}; + + arg.object = object; + arg.pq_obj = PHP_PQ_OBJ(object, NULL); + arg.ht = &arg.pq_obj->gc; + arg.gc = 1; + zend_hash_clean(arg.ht); + zend_hash_copy(arg.ht, zend_std_get_properties(object), NULL); zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg); + *table = NULL; + *n = 0; + return arg.ht; } @@ -111,17 +168,32 @@ zval *php_pq_object_read_prop(zval *object, zval *member, int type, void **cache php_pq_object_prophandler_t *handler; zval *return_value = NULL; + return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp); + if (!obj->intern) { - php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name); - return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp); - } else if (!(handler= zend_hash_find_ptr(obj->prophandler, Z_STR_P(member))) || !handler->read) { - return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp); + php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val); + } else if (!(handler = zend_hash_find_ptr(obj->prophandler, Z_STR_P(member))) || !handler->read) { + /* default handler */ } else if (type != BP_VAR_R) { php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val); - return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp); } else { + handler->read(object, obj, tmp); + zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot); return_value = tmp; - handler->read(object, obj, return_value); + + /* + zval dtor; + + ZVAL_COPY_VALUE(&dtor, return_value); + + ZVAL_ZVAL(return_value, tmp, 0, 0); + zval_ptr_dtor(&dtor); + + */ + + if (cache_slot) { + *cache_slot = NULL; + } } return return_value; @@ -144,3 +216,7 @@ void php_pq_object_write_prop(zval *object, zval *member, zval *value, void **ca } } +void php_pq_object_prophandler_dtor(zval *zv) { + pefree(Z_PTR_P(zv), 1); +} +