PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/quick/generator_vars.php

http://github.com/facebook/hiphop-php
PHP | 94 lines | 85 code | 9 blank | 0 comment | 0 complexity | 517e59d69ef0deea3c9c52677a9f14ee MD5 | raw file
 1<?hh
 2
 3class logger {
 4  static $x = 0;
 5  private $idx;
 6  function __construct() {
 7    $this->idx = self::$x++;
 8    printf("logger %d constructing\n", $this->idx);
 9  }
10}
11
12function create() {
13  $x = 5;
14  yield $x;
15  $s = 'foo';
16  yield $s;
17  $foo = 1234;
18  $z = $foo;
19  yield $z;
20  yield $foo;
21}
22
23function unusedarg($x, $y) {
24  $z = 5;
25  yield darray['x' => $x, 'z' => $z];
26  $s = 'foo';
27  yield 'almost there';
28  $foo = 'inside foo';
29  yield darray['foo' => $foo, 's' => $s];
30  yield darray['x' => $x, 'y' => $y, 'foo' => $foo, 'z' => $z];
31}
32
33function dumpgen($g) {
34  foreach ($g as $v) {
35    var_dump($v);
36  }
37}
38
39function getargs(...$args) {
40  yield 0xdeadbeef;
41  yield $args;
42  yield $args[3];
43}
44
45function genthrow() {
46  throw new Exception();
47  yield 5;
48}
49
50function manylocals() {
51  $a = 1;
52  $b = 2;
53  $c = 3;
54  $d = 4;
55  $e = 5;
56  $f = 6;
57  $g = 7;
58  $h = 8;
59  $i = 9;
60  $j = 10;
61  $k = 11;
62  $l = 12;
63  $a = yield darray['a' => $a, 'b' => $b, 'c' => $c, 'd' => $d, 'e' => $e, 'f' => $f, 'g' => $g, 'h' => $h, 'i' => $i, 'j' => $j, 'k' => $k, 'l' => $l];
64  $b = 0xdeadbeef;
65  $c = yield darray['a' => $a, 'b' => $b, 'c' => $c, 'd' => $d, 'e' => $e, 'f' => $f, 'g' => $g, 'h' => $h, 'i' => $i, 'j' => $j, 'k' => $k, 'l' => $l];
66  $d = $e = 0xba53b411;
67  yield darray['a' => $a, 'b' => $b, 'c' => $c, 'd' => $d, 'e' => $e, 'f' => $f, 'g' => $g, 'h' => $h, 'i' => $i, 'j' => $j, 'k' => $k, 'l' => $l];
68}
69
70<<__EntryPoint>> function main(): void {
71  dumpgen(create());
72  dumpgen(unusedarg(new logger(), 5));
73  dumpgen(getargs(1, 2, 3, 4, 5));
74  $g = genthrow();
75  try {
76    $g->next();
77  } catch (Exception $e) {}
78  try {
79    $g->next();
80  } catch (Exception $e) {
81    var_dump($e->getMessage());
82  }
83
84  $g = manylocals();
85  $g->next();
86  var_dump($g->current());
87  $g->send(new stdclass);
88  var_dump($g->current());
89  $g->send($g);
90  var_dump($g->current());
91  $g->next();
92  var_dump($g->current());
93  var_dump($g->valid());
94}