Skip to content

Commit 495551a

Browse files
committed
Fixed iterators API. zend_iterator_init() has to be used in each get_iterator() callback.
1 parent a4a86df commit 495551a

12 files changed

+45
-23
lines changed

Zend/zend_generators.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -675,19 +675,7 @@ zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *ob
675675

676676
iterator = &generator->iterator;
677677

678-
//???
679-
#if 1
680-
iterator->intern.std.gc.refcount = 1;
681-
iterator->intern.std.gc.u.v.type = IS_OBJECT;
682-
iterator->intern.std.gc.u.v.flags = 0;
683-
iterator->intern.std.gc.u.v.buffer = 0;
684-
iterator->intern.std.ce = NULL;
685-
iterator->intern.std.properties = NULL;
686-
iterator->intern.std.guards = NULL;
687-
zend_objects_store_put(&iterator->intern.std);
688-
#else
689-
zend_object_std_init(&iterator->intern.std, ???);
690-
#endif
678+
zend_iterator_init(&iterator->intern TSRMLS_CC);
691679

692680
iterator->intern.funcs = &zend_generator_iterator_functions;
693681
iterator->intern.data = (void *) generator;

Zend/zend_interfaces.c

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ static zend_object_iterator *zend_user_it_get_iterator(zend_class_entry *ce, zva
259259

260260
iterator = emalloc(sizeof(zend_user_iterator));
261261

262+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
263+
262264
Z_ADDREF_P(object);
263265
iterator->it.data = (void*)object;
264266
iterator->it.funcs = ce->iterator_funcs.funcs;

Zend/zend_iterators.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ static void iter_wrapper_dtor(zend_object *object TSRMLS_DC)
6565
iter->funcs->dtor(iter TSRMLS_CC);
6666
}
6767

68-
ZEND_API void zend_iterator_wrap(zend_object_iterator *iter, zval *wrapped TSRMLS_DC)
68+
ZEND_API void zend_iterator_init(zend_object_iterator *iter TSRMLS_DC)
6969
{
70-
ZVAL_OBJ(wrapped, &iter->std);
71-
//??? hack (it would be better implement it in anothe way)
72-
Z_OBJ_HT_P(wrapped) = &iterator_object_handlers;
70+
zend_object_std_init(&iter->std, &zend_iterator_class_entry);
71+
iter->std.handlers = &iterator_object_handlers;
7372
}
7473

