2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include "php_pq_object.h"
21 void *php_pq_object_create(zend_class_entry
*ce
, void *intern
, size_t obj_size
, zend_object_handlers
*oh
, HashTable
*ph
)
23 php_pq_object_t
*o
= ecalloc(1, obj_size
+ zend_object_properties_size(ce
));
25 zend_object_std_init(&o
->zo
, ce
);
26 object_properties_init(&o
->zo
, ce
);
31 zend_hash_init(&o
->gc
, 0, NULL
, NULL
, 0);
36 void php_pq_object_dtor(zend_object
*o
)
38 php_pq_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
40 zend_hash_destroy(&obj
->gc
);
41 zend_object_std_dtor(o
);
44 void php_pq_object_to_zval(void *o
, zval
*zv
)
46 php_pq_object_t
*obj
= o
;
48 ZVAL_OBJ(zv
, &obj
->zo
);
52 void php_pq_object_to_zval_no_addref(void *o
, zval
*zv
)
54 php_pq_object_t
*obj
= o
;
56 ZVAL_OBJ(zv
, &obj
->zo
);
59 void php_pq_object_addref(void *o
)
61 php_pq_object_t
*obj
= o
;
65 ++GC_REFCOUNT(&obj
->zo
);
69 void php_pq_object_delref(void *o
)
71 php_pq_object_t
*obj
= o
;
74 /* this should gc immediately */
75 ZVAL_OBJ(&tmp
, &obj
->zo
);
79 struct apply_pi_to_ht_arg
{
82 php_pq_object_t
*pq_obj
;
86 static int apply_pi_to_ht(zval
*p
, void *a
)
88 zend_property_info
*pi
= Z_PTR_P(p
);
89 struct apply_pi_to_ht_arg
*arg
= a
;
92 php_pq_object_prophandler_t
*handler
;
94 if ((handler
= zend_hash_find_ptr(arg
->pq_obj
->prophandler
, pi
->name
)) && handler
->gc
) {
95 zval member
, return_value
;
97 ZVAL_STR(&member
, pi
->name
);
98 ZVAL_ARR(&return_value
, arg
->ht
);
99 handler
->gc(arg
->object
, arg
->pq_obj
, &return_value
);
102 zval tmp_prop
, *property
= NULL
;
104 property
= zend_read_property(arg
->pq_obj
->zo
.ce
, arg
->object
, pi
->name
->val
, pi
->name
->len
, 0, &tmp_prop
);
105 zend_hash_update(arg
->ht
, pi
->name
, property
);
108 return ZEND_HASH_APPLY_KEEP
;
111 HashTable
*php_pq_object_debug_info(zval
*object
, int *temp
)
113 struct apply_pi_to_ht_arg arg
= {NULL
};
116 ALLOC_HASHTABLE(arg
.ht
);
117 ZEND_INIT_SYMTABLE(arg
.ht
);
120 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
123 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
128 HashTable
*php_pq_object_properties(zval
*object
)
130 struct apply_pi_to_ht_arg arg
= {NULL
};
132 arg
.ht
= zend_get_std_object_handlers()->get_properties(object
);
134 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
137 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
142 HashTable
*php_pq_object_get_gc(zval
*object
, zval
**table
, int *n
)
144 struct apply_pi_to_ht_arg arg
= {NULL
};
147 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
148 arg
.ht
= &arg
.pq_obj
->gc
;
151 zend_hash_clean(arg
.ht
);
152 zend_hash_copy(arg
.ht
, zend_std_get_properties(object
), NULL
);
153 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
161 zend_class_entry
*ancestor(zend_class_entry
*ce
)
169 zval
*php_pq_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
171 php_pq_object_t
*obj
= PHP_PQ_OBJ(object
, NULL
);
172 php_pq_object_prophandler_t
*handler
;
173 zval
*return_value
= NULL
;
175 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
178 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
179 } else if (!(handler
= zend_hash_find_ptr(obj
->prophandler
, Z_STR_P(member
))) || !handler
->read
) {
180 /* default handler */
181 } else if (type
!= BP_VAR_R
) {
182 php_error(E_WARNING
, "Cannot access %s properties by reference or array key/index", ancestor(obj
->zo
.ce
)->name
->val
);
184 handler
->read(object
, obj
, tmp
);
185 zend_get_std_object_handlers()->write_property(object
, member
, tmp
, cache_slot
);
191 ZVAL_COPY_VALUE(&dtor, return_value);
193 ZVAL_ZVAL(return_value, tmp, 0, 0);
194 zval_ptr_dtor(&dtor);
206 void php_pq_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
208 php_pq_object_t
*obj
= PHP_PQ_OBJ(object
, NULL
);
209 php_pq_object_prophandler_t
*handler
;
212 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
213 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
214 } else if ((handler
= zend_hash_find_ptr(obj
->prophandler
, Z_STR_P(member
)))) {
215 if (handler
->write
) {
216 handler
->write(object
, obj
, value
);
219 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
223 void php_pq_object_prophandler_dtor(zval
*zv
) {
224 pefree(Z_PTR_P(zv
), 1);