Skip to content

Commit 1ac82d7

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

9 files changed

+423
-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: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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 "zend_API.h"
19+
#include "object_handlers_arginfo.h"
20+
21+
/* donc refers to DoOperationNoCast */
22+
static zend_class_entry *donc_ce;
23+
static zend_object_handlers donc_object_handlers;
24+
25+
static zend_object* donc_object_create_ex(zend_class_entry* ce, zend_long l) {
26+
zend_object *obj = zend_objects_new(ce);
27+
object_properties_init(obj, ce);
28+
obj->handlers = &donc_object_handlers;
29+
ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
30+
return obj;
31+
}
32+
static zend_object *donc_object_create(zend_class_entry *ce) /* {{{ */
33+
{
34+
return donc_object_create_ex(ce, 0);
35+
}
36+
/* }}} */
37+
38+
static 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+
46+
static 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+
}
63+
static 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+
81+
static zend_result donc_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2)
82+
{
83+
zval op1_copy;
84+
zend_result status;
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);
94+
if (UNEXPECTED(EG(exception))) { status = FAILURE; }
95+
status = SUCCESS;
96+
break;
97+
case ZEND_MUL:
98+
donc_mul(result, op1, op2);
99+
if (UNEXPECTED(EG(exception))) { status = FAILURE; }
100+
status = SUCCESS;
101+
break;
102+
default:
103+
status = FAILURE;
104+
break;
105+
}
106+
107+
if (status == SUCCESS && op1 == &op1_copy) {
108+
zval_ptr_dtor(op1);
109+
}
110+
111+
return status;
112+
}
113+
114+
ZEND_METHOD(DoOperationNoCast, __construct)
115+
{
116+
zend_long l;
117+
118+
ZEND_PARSE_PARAMETERS_START(1, 1)
119+
Z_PARAM_LONG(l)
120+
ZEND_PARSE_PARAMETERS_END();
121+
122+
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), l);
123+
}
124+
125+
static zend_class_entry *long_castable_no_operation_ce;
126+
static zend_object_handlers long_castable_no_operation_object_handlers;
127+
128+
static 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+
136+
static 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+
141+
static 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+
150+
ZEND_METHOD(LongCastableNoOperations, __construct)
151+
{
152+
zend_long l;
153+
154+
ZEND_PARSE_PARAMETERS_START(1, 1)
155+
Z_PARAM_LONG(l)
156+
ZEND_PARSE_PARAMETERS_END();
157+
158+
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), l);
159+
}
160+
161+
static zend_class_entry *float_castable_no_operation_ce;
162+
static zend_object_handlers float_castable_no_operation_object_handlers;
163+
164+
static 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+
172+
static 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+
177+
static 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+
186+
ZEND_METHOD(FloatCastableNoOperations, __construct)
187+
{
188+
double d;
189+
190+
ZEND_PARSE_PARAMETERS_START(1, 1)
191+
Z_PARAM_DOUBLE(d)
192+
ZEND_PARSE_PARAMETERS_END();
193+
194+
ZVAL_DOUBLE(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), d);
195+
}
196+
197+
static zend_class_entry *numeric_castable_no_operation_ce;
198+
static zend_object_handlers numeric_castable_no_operation_object_handlers;
199+
200+
static 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+
208+
static 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+
215+
static 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+
224+
ZEND_METHOD(NumericCastableNoOperations, __construct)
225+
{
226+
zval *n;
227+
228+
ZEND_PARSE_PARAMETERS_START(1, 1)
229+
Z_PARAM_NUMBER(n)
230+
ZEND_PARSE_PARAMETERS_END();
231+
232+
ZVAL_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), n);
233+
}
234+
235+
void zend_test_object_handlers_init(void)
236+
{
237+
/* DoOperationNoCast class */
238+
donc_ce = register_class_DoOperationNoCast();
239+
donc_ce->create_object = donc_object_create;
240+
memcpy(&donc_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
241+
donc_object_handlers.do_operation = donc_do_operation;
242+
243+
/* CastableNoOperation classes */
244+
long_castable_no_operation_ce = register_class_LongCastableNoOperations();
245+
long_castable_no_operation_ce->create_object = long_castable_no_operation_object_create;
246+
memcpy(&long_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
247+
long_castable_no_operation_object_handlers.cast_object = long_castable_no_operation_cast_object;
248+
249+
float_castable_no_operation_ce = register_class_FloatCastableNoOperations();
250+
float_castable_no_operation_ce->create_object = float_castable_no_operation_object_create;
251+
memcpy(&float_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
252+
float_castable_no_operation_object_handlers.cast_object = float_castable_no_operation_cast_object;
253+
254+
numeric_castable_no_operation_ce = register_class_NumericCastableNoOperations();
255+
numeric_castable_no_operation_ce->create_object = numeric_castable_no_operation_object_create;
256+
memcpy(&numeric_castable_no_operation_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
257+
numeric_castable_no_operation_object_handlers.cast_object = numeric_castable_no_operation_cast_object;
258+
}

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)