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
;
62 ++GC_REFCOUNT(&obj
->zo
);
65 void php_pq_object_delref(void *o
)
67 php_pq_object_t
*obj
= o
;
70 /* this should gc immediately */
71 ZVAL_OBJ(&tmp
, &obj
->zo
);
75 struct apply_pi_to_ht_arg
{
78 php_pq_object_t
*pq_obj
;
82 static int apply_pi_to_ht(zval
*p
, void *a
)
84 zend_property_info
*pi
= Z_PTR_P(p
);
85 struct apply_pi_to_ht_arg
*arg
= a
;
88 php_pq_object_prophandler_t
*handler
;
90 if ((handler
= zend_hash_find_ptr(arg
->pq_obj
->prophandler
, pi
->name
)) && handler
->gc
) {
91 zval member
, return_value
;
93 ZVAL_STR(&member
, pi
->name
);
94 ZVAL_ARR(&return_value
, arg
->ht
);
95 handler
->gc(arg
->object
, arg
->pq_obj
, &return_value
);
98 zval tmp_prop
, *property
= NULL
;
100 property
= zend_read_property(arg
->pq_obj
->zo
.ce
, arg
->object
, pi
->name
->val
, pi
->name
->len
, 0, &tmp_prop
);
101 zend_hash_update(arg
->ht
, pi
->name
, property
);
104 return ZEND_HASH_APPLY_KEEP
;
107 HashTable
*php_pq_object_debug_info(zval
*object
, int *temp
)
109 struct apply_pi_to_ht_arg arg
= {NULL
};
112 ALLOC_HASHTABLE(arg
.ht
);
113 ZEND_INIT_SYMTABLE(arg
.ht
);
116 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
119 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
124 HashTable
*php_pq_object_properties(zval
*object
)
126 struct apply_pi_to_ht_arg arg
= {NULL
};
128 arg
.ht
= zend_get_std_object_handlers()->get_properties(object
);
130 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
133 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
138 HashTable
*php_pq_object_get_gc(zval
*object
, zval
**table
, int *n
)
140 struct apply_pi_to_ht_arg arg
= {NULL
};
143 arg
.pq_obj
= PHP_PQ_OBJ(object
, NULL
);
144 arg
.ht
= &arg
.pq_obj
->gc
;
147 zend_hash_clean(arg
.ht
);
148 zend_hash_copy(arg
.ht
, zend_std_get_properties(object
), NULL
);
149 zend_hash_apply_with_argument(&arg
.pq_obj
->zo
.ce
->properties_info
, apply_pi_to_ht
, &arg
);
157 zend_class_entry
*ancestor(zend_class_entry
*ce
)
165 zval
*php_pq_object_read_prop(zval
*object
, zval
*member
, int type
, void **cache_slot
, zval
*tmp
)
167 php_pq_object_t
*obj
= PHP_PQ_OBJ(object
, NULL
);
168 php_pq_object_prophandler_t
*handler
;
169 zval
*return_value
= NULL
;
171 return_value
= zend_get_std_object_handlers()->read_property(object
, member
, type
, cache_slot
, tmp
);
174 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
175 } else if (!(handler
= zend_hash_find_ptr(obj
->prophandler
, Z_STR_P(member
))) || !handler
->read
) {
176 /* default handler */
177 } else if (type
!= BP_VAR_R
) {
178 php_error(E_WARNING
, "Cannot access %s properties by reference or array key/index", ancestor(obj
->zo
.ce
)->name
->val
);
180 handler
->read(object
, obj
, tmp
);
181 zend_get_std_object_handlers()->write_property(object
, member
, tmp
, cache_slot
);
187 ZVAL_COPY_VALUE(&dtor, return_value);
189 ZVAL_ZVAL(return_value, tmp, 0, 0);
190 zval_ptr_dtor(&dtor);
202 void php_pq_object_write_prop(zval
*object
, zval
*member
, zval
*value
, void **cache_slot
)
204 php_pq_object_t
*obj
= PHP_PQ_OBJ(object
, NULL
);
205 php_pq_object_prophandler_t
*handler
;
208 php_error(E_RECOVERABLE_ERROR
, "%s not initialized", ancestor(obj
->zo
.ce
)->name
->val
);
209 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
210 } else if ((handler
= zend_hash_find_ptr(obj
->prophandler
, Z_STR_P(member
)))) {
211 if (handler
->write
) {
212 handler
->write(object
, obj
, value
);
215 zend_get_std_object_handlers()->write_property(object
, member
, value
, cache_slot
);
219 void php_pq_object_prophandler_dtor(zval
*zv
) {
220 pefree(Z_PTR_P(zv
), 1);