7574
ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(

Zend/zend_iterators.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ BEGIN_EXTERN_C()
8383
ZEND_API enum zend_object_iterator_kind zend_iterator_unwrap(zval *array_ptr, zend_object_iterator **iter TSRMLS_DC);
8484

8585
/* given an iterator, wrap it up as a zval for use by the engine opcodes */
86-
ZEND_API void zend_iterator_wrap(zend_object_iterator *iter, zval *zv TSRMLS_DC);
86+
ZEND_API void zend_iterator_init(zend_object_iterator *iter TSRMLS_DC);
8787

8888
ZEND_API void zend_register_iterator_wrapper(TSRMLS_D);
8989
END_EXTERN_C()

Zend/zend_vm_def.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -4253,7 +4253,10 @@ ZEND_VM_HANDLER(77, ZEND_FE_RESET, CONST|TMP|VAR|CV, ANY)
42534253
FREE_OP1_IF_VAR();
42544254
}
42554255
if (iter && EXPECTED(EG(exception) == NULL)) {
4256-
zend_iterator_wrap(iter, array_ptr TSRMLS_CC);
4256+
zval iterator;
4257+
4258+
array_ptr = &iterator;
4259+
ZVAL_OBJ(array_ptr, &iter->std);
42574260
} else {
42584261
if (OP1_TYPE == IS_VAR && opline->extended_value & ZEND_FE_RESET_VARIABLE) {
42594262
FREE_OP1_VAR_PTR();

Zend/zend_vm_execute.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -3044,7 +3044,10 @@ static int ZEND_FASTCALL ZEND_FE_RESET_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_A
30443044

30453045
}
30463046
if (iter && EXPECTED(EG(exception) == NULL)) {
3047-
zend_iterator_wrap(iter, array_ptr TSRMLS_CC);
3047+
zval iterator;
3048+
3049+
array_ptr = &iterator;
3050+
ZVAL_OBJ(array_ptr, &iter->std);
30483051
} else {
30493052
if (IS_CONST == IS_VAR && opline->extended_value & ZEND_FE_RESET_VARIABLE) {
30503053

@@ -8095,7 +8098,10 @@ static int ZEND_FASTCALL ZEND_FE_RESET_SPEC_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARG
80958098

80968099
}
80978100
if (iter && EXPECTED(EG(exception) == NULL)) {
8098-
zend_iterator_wrap(iter, array_ptr TSRMLS_CC);
8101+
zval iterator;
8102+
8103+
array_ptr = &iterator;
8104+
ZVAL_OBJ(array_ptr, &iter->std);
80998105
} else {
81008106
if (IS_TMP_VAR == IS_VAR && opline->extended_value & ZEND_FE_RESET_VARIABLE) {
81018107

@@ -13198,7 +13204,10 @@ static int ZEND_FASTCALL ZEND_FE_RESET_SPEC_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARG
1319813204
zval_ptr_dtor_nogc(free_op1.var);
1319913205
}
1320013206
if (iter && EXPECTED(EG(exception) == NULL)) {
13201-
zend_iterator_wrap(iter, array_ptr TSRMLS_CC);
13207+
zval iterator;
13208+
13209+
array_ptr = &iterator;
13210+
ZVAL_OBJ(array_ptr, &iter->std);
1320213211
} else {
1320313212
if (IS_VAR == IS_VAR && opline->extended_value & ZEND_FE_RESET_VARIABLE) {
1320413213
if (free_op1.var) {zval_ptr_dtor_nogc(free_op1.var);};
@@ -30089,7 +30098,10 @@ static int ZEND_FASTCALL ZEND_FE_RESET_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS
3008930098

3009030099
}
3009130100
if (iter && EXPECTED(EG(exception) == NULL)) {
30092-
zend_iterator_wrap(iter, array_ptr TSRMLS_CC);
30101+
zval iterator;
30102+
30103+
array_ptr = &iterator;
30104+
ZVAL_OBJ(array_ptr, &iter->std);
3009330105
} else {
3009430106
if (IS_CV == IS_VAR && opline->extended_value & ZEND_FE_RESET_VARIABLE) {
3009530107

ext/date/php_date.c

+2
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,8 @@ zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval
19761976
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
19771977
}
19781978

1979+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1980+
19791981
Z_ADDREF_P(object);
19801982
iterator->intern.data = (void*) dpobj;
19811983
iterator->intern.funcs = &date_period_it_funcs;

ext/spl/spl_array.c

+2
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,8 @@ zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object,
11481148

11491149
iterator = emalloc(sizeof(spl_array_it));
11501150

1151+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1152+
11511153
Z_ADDREF_P(object);
11521154
iterator->intern.it.data = (void*)object;
11531155
iterator->intern.it.funcs = &spl_array_it_funcs;

ext/spl/spl_dllist.c

+3
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,9 @@ zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object
12911291
Z_ADDREF_P(object);
12921292

12931293
iterator = emalloc(sizeof(spl_dllist_it));
1294+
1295+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1296+
12941297
iterator->intern.it.data = (void*)object;
12951298
iterator->intern.it.funcs = &spl_dllist_it_funcs;
12961299
iterator->intern.ce = ce;

ext/spl/spl_fixedarray.c

+3
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,9 @@ zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *ob
10521052
Z_ADDREF_P(object);
10531053

10541054
iterator = emalloc(sizeof(spl_fixedarray_it));
1055+
1056+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1057+
10551058
iterator->intern.it.data = object;
10561059
iterator->intern.it.funcs = &spl_fixedarray_it_funcs;
10571060
iterator->intern.ce = ce;

ext/spl/spl_heap.c

+6
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,9 @@ zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object,
10911091
Z_ADDREF_P(object);
10921092

10931093
iterator = emalloc(sizeof(spl_heap_it));
1094+
1095+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1096+
10941097
iterator->intern.it.data = (void*)object;
10951098
iterator->intern.it.funcs = &spl_heap_it_funcs;
10961099
iterator->intern.ce = ce;
@@ -1115,6 +1118,9 @@ zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object
11151118
Z_ADDREF_P(object);
11161119

11171120
iterator = emalloc(sizeof(spl_heap_it));
1121+
1122+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
1123+
11181124
iterator->intern.it.data = (void*)object;
11191125
iterator->intern.it.funcs = &spl_pqueue_it_funcs;
11201126
iterator->intern.ce = ce;

ext/spl/spl_iterators.c

+2
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ static zend_object_iterator *spl_recursive_it_get_iterator(zend_class_entry *ce,
423423
"the parent constructor has not been called");
424424
}
425425

426+
zend_iterator_init((zend_object_iterator*)iterator TSRMLS_CC);
427+
426428
Z_ADDREF_P(zobject);
427429
iterator->intern.data = (void*)object;
428430
iterator->intern.funcs = ce->iterator_funcs.funcs;

0 commit comments

Comments
 (0)