Arquivos
hhvm/hphp/test/quick/static_closure_gen.php
T
Jan Oravec c839f3c3d0 Fix static variables in closure generators
Each closure instance carries its own set of static variables. Multiple
generators created using the same closure instance should share their
static variables.

This was not the case and static variables in closure generators behaved
like normal local variables. Let's fix it by referencing closure and
using its static locals.

Pointer to the closure is located in the local following original input
arguments. These are shifted by one, as Continuation occupies local 0.

This also frees 8 bytes from c_Continuation (or 16 if you count 16-byte
alignment).
2013-06-06 15:57:06 -07:00

63 linhas
892 B
PHP

<?php
function dumpEach($gen) {
foreach ($gen as $v) {
var_dump($v);
}
}
function dumpClosure($f) {
echo $f(), $f(), $f(), "\n";
}
function makeClosureGen() {
return function () {
static $x = 0;
static $y = 0;
yield $x++;
yield $y++;
yield $x++;
yield $y++;
};
}
echo "\ngenerator closure\n";
$cg = makeClosureGen();
$cgg = $cg();
dumpEach($cgg);
$cgg = null;
$cgg = $cg();
dumpEach($cgg);
$cg = makeClosureGen();
$cgg = $cg();
dumpEach($cgg);
$cgg = null;
$cgg = $cg();
dumpEach($cgg);
function makeClosure() {
return function() {
static $x = 0;
return $x++;
};
}
echo "\nplain closure:\n";
$c = makeClosure();
dumpClosure($c);
$c = null;
$c = makeClosure();
dumpClosure($c);
function makeGen() {
static $x = 0;
yield $x++;
yield $x++;
}
echo "\nplain generator:\n";
$g = makeGen();
dumpEach($g);
$g = makeGen();
dumpEach($g);