xref: /PHP-8.0/Zend/zend_object_handlers.h (revision cb9785ad)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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)(zend_object *object, zend_string *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)(zend_object *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    You must return the final value of the assigned property.
55 */
56 /* Used to set property of the object */
57 typedef zval *(*zend_object_write_property_t)(zend_object *object, zend_string *member, zval *value, void **cache_slot);
58 
59 /* Used to set dimension of the object */
60 typedef void (*zend_object_write_dimension_t)(zend_object *object, zval *offset, zval *value);
61 
62 
63 /* Used to create pointer to the property of the object, for future direct r/w access.
64  * May return one of:
65  *  * A zval pointer, without incrementing the reference count.
66  *  * &EG(error_zval), if an exception has been thrown.
67  *  * NULL, if acquiring a direct pointer is not possible.
68  *    In this case, the VM will fall back to using read_property and write_property.
69  */
70 typedef zval *(*zend_object_get_property_ptr_ptr_t)(zend_object *object, zend_string *member, int type, void **cache_slot);
71 
72 /* Used to check if a property of the object exists */
73 /* param has_set_exists:
74  * 0 (has) whether property exists and is not NULL
75  * 1 (set) whether property exists and is true
76  * 2 (exists) whether property exists
77  */
78 typedef int (*zend_object_has_property_t)(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot);
79 
80 /* Used to check if a dimension of the object exists */
81 typedef int (*zend_object_has_dimension_t)(zend_object *object, zval *member, int check_empty);
82 
83 /* Used to remove a property of the object */
84 typedef void (*zend_object_unset_property_t)(zend_object *object, zend_string *member, void **cache_slot);
85 
86 /* Used to remove a dimension of the object */
87 typedef void (*zend_object_unset_dimension_t)(zend_object *object, zval *offset);
88 
89 /* Used to get hash of the properties of the object, as hash of zval's */
90 typedef HashTable *(*zend_object_get_properties_t)(zend_object *object);
91 
92 typedef HashTable *(*zend_object_get_debug_info_t)(zend_object *object, int *is_temp);
93 
94 typedef enum _zend_prop_purpose {
95 	/* Used for debugging. Supersedes get_debug_info handler. */
96 	ZEND_PROP_PURPOSE_DEBUG,
97 	/* Used for (array) casts. */
98 	ZEND_PROP_PURPOSE_ARRAY_CAST,
99 	/* Used for serialization using the "O" scheme.
100 	 * Unserialization will use __wakeup(). */
101 	ZEND_PROP_PURPOSE_SERIALIZE,
102 	/* Used for var_export().
103 	 * The data will be passed to __set_state() when evaluated. */
104 	ZEND_PROP_PURPOSE_VAR_EXPORT,
105 	/* Used for json_encode(). */
106 	ZEND_PROP_PURPOSE_JSON,
107 	/* Dummy member to ensure that "default" is specified. */
108 	_ZEND_PROP_PURPOSE_NON_EXHAUSTIVE_ENUM
109 } zend_prop_purpose;
110 
111 /* The return value must be released using zend_release_properties(). */
112 typedef zend_array *(*zend_object_get_properties_for_t)(zend_object *object, zend_prop_purpose purpose);
113 
114 /* Used to call methods */
115 /* args on stack! */
116 /* 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.
117  */
118 typedef zend_function *(*zend_object_get_method_t)(zend_object **object, zend_string *method, const zval *key);
119 typedef zend_function *(*zend_object_get_constructor_t)(zend_object *object);
120 
121 /* Object maintenance/destruction */
122 typedef void (*zend_object_dtor_obj_t)(zend_object *object);
123 typedef void (*zend_object_free_obj_t)(zend_object *object);
124 typedef zend_object* (*zend_object_clone_obj_t)(zend_object *object);
125 
126 /* Get class name for display in var_dump and other debugging functions.
127  * Must be defined and must return a non-NULL value. */
128 typedef zend_string *(*zend_object_get_class_name_t)(const zend_object *object);
129 
130 typedef int (*zend_object_compare_t)(zval *object1, zval *object2);
131 
132 /* Cast an object to some other type.
133  * readobj and retval must point to distinct zvals.
134  */
135 typedef int (*zend_object_cast_t)(zend_object *readobj, zval *retval, int type);
136 
137 /* updates *count to hold the number of elements present and returns SUCCESS.
138  * Returns FAILURE if the object does not have any sense of overloaded dimensions */
139 typedef int (*zend_object_count_elements_t)(zend_object *object, zend_long *count);
140 
141 typedef int (*zend_object_get_closure_t)(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, zend_bool check_only);
142 
143 typedef HashTable *(*zend_object_get_gc_t)(zend_object *object, zval **table, int *n);
144 
145 typedef int (*zend_object_do_operation_t)(zend_uchar opcode, zval *result, zval *op1, zval *op2);
146 
147 struct _zend_object_handlers {
148 	/* offset of real object header (usually zero) */
149 	int										offset;
150 	/* object handlers */
151 	zend_object_free_obj_t					free_obj;             /* required */
152 	zend_object_dtor_obj_t					dtor_obj;             /* required */
153 	zend_object_clone_obj_t					clone_obj;            /* optional */
154 	zend_object_read_property_t				read_property;        /* required */
155 	zend_object_write_property_t			write_property;       /* required */
156 	zend_object_read_dimension_t			read_dimension;       /* required */
157 	zend_object_write_dimension_t			write_dimension;      /* required */
158 	zend_object_get_property_ptr_ptr_t		get_property_ptr_ptr; /* required */
159 	zend_object_has_property_t				has_property;         /* required */
160 	zend_object_unset_property_t			unset_property;       /* required */
161 	zend_object_has_dimension_t				has_dimension;        /* required */
162 	zend_object_unset_dimension_t			unset_dimension;      /* required */
163 	zend_object_get_properties_t			get_properties;       /* required */
164 	zend_object_get_method_t				get_method;           /* required */
165 	zend_object_get_constructor_t			get_constructor;      /* required */
166 	zend_object_get_class_name_t			get_class_name;       /* required */
167 	zend_object_cast_t						cast_object;          /* required */
168 	zend_object_count_elements_t			count_elements;       /* optional */
169 	zend_object_get_debug_info_t			get_debug_info;       /* optional */
170 	zend_object_get_closure_t				get_closure;          /* optional */
171 	zend_object_get_gc_t					get_gc;               /* required */
172 	zend_object_do_operation_t				do_operation;         /* optional */
173 	zend_object_compare_t					compare;              /* required */
174 	zend_object_get_properties_for_t		get_properties_for;   /* optional */
175 };
176 
177 BEGIN_EXTERN_C()
178 extern const ZEND_API zend_object_handlers std_object_handlers;
179 
180 #define zend_get_std_object_handlers() \
181 	(&std_object_handlers)
182 
183 #define zend_get_function_root_class(fbc) \
184 	((fbc)->common.prototype ? (fbc)->common.prototype->common.scope : (fbc)->common.scope)
185 
186 #define ZEND_PROPERTY_ISSET     0x0          /* Property exists and is not NULL */
187 #define ZEND_PROPERTY_NOT_EMPTY ZEND_ISEMPTY /* Property is not empty */
188 #define ZEND_PROPERTY_EXISTS    0x2          /* Property exists */
189 
190 ZEND_API void zend_class_init_statics(zend_class_entry *ce);
191 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_string *function_name_strval, const zval *key);
192 ZEND_API zval *zend_std_get_static_property_with_info(zend_class_entry *ce, zend_string *property_name, int type, struct _zend_property_info **prop_info);
193 ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type);
194 ZEND_API ZEND_COLD zend_bool zend_std_unset_static_property(zend_class_entry *ce, zend_string *property_name);
195 ZEND_API zend_function *zend_std_get_constructor(zend_object *object);
196 ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zend_string *member, int silent);
197 ZEND_API HashTable *zend_std_get_properties(zend_object *object);
198 ZEND_API HashTable *zend_std_get_gc(zend_object *object, zval **table, int *n);
199 ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp);
200 ZEND_API int zend_std_cast_object_tostring(zend_object *object, zval *writeobj, int type);
201 ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
202 ZEND_API zval *zend_std_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
203 ZEND_API zval *zend_std_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
204 ZEND_API int zend_std_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot);
205 ZEND_API void zend_std_unset_property(zend_object *object, zend_string *member, void **cache_slot);
206 ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
207 ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value);
208 ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty);
209 ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset);
210 ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key);
211 ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj);
212 ZEND_API int zend_std_compare_objects(zval *o1, zval *o2);
213 ZEND_API int zend_std_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, zend_bool check_only);
214 ZEND_API void rebuild_object_properties(zend_object *zobj);
215 
216 /* Handler for objects that cannot be meaningfully compared.
217  * Only objects with the same identity will be considered equal. */
218 ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2);
219 
220 ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope);
221 
222 ZEND_API int zend_check_property_access(zend_object *zobj, zend_string *prop_info_name, zend_bool is_dynamic);
223 
224 ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend_string *method_name, int is_static);
225 
226 ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member);
227 
228 /* Default behavior for get_properties_for. For use as a fallback in custom
229  * get_properties_for implementations. */
230 ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose);
231 
232 /* Will call get_properties_for handler or use default behavior. For use by
233  * consumers of the get_properties_for API. */
234 ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose);
235 
236 #define zend_release_properties(ht) do { \
237 	if ((ht) && !(GC_FLAGS(ht) & GC_IMMUTABLE) && !GC_DELREF(ht)) { \
238 		zend_array_destroy(ht); \
239 	} \
240 } while (0)
241 
242 #define zend_free_trampoline(func) do { \
243 		if ((func) == &EG(trampoline)) { \
244 			EG(trampoline).common.function_name = NULL; \
245 		} else { \
246 			efree(func); \
247 		} \
248 	} while (0)
249 
250 /* Fallback to default comparison implementation if the arguments aren't both objects
251  * and have the same compare() handler. You'll likely want to use this unless you
252  * explicitly wish to support comparisons between objects and non-objects. */
253 #define ZEND_COMPARE_OBJECTS_FALLBACK(op1, op2) \
254 	if (Z_TYPE_P(op1) != IS_OBJECT || \
255 			Z_TYPE_P(op2) != IS_OBJECT || \
256 			Z_OBJ_HT_P(op1)->compare != Z_OBJ_HT_P(op2)->compare) { \
257 		return zend_std_compare_objects(op1, op2); \
258 	}
259 
260 END_EXTERN_C()
261 
262 #endif
263