3v4l.org

run code in 300+ PHP versions simultaneously
<?php function coroutine1() { for ($i = 1; $i <= 5; $i++) { echo "coroutine1: {$i}\n"; yield; } } function coroutine2() { for ($i = 1; $i <= 10; $i++) { echo "coroutine2: {$i}\n"; $loop = yield; if ($i == 3) { echo "coroutine2 spawning coroutine3\n"; $loop->enqueue("#3", coroutine3()); } } } function coroutine3() { for ($i = 1; $i <= 5; $i++) { echo "coroutine3: {$i}\n"; yield; } } class EventLoop { private $tasks = []; public function enqueue($key, Generator $task) { $this->tasks[$key] = $task; } public function run() { foreach ($this->tasks as $key => $task) { $task->send($this); if (!$task->valid()) { echo "task {$key} complete\n"; unset($this->tasks[$key]); } } return !empty($this->tasks); } } $loop = new EventLoop(); $loop->enqueue("#1", coroutine1()); $loop->enqueue("#2", coroutine2()); while ($loop->run());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 16
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/qlXCj
function name:  (null)
number of ops:  20
compiled vars:  !0 = $loop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   NEW                                              $1      'EventLoop'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   50     3        INIT_METHOD_CALL                                         !0, 'enqueue'
          4        SEND_VAL_EX                                              '%231'
          5        INIT_FCALL                                               'coroutine1'
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR_NO_REF_EX                                       $4
          8        DO_FCALL                                      0          
   51     9        INIT_METHOD_CALL                                         !0, 'enqueue'
         10        SEND_VAL_EX                                              '%232'
         11        INIT_FCALL                                               'coroutine2'
         12        DO_FCALL                                      0  $6      
         13        SEND_VAR_NO_REF_EX                                       $6
         14        DO_FCALL                                      0          
   53    15      > JMP                                                      ->16
         16    >   INIT_METHOD_CALL                                         !0, 'run'
         17        DO_FCALL                                      0  $8      
         18      > JMPNZ                                                    $8, ->16
         19    > > RETURN                                                   1

Function coroutine1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
Branch analysis from position: 3
filename:       /in/qlXCj
function name:  coroutine1
number of ops:  12
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   GENERATOR_CREATE                                         
    4     1        ASSIGN                                                   !0, 1
          2      > JMP                                                      ->9
    5     3    >   ROPE_INIT                                     3  ~3      'coroutine1%3A+'
          4        ROPE_ADD                                      1  ~3      ~3, !0
          5        ROPE_END                                      2  ~2      ~3, '%0A'
          6        ECHO                                                     ~2
    6     7        YIELD                                                    
    4     8        PRE_INC                                                  !0
          9    >   IS_SMALLER_OR_EQUAL                                      !0, 5
         10      > JMPNZ                                                    ~7, ->3
    8    11    > > GENERATOR_RETURN                                         

End of function coroutine1

Function coroutine2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 3
Branch analysis from position: 21
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 3
Branch analysis from position: 21
Branch analysis from position: 3
Branch analysis from position: 18
filename:       /in/qlXCj
function name:  coroutine2
number of ops:  22
compiled vars:  !0 = $i, !1 = $loop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   GENERATOR_CREATE                                         
   11     1        ASSIGN                                                   !0, 1
          2      > JMP                                                      ->19
   12     3    >   ROPE_INIT                                     3  ~4      'coroutine2%3A+'
          4        ROPE_ADD                                      1  ~4      ~4, !0
          5        ROPE_END                                      2  ~3      ~4, '%0A'
          6        ECHO                                                     ~3
   14     7        YIELD                                            $6      
          8        ASSIGN                                                   !1, $6
   15     9        IS_EQUAL                                                 !0, 3
         10      > JMPZ                                                     ~8, ->18
   16    11    >   ECHO                                                     'coroutine2+spawning+coroutine3%0A'
   17    12        INIT_METHOD_CALL                                         !1, 'enqueue'
         13        SEND_VAL_EX                                              '%233'
         14        INIT_FCALL_BY_NAME                                       'coroutine3'
         15        DO_FCALL                                      0  $9      
         16        SEND_VAR_NO_REF_EX                                       $9
         17        DO_FCALL                                      0          
   11    18    >   PRE_INC                                                  !0
         19    >   IS_SMALLER_OR_EQUAL                                      !0, 10
         20      > JMPNZ                                                    ~12, ->3
   20    21    > > GENERATOR_RETURN                                         

End of function coroutine2

Function coroutine3:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
Branch analysis from position: 3
filename:       /in/qlXCj
function name:  coroutine3
number of ops:  12
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   GENERATOR_CREATE                                         
   23     1        ASSIGN                                                   !0, 1
          2      > JMP                                                      ->9
   24     3    >   ROPE_INIT                                     3  ~3      'coroutine3%3A+'
          4        ROPE_ADD                                      1  ~3      ~3, !0
          5        ROPE_END                                      2  ~2      ~3, '%0A'
          6        ECHO                                                     ~2
   25     7        YIELD                                                    
   23     8        PRE_INC                                                  !0
          9    >   IS_SMALLER_OR_EQUAL                                      !0, 5
         10      > JMPNZ                                                    ~7, ->3
   27    11    > > GENERATOR_RETURN                                         

End of function coroutine3

Class EventLoop:
Function enqueue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlXCj
function name:  enqueue
number of ops:  6
compiled vars:  !0 = $key, !1 = $task
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   33     2        FETCH_OBJ_W                                      $2      'tasks'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   34     5      > RETURN                                                   null

End of function enqueue

Function run:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 19
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 18
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/qlXCj
function name:  run
number of ops:  24
compiled vars:  !0 = $task, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   FETCH_OBJ_R                                      ~2      'tasks'
          1      > FE_RESET_R                                       $3      ~2, ->19
          2    > > FE_FETCH_R                                       ~4      $3, !0, ->19
          3    >   ASSIGN                                                   !1, ~4
   38     4        INIT_METHOD_CALL                                         !0, 'send'
          5        FETCH_THIS                                       $6      
          6        SEND_VAR_EX                                              $6
          7        DO_FCALL                                      0          
   39     8        INIT_METHOD_CALL                                         !0, 'valid'
          9        DO_FCALL                                      0  $8      
         10        BOOL_NOT                                         ~9      $8
         11      > JMPZ                                                     ~9, ->18
   40    12    >   ROPE_INIT                                     3  ~11     'task+'
         13        ROPE_ADD                                      1  ~11     ~11, !1
         14        ROPE_END                                      2  ~10     ~11, '+complete%0A'
         15        ECHO                                                     ~10
   41    16        FETCH_OBJ_UNSET                                  $13     'tasks'
         17        UNSET_DIM                                                $13, !1
   37    18    > > JMP                                                      ->2
         19    >   FE_FREE                                                  $3
   45    20        ISSET_ISEMPTY_PROP_OBJ                           ~14     'tasks'
         21        BOOL_NOT                                         ~15     ~14
         22      > RETURN                                                   ~15
   46    23*     > RETURN                                                   null

End of function run

End of class EventLoop.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.94 ms | 1411 KiB | 15 Q