php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
object_handlers.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: George Peter Banyard <girgias@php.net> |
14 +----------------------------------------------------------------------+
15*/
16
17#include "object_handlers.h"
18#include "zend_API.h"
20
21/* donc refers to DoOperationNoCast */
22static zend_class_entry *donc_ce;
23static zend_object_handlers donc_object_handlers;
24
25static zend_object* donc_object_create_ex(zend_class_entry* ce, zend_long l) {
28 obj->handlers = &donc_object_handlers;
29 ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
30 return obj;
31}
32static zend_object *donc_object_create(zend_class_entry *ce) /* {{{ */
33{
34 return donc_object_create_ex(ce, 0);
35}
36/* }}} */
37
38static inline void donc_create(zval *target, zend_long l) /* {{{ */
39{
40 ZVAL_OBJ(target, donc_object_create_ex(donc_ce, l));
41}
42
43#define IS_DONC(zval) \
44 (Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), donc_ce))
45
46static void donc_add(zval *result, zval *op1, zval *op2)
47{
48 zend_long val_1;
49 zend_long val_2;
50 if (IS_DONC(op1)) {
51 val_1 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op1), 0));
52 } else {
53 val_1 = zval_get_long(op1);
54 }
55 if (IS_DONC(op2)) {
56 val_2 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op2), 0));
57 } else {
58 val_2 = zval_get_long(op2);
59 }
60
61 donc_create(result, val_1 + val_2);
62}
63static void donc_mul(zval *result, zval *op1, zval *op2)
64{
65 zend_long val_1;
66 zend_long val_2;
67 if (IS_DONC(op1)) {
68 val_1 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op1), 0));
69 } else {
70 val_1 = zval_get_long(op1);
71 }
72 if (IS_DONC(op2)) {
73 val_2 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op2), 0));
74 } else {
75 val_2 = zval_get_long(op2);
76 }
77
78 donc_create(result, val_1 * val_2);
79}
80
81static zend_result donc_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2)
82{
83 zval op1_copy;
85
86 if (result == op1) {
87 ZVAL_COPY_VALUE(&op1_copy, op1);
88 op1 = &op1_copy;
89 }
90
91 switch (opcode) {
92 case ZEND_ADD:
93 donc_add(result, op1, op2);
96 break;
97 case ZEND_MUL:
98 donc_mul(result, op1, op2);
100 status = SUCCESS;
101 break;
102 default:
103 status = FAILURE;
104 break;
105 }
106
107 if (status == SUCCESS && op1 == &op1_copy) {
109 }
110
111 return status;
112}
113
124
125static zend_class_entry *long_castable_no_operation_ce;
126static zend_object_handlers long_castable_no_operation_object_handlers;
127
128static zend_object* long_castable_no_operation_object_create_ex(zend_class_entry* ce, zend_long l) {
129 zend_object *obj = zend_objects_new(ce);
130 object_properties_init(obj, ce);
131 obj->handlers = &long_castable_no_operation_object_handlers;
132 ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
133 return obj;
134}
135
136static zend_object *long_castable_no_operation_object_create(zend_class_entry *ce)
137{
138 return long_castable_no_operation_object_create_ex(ce, 0);
139}
140
141static zend_result long_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
142{
143 if (type == IS_LONG) {
144 ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
145 return SUCCESS;
146 }
147 return FAILURE;
148}
149
160
161static zend_class_entry *float_castable_no_operation_ce;
162static zend_object_handlers float_castable_no_operation_object_handlers;
163
164static zend_object* float_castable_no_operation_object_create_ex(zend_class_entry* ce, double d) {
165 zend_object *obj = zend_objects_new(ce);
166 object_properties_init(obj, ce);
167 obj->handlers = &float_castable_no_operation_object_handlers;
168 ZVAL_DOUBLE(OBJ_PROP_NUM(obj, 0), d);
169 return obj;
170}
171
172static zend_object *float_castable_no_operation_object_create(zend_class_entry *ce)
173{
174 return float_castable_no_operation_object_create_ex(ce, 0.0);
175}
176
177static zend_result float_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
178{
179 if (type == IS_DOUBLE) {
180 ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
181 return SUCCESS;
182 }
183 return FAILURE;
184}
185
196
197static zend_class_entry *numeric_castable_no_operation_ce;
198static zend_object_handlers numeric_castable_no_operation_object_handlers;
199
200static zend_object* numeric_castable_no_operation_object_create_ex(zend_class_entry* ce, const zval *n) {
201 zend_object *obj = zend_objects_new(ce);
202 object_properties_init(obj, ce);
203 obj->handlers = &numeric_castable_no_operation_object_handlers;
204 ZVAL_COPY(OBJ_PROP_NUM(obj, 0), n);
205 return obj;
206}
207
208static zend_object *numeric_castable_no_operation_object_create(zend_class_entry *ce)
209{
210 zval tmp;
211 ZVAL_LONG(&tmp, 0);
212 return numeric_castable_no_operation_object_create_ex(ce, &tmp);
213}
214
215static zend_result numeric_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
216{
217 if (type == _IS_NUMBER) {
218 ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
219 return SUCCESS;
220 }
221 return FAILURE;
222}
223
234
235static zend_class_entry *dimension_handlers_no_ArrayAccess_ce;
236static zend_object_handlers dimension_handlers_no_ArrayAccess_object_handlers;
237
238static zend_object* dimension_handlers_no_ArrayAccess_object_create(zend_class_entry* ce) {
239 zend_object *object = zend_objects_new(ce);
240 object_properties_init(object, ce);
241 object->handlers = &dimension_handlers_no_ArrayAccess_object_handlers;
242 return object;
243}
244
245static void dimension_common_helper(zend_object *object, zval *offset, int prop_access_type) {
246 ZVAL_BOOL(OBJ_PROP_NUM(object, prop_access_type), true);
247 /* hasOffset */
248 ZVAL_BOOL(OBJ_PROP_NUM(object, 5), offset != NULL);
249 if (offset) {
250 ZVAL_COPY(OBJ_PROP_NUM(object, 7), offset);
251 }
252}
253
254static zval* dimension_handlers_no_ArrayAccess_read_dimension(zend_object *object, zval *offset, int type, zval *rv) {
255 dimension_common_helper(object, offset, 0);
256 /* ReadType */
257 ZVAL_LONG(OBJ_PROP_NUM(object, 4), type);
258
259 /* Normal logic */
260 ZVAL_BOOL(rv, true);
261 return rv;
262}
263
264static void dimension_handlers_no_ArrayAccess_write_dimension(zend_object *object, zval *offset, zval *value) {
265 dimension_common_helper(object, offset, 1);
266}
267
268static int dimension_handlers_no_ArrayAccess_has_dimension(zend_object *object, zval *offset, int check_empty) {
269 /* checkEmpty */
270 ZVAL_LONG(OBJ_PROP_NUM(object, 6), check_empty);
271 dimension_common_helper(object, offset, 2);
272
273 /* Normal logic */
274 return 1;
275}
276
277static void dimension_handlers_no_ArrayAccess_unset_dimension(zend_object *object, zval *offset) {
278 dimension_common_helper(object, offset, 3);
279}
280
282{
283 /* DoOperationNoCast class */
284 donc_ce = register_class_DoOperationNoCast();
285 donc_ce->create_object = donc_object_create;
286 memcpy(&donc_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
287 donc_object_handlers.do_operation = donc_do_operation;
288
289 /* CastableNoOperation classes */
290 long_castable_no_operation_ce = register_class_LongCastableNoOperations();
291 long_castable_no_operation_ce->create_object = long_castable_no_operation_object_create;
292 memcpy(&long_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
293 long_castable_no_operation_object_handlers.cast_object = long_castable_no_operation_cast_object;
294
295 float_castable_no_operation_ce = register_class_FloatCastableNoOperations();
296 float_castable_no_operation_ce->create_object = float_castable_no_operation_object_create;
297 memcpy(&float_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
298 float_castable_no_operation_object_handlers.cast_object = float_castable_no_operation_cast_object;
299
300 numeric_castable_no_operation_ce = register_class_NumericCastableNoOperations();
301 numeric_castable_no_operation_ce->create_object = numeric_castable_no_operation_object_create;
302 memcpy(&numeric_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
303 numeric_castable_no_operation_object_handlers.cast_object = numeric_castable_no_operation_cast_object;
304
305 dimension_handlers_no_ArrayAccess_ce = register_class_DimensionHandlersNoArrayAccess();
306 dimension_handlers_no_ArrayAccess_ce->create_object = dimension_handlers_no_ArrayAccess_object_create;
307 memcpy(&dimension_handlers_no_ArrayAccess_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
308 dimension_handlers_no_ArrayAccess_object_handlers.read_dimension = dimension_handlers_no_ArrayAccess_read_dimension;
309 dimension_handlers_no_ArrayAccess_object_handlers.write_dimension = dimension_handlers_no_ArrayAccess_write_dimension;
310 dimension_handlers_no_ArrayAccess_object_handlers.has_dimension = dimension_handlers_no_ArrayAccess_has_dimension;
311 dimension_handlers_no_ArrayAccess_object_handlers.unset_dimension = dimension_handlers_no_ArrayAccess_unset_dimension;
312}
bool exception
Definition assert.c:30
DNS_STATUS status
Definition dns_win32.c:49
zend_ffi_type * type
Definition ffi.c:3812
zend_long n
Definition ffi.c:4979
memcpy(ptr1, ptr2, size)
zend_long offset
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
#define IS_DONC(zval)
void zend_test_object_handlers_init(void)
zval rv
Definition session.c:1024
const zend_object_handlers * handlers
Definition zend_types.h:561
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
#define Z_PARAM_NUMBER(dest)
Definition zend_API.h:1914
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define ZEND_METHOD(classname, name)
Definition zend_API.h:76
#define Z_PARAM_DOUBLE(dest)
Definition zend_API.h:1803
#define ZEND_THIS
Definition zend_API.h:523
struct _zval_struct zval
#define OBJ_PROP_NUM(obj, num)
#define EG(v)
int32_t zend_long
Definition zend_long.h:42
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API zend_object *ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZVAL_LONG(z, l)
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define IS_DOUBLE
Definition zend_types.h:605
#define ZVAL_OBJ(z, o)
@ FAILURE
Definition zend_types.h:61
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_COPY(z, v)
#define ZVAL_DOUBLE(z, d)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
unsigned char zend_uchar
Definition zend_types.h:57
#define _IS_NUMBER
Definition zend_types.h:630
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define ZVAL_COPY_VALUE(z, v)
#define ZVAL_BOOL(z, b)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
bool result
op2
op1
object
value
#define ZEND_MUL
#define ZEND_ADD