PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. class logger {
  3. static $x = 0;
  4. private $idx;
  5. function __construct() {
  6. $this->idx = self::$x++;
  7. printf("logger %d constructing\n", $this->idx);
  8. }
  9. }
  10. function create() {
  11. $x = 5;
  12. yield $x;
  13. $s = 'foo';
  14. yield $s;
  15. $foo = 1234;
  16. $z = $foo;
  17. yield $z;
  18. yield $foo;
  19. }
  20. function unusedarg($x, $y) {
  21. $z = 5;
  22. yield darray['x' => $x, 'z' => $z];
  23. $s = 'foo';
  24. yield 'almost there';
  25. $foo = 'inside foo';
  26. yield darray['foo' => $foo, 's' => $s];
  27. yield darray['x' => $x, 'y' => $y, 'foo' => $foo, 'z' => $z];
  28. }
  29. function dumpgen($g) {
  30. foreach ($g as $v) {
  31. var_dump($v);
  32. }
  33. }
  34. function getargs(...$args) {
  35. yield 0xdeadbeef;
  36. yield $args;
  37. yield $args[3];
  38. }
  39. function genthrow() {
  40. throw new Exception();
  41. yield 5;
  42. }
  43. function manylocals() {
  44. $a = 1;
  45. $b = 2;
  46. $c = 3;
  47. $d = 4;
  48. $e = 5;
  49. $f = 6;
  50. $g = 7;
  51. $h = 8;
  52. $i = 9;
  53. $j = 10;
  54. $k = 11;
  55. $l = 12;
  56. $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];
  57. $b = 0xdeadbeef;
  58. $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];
  59. $d = $e = 0xba53b411;
  60. 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];
  61. }
  62. <<__EntryPoint>> function main(): void {
  63. dumpgen(create());
  64. dumpgen(unusedarg(new logger(), 5));
  65. dumpgen(getargs(1, 2, 3, 4, 5));
  66. $g = genthrow();
  67. try {
  68. $g->next();
  69. } catch (Exception $e) {}
  70. try {
  71. $g->next();
  72. } catch (Exception $e) {
  73. var_dump($e->getMessage());
  74. }
  75. $g = manylocals();
  76. $g->next();
  77. var_dump($g->current());
  78. $g->send(new stdclass);
  79. var_dump($g->current());
  80. $g->send($g);
  81. var_dump($g->current());
  82. $g->next();
  83. var_dump($g->current());
  84. var_dump($g->valid());
  85. }