Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4638f7b
Change "foreach" statement behavior (this is just a PoC yet)
dstogov Jan 28, 2015
dd2a36a
Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REF…
dstogov Jan 28, 2015
92e90c0
Fixed operand destruction in case of exceptions in iterator
dstogov Jan 28, 2015
61e7391
Fixed temporary variable re-allocation pass
dstogov Jan 28, 2015
eef80c5
Fixed foreach by reference iteration over constant array
dstogov Jan 28, 2015
994d435
Merge branch 'master' into foreach
dstogov Jan 28, 2015
2705d17
Merge branch 'master' into foreach
dstogov Jan 29, 2015
10a3260
New test
dstogov Jan 29, 2015
15a23b1
Reimplement iteration magic with HashTableIterators (see https://wiki…
dstogov Jan 29, 2015
721fc9e
Added new test
dstogov Jan 29, 2015
621fd74
Merge branch 'master' into foreach
dstogov Jan 30, 2015
4c5b385
More careful iterators update.
dstogov Jan 30, 2015
5aa9712
Implement consistent behavior for foreach by value over plain object
dstogov Jan 30, 2015
cc4b7be
Make internal function, operation on array passed by reference, to pr…
dstogov Jan 30, 2015
08302c0
Make array_splice() to preserve foreach hash position
dstogov Jan 30, 2015
fb2d079
API cleanup
dstogov Jan 30, 2015
b37f1d5
Fixed test name
dstogov Jan 30, 2015
5406f21
Reduced alghorithms complexity
dstogov Jan 30, 2015
1e41295
Generalize HashTableIterator API to allows its usage without iinvolve…
dstogov Jan 31, 2015
6c168e5
Merge branch 'master' into foreach
dstogov Feb 2, 2015
9d17a42
Merge branch 'master' into foreach
dstogov Feb 5, 2015
79b8bb1
Merge branch 'master' into foreach
dstogov Feb 9, 2015
7a50a32
Merge branch 'master' into foreach
dstogov Feb 11, 2015
b00f3bd
Merge branch 'master' into foreach
dstogov Feb 12, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/bug40509.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ var_dump(key($arr["v"]));
int(0)
int(0)
int(0)
NULL
int(0)
2 changes: 1 addition & 1 deletion Zend/tests/bug40705.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ int(0)
int(0)
int(1)
int(2)
NULL
int(0)
71 changes: 71 additions & 0 deletions Zend/tests/foreach_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
Iterator exceptions in foreach by value
--FILE--
<?php
class IT implements Iterator {
private $n = 0;
private $count = 0;
private $trap = null;

function __construct($count, $trap = null) {
$this->count = $count;
$this->trap = $trap;
}

function trap($trap) {
if ($trap === $this->trap) {
throw new Exception($trap);
}
}

function rewind() {$this->trap(__FUNCTION__); $this->n = 0;}
function valid() {$this->trap(__FUNCTION__); return $this->n < $this->count;}
function key() {$this->trap(__FUNCTION__); return $this->n;}
function current() {$this->trap(__FUNCTION__); return $this->n;}
function next() {$this->trap(__FUNCTION__); $this->n++;}
}

foreach(['rewind', 'valid', 'key', 'current', 'next'] as $trap) {
$obj = new IT(3, $trap);
try {
// IS_CV
foreach ($obj as $key => $val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
unset($obj);

try {
// IS_VAR
foreach (new IT(3, $trap) as $key => $val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}

try {
// IS_TMP_VAR
foreach ((object)new IT(2, $trap) as $key => $val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
}
?>
--EXPECT--
rewind
rewind
rewind
valid
valid
valid
key
key
key
current
current
current
0
next
0
next
0
next
65 changes: 65 additions & 0 deletions Zend/tests/foreach_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Iterator exceptions in foreach by reference
--FILE--
<?php
class IT extends ArrayIterator {
private $n = 0;

function __construct($trap = null) {
parent::__construct([0, 1]);
$this->trap = $trap;
}

function trap($trap) {
if ($trap === $this->trap) {
throw new Exception($trap);
}
}

function rewind() {$this->trap(__FUNCTION__); return parent::rewind();}
function valid() {$this->trap(__FUNCTION__); return parent::valid();}
function key() {$this->trap(__FUNCTION__); return parent::key();}
function next() {$this->trap(__FUNCTION__); return parent::next();}
}

foreach(['rewind', 'valid', 'key', 'next'] as $trap) {
$obj = new IT($trap);
try {
// IS_CV
foreach ($obj as $key => &$val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
unset($obj);

try {
// IS_VAR
foreach (new IT($trap) as $key => &$val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}

try {
// IS_TMP_VAR
foreach ((object)new IT($trap) as $key => &$val) echo "$val\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
}
?>
--EXPECT--
rewind
rewind
rewind
valid
valid
valid
key
key
key
0
next
0
next
0
next
22 changes: 22 additions & 0 deletions Zend/tests/foreach_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Nested foreach by reference on the same array
--FILE--
<?php
$a = [1,2,3];
foreach($a as &$x) {
foreach($a as &$y) {
echo "$x-$y\n";
$y++;
}
}
?>
--EXPECT--
1-1
2-2
2-3
3-2
3-3
4-4
5-3
5-4
5-5
20 changes: 20 additions & 0 deletions Zend/tests/foreach_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Foreach by reference on constant
--FILE--
<?php
for ($i = 0; $i < 3; $i++) {
foreach ([1,2,3] as &$val) {
echo "$val\n";
}
}
?>
--EXPECT--
1
2
3
1
2
3
1
2
3
13 changes: 13 additions & 0 deletions Zend/tests/foreach_007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Foreach by reference and inserting new element when we are already at the end
--FILE--
<?php
$a = [1];
foreach($a as &$v) {
echo "$v\n";
$a[1]=2;
}
?>
--EXPECT--
1
2
21 changes: 21 additions & 0 deletions Zend/tests/foreach_008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Nested foreach by reference and array modification
--FILE--
<?php
$a = [0, 1, 2, 3];
foreach ($a as &$x) {
foreach ($a as &$y) {
echo "$x - $y\n";
if ($x == 0 && $y == 1) {
unset($a[2]);
unset($a[1]);
}
}
}
?>
--EXPECT--
0 - 0
0 - 1
0 - 3
3 - 0
3 - 3
40 changes: 40 additions & 0 deletions Zend/tests/foreach_009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Nested foreach by reference and array modification with resize
--FILE--
<?php
$a = [0, 1, 2, 3, 4, 5, 6, 7];
unset($a[0], $a[1], $a[2], $a[3]);
foreach ($a as &$ref) {
foreach ($a as &$ref2) {
echo "$ref-$ref2\n";
if ($ref == 5 && $ref2 == 6) {
$a[42] = 8;
}
}
}
?>
--EXPECT--
4-4
4-5
4-6
4-7
5-4
5-5
5-6
5-7
5-8
6-4
6-5
6-6
6-7
6-8
7-4
7-5
7-6
7-7
7-8
8-4
8-5
8-6
8-7
8-8
40 changes: 40 additions & 0 deletions Zend/tests/foreach_010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Nested foreach by value over object and object modification with resize
--FILE--
<?php
$o = (object)['a'=>0, 'b'=>1, 'c'=>2, 'd'=>3, 'e'=>4, 'f'=>5, 'g'=>6, 'h'=>7];
unset($o->a, $o->b, $o->c, $o->d);
foreach ($o as $v1) {
foreach ($o as $v2) {
echo "$v1-$v2\n";
if ($v1 == 5 && $v2 == 6) {
$o->i = 8;
}
}
}
?>
--EXPECT--
4-4
4-5
4-6
4-7
5-4
5-5
5-6
5-7
5-8
6-4
6-5
6-6
6-7
6-8
7-4
7-5
7-6
7-7
7-8
8-4
8-5
8-6
8-7
8-8
19 changes: 19 additions & 0 deletions Zend/tests/foreach_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
sort() functions precerve foreach by reference iterator pointer
--FILE--
<?php
$a = [1,2,3,4,5,0];
foreach($a as &$v) {
echo "$v\n";
if ($v == 3) {
rsort($a);
}
}
?>
--EXPECT--
1
2
3
2
1
0
18 changes: 18 additions & 0 deletions Zend/tests/foreach_012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
array_walk() function precerve foreach by reference iterator pointer
--FILE--
<?php
$a = [1,2,3,4,5];
foreach($a as &$v) {
echo "$v\n";
if ($v == 3) {
array_walk($a, function (&$x) {$x+=10;});
}
}
?>
--EXPECT--
1
2
3
14
15
17 changes: 17 additions & 0 deletions Zend/tests/foreach_013.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
array_push() function precerve foreach by reference iterator pointer
--FILE--
<?php
$a = [1,2,3];
foreach($a as &$v) {
echo "$v\n";
if ($v == 3) {
array_push($a, 4);
}
}
?>
--EXPECT--
1
2
3
4
15 changes: 15 additions & 0 deletions Zend/tests/foreach_014.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
array_pop() function precerve foreach by reference iterator pointer
--FILE--
<?php
$a = [1,2,3];
foreach($a as &$v) {
echo "$v\n";
if ($v == 2) {
array_pop($a);
}
}
?>
--EXPECT--
1
2
Loading