3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace fiberEnabled { /** @return iterable<int> */ function fibonacciSequence(): iterable { $a = 0; $b = 1; while (true) { yield $a; // Imagine if this had to yield a Promise: // All callers would also have to return a promise and know how to wait for it to resolve if (\Fiber::getCurrent()) { \Fiber::suspend(); } $c = $a; $a = $b; $b += $c; } } function fetch5th(): int { foreach (fibonacciSequence() as $key => $i) { if ($key === 19) { return $i; } } } function fetchFirstTen(): array { $result = []; foreach (fibonacciSequence() as $key => $i) { $result[] = $i; if ($key === 9) { return $result; } } } function file_get_contents(string $file): string { if (!\Fiber::getCurrent()) { return \file_get_contents($file); } $stream = fopen($file, 'r'); \stream_set_blocking($stream, false); \stream_set_read_buffer($stream, 0); $data = ''; while (!\feof($stream)) { \Fiber::suspend(); $partial = \stream_get_contents($stream, length: 4); // Simulate read latency var_dump($partial); $data .= $partial; } \fclose($stream); return $data; } // NOTE: A real fiber event loop would be able to pool these and 'await' them alternatingly. // If it was waiting for a network request they may return in a different order. // Fibers can repeatedly suspend and let other fibers continue while waiting for an external condition. function fiberAll(array $fibers): array { foreach ($fibers as $key => $fiber) { if (!$fiber->isStarted()) { $fiber->start(); } while ($fiber->isSuspended()) { echo 'Fiber suspended, resuming...', "\n"; $fiber->resume(); } } return $fibers; } } namespace { file_put_contents('/tmp/test123', 'Some test text data'); // Whether used directly, outside of an event loop... print_r(\fiberEnabled\fetch5th()); echo "\n"; print_r(\fiberEnabled\fetchFirstTen()); print_r(\fiberEnabled\file_get_contents('/tmp/test123')); echo "\n"; // Or used as part of an event loop... $fibers = \fiberEnabled\fiberAll([ 'fetch5th' => new Fiber(\fiberEnabled\fetch5th(...)), 'fetchFirstTen' => new Fiber(\fiberEnabled\fetchFirstTen(...)), 'fileContent' => new Fiber(fn () => \fiberEnabled\file_get_contents('/tmp/test123')), ]); print_r($fibers['fetch5th']->getReturn()); echo "\n"; print_r($fibers['fetchFirstTen']->getReturn()); print_r($fibers['fileContent']->getReturn()); echo "\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RYBmm
function name:  (null)
number of ops:  64
compiled vars:  !0 = $fibers
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   87     0  E >   INIT_FCALL                                               'file_put_contents'
          1        SEND_VAL                                                 '%2Ftmp%2Ftest123'
          2        SEND_VAL                                                 'Some+test+text+data'
          3        DO_ICALL                                                 
   90     4        INIT_FCALL                                               'print_r'
          5        INIT_FCALL                                               'fiberenabled%5Cfetch5th'
          6        DO_FCALL                                      0  $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                                 
          9        ECHO                                                     '%0A'
   91    10        INIT_FCALL                                               'print_r'
         11        INIT_FCALL                                               'fiberenabled%5Cfetchfirstten'
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
   92    15        INIT_FCALL                                               'print_r'
         16        INIT_FCALL                                               'fiberenabled%5Cfile_get_contents'
         17        SEND_VAL                                                 '%2Ftmp%2Ftest123'
         18        DO_FCALL                                      0  $6      
         19        SEND_VAR                                                 $6
         20        DO_ICALL                                                 
         21        ECHO                                                     '%0A'
   95    22        INIT_FCALL                                               'fiberenabled%5Cfiberall'
   96    23        NEW                                              $8      'Fiber'
         24        INIT_FCALL                                               'fiberenabled%5Cfetch5th'
         25        ZEND_CALLABLE_CONVERT                            ~9      
         26        SEND_VAL_EX                                              ~9
         27        DO_FCALL                                      0          
         28        INIT_ARRAY                                       ~11     $8, 'fetch5th'
   97    29        NEW                                              $12     'Fiber'
         30        INIT_FCALL                                               'fiberenabled%5Cfetchfirstten'
         31        ZEND_CALLABLE_CONVERT                            ~13     
         32        SEND_VAL_EX                                              ~13
         33        DO_FCALL                                      0          
         34        ADD_ARRAY_ELEMENT                                ~11     $12, 'fetchFirstTen'
   98    35        NEW                                              $15     'Fiber'
         36        DECLARE_LAMBDA_FUNCTION                          ~16     [0]
         37        SEND_VAL_EX                                              ~16
         38        DO_FCALL                                      0          
         39        ADD_ARRAY_ELEMENT                                ~11     $15, 'fileContent'
         40        SEND_VAL                                                 ~11
   95    41        DO_FCALL                                      0  $18     
         42        ASSIGN                                                   !0, $18
  101    43        INIT_FCALL                                               'print_r'
         44        FETCH_DIM_R                                      ~20     !0, 'fetch5th'
         45        INIT_METHOD_CALL                                         ~20, 'getReturn'
         46        DO_FCALL                                      0  $21     
         47        SEND_VAR                                                 $21
         48        DO_ICALL                                                 
         49        ECHO                                                     '%0A'
  102    50        INIT_FCALL                                               'print_r'
         51        FETCH_DIM_R                                      ~23     !0, 'fetchFirstTen'
         52        INIT_METHOD_CALL                                         ~23, 'getReturn'
         53        DO_FCALL                                      0  $24     
         54        SEND_VAR                                                 $24
         55        DO_ICALL                                                 
  103    56        INIT_FCALL                                               'print_r'
         57        FETCH_DIM_R                                      ~26     !0, 'fileContent'
         58        INIT_METHOD_CALL                                         ~26, 'getReturn'
         59        DO_FCALL                                      0  $27     
         60        SEND_VAR                                                 $27
         61        DO_ICALL                                                 
         62        ECHO                                                     '%0A'
  104    63      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RYBmm
function name:  {closure}
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   INIT_FCALL                                               'fiberenabled%5Cfile_get_contents'
          1        SEND_VAL                                                 '%2Ftmp%2Ftest123'
          2        DO_FCALL                                      0  $0      
          3      > RETURN                                                   $0
          4*     > RETURN                                                   null

End of Dynamic Function 0

Function fiberenabled%5Cfibonaccisequence:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 4
Branch analysis from position: 14
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 4
Branch analysis from position: 14
Branch analysis from position: 4
Branch analysis from position: 10
filename:       /in/RYBmm
function name:  fiberEnabled\fibonacciSequence
number of ops:  15
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   GENERATOR_CREATE                                         
    7     1        ASSIGN                                                   !0, 0
    8     2        ASSIGN                                                   !1, 1
    9     3      > JMP                                                      ->13
   10     4    >   YIELD                                                    !0
   14     5        INIT_STATIC_METHOD_CALL                                  'Fiber', 'getCurrent'
          6        DO_FCALL                                      0  $6      
          7      > JMPZ                                                     $6, ->10
   15     8    >   INIT_STATIC_METHOD_CALL                                  'Fiber', 'suspend'
          9        DO_FCALL                                      0          
   18    10    >   ASSIGN                                                   !2, !0
   19    11        ASSIGN                                                   !0, !1
   20    12        ASSIGN_OP                                     1          !1, !2
    9    13    > > JMPNZ                                                    <true>, ->4
   22    14    > > GENERATOR_RETURN                                         

End of function fiberenabled%5Cfibonaccisequence

Function fiberenabled%5Cfetch5th:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/RYBmm
function name:  fiberEnabled\fetch5th
number of ops:  14
compiled vars:  !0 = $i, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   INIT_NS_FCALL_BY_NAME                                    'fiberEnabled%5CfibonacciSequence'
          1        DO_FCALL                                      0  $2      
          2      > FE_RESET_R                                       $3      $2, ->11
          3    > > FE_FETCH_R                                       ~4      $3, !0, ->11
          4    >   ASSIGN                                                   !1, ~4
   27     5        IS_IDENTICAL                                             !1, 19
          6      > JMPZ                                                     ~6, ->10
   28     7    >   VERIFY_RETURN_TYPE                                       !0
          8        FE_FREE                                                  $3
          9      > RETURN                                                   !0
   26    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $3
   31    12        VERIFY_RETURN_TYPE                                       
         13      > RETURN                                                   null

End of function fiberenabled%5Cfetch5th

Function fiberenabled%5Cfetchfirstten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 14
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/RYBmm
function name:  fiberEnabled\fetchFirstTen
number of ops:  17
compiled vars:  !0 = $result, !1 = $i, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   36     1        INIT_NS_FCALL_BY_NAME                                    'fiberEnabled%5CfibonacciSequence'
          2        DO_FCALL                                      0  $4      
          3      > FE_RESET_R                                       $5      $4, ->14
          4    > > FE_FETCH_R                                       ~6      $5, !1, ->14
          5    >   ASSIGN                                                   !2, ~6
   37     6        ASSIGN_DIM                                               !0
          7        OP_DATA                                                  !1
   38     8        IS_IDENTICAL                                             !2, 9
          9      > JMPZ                                                     ~9, ->13
   39    10    >   VERIFY_RETURN_TYPE                                       !0
         11        FE_FREE                                                  $5
         12      > RETURN                                                   !0
   36    13    > > JMP                                                      ->4
         14    >   FE_FREE                                                  $5
   42    15        VERIFY_RETURN_TYPE                                       
         16      > RETURN                                                   null

End of function fiberenabled%5Cfetchfirstten

Function fiberenabled%5Cfile_get_contents:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 25
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 25
Branch analysis from position: 41
Branch analysis from position: 25
filename:       /in/RYBmm
function name:  fiberEnabled\file_get_contents
number of ops:  48
compiled vars:  !0 = $file, !1 = $stream, !2 = $data, !3 = $partial
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        INIT_STATIC_METHOD_CALL                                  'Fiber', 'getCurrent'
          2        DO_FCALL                                      0  $4      
          3        BOOL_NOT                                         ~5      $4
          4      > JMPZ                                                     ~5, ->10
   47     5    >   INIT_FCALL                                               'file_get_contents'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $6      
          8        VERIFY_RETURN_TYPE                                       $6
          9      > RETURN                                                   $6
   50    10    >   INIT_NS_FCALL_BY_NAME                                    'fiberEnabled%5Cfopen'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              'r'
         13        DO_FCALL                                      0  $7      
         14        ASSIGN                                                   !1, $7
   51    15        INIT_FCALL                                               'stream_set_blocking'
         16        SEND_VAR                                                 !1
         17        SEND_VAL                                                 <false>
         18        DO_ICALL                                                 
   52    19        INIT_FCALL                                               'stream_set_read_buffer'
         20        SEND_VAR                                                 !1
         21        SEND_VAL                                                 0
         22        DO_ICALL                                                 
   54    23        ASSIGN                                                   !2, ''
   55    24      > JMP                                                      ->36
   56    25    >   INIT_STATIC_METHOD_CALL                                  'Fiber', 'suspend'
         26        DO_FCALL                                      0          
   57    27        INIT_FCALL                                               'stream_get_contents'
         28        SEND_VAR                                                 !1
         29        SEND_VAL                                                 4
         30        DO_ICALL                                         $13     
         31        ASSIGN                                                   !3, $13
   58    32        INIT_NS_FCALL_BY_NAME                                    'fiberEnabled%5Cvar_dump'
         33        SEND_VAR_EX                                              !3
         34        DO_FCALL                                      0          
   59    35        ASSIGN_OP                                     8          !2, !3
   55    36    >   INIT_FCALL                                               'feof'
         37        SEND_VAR                                                 !1
         38        DO_ICALL                                         $17     
         39        BOOL_NOT                                         ~18     $17
         40      > JMPNZ                                                    ~18, ->25
   62    41    >   INIT_FCALL                                               'fclose'
         42        SEND_VAR                                                 !1
         43        DO_ICALL                                                 
   64    44        VERIFY_RETURN_TYPE                                       !2
         45      > RETURN                                                   !2
   65    46*       VERIFY_RETURN_TYPE                                       
         47*     > RETURN                                                   null

End of function fiberenabled%5Cfile_get_contents

Function fiberenabled%5Cfiberall:
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 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 11
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 11
Branch analysis from position: 18
Branch analysis from position: 11
Branch analysis from position: 10
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/RYBmm
function name:  fiberEnabled\fiberAll
number of ops:  24
compiled vars:  !0 = $fibers, !1 = $fiber, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1      > FE_RESET_R                                       $3      !0, ->19
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->19
          3    >   ASSIGN                                                   !2, ~4
   72     4        INIT_METHOD_CALL                                         !1, 'isStarted'
          5        DO_FCALL                                      0  $6      
          6        BOOL_NOT                                         ~7      $6
          7      > JMPZ                                                     ~7, ->10
   73     8    >   INIT_METHOD_CALL                                         !1, 'start'
          9        DO_FCALL                                      0          
   76    10    > > JMP                                                      ->15
   77    11    >   ECHO                                                     'Fiber+suspended%2C+resuming...'
         12        ECHO                                                     '%0A'
   78    13        INIT_METHOD_CALL                                         !1, 'resume'
         14        DO_FCALL                                      0          
   76    15    >   INIT_METHOD_CALL                                         !1, 'isSuspended'
         16        DO_FCALL                                      0  $10     
         17      > JMPNZ                                                    $10, ->11
   71    18    > > JMP                                                      ->2
         19    >   FE_FREE                                                  $3
   82    20        VERIFY_RETURN_TYPE                                       !0
         21      > RETURN                                                   !0
   83    22*       VERIFY_RETURN_TYPE                                       
         23*     > RETURN                                                   null

End of function fiberenabled%5Cfiberall

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.16 ms | 1033 KiB | 32 Q