xref: /PHP-7.3/Zend/zend_object_handlers.h (revision f78e6814)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifndef ZEND_OBJECT_HANDLERS_H
21 #define ZEND_OBJECT_HANDLERS_H
22 
23 struct _zend_property_info;
24 
25 #define ZEND_WRONG_PROPERTY_INFO \
26 	((struct _zend_property_info*)((intptr_t)-1))
27 
28 #define ZEND_DYNAMIC_PROPERTY_OFFSET               ((uintptr_t)(intptr_t)(-1))
29 
30 #define IS_VALID_PROPERTY_OFFSET(offset)           ((intptr_t)(offset) > 0)
31 #define IS_WRONG_PROPERTY_OFFSET(offset)           ((intptr_t)(offset) == 0)
32 #define IS_DYNAMIC_PROPERTY_OFFSET(offset)         ((intptr_t)(offset) < 0)
33 
34 #define IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(offset) (offset == ZEND_DYNAMIC_PROPERTY_OFFSET)
35 #define ZEND_DECODE_DYN_PROP_OFFSET(offset)        ((uintptr_t)(-(intptr_t)(offset) - 2))
36 #define ZEND_ENCODE_DYN_PROP_OFFSET(offset)        ((uintptr_t)(-((intptr_t)(offset) + 2)))
37 
38 
39 /* The following rule applies to read_property() and read_dimension() implementations:
40    If you return a zval which is not otherwise referenced by the extension or the engine's
41    symbol table, its reference count should be 0.
42 */
43 /* Used to fetch property from the object, read-only */
44 typedef zval *(*zend_object_read_property_t)(zval *object, zval *member, int type, void **cache_slot, zval *rv);
45 
46 /* Used to fetch dimension from the object, read-only */
47 typedef zval *(*zend_object_read_dimension_t)(zval *object, zval *offset, int type, zval *rv);
48 
49 
50 /* The following rule applies to write_property() and write_dimension() implementations:
51    If you receive a value zval in write_property/write_dimension, you may only modify it if
52    its reference count is 1.  Otherwise, you must create a copy of that zval before making
53    any changes.  You should NOT modify the reference count of the value passed to you.
54 */
55 /* Used to set property of the object */
56 typedef void (*zend_object_write_property_t)(zval *object, zval *member, zval *value, void **cache_slot);
57 
58 /* Used to set dimension of the object */
59 typedef void (*zend_object_write_dimension_t)(zval *object, zval *offset, zval *value);
60 
61 
62 /* Used to create pointer to the property of the object, for future direct r/w access */
63 typedef zval *(*zend_object_get_property_ptr_ptr_t)(zval *object, zval *member, int type, void **cache_slot);
64 
65 /* Used to set object value. Can be used to override assignments and scalar
66    write ops (like ++, +=) on the object */
67 typedef void (*zend_object_set_t)(zval *object, zval *value);
68 
69 /* Used to get object value. Can be used when converting object value to
70  * one of the basic types and when using scalar ops (like ++, +=) on the object
71  */
72 typedef zval* (*zend_object_get_t)(zval *object, zval *rv);
73 
74 /* Used to check if a property of the object exists */
75 /* param has_set_exists:
76  * 0 (has) whether property exists and is not NULL
77  * 1 (set) whether property exists and is true
78  * 2 (exists) whether property exists
79  */
80 typedef int (*zend_object_has_property_t)(zval *object, zval *member, int has_set_exists, void **cache_slot);
81 
82 /* Used to check if a dimension of the object exists */
83 typedef int (*zend_object_has_dimension_t)(zval *object, zval *member, int check_empty);
84 
85 /* Used to remove a property of the object */
86 typedef void (*zend_object_unset_property_t)(zval *object, zval *member, void **cache_slot);
87 
88 /* Used to remove a dimension of the object */
89 typedef void (*zend_object_unset_dimension_t)(zval *object, zval *offset);
90 
91 /* Used to get hash of the properties of the object, as hash of zval's */
92 typedef HashTable *(*zend_object_get_properties_t)(zval *object);
93 
94 typedef HashTable *(*zend_object_get_debug_info_t)(zval *object, int *is_temp);
95 
96 /* Used to call methods */
97 /* args on stack! */
98 /* Andi - EX(fbc) (function being called) needs to be initialized already in the INIT fcall opcode so that the parameters can be parsed the right way. We need to add another callback for this.
99  */
100 typedef int (*zend_object_call_method_t)(zend_string *method, zend_object *object, INTERNAL_FUNCTION_PARAMETERS);
101 typedef zend_function *(*zend_object_get_method_t)(zend_object **object, zend_string *method, const zval *key);
102 typedef zend_function *(*zend_object_get_constructor_t)(zend_object *object);
103 
104 /* Object maintenance/destruction */
105 typedef void (*zend_object_dtor_obj_t)(zend_object *object);
106 typedef void (*zend_object_free_obj_t)(zend_object *object);
107 typedef zend_object* (*zend_object_clone_obj_t)(zval *object);
108 
109 /* Get class name for display in var_dump and other debugging functions.
110  * Must be defined and must return a non-NULL value. */
111 typedef zend_string *(*zend_object_get_class_name_t)(const zend_object *object);
112 
113 typedef int (*zend_object_compare_t)(zval *object1, zval *object2);
114 typedef int (*zend_object_compare_zvals_t)(zval *resul, zval *op1, zval *op2);
115 
116 /* Cast an object to some other type.
117  * readobj and retval must point to distinct zvals.
118  */
119 typedef int (*zend_object_cast_t)(zval *readobj, zval *retval, int type);
120 
121 /* updates *count to hold the number of elements present and returns SUCCESS.
122  * Returns FAILURE if the object does not have any sense of overloaded dimensions */
123 typedef int (*zend_object_count_elements_t)(zval *object, zend_long *count);
124 
125 typedef int (*zend_object_get_closure_t)(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr);
126 
127 typedef HashTable *(*zend_object_get_gc_t)(zval *object, zval **table, int *n);
128 
129 typedef int (*zend_object_do_operation_t)(zend_uchar opcode, zval *result, zval *op1, zval *op2);
130 
131 struct _zend_object_handlers {
132 	/* offset of real object header (usually zero) */
133 	int										offset;
134 	/* general object functions */
135 	zend_object_free_obj_t					free_obj;
136 	zend_object_dtor_obj_t					dtor_obj;
137 	zend_object_clone_obj_t					clone_obj;
138 	/* individual object functions */
139 	zend_object_read_property_t				read_property;
140 	zend_object_write_property_t			write_property;
141 	zend_object_read_dimension_t			read_dimension;
142 	zend_object_write_dimension_t			write_dimension;
143 	zend_object_get_property_ptr_ptr_t		get_property_ptr_ptr;
144 	zend_object_get_t						get;
145 	zend_object_set_t						set;
146 	zend_object_has_property_t				has_property;
147 	zend_object_unset_property_t			unset_property;
148 	zend_object_has_dimension_t				has_dimension;
149 	zend_object_unset_dimension_t			unset_dimension;
150 	zend_object_get_properties_t			get_properties;
151 	zend_object_get_method_t				get_method;
152 	zend_object_call_method_t				call_method;
153 	zend_object_get_constructor_t			get_constructor;
154 	zend_object_get_class_name_t			get_class_name;
155 	zend_object_compare_t					compare_objects;
156 	zend_object_cast_t						cast_object;
157 	zend_object_count_elements_t			count_elements;
158 	zend_object_get_debug_info_t			get_debug_info;
159 	zend_object_get_closure_t				get_closure;
160 	zend_object_get_gc_t					get_gc;
161 	zend_object_do_operation_t				do_operation;
162 	zend_object_compare_zvals_t				compare;
163 };
164 
165 BEGIN_EXTERN_C()
166 extern const ZEND_API zend_object_handlers std_object_handlers;
167 
168 #define zend_get_std_object_handlers() \
169 	(&std_object_handlers)
170 
171 #define zend_get_function_root_class(fbc) \
172 	((fbc)->common.prototype ? (fbc)->common.prototype->common.scope : (fbc)->common.scope)
173 
174 #define ZEND_PROPERTY_ISSET     0x0          /* Property exists and is not NULL */
175 #define ZEND_PROPERTY_NOT_EMPTY ZEND_ISEMPTY /* Property is not empty */
176 #define ZEND_PROPERTY_EXISTS    0x2          /* Property exists */
177 
178 ZEND_API void zend_class_init_statics(zend_class_entry *ce);
179 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_string *function_name_strval, const zval *key);
180 ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, zend_bool silent);
181 ZEND_API ZEND_COLD zend_bool zend_std_unset_static_property(zend_class_entry *ce, zend_string *property_name);
182 ZEND_API zend_function *zend_std_get_constructor(zend_object *object);
183 ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zend_string *member, int silent);
184 ZEND_API HashTable *zend_std_get_properties(zval *object);
185 ZEND_API HashTable *zend_std_get_gc(zval *object, zval **table, int *n);
186 ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp);
187 ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type);
188 ZEND_API zval *zend_std_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot);
189 ZEND_API zval *zend_std_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv);
190 ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, void **cache_slot);
191 ZEND_API int zend_std_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot);
192 ZEND_API void zend_std_unset_property(zval *object, zval *member, void **cache_slot);
193 ZEND_API zval *zend_std_read_dimension(zval *object, zval *offset, int type, zval *rv);
194 ZEND_API void zend_std_write_dimension(zval *object, zval *offset, zval *value);
195 ZEND_API int zend_std_has_dimension(zval *object, zval *offset, int check_empty);
196 ZEND_API void zend_std_unset_dimension(zval *object, zval *offset);
197 ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key);
198 ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj);
199 ZEND_API int zend_std_compare_objects(zval *o1, zval *o2);
200 ZEND_API int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr);
201 ZEND_API void rebuild_object_properties(zend_object *zobj);
202 
203 ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, zend_string *function_name);
204 
205 ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope);
206 
207 ZEND_API int zend_check_property_access(zend_object *zobj, zend_string *prop_info_name);
208 
209 ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend_string *method_name, int is_static);
210 
211 ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member);
212 
213 #define zend_free_trampoline(func) do { \
214 		if ((func) == &EG(trampoline)) { \
215 			EG(trampoline).common.function_name = NULL; \
216 		} else { \
217 			efree(func); \
218 		} \
219 	} while (0)
220 
221 END_EXTERN_C()
222 
223 #endif
224 
225 /*
226  * Local variables:
227  * tab-width: 4
228  * c-basic-offset: 4
229  * indent-tabs-mode: t
230  * End:
231  * vim600: sw=4 ts=4 fdm=marker
232  * vim<600: sw=4 ts=4
233  */
234