Skip to content

Commit a4c33b1

Browse files
committed
ext/zend_test: Move object handler test objects to their own file
1 parent e0bee2c commit a4c33b1

9 files changed

+424
-367
lines changed

ext/zend_test/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ PHP_ARG_ENABLE([zend-test],
44
[Enable zend_test extension])])
55

66
if test "$PHP_ZEND_TEST" != "no"; then
7-
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
7+
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c object_handlers.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
88
fi

ext/zend_test/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
ARG_ENABLE("zend-test", "enable zend_test extension", "no");
44

55
if (PHP_ZEND_TEST != "no") {
6-
EXTENSION("zend_test", "test.c observer.c fiber.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
6+
EXTENSION("zend_test", "test.c observer.c fiber.c object_handlers.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
77
ADD_FLAG("CFLAGS_ZEND_TEST", "/D PHP_ZEND_TEST_EXPORTS ");
88
}

ext/zend_test/object_handlers.c

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
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+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: George Peter Banyard <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "object_handlers.h"
18+
#include "object_handlers_arginfo.h"
19+
#include "php.h"
20+
#include "zend_exceptions.h"
21+
22+
/* donc refers to DoOperationNoCast */
23+
static zend_class_entry *donc_ce;
24+
static zend_object_handlers donc_object_handlers;
25+
26+
static zend_object* donc_object_create_ex(zend_class_entry* ce, zend_long l) {
27+
zend_object *obj = zend_objects_new(ce);
28+
object_properties_init(obj, ce);
29+
obj->handlers = &donc_object_handlers;
30+
ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
31+
return obj;
32+
}
33+
static zend_object *donc_object_create(zend_class_entry *ce) /* {{{ */
34+
{
35+
return donc_object_create_ex(ce, 0);
36+
}
37+
/* }}} */
38+
39+
static inline void donc_create(zval *target, zend_long l) /* {{{ */
40+
{
41+
ZVAL_OBJ(target, donc_object_create_ex(donc_ce, l));
42+
}
43+
44+
#define IS_DONC(zval) \
45+
(Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), donc_ce))
46+
47+
static void donc_add(zval *result, zval *op1, zval *op2)
48+
{
49+
zend_long val_1;
50+
zend_long val_2;
51+
if (IS_DONC(op1)) {
52+
val_1 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op1), 0));
53+
} else {
54+
val_1 = zval_get_long(op1);
55+
}
56+
if (IS_DONC(op2)) {
57+
val_2 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op2), 0));
58+
} else {
59+
val_2 = zval_get_long(op2);
60+
}
61+
62+
donc_create(result, val_1 + val_2);
63+
}
64+
static void donc_mul(zval *result, zval *op1, zval *op2)
65+
{
66+
zend_long val_1;
67+
zend_long val_2;
68+
if (IS_DONC(op1)) {
69+
val_1 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op1), 0));
70+
} else {
71+
val_1 = zval_get_long(op1);
72+
}
73+
if (IS_DONC(op2)) {
74+
val_2 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op2), 0));
75+
} else {
76+
val_2 = zval_get_long(op2);
77+
}
78+
79+
donc_create(result, val_1 * val_2);
80+
}
81+
82+
static zend_result donc_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2)
83+
{
84+
zval op1_copy;
85+
zend_result status;
86+
87+
if (result == op1) {
88+
ZVAL_COPY_VALUE(&op1_copy, op1);
89+
op1 = &op1_copy;
90+
}
91+
92+
switch (opcode) {
93+
case ZEND_ADD:
94+
donc_add(result, op1, op2);
95+
if (UNEXPECTED(EG(exception))) { status = FAILURE; }
96+
status = SUCCESS;
97+
break;
98+
case ZEND_MUL:
99+
donc_mul(result, op1, op2);
100+
if (UNEXPECTED(EG(exception))) { status = FAILURE; }
101+
status = SUCCESS;
102+
break;
103+
default:
104+
status = FAILURE;
105+
break;
106+
}
107+
108+
if (status == SUCCESS && op1 == &op1_copy) {
109+
zval_ptr_dtor(op1);
110+
}
111+
112+
return status;
113+
}
114+
115+
PHP_METHOD(DoOperationNoCast, __construct)
116+
{
117+
zend_long l;
118+
119+
ZEND_PARSE_PARAMETERS_START(1, 1)
120+
Z_PARAM_LONG(l)
121+
ZEND_PARSE_PARAMETERS_END();
122+
123+
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), l);
124+
}
125+
126+
static zend_class_entry *long_castable_no_operation_ce;
127+
static zend_object_handlers long_castable_no_operation_object_handlers;
128+
129+
static zend_object* long_castable_no_operation_object_create_ex(zend_class_entry* ce, zend_long l) {
130+
zend_object *obj = zend_objects_new(ce);
131+
object_properties_init(obj, ce);
132+
obj->handlers = &long_castable_no_operation_object_handlers;
133+
ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
134+
return obj;
135+
}
136+
137+
static zend_object *long_castable_no_operation_object_create(zend_class_entry *ce)
138+
{
139+
return long_castable_no_operation_object_create_ex(ce, 0);
140+
}
141+
142+
static zend_result long_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
143+
{
144+
if (type == IS_LONG) {
145+
ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
146+
return SUCCESS;
147+
}
148+
return FAILURE;
149+
}
150+
151+
PHP_METHOD(LongCastableNoOperations, __construct)
152+
{
153+
zend_long l;
154+
155+
ZEND_PARSE_PARAMETERS_START(1, 1)
156+
Z_PARAM_LONG(l)
157+
ZEND_PARSE_PARAMETERS_END();
158+
159+
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), l);
160+
}
161+
162+
static zend_class_entry *float_castable_no_operation_ce;
163+
static zend_object_handlers float_castable_no_operation_object_handlers;
164+
165+
static zend_object* float_castable_no_operation_object_create_ex(zend_class_entry* ce, double d) {
166+
zend_object *obj = zend_objects_new(ce);
167+
object_properties_init(obj, ce);
168+
obj->handlers = &float_castable_no_operation_object_handlers;
169+
ZVAL_DOUBLE(OBJ_PROP_NUM(obj, 0), d);
170+
return obj;
171+
}
172+
173+
static zend_object *float_castable_no_operation_object_create(zend_class_entry *ce)
174+
{
175+
return float_castable_no_operation_object_create_ex(ce, 0.0);
176+
}
177+
178+
static zend_result float_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
179+
{
180+
if (type == IS_DOUBLE) {
181+
ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
182+
return SUCCESS;
183+
}
184+
return FAILURE;
185+
}
186+
187+
PHP_METHOD(FloatCastableNoOperations, __construct)
188+
{
189+
double d;
190+
191+
ZEND_PARSE_PARAMETERS_START(1, 1)
192+
Z_PARAM_DOUBLE(d)
193+
ZEND_PARSE_PARAMETERS_END();
194+
195+
ZVAL_DOUBLE(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), d);
196+
}
197+
198+
static zend_class_entry *numeric_castable_no_operation_ce;
199+
static zend_object_handlers numeric_castable_no_operation_object_handlers;
200+
201+
static zend_object* numeric_castable_no_operation_object_create_ex(zend_class_entry* ce, const zval *n) {
202+
zend_object *obj = zend_objects_new(ce);
203+
object_properties_init(obj, ce);
204+
obj->handlers = &numeric_castable_no_operation_object_handlers;
205+
ZVAL_COPY(OBJ_PROP_NUM(obj, 0), n);
206+
return obj;
207+
}
208+
209+
static zend_object *numeric_castable_no_operation_object_create(zend_class_entry *ce)
210+
{
211+
zval tmp;
212+
ZVAL_LONG(&tmp, 0);
213+
return numeric_castable_no_operation_object_create_ex(ce, &tmp);
214+
}
215+
216+
static zend_result numeric_castable_no_operation_cast_object(zend_object *obj, zval *result, int type)
217+
{
218+
if (type == _IS_NUMBER) {
219+
ZVAL_COPY(result, OBJ_PROP_NUM(obj, 0));
220+
return SUCCESS;
221+
}
222+
return FAILURE;
223+
}
224+
225+
PHP_METHOD(NumericCastableNoOperations, __construct)
226+
{
227+
zval *n;
228+
229+
ZEND_PARSE_PARAMETERS_START(1, 1)
230+
Z_PARAM_NUMBER(n)
231+
ZEND_PARSE_PARAMETERS_END();
232+
233+
ZVAL_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), n);
234+
}
235+
236+
void zend_test_object_handlers_init(void)
237+
{
238+
/* DoOperationNoCast class */
239+
donc_ce = register_class_DoOperationNoCast();
240+
donc_ce->create_object = donc_object_create;
241+
memcpy(&donc_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
242+
donc_object_handlers.do_operation = donc_do_operation;
243+
244+
/* CastableNoOperation classes */
245+
long_castable_no_operation_ce = register_class_LongCastableNoOperations();
246+
long_castable_no_operation_ce->create_object = long_castable_no_operation_object_create;
247+
memcpy(&long_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
248+
long_castable_no_operation_object_handlers.cast_object = long_castable_no_operation_cast_object;
249+
250+
float_castable_no_operation_ce = register_class_FloatCastableNoOperations();
251+
float_castable_no_operation_ce->create_object = float_castable_no_operation_object_create;
252+
memcpy(&float_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
253+
float_castable_no_operation_object_handlers.cast_object = float_castable_no_operation_cast_object;
254+
255+
numeric_castable_no_operation_ce = register_class_NumericCastableNoOperations();
256+
numeric_castable_no_operation_ce->create_object = numeric_castable_no_operation_object_create;
257+
memcpy(&numeric_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
258+
numeric_castable_no_operation_object_handlers.cast_object = numeric_castable_no_operation_cast_object;
259+
}

ext/zend_test/object_handlers.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: George Peter Banyard <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_TEST_OBJECT_HANDLERS_H
18+
#define ZEND_TEST_OBJECT_HANDLERS_H
19+
20+
void zend_test_object_handlers_init(void);
21+
22+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @generate-class-entries static
5+
* @undocumentable
6+
*/
7+
8+
final class DoOperationNoCast {
9+
private int $val;
10+
public function __construct(int $val) {}
11+
}
12+
13+
final class LongCastableNoOperations {
14+
private int $val;
15+
public function __construct(int $val) {}
16+
}
17+
final class FloatCastableNoOperations {
18+
private float $val;
19+
public function __construct(float $val) {}
20+
}
21+
final class NumericCastableNoOperations {
22+
private int|float $val;
23+
public function __construct(int|float $val) {}
24+
}

0 commit comments

Comments
 (0)