X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pq_object.c;h=27d740eba2369776771804b8ee95073d422c73de;hp=77207ea82f35136f83f36603ed407b7652e143ee;hb=d3e3e97737cda9dafbb17c9076251c09a92bdf78;hpb=d96d8d4af09d7f32fec302908a5e4ee8d8bee7fb diff --git a/src/php_pq_object.c b/src/php_pq_object.c index 77207ea..27d740e 100644 --- a/src/php_pq_object.c +++ b/src/php_pq_object.c @@ -18,85 +18,160 @@ #include "php_pq_object.h" -void php_pq_object_to_zval(void *o, zval **zv TSRMLS_DC) +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 *obj = o; + php_pq_object_t *o = ecalloc(1, obj_size + zend_object_properties_size(ce)); - if (!*zv) { - MAKE_STD_ZVAL(*zv); - } + 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; +} - zend_objects_store_add_ref_by_handle(obj->zv.handle TSRMLS_CC); +void php_pq_object_dtor(zend_object *o) +{ + php_pq_object_t *obj = PHP_PQ_OBJ(NULL, o); - (*zv)->type = IS_OBJECT; - (*zv)->value.obj = obj->zv; + zend_hash_destroy(&obj->gc); + zend_object_std_dtor(o); } -void php_pq_object_to_zval_no_addref(void *o, zval **zv TSRMLS_DC) +void php_pq_object_to_zval(void *o, zval *zv) { php_pq_object_t *obj = o; - if (!*zv) { - MAKE_STD_ZVAL(*zv); - } + ZVAL_OBJ(zv, &obj->zo); + Z_ADDREF_P(zv); +} - /* no add ref */ +void php_pq_object_to_zval_no_addref(void *o, zval *zv) +{ + php_pq_object_t *obj = o; - (*zv)->type = IS_OBJECT; - (*zv)->value.obj = obj->zv; + ZVAL_OBJ(zv, &obj->zo); } -void php_pq_object_addref(void *o TSRMLS_DC) +void php_pq_object_addref(void *o) { php_pq_object_t *obj = o; - zend_objects_store_add_ref_by_handle(obj->zv.handle TSRMLS_CC); +#ifdef GC_ADDREF + GC_ADDREF(&obj->zo); +#else + ++GC_REFCOUNT(&obj->zo); +#endif } -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_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC); + zval tmp; + + /* this should gc immediately */ + ZVAL_OBJ(&tmp, &obj->zo); + zval_ptr_dtor(&tmp); } -static int apply_pi_to_ht(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) +struct apply_pi_to_ht_arg { + HashTable *ht; + php_pq_object_t *pq_obj; + unsigned gc:1; +}; + +static int apply_pi_to_ht(zval *p, void *a) { - zend_property_info *pi = p; - HashTable *ht = va_arg(argv, HashTable *); - zval *object = va_arg(argv, zval *); - php_pq_object_t *obj = va_arg(argv, php_pq_object_t *); - int addref = va_arg(argv, int); - zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC); + zend_property_info *pi = Z_PTR_P(p); + struct apply_pi_to_ht_arg *arg = a; + + 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; - if (addref) { - Z_ADDREF_P(property); + ZVAL_STR(&member, pi->name); + ZVAL_ARR(&return_value, arg->ht); + handler->gc(arg->pq_obj, &return_value); + } + } else { + zval tmp_prop, *property = NULL; + + property = php_pq_object_read_prop_80(&arg->pq_obj->zo, pi->name, BP_VAR_R, NULL, &tmp_prop); + zend_hash_update(arg->ht, pi->name, property); } - zend_hash_update(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL); return ZEND_HASH_APPLY_KEEP; } -HashTable *php_pq_object_debug_info(zval *object, int *temp TSRMLS_DC) +HashTable *php_pq_object_debug_info_80(zend_object *object, int *temp) { - HashTable *ht; - php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + struct apply_pi_to_ht_arg arg = {NULL}; *temp = 1; - ALLOC_HASHTABLE(ht); - ZEND_INIT_SYMTABLE(ht); + ALLOC_HASHTABLE(arg.ht); + ZEND_INIT_SYMTABLE(arg.ht); - zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_ht, 4, ht, object, obj, 1); + arg.pq_obj = PHP_PQ_OBJ(NULL, object); + arg.gc = 0; - return ht; + zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg); + + return arg.ht; +} +HashTable *php_pq_object_debug_info_70(zval *object, int *temp) +{ + return php_pq_object_debug_info_80(Z_OBJ_P(object), temp); +} + +static inline HashTable *php_pq_object_properties_ex(zend_object *object, HashTable *props) +{ + struct apply_pi_to_ht_arg arg = {NULL}; + + arg.ht = props; + arg.pq_obj = PHP_PQ_OBJ(NULL, object); + 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_properties_80(zend_object *object) +{ + return php_pq_object_properties_ex(object, zend_std_get_properties(object)); +} +HashTable *php_pq_object_properties_70(zval *object) +{ + return php_pq_object_properties_ex(Z_OBJ_P(object), zend_std_get_properties(object)); } -HashTable *php_pq_object_properties(zval *object TSRMLS_DC) +static inline HashTable *php_pq_object_get_gc_ex(zend_object *object, HashTable *props, zval **table, int *n) { - HashTable *ht = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC); - php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + struct apply_pi_to_ht_arg arg = {NULL}; + + arg.pq_obj = PHP_PQ_OBJ(NULL, object); + arg.ht = &arg.pq_obj->gc; + arg.gc = 1; + + zend_hash_clean(arg.ht); + zend_hash_copy(arg.ht, props, NULL); + zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg); - zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_ht, 4, ht, object, obj, 1); + *table = NULL; + *n = 0; - return ht; + return arg.ht; +} +HashTable *php_pq_object_get_gc_80(zend_object *object, zval **table, int *n) +{ + return php_pq_object_get_gc_ex(object, zend_std_get_properties(object), table, n); +} +HashTable *php_pq_object_get_gc_70(zval *object, zval **table, int *n) +{ + return php_pq_object_get_gc_ex(Z_OBJ_P(object), zend_std_get_properties(object), table, n); } zend_class_entry *ancestor(zend_class_entry *ce) @@ -107,45 +182,95 @@ zend_class_entry *ancestor(zend_class_entry *ce) return ce; } -zval *php_pq_object_read_prop(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) +zval *php_pq_object_read_prop_ex(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp, zval *def) { - php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object); php_pq_object_prophandler_t *handler; - zval *return_value = NULL; + zval *return_value = def; 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, key TSRMLS_CC); - } else if ((SUCCESS != zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) || !handler->read) { - return_value = zend_get_std_object_handlers()->read_property(object, member, type, key TSRMLS_CC); + php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val); + } else if (!(handler = zend_hash_find_ptr(obj->prophandler, 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); - return_value = zend_get_std_object_handlers()->read_property(object, member, type, key TSRMLS_CC); + php_error(E_WARNING, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name->val); } else { - ALLOC_ZVAL(return_value); - Z_SET_REFCOUNT_P(return_value, 0); - Z_UNSET_ISREF_P(return_value); + handler->read(obj, tmp); + zend_get_std_object_handlers()->write_property(object, member, tmp, cache_slot); + return_value = tmp; - handler->read(object, obj, return_value TSRMLS_CC); + if (cache_slot) { + *cache_slot = NULL; + } } return return_value; } +zval *php_pq_object_read_prop_80(zend_object *object, zend_string *member, int type, void **cache_slot, zval *tmp) +{ + return php_pq_object_read_prop_ex(object, member, type, cache_slot, tmp, zend_std_read_property(object, member, type, cache_slot, tmp)); +} +zval *php_pq_object_read_prop_70(zval *object, zval *member, int type, void **cache_slot, zval *tmp) +{ + zend_string *member_str = zval_get_string(member); + zval *return_value = php_pq_object_read_prop_ex(Z_OBJ_P(object), member_str, type, cache_slot, tmp, zend_std_read_property(object, member, type, cache_slot, tmp)); + zend_string_release(member_str); + return return_value; +} -void php_pq_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) +zval *php_pq_object_write_prop_ex(zend_object *object, zend_string *member, zval *value, void **cache_slot, int *update_std) { - php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + php_pq_object_t *obj = PHP_PQ_OBJ(NULL, object); php_pq_object_prophandler_t *handler; if (!obj->intern) { - php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name); - zend_get_std_object_handlers()->write_property(object, member, value, key TSRMLS_CC); - } else if (SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) { + php_error(E_RECOVERABLE_ERROR, "%s not initialized", ancestor(obj->zo.ce)->name->val); + *update_std = 1; + } else if ((handler = zend_hash_find_ptr(obj->prophandler, member))) { if (handler->write) { - handler->write(object, obj, value TSRMLS_CC); + handler->write(obj, value); } + *update_std = 0; } else { - zend_get_std_object_handlers()->write_property(object, member, value, key TSRMLS_CC); + *update_std = 1; } + return value; +} +zval *php_pq_object_write_prop_80(zend_object *object, zend_string *member, zval *value, void **cache_slot) +{ + int update_std = 0; + zval *return_value = php_pq_object_write_prop_ex(object, member, value, cache_slot, &update_std); + if (update_std) { + return_value = zend_std_write_property(object, member, value, cache_slot); + } + return return_value; +} +zval *php_pq_object_write_prop_74(zval *object, zval *member, zval *value, void **cache_slot) +{ + int update_std = 0; + zend_string *member_str = zval_get_string(member); + zval *return_value = php_pq_object_write_prop_ex(Z_OBJ_P(object), member_str, value, cache_slot, &update_std); + zend_string_release(member_str); + if (update_std) { + return_value = zend_std_write_property(object, member, value, cache_slot); + } + return return_value; +} +void php_pq_object_write_prop_70(zval *object, zval *member, zval *value, void **cache_slot) +{ + (void) php_pq_object_write_prop_74(object, member, value, cache_slot); +} + +zval *php_pq_object_get_prop_ptr_null_80(zend_object *object, zend_string *member, int type, void **cache_slot) +{ + return NULL; +} +zval *php_pq_object_get_prop_ptr_null_70(zval *object, zval *member, int type, void **cache_slot) +{ + return NULL; +} + +void php_pq_object_prophandler_dtor(zval *zv) { + pefree(Z_PTR_P(zv), 1); }