3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen1() { yield 1; yield 2; yield 3; return 42; } function yieldFrom($iter) { return yield from $iter; } $g = function($iter) { $isSubgenerator = $iter instanceof Generator; $received = null; $send = true; while ($iter->valid()) { if ($isSubgenerator) { $next = $send ? $iter->send($received) : $iter->throw($received); } else { $next = $iter->current(); $iter->next(); } try { $received = yield $next; $send = true; } catch (Exception $e) { if ($isSubgenerator) { $received = $e; $send = false; } else { throw $e; } } } return $isSubgenerator ? $iter->getReturn() : null; }; $gen = $g(gen1()); foreach($gen as $val) { var_dump($val); } var_dump($gen->getReturn()); echo "=============\n"; $gen = yieldFrom(gen1()); foreach($gen as $val) { var_dump($val); } var_dump($gen->getReturn());
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 33
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 33
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 14
filename:       /in/ltILH
function name:  (null)
number of ops:  40
compiled vars:  !0 = $g, !1 = $gen, !2 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FltILH%3A14%240'
          1        ASSIGN                                                   !0, ~3
   42     2        INIT_DYNAMIC_CALL                                        !0
          3        INIT_FCALL                                               'gen1'
          4        DO_FCALL                                      0  $5      
          5        SEND_VAR_NO_REF_EX                                       $5
          6        DO_FCALL                                      0  $6      
          7        ASSIGN                                                   !1, $6
   43     8      > FE_RESET_R                                       $8      !1, ->14
          9    > > FE_FETCH_R                                               $8, !2, ->14
         10    >   INIT_FCALL                                               'var_dump'
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                                 
         13      > JMP                                                      ->9
         14    >   FE_FREE                                                  $8
   44    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         !1, 'getReturn'
         17        DO_FCALL                                      0  $10     
         18        SEND_VAR                                                 $10
         19        DO_ICALL                                                 
   45    20        ECHO                                                     '%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A'
   46    21        INIT_FCALL                                               'yieldfrom'
         22        INIT_FCALL                                               'gen1'
         23        DO_FCALL                                      0  $12     
         24        SEND_VAR                                                 $12
         25        DO_FCALL                                      0  $13     
         26        ASSIGN                                                   !1, $13
   47    27      > FE_RESET_R                                       $15     !1, ->33
         28    > > FE_FETCH_R                                               $15, !2, ->33
         29    >   INIT_FCALL                                               'var_dump'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                                 
         32      > JMP                                                      ->28
         33    >   FE_FREE                                                  $15
   48    34        INIT_FCALL                                               'var_dump'
         35        INIT_METHOD_CALL                                         !1, 'getReturn'
         36        DO_FCALL                                      0  $17     
         37        SEND_VAR                                                 $17
         38        DO_ICALL                                                 
         39      > RETURN                                                   1

Function gen1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/ltILH
function name:  gen1
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   GENERATOR_CREATE                                         
    4     1        YIELD                                                    1
    5     2        YIELD                                                    2
    6     3        YIELD                                                    3
    7     4      > GENERATOR_RETURN                                         
    8     5*     > GENERATOR_RETURN                                         

End of function gen1

Function yieldfrom:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 161) Position 1 = -2
filename:       /in/ltILH
function name:  yieldFrom
number of ops:  5
compiled vars:  !0 = $iter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
   11     2        YIELD_FROM                                       ~1      !0
          3      > GENERATOR_RETURN                                         
   12     4*     > GENERATOR_RETURN                                         

End of function yieldfrom

Function %00%7Bclosure%7D%2Fin%2FltILH%3A14%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 7
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 43
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 43
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 20
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Found catch point at position: 29
Branch analysis from position: 29
2 jumps found. (Code = 107) Position 1 = 30, Position 2 = -2
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 34
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/ltILH
function name:  {closure}
number of ops:  46
compiled vars:  !0 = $iter, !1 = $isSubgenerator, !2 = $received, !3 = $send, !4 = $next, !5 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
   15     2        INSTANCEOF                                       ~6      !0, 'Generator'
          3        ASSIGN                                                   !1, ~6
   16     4        ASSIGN                                                   !2, null
   17     5        ASSIGN                                                   !3, <true>
   19     6      > JMP                                                      ->35
   20     7    > > JMPZ                                                     !1, ->20
   21     8    > > JMPZ                                                     !3, ->14
          9    >   INIT_METHOD_CALL                                         !0, 'send'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0  $10     
         12        QM_ASSIGN                                        ~11     $10
         13      > JMP                                                      ->18
         14    >   INIT_METHOD_CALL                                         !0, 'throw'
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0  $12     
         17        QM_ASSIGN                                        ~11     $12
         18    >   ASSIGN                                                   !4, ~11
         19      > JMP                                                      ->25
   23    20    >   INIT_METHOD_CALL                                         !0, 'current'
         21        DO_FCALL                                      0  $14     
         22        ASSIGN                                                   !4, $14
   24    23        INIT_METHOD_CALL                                         !0, 'next'
         24        DO_FCALL                                      0          
   27    25    >   YIELD                                            $17     !4
         26        ASSIGN                                                   !2, $17
   28    27        ASSIGN                                                   !3, <true>
         28      > JMP                                                      ->35
   29    29  E > > CATCH                                       last         'Exception'
   30    30    > > JMPZ                                                     !1, ->34
   31    31    >   ASSIGN                                                   !2, !5
   32    32        ASSIGN                                                   !3, <false>
         33      > JMP                                                      ->35
   34    34    > > THROW                                         0          !5
   19    35    >   INIT_METHOD_CALL                                         !0, 'valid'
         36        DO_FCALL                                      0  $22     
         37      > JMPNZ                                                    $22, ->7
   39    38    > > JMPZ                                                     !1, ->43
         39    >   INIT_METHOD_CALL                                         !0, 'getReturn'
         40        DO_FCALL                                      0  $23     
         41        QM_ASSIGN                                        ~24     $23
         42      > JMP                                                      ->44
         43    >   QM_ASSIGN                                        ~24     null
         44    > > GENERATOR_RETURN                                         
   40    45*     > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FltILH%3A14%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.5 ms | 1407 KiB | 18 Q