Skip to content

Commit f4fee1a

Browse files
committed
First steps to PHP 8 support
1 parent 23d055d commit f4fee1a

31 files changed

+522
-221
lines changed

src/ds/ds_deque.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,14 @@ void ds_deque_to_array(ds_deque_t *deque, zval *array)
566566
}
567567
}
568568

569-
int ds_deque_index_exists(ds_deque_t *deque, zend_long index)
569+
bool ds_deque_index_exists(ds_deque_t *deque, zend_long index)
570570
{
571571
return index >= 0 && index < deque->size;
572572
}
573573

574574
bool ds_deque_isset(ds_deque_t *deque, zend_long index, int check_empty)
575575
{
576-
if (index < 0 || index >= deque->size) {
576+
if (!ds_deque_index_exists(deque, index)) {
577577
return false;
578578
}
579579

src/ds/ds_deque.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ zval *ds_deque_get_first_throw(ds_deque_t *deque);
6262

6363
bool ds_deque_contains_va(ds_deque_t *deque, VA_PARAMS);
6464
bool ds_deque_isset(ds_deque_t *deque, zend_long index, int check_empty);
65+
bool ds_deque_index_exists(ds_deque_t *deque, zend_long index);
6566

6667
ds_deque_t *ds_deque_map(ds_deque_t *deque, FCI_PARAMS);
6768
ds_deque_t *ds_deque_filter(ds_deque_t *deque);

src/ds/ds_htable.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ static inline bool implements_hashable(zval *key) {
183183

184184
} else {
185185
zval equals;
186+
#if PHP_VERSION_ID >= 80000
187+
zend_call_method_with_1_params(Z_OBJ_P(a), Z_OBJCE_P(a), NULL, "equals", &equals, b);
188+
#else
186189
zend_call_method_with_1_params(a, Z_OBJCE_P(a), NULL, "equals", &equals, b);
190+
#endif
187191
return Z_TYPE(equals) == IS_TRUE;
188192
}
189193
}
@@ -263,8 +267,12 @@ static uint32_t get_object_hash(zval *obj)
263267
{
264268
if (implements_hashable(obj)) {
265269
zval hash;
270+
#if PHP_VERSION_ID >= 80000
271+
zend_call_method_with_0_params(Z_OBJ_P(obj), Z_OBJCE_P(obj), NULL, "hash", &hash);
272+
#else
266273
zend_call_method_with_0_params(obj, Z_OBJCE_P(obj), NULL, "hash", &hash);
267-
274+
#endif
275+
268276
switch (Z_TYPE(hash)) {
269277
case IS_LONG:
270278
return Z_LVAL(hash);

src/php/arginfo.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,32 @@ ZEND_ARG_TYPE_INFO(0, i, IS_LONG, 0) \
8282
ZEND_ARG_VARIADIC_INFO(0, v) \
8383
ZEND_END_ARG_INFO()
8484

85-
#if PHP_VERSION_ID >= 70200
86-
#define DS_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
85+
#if PHP_VERSION_ID >= 80000
86+
#define DS_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, pass_by_ref, required_num_args, class_name, allow_null) \
87+
static const zend_internal_arg_info arginfo_##name[] = { \
88+
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_CLASS_CONST(PHP_DS_NS_NAME#class_name, allow_null, 0), pass_by_ref},
89+
#elif PHP_VERSION_ID >= 70200
90+
#define DS_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, pass_by_ref, required_num_args, class_name, allow_null) \
8791
static const zend_internal_arg_info arginfo_##name[] = { \
88-
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_ENCODE_CLASS_CONST(PHP_DS_NS_NAME#class_name, allow_null), return_reference, 0 },
92+
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_ENCODE_CLASS_CONST(PHP_DS_NS_NAME#class_name, allow_null), pass_by_ref, 0 },
8993
#else
90-
#define DS_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
94+
#define DS_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, pass_by_ref, required_num_args, class_name, allow_null) \
9195
static const zend_internal_arg_info arginfo_##name[] = { \
92-
{ (const char*)(zend_uintptr_t)(required_num_args), PHP_DS_NS_NAME#class_name, IS_OBJECT, return_reference, allow_null, 0 },
96+
{ (const char*)(zend_uintptr_t)(required_num_args), PHP_DS_NS_NAME#class_name, IS_OBJECT, pass_by_ref, allow_null, 0 },
9397
#endif
9498

95-
#if PHP_VERSION_ID >= 70200
96-
#define DS_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
99+
#if PHP_VERSION_ID >= 80000
100+
#define DS_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, pass_by_ref, required_num_args, type, allow_null) \
101+
static const zend_internal_arg_info arginfo_##name[] = { \
102+
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_CODE(type, allow_null, _ZEND_ARG_INFO_FLAGS(pass_by_ref, 0)) },
103+
#elif PHP_VERSION_ID >= 70200
104+
#define DS_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, pass_by_ref, required_num_args, type, allow_null) \
97105
static const zend_internal_arg_info arginfo_##name[] = { \
98-
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_ENCODE(type, allow_null), return_reference, 0 },
106+
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_ENCODE(type, allow_null), pass_by_ref, 0 },
99107
#else
100-
#define DS_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
108+
#define DS_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, pass_by_ref, required_num_args, type, allow_null) \
101109
static const zend_internal_arg_info arginfo_##name[] = { \
102-
{ (const char*)(zend_uintptr_t)(required_num_args), NULL, type, return_reference, allow_null, 0 },
110+
{ (const char*)(zend_uintptr_t)(required_num_args), NULL, type, pass_by_ref, allow_null, 0 },
103111
#endif
104112

105113
#define ARGINFO_ZVAL_RETURN_BOOL(name, z) \

src/php/classes/php_collection_ce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void php_ds_register_collection()
2121
INIT_CLASS_ENTRY(ce, PHP_DS_NS(Collection), methods);
2222
collection_ce = zend_register_internal_interface(&ce);
2323
zend_class_implements(collection_ce, 3,
24-
zend_ce_traversable, // Traversable
24+
zend_ce_aggregate, // IteratorAggregate
2525
spl_ce_Countable, // Countable
2626
php_json_serializable_ce // Serializable
2727
);

src/php/classes/php_deque_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,18 @@ METHOD(jsonSerialize)
263263
ds_deque_to_array(THIS_DS_DEQUE(), return_value);
264264
}
265265

266+
METHOD(getIterator) {
267+
PARSE_NONE;
268+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
269+
}
270+
266271
void php_ds_register_deque()
267272
{
268273
zend_class_entry ce;
269274

270275
zend_function_entry methods[] = {
271276
PHP_DS_ME(Deque, __construct)
277+
PHP_DS_ME(Deque, getIterator)
272278

273279
PHP_DS_COLLECTION_ME_LIST(Deque)
274280
PHP_DS_SEQUENCE_ME_LIST(Deque)

src/php/classes/php_deque_ce.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
extern zend_class_entry *php_ds_deque_ce;
99

1010
ARGINFO_OPTIONAL_ZVAL(Deque___construct, values);
11+
ARGINFO_NONE_RETURN_OBJ(Deque_getIterator, Traversable);
12+
1113

1214
void php_ds_register_deque();
1315

src/php/classes/php_map_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ METHOD(xor)
280280
RETURN_DS_MAP(ds_map_xor(THIS_DS_MAP(), Z_DS_MAP_P(obj)));
281281
}
282282

283+
METHOD(getIterator) {
284+
PARSE_NONE;
285+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
286+
}
287+
283288
void php_ds_register_map()
284289
{
285290
zend_class_entry ce;
@@ -317,6 +322,7 @@ void php_ds_register_map()
317322
PHP_DS_ME(Map, union)
318323
PHP_DS_ME(Map, values)
319324
PHP_DS_ME(Map, xor)
325+
PHP_DS_ME(Map, getIterator)
320326

321327
PHP_DS_COLLECTION_ME_LIST(Map)
322328
PHP_FE_END

src/php/classes/php_map_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ARGINFO_NONE( Map_sum);
4040
ARGINFO_ZVAL_RETURN_DS( Map_union, map, Map);
4141
ARGINFO_NONE_RETURN_DS( Map_values, Sequence);
4242
ARGINFO_DS_RETURN_DS( Map_xor, map, Map, Map);
43+
ARGINFO_NONE_RETURN_OBJ( Map_getIterator, Traversable);
4344

4445
void php_ds_register_map();
4546

src/php/classes/php_priority_queue_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ METHOD(jsonSerialize)
8585
ds_priority_queue_to_array(THIS_DS_PRIORITY_QUEUE(), return_value);
8686
}
8787

88+
METHOD(getIterator) {
89+
PARSE_NONE;
90+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
91+
}
92+
8893
void php_ds_register_priority_queue()
8994
{
9095
zend_class_entry ce;
@@ -96,6 +101,7 @@ void php_ds_register_priority_queue()
96101
PHP_DS_ME(PriorityQueue, peek)
97102
PHP_DS_ME(PriorityQueue, pop)
98103
PHP_DS_ME(PriorityQueue, push)
104+
PHP_DS_ME(PriorityQueue, getIterator)
99105

100106
PHP_DS_COLLECTION_ME_LIST(PriorityQueue)
101107
PHP_FE_END

src/php/classes/php_priority_queue_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ARGINFO_NONE_RETURN_DS( PriorityQueue_copy, PriorityQueue);
1414
ARGINFO_ZVAL_ZVAL( PriorityQueue_push, value, priority);
1515
ARGINFO_NONE( PriorityQueue_pop);
1616
ARGINFO_NONE( PriorityQueue_peek);
17+
ARGINFO_NONE_RETURN_OBJ( PriorityQueue_getIterator, Traversable);
1718

1819
void php_ds_register_priority_queue();
1920

src/php/classes/php_queue_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ METHOD(jsonSerialize)
8989
ds_queue_to_array(THIS_DS_QUEUE(), return_value);
9090
}
9191

92+
METHOD(getIterator) {
93+
PARSE_NONE;
94+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
95+
}
96+
9297
void php_ds_register_queue()
9398
{
9499
zend_class_entry ce;
@@ -100,6 +105,7 @@ void php_ds_register_queue()
100105
PHP_DS_ME(Queue, peek)
101106
PHP_DS_ME(Queue, pop)
102107
PHP_DS_ME(Queue, push)
108+
PHP_DS_ME(Queue, getIterator)
103109

104110
PHP_DS_COLLECTION_ME_LIST(Queue)
105111
PHP_FE_END

src/php/classes/php_queue_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ARGINFO_NONE_RETURN_LONG( Queue_capacity);
1313
ARGINFO_VARIADIC_ZVAL( Queue_push, values);
1414
ARGINFO_NONE( Queue_pop);
1515
ARGINFO_NONE( Queue_peek);
16+
ARGINFO_NONE_RETURN_OBJ( Queue_getIterator, Traversable);
1617

1718
void php_ds_register_queue();
1819

src/php/classes/php_set_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ METHOD(jsonSerialize)
221221
ds_set_to_array(THIS_DS_SET(), return_value);
222222
}
223223

224+
METHOD(getIterator) {
225+
PARSE_NONE;
226+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
227+
}
228+
224229
void php_ds_register_set()
225230
{
226231
zend_class_entry ce;
@@ -250,6 +255,7 @@ void php_ds_register_set()
250255
PHP_DS_ME(Set, sum)
251256
PHP_DS_ME(Set, union)
252257
PHP_DS_ME(Set, xor)
258+
PHP_DS_ME(Set, getIterator)
253259

254260
PHP_DS_COLLECTION_ME_LIST(Set)
255261
PHP_FE_END

src/php/classes/php_set_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ARGINFO_CALLABLE_RETURN_DS( Set_map, callback, Set);
3131
ARGINFO_NONE( Set_reverse);
3232
ARGINFO_NONE_RETURN_DS( Set_reversed, Set);
3333
ARGINFO_NONE( Set_sum);
34+
ARGINFO_NONE_RETURN_OBJ( Set_getIterator, Traversable);
3435

3536
void php_ds_register_set();
3637

src/php/classes/php_stack_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ METHOD(jsonSerialize)
9090
ds_stack_to_array(THIS_DS_STACK(), return_value);
9191
}
9292

93+
METHOD(getIterator) {
94+
PARSE_NONE;
95+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
96+
}
97+
9398
void php_ds_register_stack()
9499
{
95100
zend_class_entry ce;
@@ -101,6 +106,7 @@ void php_ds_register_stack()
101106
PHP_DS_ME(Stack, peek)
102107
PHP_DS_ME(Stack, pop)
103108
PHP_DS_ME(Stack, push)
109+
PHP_DS_ME(Stack, getIterator)
104110

105111
PHP_DS_COLLECTION_ME_LIST(Stack)
106112
PHP_FE_END

src/php/classes/php_stack_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ARGINFO_NONE_RETURN_LONG( Stack_capacity);
1313
ARGINFO_VARIADIC_ZVAL( Stack_push, values);
1414
ARGINFO_NONE( Stack_pop);
1515
ARGINFO_NONE( Stack_peek);
16+
ARGINFO_NONE_RETURN_OBJ( Stack_getIterator, Traversable);
1617

1718
void php_ds_register_stack();
1819

src/php/classes/php_vector_ce.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,18 @@ METHOD(unshift)
257257
ds_vector_unshift_va(THIS_DS_VECTOR(), argc, argv);
258258
}
259259

260+
METHOD(getIterator) {
261+
PARSE_NONE;
262+
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
263+
}
264+
260265
void php_ds_register_vector()
261266
{
262267
zend_class_entry ce;
263268

264269
zend_function_entry methods[] = {
265270
PHP_DS_ME(Vector, __construct)
271+
PHP_DS_ME(Vector, getIterator)
266272

267273
PHP_DS_SEQUENCE_ME_LIST(Vector)
268274
PHP_DS_COLLECTION_ME_LIST(Vector)

src/php/classes/php_vector_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
extern zend_class_entry *php_ds_vector_ce;
99

1010
ARGINFO_OPTIONAL_ZVAL(Vector___construct, values);
11+
ARGINFO_NONE_RETURN_OBJ(Vector_getIterator, Traversable);
1112

1213
void php_ds_register_vector();
1314

src/php/handlers/php_common_handlers.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#include "php_common_handlers.h"
22
#include "zend_smart_str.h"
33

4-
int php_ds_default_cast_object(zval *obj, zval *return_value, int type)
5-
{
4+
int php_ds_default_cast_object
5+
#if PHP_VERSION_ID >= 80000
6+
(zend_object *obj, zval *return_value, int type) {
7+
zend_class_entry *ce = obj->ce;
8+
#else
9+
(zval *obj, zval *return_value, int type) {
10+
zend_class_entry *ce = Z_OBJCE_P(obj);
11+
#endif
612
switch (type) {
713
case IS_STRING: {
814
smart_str buffer = {0};
915

1016
smart_str_appendl(&buffer, "object(", 7);
11-
smart_str_append (&buffer, Z_OBJCE_P(obj)->name);
17+
smart_str_append (&buffer, ce->name);
1218
smart_str_appendc(&buffer, ')');
1319

1420
smart_str_0(&buffer);

src/php/handlers/php_common_handlers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
/**
77
* Default object cast handler.
88
*/
9+
#if PHP_VERSION_ID >= 80000
10+
int php_ds_default_cast_object(zend_object *obj, zval *return_value, int type);
11+
#else
912
int php_ds_default_cast_object(zval *obj, zval *return_value, int type);
13+
#endif
1014

1115
#endif

0 commit comments

Comments
 (0)