3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Illustrates: $fiber->throw($exception) resumes the fiber and raises the // exception exactly where Fiber::suspend() paused it, not in the caller's scope. $fiber = new Fiber(function (): void { echo "fiber: starting\n"; try { $value = Fiber::suspend('paused, waiting for input'); echo "fiber: resumed normally with '{$value}'\n"; } catch (RuntimeException $e) { // This ordinary try/catch, wrapped around suspend(), catches the // exception injected by Fiber::throw() -- no special API needed. echo "fiber: caught injected exception: '{$e->getMessage()}'\n"; } echo "fiber: finishing\n"; }); echo "main: getCurrent() outside any fiber is: "; var_dump(Fiber::getCurrent()); $suspendedValue = $fiber->start(); echo "main: fiber suspended with '{$suspendedValue}'\n"; // This does NOT throw here, in main's scope. It resumes the fiber and the // exception surfaces at the suspend() call above, inside the fiber's body. $fiber->throw(new RuntimeException('something went wrong')); echo "main: fiber finished? " . ($fiber->isTerminated() ? 'yes' : 'no') . "\n"; // Starting an already-started (and now terminated) fiber is not a silent // no-op: it throws a FiberError. try { $fiber->start(); } catch (FiberError $e) { echo "main: caught FiberError: '{$e->getMessage()}'\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 29
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Found catch point at position: 36
Branch analysis from position: 36
2 jumps found. (Code = 107) Position 1 = 37, Position 2 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tLEP5
function name:  (null)
number of ops:  44
compiled vars:  !0 = $fiber, !1 = $suspendedValue, !2 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    6     0  E >   NEW                                                  $3      'Fiber'
          1        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
   19     2        SEND_VAL_EX                                                  ~4
    6     3        DO_FCALL                                          0          
          4        ASSIGN                                                       !0, $3
   21     5        ECHO                                                         'main%3A+getCurrent%28%29+outside+any+fiber+is%3A+'
   22     6        INIT_FCALL                                                   'var_dump'
          7        INIT_STATIC_METHOD_CALL                                      'Fiber', 'getCurrent'
          8        DO_FCALL                                          0  $7      
          9        SEND_VAR                                                     $7
         10        DO_ICALL                                                     
   24    11        INIT_METHOD_CALL                                             !0, 'start'
         12        DO_FCALL                                          0  $9      
         13        ASSIGN                                                       !1, $9
   25    14        ROPE_INIT                                         3  ~12     'main%3A+fiber+suspended+with+%27'
         15        ROPE_ADD                                          1  ~12     ~12, !1
         16        ROPE_END                                          2  ~11     ~12, '%27%0A'
         17        ECHO                                                         ~11
   29    18        INIT_METHOD_CALL                                             !0, 'throw'
         19        NEW                                                  $14     'RuntimeException'
         20        SEND_VAL_EX                                                  'something+went+wrong'
         21        DO_FCALL                                          0          
         22        SEND_VAR_NO_REF_EX                                           $14
         23        DO_FCALL                                          0          
   31    24        INIT_METHOD_CALL                                             !0, 'isTerminated'
         25        DO_FCALL                                          0  $17     
         26      > JMPZ                                                         $17, ->29
         27    >   QM_ASSIGN                                            ~18     'yes'
         28      > JMP                                                          ->30
         29    >   QM_ASSIGN                                            ~18     'no'
         30    >   CONCAT                                               ~19     'main%3A+fiber+finished%3F+', ~18
         31        CONCAT                                               ~20     ~19, '%0A'
         32        ECHO                                                         ~20
   36    33        INIT_METHOD_CALL                                             !0, 'start'
         34        DO_FCALL                                          0          
         35      > JMP                                                          ->43
   37    36  E > > CATCH                                           last         'FiberError'
   38    37    >   ROPE_INIT                                         3  ~24     'main%3A+caught+FiberError%3A+%27'
         38        INIT_METHOD_CALL                                             !2, 'getMessage'
         39        DO_FCALL                                          0  $22     
         40        ROPE_ADD                                          1  ~24     ~24, $22
         41        ROPE_END                                          2  ~23     ~24, '%27%0A'
         42        ECHO                                                         ~23
   39    43    > > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 10
Branch analysis from position: 10
2 jumps found. (Code = 107) Position 1 = 11, Position 2 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tLEP5
function name:  {closure:/in/tLEP5:6}
number of ops:  19
compiled vars:  !0 = $value, !1 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   ECHO                                                         'fiber%3A+starting%0A'
   10     1        INIT_STATIC_METHOD_CALL                                      'Fiber', 'suspend'
          2        SEND_VAL                                                     'paused%2C+waiting+for+input'
          3        DO_FCALL                                          0  $2      
          4        ASSIGN                                                       !0, $2
   11     5        ROPE_INIT                                         3  ~5      'fiber%3A+resumed+normally+with+%27'
          6        ROPE_ADD                                          1  ~5      ~5, !0
          7        ROPE_END                                          2  ~4      ~5, '%27%0A'
          8        ECHO                                                         ~4
          9      > JMP                                                          ->17
   12    10  E > > CATCH                                           last         'RuntimeException'
   15    11    >   ROPE_INIT                                         3  ~9      'fiber%3A+caught+injected+exception%3A+%27'
         12        INIT_METHOD_CALL                                             !1, 'getMessage'
         13        DO_FCALL                                          0  $7      
         14        ROPE_ADD                                          1  ~9      ~9, $7
         15        ROPE_END                                          2  ~8      ~9, '%27%0A'
         16        ECHO                                                         ~8
   18    17    >   ECHO                                                         'fiber%3A+finishing%0A'
   19    18      > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
145.49 ms | 1112 KiB | 14 Q